|
| 1 | +--- |
| 2 | +description: Learn how to deploy a MetaMask smart account. |
| 3 | +--- |
| 4 | + |
| 5 | +import Tabs from "@theme/Tabs"; |
| 6 | +import TabItem from "@theme/TabItem"; |
| 7 | + |
| 8 | +# Deploy a smart account |
| 9 | + |
| 10 | +You can deploy MetaMask Smart Accounts in two different ways. You can either deploy the smart account automatically when sending |
| 11 | +the first user operation, or use a manual approach. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- [Install and set up the Delegation Toolkit.](../../get-started/install.md) |
| 16 | +- [Configure the Delegation Toolkit.](../configure.md) |
| 17 | +- [Create a MetaMask smart account](create-smart-account.md) |
| 18 | + |
| 19 | +## Sending first user operation |
| 20 | + |
| 21 | +Whenever you send the first user operation, it checks whether the smart account is already deployed. If the account |
| 22 | +is not deployed, the `initCode` is added to the user operation to ensure the smart account is deployed within the |
| 23 | +same operation. Internally, the `initCode` is encoded using the factory and factory data. |
| 24 | + |
| 25 | +<Tabs> |
| 26 | +<TabItem value="example.ts"> |
| 27 | + |
| 28 | +```typescript |
| 29 | +import { bundlerClient, smartAccount } from "./config.ts"; |
| 30 | +import { parseEther } from "viem"; |
| 31 | + |
| 32 | +// Appropriate fee per gas must be determined for the specific bundler being used. |
| 33 | +const maxFeePerGas = 1n; |
| 34 | +const maxPriorityFeePerGas = 1n; |
| 35 | + |
| 36 | +const userOperationHash = await bundlerClient.sendUserOperation({ |
| 37 | + account: smartAccount, |
| 38 | + calls: [ |
| 39 | + { |
| 40 | + to: "0x1234567890123456789012345678901234567890", |
| 41 | + value: parseEther("1") |
| 42 | + } |
| 43 | + ], |
| 44 | + maxFeePerGas, |
| 45 | + maxPriorityFeePerGas |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +</TabItem> |
| 50 | + |
| 51 | +<TabItem value="config.ts"> |
| 52 | + |
| 53 | +```typescript |
| 54 | +import { createPublicClient, http } from "viem"; |
| 55 | +import { createBundlerClient } from "viem/account-abstraction"; |
| 56 | +import { sepolia as chain } from "viem/chains"; |
| 57 | +import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
| 58 | +import { |
| 59 | + Implementation, |
| 60 | + toMetaMaskSmartAccount, |
| 61 | +} from "@metamask/delegation-toolkit"; |
| 62 | + |
| 63 | + |
| 64 | +const publicClient = createPublicClient({ |
| 65 | + chain, |
| 66 | + transport: http() |
| 67 | +}); |
| 68 | + |
| 69 | +const privateKey = generatePrivateKey(); |
| 70 | +const account = privateKeyToAccount(privateKey); |
| 71 | + |
| 72 | +export const smartAccount = await toMetaMaskSmartAccount({ |
| 73 | + client: publicClient, |
| 74 | + implementation: Implementation.Hybrid, |
| 75 | + deployParams: [account.address, [], [], []], |
| 76 | + deploySalt: "0x", |
| 77 | + signatory: { account }, |
| 78 | +}); |
| 79 | + |
| 80 | +export const bundlerClient = createBundlerClient({ |
| 81 | + client: publicClient, |
| 82 | + transport: http("https://public.pimlico.io/v2/11155111/rpc") |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +</TabItem> |
| 87 | +</Tabs> |
| 88 | + |
| 89 | +## Manual approach |
| 90 | + |
| 91 | +To use the manual approach, you can call the MetaMask Smart Account [`getFactoryArgs`](../../reference/api/smart-account#getfactoryargs) |
| 92 | +method to retrieve the `factory` and `factoryData`. This allows you to use a relay account to sponsor the deployment without needing a paymaster. |
| 93 | + |
| 94 | +The `factory` represents the contract address responsible for deploying the smart account, while `factoryData` contains the |
| 95 | +calldata that will be executed by the `factory` to deploy the smart account. |
| 96 | + |
| 97 | +The relay account can be either an externally owned account (EOA) or another smart account. This example uses an EOA. |
| 98 | + |
| 99 | +<Tabs> |
| 100 | +<TabItem value="example.ts"> |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { walletClient, smartAccount } from "./config.ts"; |
| 104 | + |
| 105 | +const { factory, factoryData } = await smartAccount.getFactoryArgs(); |
| 106 | + |
| 107 | +// Deploy smart account using relay account. |
| 108 | +const hash = await walletClient.sendTransaction({ |
| 109 | + to: factory, |
| 110 | + data: factoryData, |
| 111 | +}) |
| 112 | +``` |
| 113 | + |
| 114 | +</TabItem> |
| 115 | + |
| 116 | +<TabItem value="config.ts"> |
| 117 | + |
| 118 | +```typescript |
| 119 | +import { createPublicClient, createWalletClient, http } from "viem"; |
| 120 | +import { sepolia as chain } from "viem/chains"; |
| 121 | +import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
| 122 | +import { |
| 123 | + Implementation, |
| 124 | + toMetaMaskSmartAccount, |
| 125 | +} from "@metamask/delegation-toolkit"; |
| 126 | + |
| 127 | + |
| 128 | +const publicClient = createPublicClient({ |
| 129 | + chain, |
| 130 | + transport: http() |
| 131 | +}); |
| 132 | + |
| 133 | +const privateKey = generatePrivateKey(); |
| 134 | +const account = privateKeyToAccount(privateKey); |
| 135 | + |
| 136 | +export const smartAccount = await toMetaMaskSmartAccount({ |
| 137 | + client: publicClient, |
| 138 | + implementation: Implementation.Hybrid, |
| 139 | + deployParams: [account.address, [], [], []], |
| 140 | + deploySalt: "0x", |
| 141 | + signatory: { account }, |
| 142 | +}); |
| 143 | + |
| 144 | +const relayAccountPrivateKey = "0x121.."; |
| 145 | +const relayAccount = privateKeyToAccount(relayAccountPrivateKey) |
| 146 | + |
| 147 | +export const walletClient = createWalletClient({ |
| 148 | + account: relayAccount, |
| 149 | + chain, |
| 150 | + transport: http() |
| 151 | +}) |
| 152 | +``` |
| 153 | + |
| 154 | +</TabItem> |
| 155 | +</Tabs> |
| 156 | + |
| 157 | +## Next steps |
| 158 | + |
| 159 | +- See [Send a user operation](send-user-operation.md) to learn how to send user operations. |
| 160 | +- See [Send a gasless transaction](send-gasless-transaction.md) to learn how to use gasless paymaster. |
0 commit comments