-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Delegation Toolkit Add ERC-20 paymaster tutorial #2259
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 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
65be6d7
add erc-20 paymaster tutorial
e20195f
Merge branch 'main' into feat/erc-20-paymaster-tutorial
67d2d05
merge with main
d193cff
fix typo
f5cadc9
improve steps
fca3414
edit tutorial
alexandratran c9cb918
Merge branch 'main' into feat/erc-20-paymaster-tutorial
alexandratran 5cca532
Merge branch 'main' into feat/erc-20-paymaster-tutorial
alexandratran 35321cf
edit tutorial and change to manual sidebar
alexandratran a6fffc7
Merge branch 'main' into feat/erc-20-paymaster-tutorial
alexandratran 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
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,150 @@ | ||
--- | ||
description: Follow this tutorial to use ERC-20 paymaster with MetaMask Smart Accounts. | ||
sidebar_position: 1 | ||
--- | ||
|
||
# Use an ERC-20 paymaster | ||
|
||
This tutorial walks you through using an ERC-20 paymaster with [MetaMask Smart Accounts](../concepts/smart-accounts), enabling users to pay gas fees in USDC. | ||
This tutorial uses Pimlico's paymaster, but you can use any paymaster of your choice. | ||
|
||
## About paymasters | ||
|
||
A paymaster is an important component of the [account abstraction (ERC-4337)](../concepts/smart-accounts.md) standard, responsible for abstracting gas fees for end users. | ||
There are different types of paymasters, such as gasless paymasters and ERC-20 paymasters. | ||
While a gasless paymaster covers the transaction on behalf of the user, an ERC-20 paymaster allows users to pay gas fees using a supported ERC-20 token. | ||
This removes the need for users to hold native tokens, allowing them to perform onchain actions using only stablecoins. | ||
|
||
## Prerequisites | ||
|
||
- [Install and set up the Delegation Toolkit](../get-started/install) in your project. | ||
- [Configure the Delegation Toolkit](../guides/configure). | ||
- [Create a Hybrid smart account](../guides/smart-accounts/create-smart-account), and fund it with some Sepolia USDC to pay gas fees. | ||
- [Create a Pimlico API key](https://docs.pimlico.io/guides/create-api-key#create-api-key). | ||
|
||
## Steps | ||
|
||
### 1. Create a Public Client | ||
|
||
Create a [Viem Public Client](https://viem.sh/docs/clients/public) using Viem's `createPublicClient` function. | ||
You will configure a smart account and Bundler Client with the Public Client, which you can use to query the signer's account state and interact with the blockchain network. | ||
|
||
```typescript | ||
import { createPublicClient, http } from "viem"; | ||
import { sepolia as chain } from "viem/chains"; | ||
|
||
const publicClient = createPublicClient({ | ||
chain, | ||
transport: http(), | ||
}); | ||
``` | ||
|
||
### 2. Create a Paymaster Client | ||
|
||
Create a [Viem Paymaster Client](https://viem.sh/account-abstraction/clients/paymaster) | ||
using Viem's `createPaymasterClient` function. This client interacts with the paymaster service, enabling users to pay gas fees in USDC. | ||
|
||
Replace `<YOUR-API-KEY>` with your Pimlico API key: | ||
|
||
```typescript | ||
import { createPaymasterClient } from "viem/account-abstraction"; | ||
|
||
const paymasterClient = createPaymasterClient({ | ||
transport: http("https://api.pimlico.io/v2/11155111/rpc?apikey=<YOUR-API-KEY>"), | ||
}); | ||
``` | ||
|
||
### 3. Create a Bundler Client | ||
|
||
Create a [Viem Bundler Client](https://viem.sh/account-abstraction/clients/bundler) using Viem's `createBundlerClient` function. You can use the bundler service to estimate gas for user operations and submit transactions to the network. | ||
|
||
To use the ERC-20 paymaster, configure the `paymasterContext` with the ERC-20 token you wish to use to pay for gas fees. | ||
For this tutorial, specify the Sepolia USDC token address. | ||
|
||
```typescript | ||
import { createBundlerClient } from "viem/account-abstraction"; | ||
|
||
// USDC address on Ethereum Sepolia. | ||
const token = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"; | ||
|
||
const bundlerClient = createBundlerClient({ | ||
client: publicClient, | ||
transport: http("https://your-bundler-rpc.com"), | ||
paymasterContext: { | ||
token, | ||
} | ||
}); | ||
``` | ||
|
||
### 4. Create a Hybrid smart account | ||
|
||
Configure a [Hybrid smart account](../guides/smart-accounts/create-smart-account.md#create-a-hybrid-smart-account), which is a flexible smart account implementation | ||
that supports both an externally owned account (EOA) owner and any number of passkey (WebAuthn) signers. | ||
|
||
```typescript | ||
import { Implementation, toMetaMaskSmartAccount } from "@metamask/delegation-toolkit"; | ||
import { privateKeyToAccount } from "viem/accounts"; | ||
|
||
const account = privateKeyToAccount("0x..."); | ||
|
||
const smartAccount = await toMetaMaskSmartAccount({ | ||
client: publicClient, | ||
implementation: Implementation.Hybrid, | ||
deployParams: [account.address, [], [], []], | ||
deploySalt: "0x", | ||
signatory: { account }, | ||
}); | ||
``` | ||
|
||
### 5. Send a user operation | ||
|
||
The ERC-20 paymaster works by transferring the token from the smart account, and reimbursing itself for paying the gas fees on the user's behalf. | ||
|
||
To send a user operation with the ERC-20 paymaster, use the [`sendUserOperation`](https://viem.sh/account-abstraction/actions/bundler/sendUserOperation) method from the Bundler Client. | ||
You must include a call approving the ERC-20 token to be used by the paymaster. | ||
To modify the token allowance for the paymaster, perform a write operation on the USDC contract. | ||
For this tutorial, set an allowance of 10 USDC tokens. | ||
|
||
:::note | ||
In a production dapp, you should first check the existing token allowance and only approve the amount required by the paymaster. | ||
::: | ||
|
||
Batch the approve call with other onchain actions you want to perform, and provide the `paymasterClient` from [Step 2](#2-create-a-paymaster-client) using the `paymaster` property. | ||
|
||
```typescript | ||
// Appropriate fee per gas must be determined for the specific bundler being used. | ||
const maxFeePerGas = 1n; | ||
const maxPriorityFeePerGas = 1n; | ||
|
||
const pimlicoPaymasterAddress = "0x777777777777AeC03fd955926DbF81597e66834C"; | ||
|
||
// 10 USDC in wei format. Since USDC has 6 decimals, the wei value is 10 * 10^6. | ||
const approvalAmount = 10000000n; | ||
|
||
const userOperationHash = await bundlerClient.sendUserOperation({ | ||
account: smartAccount, | ||
calls: [ | ||
// USDC approve call | ||
{ | ||
// USDC token address | ||
to: token, | ||
abi: parseAbi(["function approve(address,uint)"]), | ||
functionName: "approve", | ||
args: [pimlicoPaymasterAddress, approvalAmount], | ||
} | ||
// Batch the approve call with other onchain actions. | ||
{ | ||
to: "0x1234567890123456789012345678901234567890", | ||
value: parseEther("1"), | ||
}, | ||
alexandratran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
maxFeePerGas, | ||
maxPriorityFeePerGas, | ||
paymaster: paymasterClient, | ||
}); | ||
``` | ||
|
||
## Next steps | ||
|
||
- See [Create a MetaMask smart account](../guides/smart-accounts/create-smart-account.md) to learn more about smart account implementations. | ||
- See [Send a gasless transaction](../guides/smart-accounts/send-gasless-transaction.md) to learn how to use gasless paymaster. |
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.