|
| 1 | +--- |
| 2 | +description: Upgrade an externally owned account (EOA) to a smart account |
| 3 | +sidebar_position: 3 |
| 4 | +sidebar_label: EIP-7702 quickstart |
| 5 | +--- |
| 6 | + |
| 7 | +# EIP-7702 quickstart |
| 8 | + |
| 9 | +This page demonstrates how to upgrade your externally owned account (EOA) to support MetaMask smart account |
| 10 | +functionality using an [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) transaction. This enables your EOA to leverage the benefits of account |
| 11 | +abstraction, such as batch transactions, gas sponsorship, and [ERC-7710 delegation capabilities](./../concepts/delegation.md). |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- [Install and set up the Delegation Toolkit.](install.md) |
| 16 | +- [Install Viem](https://viem.sh/) |
| 17 | + |
| 18 | +## Steps |
| 19 | + |
| 20 | +### 1. Set up a Public Client |
| 21 | + |
| 22 | +Set up a [Viem Public Client](https://viem.sh/docs/clients/public) using Viem's `createPublicClient` function. |
| 23 | +This client will let the EOA query the account state and interact with blockchain network. |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { createPublicClient, http } from "viem"; |
| 27 | +import { sepolia as chain } from "viem/chains"; |
| 28 | + |
| 29 | +const publicClient = createPublicClient({ |
| 30 | + chain, |
| 31 | + transport: http(), |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. Set up a Bundler Client |
| 36 | + |
| 37 | +Set up a [Viem Bundler Client](https://viem.sh/account-abstraction/clients/bundler) using Viem's `createBundlerClient` function. |
| 38 | +This lets you use the bundler service to estimate gas for user operations and submit transactions to the network. |
| 39 | + |
| 40 | +```typescript |
| 41 | +import { createBundlerClient } from "viem/account-abstraction"; |
| 42 | + |
| 43 | +const bundlerClient = createBundlerClient({ |
| 44 | + client: publicClient, |
| 45 | + transport: http("https://your-bundler-rpc.com"), |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Set up a Wallet Client |
| 50 | + |
| 51 | +Set up [Viem Wallet Client](https://viem.sh/docs/clients/wallet) using Viem's `createWalletClient` function. |
| 52 | +This lets you sign and submit EIP-7702 authorization. |
| 53 | + |
| 54 | +```typescript |
| 55 | +import { createWalletClient, http } from "viem"; |
| 56 | +import { sepolia as chain } from "viem/chains"; |
| 57 | +import { privateKeyToAccount } from "viem/accounts"; |
| 58 | + |
| 59 | +export const account = privateKeyToAccount("0x..."); |
| 60 | + |
| 61 | +export const walletClient = createWalletClient({ |
| 62 | + account, |
| 63 | + chain, |
| 64 | + transport: http(), |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +### 4. Authorize a 7702 delegation |
| 69 | + |
| 70 | +Create an authorization to map the contract code to an EOA, and sign it |
| 71 | +using Viem's [`signAuthorization`](https://viem.sh/docs/eip7702/signAuthorization) action. The `signAuthorization` action |
| 72 | +does not support JSON-RPC accounts. |
| 73 | + |
| 74 | +This example uses [`EIP7702StatelessDeleGator`](https://github.com/MetaMask/delegation-framework/blob/main/src/EIP7702/EIP7702StatelessDeleGator.sol) as the EIP-7702 delegator contract. |
| 75 | +It follows a stateless design, as it does not store signer data in the contract's state. This approach |
| 76 | +provides a lightweight and secure way to upgrade an EOA to a smart account. |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { |
| 80 | + Implementation, |
| 81 | + toMetaMaskSmartAccount, |
| 82 | + getDeleGatorEnvironment, |
| 83 | +} from "@metamask/delegation-toolkit"; |
| 84 | +import { privateKeyToAccount } from "viem/accounts"; |
| 85 | + |
| 86 | +const environment = getDeleGatorEnvironment(sepolia.id); |
| 87 | +const contractAddress = environment.implementations.EIP7702StatelessDeleGatorImpl; |
| 88 | + |
| 89 | +const authorization = await walletClient.signAuthorization({ |
| 90 | + account, |
| 91 | + contractAddress, |
| 92 | + executor: "self", |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +### 5. Submit the authorization |
| 97 | + |
| 98 | +Once you have signed an authorization, you can send an EIP-7702 transaction to set the EOA code. |
| 99 | +Since the authorization cannot be sent by itself, you can include it alongside a dummy transaction. |
| 100 | + |
| 101 | +```ts |
| 102 | +import { zeroAddress } from "viem"; |
| 103 | + |
| 104 | +const hash = await walletClient.sendTransaction({ |
| 105 | + authorizationList: [authorization], |
| 106 | + data: "0x", |
| 107 | + to: zeroAddress, |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +### 6. Create a MetaMask smart account |
| 112 | + |
| 113 | +Create a smart account instance for the EOA and start |
| 114 | +leveraging the benefits of account abstraction. |
| 115 | + |
| 116 | +```ts |
| 117 | +import { |
| 118 | + Implementation, |
| 119 | + toMetaMaskSmartAccount, |
| 120 | +} from "@metamask/delegation-toolkit"; |
| 121 | + |
| 122 | +const addresses = await walletClient.getAddresses(); |
| 123 | +const address = addresses[0]; |
| 124 | + |
| 125 | +const smartAccount = await toMetaMaskSmartAccount({ |
| 126 | + client: publicClient, |
| 127 | + implementation: Implementation.Stateless7702, |
| 128 | + address, |
| 129 | + signatory: { walletClient }, |
| 130 | +}); |
| 131 | +``` |
| 132 | + |
| 133 | +### 7. Send a user operation |
| 134 | + |
| 135 | +Send a user operation through the upgraded EOA, using Viem's [`sendUserOperation`](https://viem.sh/account-abstraction/actions/bundler/sendUserOperation) method. |
| 136 | + |
| 137 | +```ts |
| 138 | +import { parseEther } from "viem"; |
| 139 | + |
| 140 | +// Appropriate fee per gas must be determined for the specific bundler being used. |
| 141 | +const maxFeePerGas = 1n; |
| 142 | +const maxPriorityFeePerGas = 1n; |
| 143 | + |
| 144 | +const userOperationHash = await bundlerClient.sendUserOperation({ |
| 145 | + account: smartAccount, |
| 146 | + calls: [ |
| 147 | + { |
| 148 | + to: "0x1234567890123456789012345678901234567890", |
| 149 | + value: parseEther("1") |
| 150 | + } |
| 151 | + ], |
| 152 | + maxFeePerGas, |
| 153 | + maxPriorityFeePerGas |
| 154 | +}); |
| 155 | +``` |
0 commit comments