|
| 1 | +#Sponsored Userop |
| 2 | + |
| 3 | +This guide will help understanding how to broadcast a [7702](https://eip7702.io/) sponsored userop using smart wallet implemented [here](https://github.com/eth-infinitism/account-abstraction/blob/develop/contracts/accounts/Simple7702Account.sol) using free bundler client and a paymaster client. |
| 4 | + |
| 5 | +### 1. Create Smart Account Client |
| 6 | +=== "account.ts" |
| 7 | + ```ts |
| 8 | + import { commonClient } from './client' |
| 9 | + import { toSimple7702SmartAccount } from 'viem/account-abstraction' |
| 10 | + import { privateKeyToAccount } from 'viem/accounts' |
| 11 | + const owner = privateKeyToAccount('0x...') // add private key here |
| 12 | + |
| 13 | + export const smartAccount = await toSimple7702SmartAccount({ |
| 14 | + client: commonClient, |
| 15 | + owner, |
| 16 | + }); |
| 17 | + ``` |
| 18 | +=== "client.ts" |
| 19 | + ```ts |
| 20 | + import { createFreeBundler } from '@etherspot/free-bundler' |
| 21 | + import { publicActions, walletActions } from 'viem' |
| 22 | + import { mainnet } from 'viem/chains' |
| 23 | + const chain = mainnet |
| 24 | + |
| 25 | + export const commonClient = createFreeBundler({chain}) |
| 26 | + .extend(publicActions) |
| 27 | + .extend(walletActions) |
| 28 | + ``` |
| 29 | + |
| 30 | +### 2. Create Paymaster client |
| 31 | +Refer [here](https://viem.sh/account-abstraction/clients/paymaster) for more options and detailed docs for paymaster client |
| 32 | +=== "paymaster.ts" |
| 33 | + ```ts |
| 34 | + import { createPaymasterClient } from "viem/account-abstraction" |
| 35 | + |
| 36 | + export const paymasterClient = createPaymasterClient({ |
| 37 | + transport: http("") // add paymaster url here |
| 38 | + }); |
| 39 | + |
| 40 | + // NOTE: this can change according to the paymaster implementation. |
| 41 | + // use the corresponding paymaster's docs to understand what has to used for context. |
| 42 | + export const paymasterContext = { policyId: "" } // add policy id here |
| 43 | + ``` |
| 44 | + |
| 45 | +### 3. Send Userop |
| 46 | +=== "example.ts" |
| 47 | + ```ts |
| 48 | + import { commonClient } from './client' |
| 49 | + import { toSimple7702SmartAccount } from 'viem/account-abstraction' |
| 50 | + import { privateKeyToAccount } from 'viem/accounts' |
| 51 | + import { SignAuthorizationReturnType } from 'viem' |
| 52 | + import { paymasterClient, paymasterContext } from './paymaster' |
| 53 | + |
| 54 | + const owner = privateKeyToAccount('0x...') // add private key here |
| 55 | + |
| 56 | + const smartAccount = await toSimple7702SmartAccount({ |
| 57 | + client: commonClient, |
| 58 | + owner, |
| 59 | + }) |
| 60 | + |
| 61 | + console.log("wallet:: ", smartAccount.address) |
| 62 | + |
| 63 | + // check sender's code to decide if eip7702Auth tuple is necessary for userOp. |
| 64 | + const senderCode = await commonClient.getCode({ |
| 65 | + address: smartAccount.address |
| 66 | + }) |
| 67 | + |
| 68 | + let authorization: SignAuthorizationReturnType | undefined |
| 69 | + const { address: delegateAddress } = smartAccount.authorization |
| 70 | + |
| 71 | + if(senderCode !== `0xef0100${delegateAddress.toLowerCase().substring(2)}`) { |
| 72 | + authorization = await commonClient.signAuthorization(smartAccount.authorization) |
| 73 | + } |
| 74 | + |
| 75 | + const userOpHash = await commonClient.sendUserOperation({ |
| 76 | + account: smartAccount, |
| 77 | + authorization, |
| 78 | + calls: [ |
| 79 | + {to: "0x09FD4F6088f2025427AB1e89257A44747081Ed59", value: parseUnits('0.0000001', 18)} |
| 80 | + ], |
| 81 | + paymaster: paymasterClient, |
| 82 | + paymasterContext: paymasterContext ? JSON.parse(paymasterContext) : undefined, |
| 83 | + }); |
| 84 | + |
| 85 | + console.log('userOpHash:: ', userOpHash) |
| 86 | + ``` |
| 87 | +=== "client.ts" |
| 88 | + ```ts |
| 89 | + import { createFreeBundler } from '@etherspot/free-bundler' |
| 90 | + import { publicActions, walletActions } from 'viem' |
| 91 | + import { mainnet } from 'viem/chains' |
| 92 | + const chain = mainnet |
| 93 | + |
| 94 | + export const commonClient = createFreeBundler({chain}) |
| 95 | + .extend(publicActions) |
| 96 | + .extend(walletActions) |
| 97 | + ``` |
| 98 | +=== "paymaster.ts" |
| 99 | + ```ts |
| 100 | + import { createPaymasterClient } from "viem/account-abstraction" |
| 101 | + |
| 102 | + export const paymasterClient = createPaymasterClient({ |
| 103 | + transport: http("") // add paymaster url here |
| 104 | + }); |
| 105 | + |
| 106 | + // NOTE: this can change according to the paymaster implementation. |
| 107 | + // use the corresponding paymaster's docs to understand what has to used for context. |
| 108 | + export const paymasterContext = { policyId: "" } // add policy id here |
| 109 | + ``` |
| 110 | +[](https://stackblitz.com/github/etherspot/free-bundler?file=examples%2Findex.ts) |
| 111 | + |
| 112 | +After opening StackBlitz, run: |
| 113 | +```bash |
| 114 | +npx tsx examples/index.ts \ |
| 115 | +--private-key 0x... \ |
| 116 | +--paymaster-url 'https://....' \ |
| 117 | +--paymaster-context '' |
| 118 | +``` |
| 119 | +<small><em>For more run options, see the <a href="https://github.com/etherspot/free-bundler/blob/master/examples/USAGE.md">GitHub examples usage</a>.</em></small> |
0 commit comments