|
| 1 | +--- |
| 2 | +title: Send your first transaction with ERC-20 Paymaster |
| 3 | +image: "guides-banners/erc20-paymaster.png" |
| 4 | +description: Learn how to use ERC-20 paymaster with Web3Auth Native Account Abstraction. |
| 5 | +type: guide |
| 6 | +tags: [pnp, account abstraction, web, paymaster, erc4337] |
| 7 | +date: October 29, 2024 |
| 8 | +author: Web3Auth Team |
| 9 | +--- |
| 10 | + |
| 11 | +import AccountAbstractionProviderInstallation from "@site/src/common/sdk/pnp/web/_aa-provider-installation.mdx"; |
| 12 | +import ConfigureSigners from "@site/src/common/sdk/pnp/web/_configure-aa-signers.mdx"; |
| 13 | +import AASendTransaction from "@site/src/common/sdk/pnp/web/_aa-send-transaction.mdx"; |
| 14 | + |
| 15 | +import SEO from "@site/src/components/SEO"; |
| 16 | +import TabItem from "@theme/TabItem"; |
| 17 | +import Tabs from "@theme/Tabs"; |
| 18 | + |
| 19 | +<SEO |
| 20 | + title="Send your first transaction with ERC-20 Paymaster" |
| 21 | + description="Learn how to use ERC-20 paymaster with Web3Auth Native Account Abstraction." |
| 22 | + image="https://web3auth.io/docs/guides-banners/erc20-paymaster.png" |
| 23 | + slug="/guides/sending-gassless-transaction" |
| 24 | +/> |
| 25 | + |
| 26 | +A paymaster is a vital component in the ERC-4337 standard, responsible for covering transaction |
| 27 | +costs on behalf of the user. There are various types of paymasters, such as gasless paymasters, |
| 28 | +ERC-20 paymasters, and more. |
| 29 | + |
| 30 | +In this guide, we'll talk about how you can use the Pimlico's ERC-20 Paymaster with Web3Auth Account |
| 31 | +Abstraction Provider to allow your users to pay for their transactions using ERC-20 tokens. |
| 32 | + |
| 33 | +If you are looking to use sponsored paymaster, you can refer to the |
| 34 | +[sponsored paymaster guide](/docs/guides/sending-gasless-transaction). |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +- Pimlico Account: Since we'll be using the Pimlico paymaster, you'll need to have an API key from |
| 39 | + Pimlico. You can get a free API key from [Pimlico Dashboard](https://dashboard.pimlico.io/). |
| 40 | +- Web3Auth Dashboard: If you haven't already, sign up on the Web3Auth platform. It is free and gives |
| 41 | + you access to the Web3Auth's base plan. Head to Web3Auth's documentation page for detailed |
| 42 | + [instructions on setting up the Web3Auth Dashboard](/docs/dashboard-setup). |
| 43 | +- Web3Auth PnP Web SDK: This guide assumes that you already know how to integrate the PnP Web SDK in |
| 44 | + your project. If not, you can learn how to |
| 45 | + [integrate Web3Auth in your Web app](/docs/sdk/pnp/web/). |
| 46 | + |
| 47 | +## Integrate AccountAbstractionProvider |
| 48 | + |
| 49 | +Once, you have set up the Web3Auth Dashboard, and created a new project, it's time to integrate |
| 50 | +Web3Auth Account Abstraction Provider in your Web application. For the implementation, we'll use the |
| 51 | +[@web3auth/account-abstraction-provider](https://www.npmjs.com/package/@web3auth/account-abstraction-provider). |
| 52 | +The provider simplifies the entire process by managing the complex logic behind configuring the |
| 53 | +account abstraction provider, bundler, and preparing user operations. |
| 54 | + |
| 55 | +If you are already using the Web3Auth Pnp SDK in your project, you just need to configure the |
| 56 | +AccountAbstractionProvider with the paymaster details, and pass it to the Web3Auth instance. No |
| 57 | +other changes are required. |
| 58 | + |
| 59 | +### Installation |
| 60 | + |
| 61 | +```bash |
| 62 | +npm install --save @web3auth/account-abstraction-provider |
| 63 | +``` |
| 64 | + |
| 65 | +### Configure ERC-20 Paymaster |
| 66 | + |
| 67 | +The `AccountAbstractionProvider` requires specific configurations to function correctly. One key |
| 68 | +configuration is the paymaster. Web3Auth supports custom paymaster configurations, allowing you to |
| 69 | +deploy your own paymaster and integrate it with the provider. |
| 70 | + |
| 71 | +You can choose from a variety of paymaster services available in the ecosystem. In this guide, we'll |
| 72 | +be configuring the Pimlico's ERC-20 Paymaster. However, it's important to note that paymaster |
| 73 | +support is not limited to the Pimlico, giving you the flexibility to integrate any compatible |
| 74 | +paymaster service that suits your requirements. |
| 75 | + |
| 76 | +To configure the ERC-20 Paymaster, you need to pass the `token` in the `paymasterContext` which |
| 77 | +allows you to specify the ERC-20 token that will be used to pay for the transaction. For this guide, |
| 78 | +we'll use the USDC token. |
| 79 | +[Find the USDC token address for your desired network](https://developers.circle.com/stablecoins/usdc-on-test-networks). |
| 80 | + |
| 81 | +For the simplicity, we have only use `SafeSmartAccount`, but you choose your favorite smart account |
| 82 | +provider from the available ones. |
| 83 | +[Learn how to configure the smart account](/docs/sdk/pnp/web/providers/aa-provider#configure-smart-account-provider). |
| 84 | + |
| 85 | +```ts |
| 86 | +// focus-start |
| 87 | +import { |
| 88 | + AccountAbstractionProvider, |
| 89 | + SafeSmartAccount, |
| 90 | +} from "@web3auth/account-abstraction-provider"; |
| 91 | +// focus-end |
| 92 | + |
| 93 | +const chainConfig = { |
| 94 | + chainNamespace: CHAIN_NAMESPACES.EIP155, |
| 95 | + chainId: "0xaa36a7", |
| 96 | + rpcTarget: "https://rpc.ankr.com/eth_sepolia", |
| 97 | + displayName: "Ethereum Sepolia Testnet", |
| 98 | + blockExplorerUrl: "https://sepolia.etherscan.io", |
| 99 | + ticker: "ETH", |
| 100 | + tickerName: "Ethereum", |
| 101 | + logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png", |
| 102 | +}; |
| 103 | + |
| 104 | +// focus-start |
| 105 | +const accountAbstractionProvider = new AccountAbstractionProvider({ |
| 106 | + config: { |
| 107 | + chainConfig, |
| 108 | + bundlerConfig: { |
| 109 | + // Get the pimlico API Key from dashboard.pimlico.io |
| 110 | + url: `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${pimlicoAPIKey}`, |
| 111 | + paymasterContext: { |
| 112 | + // USDC address on Ethereum Sepolia |
| 113 | + token: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238", |
| 114 | + }, |
| 115 | + }, |
| 116 | + smartAccountInit: new SafeSmartAccount(), |
| 117 | + paymasterConfig: { |
| 118 | + // Get the pimlico API Key from dashboard.pimlico.io |
| 119 | + url: `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${pimlicoAPIKey}`, |
| 120 | + }, |
| 121 | + }, |
| 122 | +}); |
| 123 | +// focus-end |
| 124 | +``` |
| 125 | + |
| 126 | +## Configure Web3Auth |
| 127 | + |
| 128 | +Once you have configured your `AccountAbstractionProvider`, you can now plug it in your Web3Auth |
| 129 | +Modal/No Modal instance. If you are using the external wallets like MetaMask, Coinbase, etc, you can |
| 130 | +define whether you want to use the AccountAbstractionProvider, or EthereumPrivateKeyProvider by |
| 131 | +setting the `useAAWithExternalWallet` in `IWeb3AuthCoreOptions`. |
| 132 | + |
| 133 | +If you are setting `useAAWithExternalWallet` to `true`, it'll create a new Smart Account for your |
| 134 | +user, where the signer/creator of the Smart Account would be the external wallet. |
| 135 | + |
| 136 | +If you are setting `useAAWithExternalWallet` to `false`, it'll skip creating a new Smart Account, |
| 137 | +and directly use the external wallet to sign the transactions. |
| 138 | + |
| 139 | +<Tabs |
| 140 | + defaultValue="modal" |
| 141 | + values={[ |
| 142 | + { label: "PnP Modal SDK", value: "modal" }, |
| 143 | + { label: "PnP No Modal SDK", value: "no-modal" }, |
| 144 | + ]} |
| 145 | +> |
| 146 | + |
| 147 | +<TabItem value="modal"> |
| 148 | + |
| 149 | +```ts |
| 150 | +import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider"; |
| 151 | +import { Web3Auth } from "@web3auth/modal"; |
| 152 | + |
| 153 | +const privateKeyProvider = new EthereumPrivateKeyProvider({ |
| 154 | + // Use the chain config we declared earlier |
| 155 | + config: { chainConfig }, |
| 156 | +}); |
| 157 | + |
| 158 | +const web3auth = new Web3Auth({ |
| 159 | + clientId: "YOUR_WEB3AUTH_CLIENT_ID", |
| 160 | + web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, |
| 161 | + privateKeyProvider, |
| 162 | + // Use the account abstraction provider we configured earlier |
| 163 | + accountAbstractionProvider |
| 164 | + // This will allow you to use EthereumPrivateKeyProvider for |
| 165 | + // external wallets, while use the AccountAbstractionProvider |
| 166 | + // for Web3Auth embedded wallets. |
| 167 | + useAAWithExternalWallet: false, |
| 168 | +}); |
| 169 | +``` |
| 170 | + |
| 171 | +</TabItem> |
| 172 | + |
| 173 | +<TabItem value="no-modal"> |
| 174 | + |
| 175 | +```ts |
| 176 | +import { Web3AuthNoModal } from "@web3auth/no-modal"; |
| 177 | +import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider"; |
| 178 | +import { AuthAdapter } from "@web3auth/auth-adapter"; |
| 179 | + |
| 180 | +const privateKeyProvider = new EthereumPrivateKeyProvider({ |
| 181 | + config: { chainConfig }, |
| 182 | +}); |
| 183 | + |
| 184 | +const web3auth = new Web3AuthNoModal({ |
| 185 | + clientId: "YOUR_WEB3AUTH_CLIENT_ID", |
| 186 | + web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, |
| 187 | + privateKeyProvider, |
| 188 | + // Use the account abstraction provider we configured earlier |
| 189 | + accountAbstractionProvider |
| 190 | + // This will allow you to use EthereumPrivateKeyProvider for |
| 191 | + // external wallets, while use the AccountAbstractionProvider |
| 192 | + // for Web3Auth embedded wallets. |
| 193 | + useAAWithExternalWallet: false, |
| 194 | +}); |
| 195 | + |
| 196 | +const authadapter = new AuthAdapter(); |
| 197 | +web3auth.configureAdapter(authadapter); |
| 198 | +``` |
| 199 | + |
| 200 | +</TabItem> |
| 201 | +</Tabs> |
| 202 | + |
| 203 | +## Send a transaction |
| 204 | + |
| 205 | +To submit the transaction using ERC-20 paymaster, we'll require to first need to approve ERC-20 |
| 206 | +token to be used by the paymaster. Ideally, we should first check the token allowance, and only |
| 207 | +provide approve allowance to be used by the paymaster. |
| 208 | + |
| 209 | +To modify the token allowance, you'll need to perform a write operation on the ERC-20 contract. In |
| 210 | +the example below, we're using Pimlico, but be sure to update the paymaster and ERC-20 token |
| 211 | +addresses according to your specific case. |
| 212 | + |
| 213 | +Since, we want to perform the approval transaction, and send transaction in a single call, we'll use |
| 214 | +batch transaction feature of the `AccountAbstractionProvider`. Performing a batch transaction |
| 215 | +differs slightly from the normal flow. |
| 216 | + |
| 217 | +To execute a batch transaction, you'll need to use the `BundlerClient` generated by the |
| 218 | +`AccountAbstractionProvider`. The Web3Auth instance provider can't be used for this, as it's a proxy |
| 219 | +provider designed to ensure compatibility with your preferred signer package for basic operations. |
| 220 | + |
| 221 | +Please make sure you have enough USDC balance in the wallet to pay the transaction fees. |
| 222 | +[Request faucet for USDC tokens](https://faucet.circle.com/). |
| 223 | + |
| 224 | +```ts |
| 225 | +// Use the same accountAbstractionProvider we created earlier. |
| 226 | +const bundlerClient = accountAbstractionProvider.bundlerClient!; |
| 227 | +const smartAccount = accountAbstractionProvider.smartAccount!; |
| 228 | + |
| 229 | +// Pimlico's ERC-20 Paymaster address |
| 230 | +const pimlicoPaymasterAddress = "0x0000000000000039cd5e8aE05257CE51C473ddd1"; |
| 231 | + |
| 232 | +// USDC address on Ethereum Sepolia |
| 233 | +const usdcAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"; |
| 234 | + |
| 235 | +// 0.00001 ETH in WEI format |
| 236 | +const amount = 10000000000000n; |
| 237 | + |
| 238 | +// 10 USDC in WEI format. Since USDC has 6 decimals, 10 * 10^6 |
| 239 | +const approvalAmount = 10000000n; |
| 240 | + |
| 241 | +const userOpHash = await bundlerClient.sendUserOperation({ |
| 242 | + account: smartAccount, |
| 243 | + calls: [ |
| 244 | + // Approve USDC on Sepolia chain for Pimlico's ERC 20 Paymaster |
| 245 | + { |
| 246 | + to: usdcAddress, |
| 247 | + abi: parseAbi(["function approve(address,uint)"]), |
| 248 | + functionName: "approve", |
| 249 | + args: [pimlicoPaymasterAddress, approvalAmount], |
| 250 | + }, |
| 251 | + { |
| 252 | + to: "DESTINATION_ADDRESS", |
| 253 | + value: amount, |
| 254 | + data: "0x", |
| 255 | + }, |
| 256 | + { |
| 257 | + to: "DESTINATION_ADDRESS", |
| 258 | + value: amount, |
| 259 | + data: "0x", |
| 260 | + }, |
| 261 | + ], |
| 262 | +}); |
| 263 | + |
| 264 | +// Retrieve user operation receipt |
| 265 | +const receipt = await bundlerClient.waitForUserOperationReceipt({ |
| 266 | + hash: userOpHash, |
| 267 | +}); |
| 268 | + |
| 269 | +const transactionHash = receipt.receipt.transactionHash; |
| 270 | +``` |
| 271 | + |
| 272 | +## Conclusion |
| 273 | + |
| 274 | +Voila, you have successfully sent your first transaction using the Pimlico's ERC-20 paymaster with |
| 275 | +Web3Auth Account Abstraction Provider. To learn more about advance features of the Account |
| 276 | +Abstraction Provider like performing batch transactions, using sponsored paymaster you can refer to |
| 277 | +the [Account Abstraction Provider](/docs/sdk/pnp/web/providers/aa-provider) documentation. |
0 commit comments