Skip to content

Commit

Permalink
optimize benchmark by not using multisend when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed May 21, 2024
1 parent 71afc4b commit 0f3c8af
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
3 changes: 2 additions & 1 deletion examples/4337-gas-metering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 9 additions & 10 deletions examples/4337-gas-metering/pimlico/pimlico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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.')
}
}

Expand Down Expand Up @@ -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.')

Expand All @@ -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') {
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions examples/4337-gas-metering/utils/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/4337-gas-metering/utils/userOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export const createCallData = async (
value: weiToSend,
})
}

console.log('\nAppropriate calldata created.')
return txCallData
}
13 changes: 8 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f3c8af

Please sign in to comment.