Skip to content

Commit

Permalink
Simplify pimlico bundler client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed May 16, 2024
1 parent 568e34a commit 29801fb
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 985 deletions.
4 changes: 2 additions & 2 deletions examples/4337-gas-metering/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ PIMLICO_API_KEY = "" # https://dashboard.pimlico.io/apikeys
PIMLICO_GAS_POLICY = "" # https://dashboard.pimlico.io/sponsorship-policies
PIMLICO_ENTRYPOINT_ADDRESS = "0x0000000071727De22E5E9d8BAf0edAc6f37da032"
PIMLICO_MULTISEND_ADDRESS = "0x38869bf66a61cF6bDB996A6aE40D5853Fd43B526" # https://github.com/safe-global/safe-deployments/blob/main/src/assets/v1.4.1/multi_send.json
PIMLICO_ERC20_PAYMASTER_ADDRESS = "0x00000000002E3A39aFEf1132214fEee5a55ce127" # https://docs.pimlico.io/paymaster/erc20-paymaster/contract-addresses
PIMLICO_USDC_TOKEN_ADDRESS = "0x036CbD53842c5426634e7929541eC2318f3dCF7e" # Sepolia USDC Address used by Pimlico
PIMLICO_ERC20_PAYMASTER_ADDRESS = "0x000000000041F3aFe8892B48D88b6862efe0ec8d" # https://docs.pimlico.io/paymaster/erc20-paymaster/contract-addresses
PIMLICO_USDC_TOKEN_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238" # Sepolia USDC Address used by Pimlico
# Make sure all nonce are unique for it to deploy account when run initially.
PIMLICO_NONCE = "3"
# Pimlico Token Operation Values (Based on the chain selected, these values should also change accordingly.)
Expand Down
10 changes: 5 additions & 5 deletions examples/4337-gas-metering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
"@alchemy/aa-core": "2.3.1",
"alchemy-sdk": "3.1.2",
"dotenv": "16.4.5",
"permissionless": "0.1.16",
"viem": "2.9.3"
"permissionless": "0.1.29",
"viem": "2.10.8"
},
"devDependencies": {
"@gelatonetwork/relay-sdk": "^5.5.6",
"@types/node": "20.11.30",
"tsx": "4.7.1",
"typescript": "^5.4.3"
"@types/node": "20.12.12",
"tsx": "4.10.3",
"typescript": "^5.4.5"
}
}
39 changes: 38 additions & 1 deletion examples/4337-gas-metering/pimlico/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
# Pimlico

## Safe Deployment with Pimlico Paymaster (Own Sponsorship)
## Safe Deployment With Pimlico (No Paymaster)

```
npm run pimlico:account
> @safe-global/safe-modules-example-4337-gas-metering@1.0.0 pimlico:account
> tsx ./pimlico/pimlico.ts account
Signer Extracted from Private Key.
Init Code Created.
Counterfactual Sender Address Created: 0x967c56B4Bc5628Fd275b3d466Cc075BeB33Fc7Fc
Address Link: https://sepolia.etherscan.io/address/0x967c56B4Bc5628Fd275b3d466Cc075BeB33Fc7Fc
Deploying a new Safe and executing calldata passed with it (if any).
Nonce for the sender received from EntryPoint.
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
Querying for receipts...
Receipt found!
Transaction hash: 0x09c9cad7d6a4fe405531e132f3e53e02599a7a379f33fc3378d75a80ae80ef7f
Transaction Link: https://sepolia.etherscan.io/tx/0x09c9cad7d6a4fe405531e132f3e53e02599a7a379f33fc3378d75a80ae80ef7f
Gas Used (Account or Paymaster): 433672
Gas Used (Transaction): 419491
```


## Safe Deployment with Pimlico Paymaster (Own Sponsorship)

```
npm run pimlico:account:paymaster
> @safe-global/4337-gas-metering@1.0.0 pimlico:account
> tsx ./pimlico/pimlico.ts account
Expand Down
61 changes: 22 additions & 39 deletions examples/4337-gas-metering/pimlico/pimlico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ if (argv.length < 1 || argv.length > 2) {
if (policyID) {
usePaymaster = true
} else {
throw new Error('Paymaster requires policyID to be set.')
// throw new Error('Paymaster requires policyID to be set.')
usePaymaster = true
}
}

Expand All @@ -77,51 +78,33 @@ if (!privateKey) {
throw new Error('Please populate .env file with demo Private Key. Recommended to not use your personal private key.')
}

// Check if the network is supported.
if (chain != 'sepolia' && chain != 'base-sepolia') {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}

// Extract Signer from Private Key.
const signer = privateKeyToAccount(privateKey as Hash)
console.log('Signer Extracted from Private Key.')

// Create a public, bundler and paymaster Client for the Chain.
let bundlerClient
let publicClient
let pimlicoPaymasterClient
if (chain == 'sepolia') {
bundlerClient = createClient({
transport: http(`https://api.pimlico.io/v2/${chain}/rpc?apikey=${apiKey}`),
chain: sepolia,
})
.extend(bundlerActions(ENTRYPOINT_ADDRESS_V07))
.extend(pimlicoBundlerActions(ENTRYPOINT_ADDRESS_V07))

publicClient = createPublicClient({
transport: http(rpcURL),
chain: sepolia,
})

pimlicoPaymasterClient = createClient({
transport: http(`https://api.pimlico.io/v2/${chain}/rpc?apikey=${apiKey}`),
chain: sepolia,
}).extend(pimlicoPaymasterActions(ENTRYPOINT_ADDRESS_V07))
} else if (chain == 'base-sepolia') {
bundlerClient = createClient({
transport: http(`https://api.pimlico.io/v2/${chain}/rpc?apikey=${apiKey}`),
chain: baseSepolia,
})
.extend(bundlerActions(ENTRYPOINT_ADDRESS_V07))
.extend(pimlicoBundlerActions(ENTRYPOINT_ADDRESS_V07))
const viemChain = chain == 'sepolia' ? sepolia : baseSepolia
const bundlerClient = createClient({
transport: http(`https://api.pimlico.io/v2/${chain}/rpc?apikey=${apiKey}`),
chain: viemChain,
})
.extend(bundlerActions(ENTRYPOINT_ADDRESS_V07))
.extend(pimlicoBundlerActions(ENTRYPOINT_ADDRESS_V07))

publicClient = createPublicClient({
transport: http(rpcURL),
chain: baseSepolia,
})
const publicClient = createPublicClient({
transport: http(rpcURL),
chain: viemChain,
})

pimlicoPaymasterClient = createClient({
transport: http(`https://api.pimlico.io/v2/${chain}/rpc?apikey=${apiKey}`),
chain: baseSepolia,
}).extend(pimlicoPaymasterActions(ENTRYPOINT_ADDRESS_V07))
} else {
throw new Error('Current code only support limited networks. Please make required changes if you want to use custom network.')
}
const pimlicoPaymasterClient = createClient({
transport: http(`https://api.pimlico.io/v2/${chain}/rpc?apikey=${apiKey}`),
chain: viemChain,
}).extend(pimlicoPaymasterActions(ENTRYPOINT_ADDRESS_V07))

// Get Safe Init Code.
const initCode = await getAccountInitCode({
Expand Down
8 changes: 4 additions & 4 deletions examples/4337-gas-metering/utils/userOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type gasData = {
export const submitUserOperationPimlico = async (
userOperation: UserOperation,
bundlerClient: any,
entryPointAddress: any,
entryPointAddress: string,
chain: string,
) => {
const userOperationHash = await bundlerClient.sendUserOperation({
Expand Down Expand Up @@ -85,9 +85,9 @@ export const submitUserOperationPimlico = async (
export const signUserOperation = async (
userOperation: UserOperation,
signer: PrivateKeyAccount,
chainID: any,
entryPointAddress: any,
safe4337ModuleAddress: any,
chainID: number,
entryPointAddress: `0x${string}`,
safe4337ModuleAddress: `0x${string}`,
) => {
const signatures = [
{
Expand Down
3 changes: 2 additions & 1 deletion modules/4337/test/e2e/LocalBundler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ describe('Local Bundler [@4337]', () => {
signature,
})

await bundler.sendUserOperation(userOp, await entryPoint.getAddress())
const userOpHash = await bundler.sendUserOperation(userOp, await entryPoint.getAddress())

await waitForUserOp(userOp)
console.log(await bundler.send("eth_getUserOperationReceipt", [userOpHash]))
expect(await token.balanceOf(safe.address)).to.equal(0n)
expect(await ethers.provider.getBalance(safe.address)).to.be.lessThan(ethers.parseEther('0.5'))
})
Expand Down
Loading

0 comments on commit 29801fb

Please sign in to comment.