Skip to content

Commit 0e7a027

Browse files
author
“AyushBherwani1998”
committed
add deploy smart account guide
1 parent 92c81a8 commit 0e7a027

File tree

8 files changed

+167
-6
lines changed

8 files changed

+167
-6
lines changed

delegation-toolkit/guides/smart-accounts/create-smart-account.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export const walletClient = createWalletClient({
413413

414414
With a MetaMask smart account, you can perform the following functions:
415415

416-
- In conjunction with [Viem Account Abstraction clients](../configure.md), deploy the smart account
416+
- In conjunction with [Viem Account Abstraction clients](../configure.md), [deploy the smart account](deploy-smart-account.md)
417417
and [send user operations](send-user-operation.md).
418418
- [Create delegations](../delegation/execute-on-smart-accounts-behalf.md) that can be used to grant specific rights and permissions to other accounts.
419419
Smart accounts that create delegations are called *delegator accounts*.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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.

delegation-toolkit/guides/smart-accounts/send-user-operation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const smartAccount = await toMetaMaskSmartAccount({
9393

9494
export const bundlerClient = createBundlerClient({
9595
client: publicClient,
96-
transport: http("https://public.pimlico.io/v2/1/rpc")
96+
transport: http("https://public.pimlico.io/v2/11155111/rpc")
9797
});
9898
```
9999

gator-sidebar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const sidebar = {
5858
collapsed: false,
5959
items: [
6060
'guides/smart-accounts/create-smart-account',
61+
'guides/smart-accounts/deploy-smart-account',
6162
'guides/smart-accounts/send-user-operation',
6263
'guides/smart-accounts/send-gasless-transaction',
6364
'guides/smart-accounts/generate-multisig-signature',

gator_versioned_docs/version-0.10.1/how-to/send-user-operation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const smartAccount = await toMetaMaskSmartAccount({
9191

9292
export const bundlerClient = createBundlerClient({
9393
publicClient,
94-
transport: http("https://public.pimlico.io/v2/1/rpc")
94+
transport: http("https://public.pimlico.io/v2/11155111/rpc")
9595
});
9696
```
9797

gator_versioned_docs/version-0.10.2/how-to/send-user-operation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const smartAccount = await toMetaMaskSmartAccount({
9191

9292
export const bundlerClient = createBundlerClient({
9393
publicClient,
94-
transport: http("https://public.pimlico.io/v2/1/rpc")
94+
transport: http("https://public.pimlico.io/v2/11155111/rpc")
9595
});
9696
```
9797

gator_versioned_docs/version-0.11.0/how-to/send-user-operation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const smartAccount = await toMetaMaskSmartAccount({
9494

9595
export const bundlerClient = createBundlerClient({
9696
client: publicClient,
97-
transport: http("https://public.pimlico.io/v2/1/rpc")
97+
transport: http("https://public.pimlico.io/v2/11155111/rpc")
9898
});
9999
```
100100

gator_versioned_docs/version-0.12.0/how-to/send-user-operation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const smartAccount = await toMetaMaskSmartAccount({
9494

9595
export const bundlerClient = createBundlerClient({
9696
client: publicClient,
97-
transport: http("https://public.pimlico.io/v2/1/rpc")
97+
transport: http("https://public.pimlico.io/v2/11155111/rpc")
9898
});
9999
```
100100

0 commit comments

Comments
 (0)