-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Delegation Toolkit] Deploy smart account guide #2270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e7a027
add deploy smart account guide
d852030
update lineaSepolia to sepolia
2b33b34
resolve cursor bot comments
16342ec
revert sepolia change
558a14b
Merge branch 'main' into feat/deploy-smart-account
AyushBherwani1998 94fa0eb
Apply suggestions from code review
AyushBherwani1998 4029226
Accept review suggestions
AyushBherwani1998 3778470
Merge branch 'main' into feat/deploy-smart-account
AyushBherwani1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
delegation-toolkit/guides/smart-accounts/deploy-smart-account.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
--- | ||
description: Learn how to deploy a MetaMask smart account. | ||
--- | ||
|
||
import Tabs from "@theme/Tabs"; | ||
import TabItem from "@theme/TabItem"; | ||
|
||
# Deploy a smart account | ||
|
||
You can deploy MetaMask Smart Accounts in two different ways. You can either deploy a smart account automatically when sending | ||
the first user operation, or manually deploy the account. | ||
|
||
## Prerequisites | ||
|
||
- [Install and set up the Delegation Toolkit.](../../get-started/install.md) | ||
- [Configure the Delegation Toolkit.](../configure.md) | ||
- [Create a MetaMask smart account.](create-smart-account.md) | ||
|
||
## Deploy with the first user operation | ||
|
||
When you send the first user operation from a smart account, the Delegation Toolkit checks whether the account is already deployed. If the account | ||
is not deployed, the toolkit adds the `initCode` to the user operation to deploy the account within the | ||
same operation. Internally, the `initCode` is encoded using the `factory` and `factoryData`. | ||
|
||
<Tabs> | ||
<TabItem value="example.ts"> | ||
|
||
```typescript | ||
import { bundlerClient, smartAccount } from "./config.ts"; | ||
import { parseEther } from "viem"; | ||
|
||
// Appropriate fee per gas must be determined for the specific bundler being used. | ||
const maxFeePerGas = 1n; | ||
const maxPriorityFeePerGas = 1n; | ||
|
||
const userOperationHash = await bundlerClient.sendUserOperation({ | ||
account: smartAccount, | ||
calls: [ | ||
{ | ||
to: "0x1234567890123456789012345678901234567890", | ||
value: parseEther("1") | ||
} | ||
], | ||
maxFeePerGas, | ||
maxPriorityFeePerGas | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="config.ts"> | ||
|
||
```typescript | ||
import { createPublicClient, http } from "viem"; | ||
import { createBundlerClient } from "viem/account-abstraction"; | ||
import { sepolia as chain } from "viem/chains"; | ||
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; | ||
import { | ||
Implementation, | ||
toMetaMaskSmartAccount, | ||
} from "@metamask/delegation-toolkit"; | ||
|
||
|
||
const publicClient = createPublicClient({ | ||
chain, | ||
transport: http() | ||
}); | ||
|
||
const privateKey = generatePrivateKey(); | ||
const account = privateKeyToAccount(privateKey); | ||
|
||
export const smartAccount = await toMetaMaskSmartAccount({ | ||
client: publicClient, | ||
implementation: Implementation.Hybrid, | ||
deployParams: [account.address, [], [], []], | ||
deploySalt: "0x", | ||
signatory: { account }, | ||
}); | ||
|
||
export const bundlerClient = createBundlerClient({ | ||
client: publicClient, | ||
transport: http("https://public.pimlico.io/v2/11155111/rpc") | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Deploy manually | ||
|
||
To deploy a smart account manually, call the [`getFactoryArgs`](../../reference/api/smart-account.md#getfactoryargs) | ||
method from the smart account to retrieve the `factory` and `factoryData`. This allows you to use a relay account to sponsor the deployment without needing a paymaster. | ||
|
||
The `factory` represents the contract address responsible for deploying the smart account, while `factoryData` contains the | ||
calldata that will be executed by the `factory` to deploy the smart account. | ||
|
||
The relay account can be either an externally owned account (EOA) or another smart account. This example uses an EOA. | ||
|
||
<Tabs> | ||
<TabItem value="example.ts"> | ||
|
||
```typescript | ||
import { walletClient, smartAccount } from "./config.ts"; | ||
|
||
const { factory, factoryData } = await smartAccount.getFactoryArgs(); | ||
|
||
// Deploy smart account using relay account. | ||
const hash = await walletClient.sendTransaction({ | ||
to: factory, | ||
data: factoryData, | ||
}) | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="config.ts"> | ||
|
||
```typescript | ||
import { createPublicClient, createWalletClient, http } from "viem"; | ||
import { sepolia as chain } from "viem/chains"; | ||
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; | ||
import { | ||
Implementation, | ||
toMetaMaskSmartAccount, | ||
} from "@metamask/delegation-toolkit"; | ||
|
||
|
||
const publicClient = createPublicClient({ | ||
chain, | ||
transport: http() | ||
}); | ||
|
||
const privateKey = generatePrivateKey(); | ||
const account = privateKeyToAccount(privateKey); | ||
|
||
export const smartAccount = await toMetaMaskSmartAccount({ | ||
client: publicClient, | ||
implementation: Implementation.Hybrid, | ||
deployParams: [account.address, [], [], []], | ||
deploySalt: "0x", | ||
signatory: { account }, | ||
}); | ||
|
||
const relayAccountPrivateKey = "0x121.."; | ||
const relayAccount = privateKeyToAccount(relayAccountPrivateKey) | ||
|
||
export const walletClient = createWalletClient({ | ||
account: relayAccount, | ||
chain, | ||
transport: http() | ||
}) | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Next steps | ||
|
||
- Learn more about [sending user operations](send-user-operation.md). | ||
- To sponsor gas for end users, see how to [send a gasless transaction](send-gasless-transaction.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.