From 0f3c8af1944db88b7852f24620d274d4df8d99ce Mon Sep 17 00:00:00 2001 From: Mikhail <16622558+mmv08@users.noreply.github.com> Date: Tue, 21 May 2024 12:20:44 +0200 Subject: [PATCH] optimize benchmark by not using multisend when not needed --- examples/4337-gas-metering/package.json | 3 ++- examples/4337-gas-metering/pimlico/pimlico.ts | 19 +++++++++---------- examples/4337-gas-metering/utils/safe.ts | 5 +++-- examples/4337-gas-metering/utils/userOps.ts | 1 + pnpm-lock.yaml | 13 ++++++++----- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/examples/4337-gas-metering/package.json b/examples/4337-gas-metering/package.json index 28b5256e..8c500411 100644 --- a/examples/4337-gas-metering/package.json +++ b/examples/4337-gas-metering/package.json @@ -52,13 +52,14 @@ "@alchemy/aa-accounts": "2.4.0", "@alchemy/aa-alchemy": "2.4.0", "@alchemy/aa-core": "2.3.1", + "@gelatonetwork/relay-sdk": "^5.5.6", "alchemy-sdk": "3.1.2", "dotenv": "16.4.5", + "ethers": "^6.12.1", "permissionless": "0.1.29", "viem": "2.10.8" }, "devDependencies": { - "@gelatonetwork/relay-sdk": "^5.5.6", "@types/node": "20.12.12", "tsx": "4.10.3", "typescript": "^5.4.5" diff --git a/examples/4337-gas-metering/pimlico/pimlico.ts b/examples/4337-gas-metering/pimlico/pimlico.ts index 3c1ecf28..3a247ee8 100644 --- a/examples/4337-gas-metering/pimlico/pimlico.ts +++ b/examples/4337-gas-metering/pimlico/pimlico.ts @@ -3,7 +3,7 @@ import { ethers } from 'ethers' import { getAccountNonce, bundlerActions, ENTRYPOINT_ADDRESS_V07, getRequiredPrefund } from 'permissionless' import { pimlicoBundlerActions, pimlicoPaymasterActions } from 'permissionless/actions/pimlico' import { setTimeout } from 'timers/promises' -import { Client, Hash, createClient, createPublicClient, http, parseEther } from 'viem' +import { Client, Hash, createClient, createPublicClient, http, parseEther, zeroAddress } from 'viem' import { privateKeyToAccount } from 'viem/accounts' import { baseSepolia, sepolia } from 'viem/chains' import { getAccountAddress, getAccountInitCode } from '../utils/safe' @@ -44,15 +44,14 @@ const erc721TokenAddress = process.env.PIMLICO_ERC721_TOKEN_CONTRACT as `0x${str // Detecting Paymaster based transaction or not. const argv = process.argv.slice(2) -let usePaymaster!: boolean +let usePaymaster = false if (argv.length < 1 || argv.length > 2) { throw new Error('TX Type Argument not passed.') } else if (argv.length == 2 && argv[1] == 'paymaster=true') { if (policyID) { usePaymaster = true } else { - // throw new Error('Paymaster requires policyID to be set.') - usePaymaster = true + throw new Error('Paymaster requires policyID to be set.') } } @@ -114,8 +113,8 @@ const initCode = await getAccountInitCode({ safeSingletonAddress: chainAddresses.SAFE_SINGLETON_ADDRESS, saltNonce: saltNonce, multiSendAddress: multiSendAddress, - erc20TokenAddress: usdcTokenAddress, - paymasterAddress: erc20PaymasterAddress, + erc20TokenAddress: usePaymaster ? usdcTokenAddress : zeroAddress, + paymasterAddress: usePaymaster ? erc20PaymasterAddress : zeroAddress, }) console.log('\nInit Code Created.') @@ -129,8 +128,8 @@ const senderAddress = await getAccountAddress({ safeSingletonAddress: chainAddresses.SAFE_SINGLETON_ADDRESS, saltNonce: saltNonce, multiSendAddress: multiSendAddress, - erc20TokenAddress: usdcTokenAddress, - paymasterAddress: erc20PaymasterAddress, + erc20TokenAddress: usePaymaster ? usdcTokenAddress : zeroAddress, + paymasterAddress: usePaymaster ? erc20PaymasterAddress : zeroAddress, }) console.log('\nCounterfactual Sender Address Created:', senderAddress) if (chain == 'base-sepolia') { @@ -234,11 +233,11 @@ if (usePaymaster) { // Check Sender ETH Balance. let senderETHBalance = await publicClient.getBalance({ address: senderAddress }) - console.log('\nSender ETH Balance:', ethers.utils.formatEther(senderETHBalance)) + console.log('\nSender ETH Balance:', ethers.formatEther(senderETHBalance)) // Checking required preFund. const requiredPrefund = getRequiredPrefund({ userOperation: userOp, entryPoint: ENTRYPOINT_ADDRESS_V07 }) - console.log('\nRequired Prefund:', ethers.utils.formatEther(requiredPrefund)) + console.log('\nRequired Prefund:', ethers.formatEther(requiredPrefund)) const requiredBalance = requiredPrefund + (txType == 'native-transfer' ? parseEther('0.000001') : 0n) diff --git a/examples/4337-gas-metering/utils/safe.ts b/examples/4337-gas-metering/utils/safe.ts index 26d8bab0..92544eaa 100644 --- a/examples/4337-gas-metering/utils/safe.ts +++ b/examples/4337-gas-metering/utils/safe.ts @@ -164,12 +164,13 @@ const getInitializerCode = async ({ }) } - const multiSendCallData = encodeMultiSend(setupTxs) + const recipient = setupTxs.length > 1 ? multiSendAddress : safeModuleSetupAddress + const calldata = setupTxs.length > 1 ? encodeMultiSend(setupTxs) : setupTxs[0].data return encodeFunctionData({ abi: SAFE_SETUP_ABI, functionName: 'setup', - args: [[owner], 1n, multiSendAddress, multiSendCallData, safe4337ModuleAddress, zeroAddress, 0n, zeroAddress], + args: [[owner], 1n, recipient, calldata, safe4337ModuleAddress, zeroAddress, 0n, zeroAddress], }) } diff --git a/examples/4337-gas-metering/utils/userOps.ts b/examples/4337-gas-metering/utils/userOps.ts index 4f30f803..51a39fbc 100644 --- a/examples/4337-gas-metering/utils/userOps.ts +++ b/examples/4337-gas-metering/utils/userOps.ts @@ -412,6 +412,7 @@ export const createCallData = async ( value: weiToSend, }) } + console.log('\nAppropriate calldata created.') return txCallData } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3753c5ee..2d82567d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -56,12 +56,18 @@ importers: '@alchemy/aa-core': specifier: 2.3.1 version: 2.3.1(bufferutil@4.0.8)(typescript@5.4.5)(utf-8-validate@5.0.10) + '@gelatonetwork/relay-sdk': + specifier: ^5.5.6 + version: 5.5.6(bufferutil@4.0.8)(utf-8-validate@5.0.10) alchemy-sdk: specifier: 3.1.2 version: 3.1.2(bufferutil@4.0.8)(utf-8-validate@5.0.10) dotenv: specifier: 16.4.5 version: 16.4.5 + ethers: + specifier: ^6.12.1 + version: 6.12.1(bufferutil@4.0.8)(utf-8-validate@5.0.10) permissionless: specifier: 0.1.29 version: 0.1.29(viem@2.10.8(bufferutil@4.0.8)(typescript@5.4.5)(utf-8-validate@5.0.10)(zod@3.23.8)) @@ -69,9 +75,6 @@ importers: specifier: 2.10.8 version: 2.10.8(bufferutil@4.0.8)(typescript@5.4.5)(utf-8-validate@5.0.10)(zod@3.23.8) devDependencies: - '@gelatonetwork/relay-sdk': - specifier: ^5.5.6 - version: 5.5.6(bufferutil@4.0.8)(utf-8-validate@5.0.10) '@types/node': specifier: 20.12.12 version: 20.12.12 @@ -6103,7 +6106,7 @@ snapshots: '@scure/bip32@1.3.2': dependencies: '@noble/curves': 1.2.0 - '@noble/hashes': 1.3.2 + '@noble/hashes': 1.3.3 '@scure/base': 1.1.6 '@scure/bip32@1.3.3': @@ -6119,7 +6122,7 @@ snapshots: '@scure/bip39@1.2.1': dependencies: - '@noble/hashes': 1.3.2 + '@noble/hashes': 1.3.3 '@scure/base': 1.1.6 '@scure/bip39@1.2.2':