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 f645d0b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 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
20 changes: 10 additions & 10 deletions examples/4337-gas-metering/pimlico/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Safe Deployment With Pimlico (No Paymaster)

```
npm run pimlico:account
pnpm run pimlico:account
> @safe-global/safe-modules-example-4337-gas-metering@1.0.0 pimlico:account
> tsx ./pimlico/pimlico.ts account
Expand All @@ -12,8 +12,8 @@ Signer Extracted from Private Key.
Init Code Created.
Counterfactual Sender Address Created: 0x967c56B4Bc5628Fd275b3d466Cc075BeB33Fc7Fc
Address Link: https://sepolia.etherscan.io/address/0x967c56B4Bc5628Fd275b3d466Cc075BeB33Fc7Fc
Counterfactual Sender Address Created: 0x90BFbCEf51C6b170D309C0A80B6062ffDfc8FbB8
Address Link: https://sepolia.etherscan.io/address/0x90BFbCEf51C6b170D309C0A80B6062ffDfc8FbB8
Deploying a new Safe and executing calldata passed with it (if any).
Expand All @@ -23,17 +23,17 @@ Appropriate calldata created.
Sender ETH Balance: 0.0
Required Prefund: 0.065434566830611091
UserOperation submitted. Hash: 0x4d02a5c5b7f78843e392a0ba7ab3abc23e34ebcf9dbb89bcb48112805806d15a
UserOp Link: https://jiffyscan.xyz/userOpHash/0x4d02a5c5b7f78843e392a0ba7ab3abc23e34ebcf9dbb89bcb48112805806d15a?network=sepolia
Required Prefund: 0.071222563201076969
UserOperation submitted. Hash: 0x1a2644dfcb8ba01986faf3fb7b6adde6945e7aa2410405541032a312071e6f8a
UserOp Link: https://jiffyscan.xyz/userOpHash/0x1a2644dfcb8ba01986faf3fb7b6adde6945e7aa2410405541032a312071e6f8a?network=sepolia
Querying for receipts...
Receipt found!
Transaction hash: 0x09c9cad7d6a4fe405531e132f3e53e02599a7a379f33fc3378d75a80ae80ef7f
Transaction Link: https://sepolia.etherscan.io/tx/0x09c9cad7d6a4fe405531e132f3e53e02599a7a379f33fc3378d75a80ae80ef7f
Transaction hash: 0xe3656d4b26c17871350e3c589d6afee340c538cfd4636c68dedd76706dbd08fd
Transaction Link: https://sepolia.etherscan.io/tx/0xe3656d4b26c17871350e3c589d6afee340c538cfd4636c68dedd76706dbd08fd
Gas Used (Account or Paymaster): 433672
Gas Used (Transaction): 419491
Gas Used (Account or Paymaster): 423757
Gas Used (Transaction): 409872
```


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 f645d0b

Please sign in to comment.