|
| 1 | +--- |
| 2 | +title: Usage |
| 3 | +sidebar_label: Usage |
| 4 | +description: "@web3auth/modal Native Account Abstraction | Documentation - Web3Auth" |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from "@theme/Tabs"; |
| 8 | +import TabItem from "@theme/TabItem"; |
| 9 | + |
| 10 | +import ConfigureSigners from "@site/src/common/sdk/pnp/web/_configure-aa-signers.mdx"; |
| 11 | +import SmartAccountAddress from "@site/src/common/sdk/pnp/web/_aa-address.mdx"; |
| 12 | +import SmartAccountSendTransaction from "@site/src/common/sdk/pnp/web/_aa-send-transaction.mdx"; |
| 13 | + |
| 14 | +// TODO: Add section that Wagmi doesn't require any configuration for signer. |
| 15 | + |
| 16 | +### Configure Signer |
| 17 | + |
| 18 | +<ConfigureSigners /> |
| 19 | + |
| 20 | +## Smart Account Address |
| 21 | + |
| 22 | +<SmartAccountAddress /> |
| 23 | + |
| 24 | +## Send Transaction |
| 25 | + |
| 26 | +<SmartAccountSendTransaction /> |
| 27 | + |
| 28 | +## Sign transaction |
| 29 | + |
| 30 | +Developers have the option to sign a transaction without immediately sending it. If it is the user's |
| 31 | +first transaction, the `UserOperation` calldata will automatically include the required deployment |
| 32 | +data to create the user's Smart Account on-chain. |
| 33 | + |
| 34 | +:::info |
| 35 | + |
| 36 | +Wagmi doesn't have hooks for signing transactions yet. |
| 37 | + |
| 38 | +::: |
| 39 | + |
| 40 | +<Tabs |
| 41 | + defaultValue="viem" |
| 42 | + values={[ |
| 43 | + { label: "Viem", value: "viem" }, |
| 44 | + { label: "Ethers", value: "ethers" }, |
| 45 | + ]} |
| 46 | +> |
| 47 | + |
| 48 | +<TabItem value="ethers"> |
| 49 | + |
| 50 | +```tsx |
| 51 | +// Convert 1 ether to WEI format |
| 52 | +const amount = parseEther("1"); |
| 53 | + |
| 54 | +const signature = await signer.signTransaction({ |
| 55 | + to: "DESTINATION_ADDRESS", |
| 56 | + value: amount, |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +</TabItem> |
| 61 | +<TabItem value="viem"> |
| 62 | + |
| 63 | +```tsx |
| 64 | +// Convert 1 ether to WEI format |
| 65 | +const amount = parseEther("1"); |
| 66 | +const addresses = await walletClient.getAddresses(); |
| 67 | + |
| 68 | +const request = await walletClient.prepareTransactionRequest({ |
| 69 | + account: addresses[0], |
| 70 | + to: "DESTINATION_ADDRESS", |
| 71 | + value: amount, |
| 72 | +}); |
| 73 | + |
| 74 | +const signature = await walletClient.signTransaction(request as any); |
| 75 | +``` |
| 76 | + |
| 77 | +</TabItem> |
| 78 | +</Tabs> |
| 79 | + |
| 80 | +## Sign Message |
| 81 | + |
| 82 | +Message signing is also supported with Smart Accounts; however, the signature and verification |
| 83 | +process differs from that of Externally Owned Accounts (EOAs). Smart Accounts adhere to the EIP-1271 |
| 84 | +standard for signature verification, utilizing the isValidSignature method defined in the smart |
| 85 | +contract wallet, rather than relying on the ecrecover function used by EOAs. |
| 86 | + |
| 87 | +[Learn more about EIP 1271](https://eips.ethereum.org/EIPS/eip-1271). |
| 88 | + |
| 89 | +<Tabs |
| 90 | + defaultValue="viem" |
| 91 | + values={[ |
| 92 | + { label: "Viem", value: "viem" }, |
| 93 | + { label: "Ethers", value: "ethers" }, |
| 94 | + { label: "Wagmi", value: "wagmi" }, |
| 95 | + ]} |
| 96 | +> |
| 97 | + |
| 98 | +<TabItem value="wagmi"> |
| 99 | + |
| 100 | +```tsx |
| 101 | +import { useSignMessage } from "wagmi"; |
| 102 | +const { data: signature, signMessage } = useSignMessage(); |
| 103 | + |
| 104 | +const message = "YOUR_MESSAGE"; |
| 105 | + |
| 106 | +signMessage({ message }); |
| 107 | + |
| 108 | +// Use signature as needed |
| 109 | +``` |
| 110 | + |
| 111 | +</TabItem> |
| 112 | +<TabItem value="ethers"> |
| 113 | + |
| 114 | +```tsx |
| 115 | +const originalMessage = "YOUR_MESSAGE"; |
| 116 | + |
| 117 | +// Sign the message |
| 118 | +const signedMessage = await signer.signMessage(originalMessage); |
| 119 | +``` |
| 120 | + |
| 121 | +</TabItem> |
| 122 | +<TabItem value="viem"> |
| 123 | + |
| 124 | +```tsx |
| 125 | +const originalMessage = "YOUR_MESSAGE"; |
| 126 | + |
| 127 | +const addresses = await walletClient.getAddresses(); |
| 128 | + |
| 129 | +const signedMessage = await walletClient.signMessage({ |
| 130 | + account: address[0], |
| 131 | + message: originalMessage, |
| 132 | +}); |
| 133 | +``` |
| 134 | + |
| 135 | +</TabItem> |
| 136 | +</Tabs> |
| 137 | + |
| 138 | +## Send Batch Transaction |
| 139 | + |
| 140 | +One of the key features of Smart Accounts is the ability to perform batch transactions. |
| 141 | +Traditionally, if a user wants to swap Token A for Token B, they must first approve Token A and then |
| 142 | +execute the swap in a separate transaction. With batch transaction support in Smart Accounts, both |
| 143 | +steps can be combined into a single UserOperation. |
| 144 | + |
| 145 | +Executing a batch transaction differs slightly from the standard transaction flow. To perform it, |
| 146 | +developers must use the `BundlerClient` provided by Web3Auth. The default Web3Auth provider instance |
| 147 | +cannot be used for this purpose, as it is a proxy designed to work seamlessly with common signer |
| 148 | +libraries for basic operations. |
| 149 | + |
| 150 | +When calling `sendUserOperation`, it returns a UserOperation hash, which is the keccak256 hash of |
| 151 | +the entire user operation, not the transaction hash. To retrieve the final transaction details, |
| 152 | +developers must use the `waitForUserOperationReceipt` method. This function waits for the |
| 153 | +UserOperation to be included in a block and returns a complete receipt, including the entryPoint, |
| 154 | +blockNumber, blockHash, and the actual transactionHash. |
| 155 | + |
| 156 | +```ts |
| 157 | +// Use your Web3Auth instance |
| 158 | +const accountAbstractionProvider = web3auth.accountAbstractionProvider; |
| 159 | +// Use the same accountAbstractionProvider we created earlier. |
| 160 | +const bundlerClient = accountAbstractionProvider.bundlerClient!; |
| 161 | +const smartAccount = accountAbstractionProvider.smartAccount!; |
| 162 | + |
| 163 | +// 0.00001 ETH in WEI format |
| 164 | +const amount = 10000000000000n; |
| 165 | + |
| 166 | +const userOpHash = await bundlerClient.sendUserOperation({ |
| 167 | + account: smartAccount, |
| 168 | + calls: [ |
| 169 | + { |
| 170 | + to: "DESTINATION_ADDRESS", |
| 171 | + value: amount, |
| 172 | + data: "0x", |
| 173 | + }, |
| 174 | + { |
| 175 | + to: "DESTINATION_ADDRESS", |
| 176 | + value: amount, |
| 177 | + data: "0x", |
| 178 | + }, |
| 179 | + ], |
| 180 | +}); |
| 181 | + |
| 182 | +// Retrieve user operation receipt |
| 183 | +const receipt = await bundlerClient.waitForUserOperationReceipt({ |
| 184 | + hash: userOpHash, |
| 185 | +}); |
| 186 | + |
| 187 | +const transactionHash = receipt.receipt.transactionHash; |
| 188 | +``` |
| 189 | + |
| 190 | +## Send transaction using ERC-20 Paymaster |
| 191 | + |
| 192 | +To submit a transaction using an ERC-20 paymaster, the associated ERC-20 token must first be |
| 193 | +approved for use by the paymaster. Ideally, the application should first check the existing token |
| 194 | +allowance and only initiate an approval transaction if the allowance is insufficient. |
| 195 | + |
| 196 | +Modifying the token allowance requires a write operation on the ERC-20 token contract. In the |
| 197 | +example below, Pimlico is used as the paymaster, but developers should update the paymaster and |
| 198 | +ERC-20 token addresses to match their specific implementation. To use an ERC-20 paymaster, refer to |
| 199 | +the configuration guide for |
| 200 | +[ERC-20 paymaster context.](/docs/sdk/pnp/web/modal/account-abstraction/configure/sdk). |
| 201 | + |
| 202 | +```ts |
| 203 | +// Use your Web3Auth instance |
| 204 | +const accountAbstractionProvider = web3auth.accountAbstractionProvider; |
| 205 | + |
| 206 | +// Use the same accountAbstractionProvider we created earlier. |
| 207 | +const bundlerClient = accountAbstractionProvider.bundlerClient!; |
| 208 | +const smartAccount = accountAbstractionProvider.smartAccount!; |
| 209 | + |
| 210 | +// Pimlico's ERC-20 Paymaster address |
| 211 | +const pimlicoPaymasterAddress = "0x0000000000000039cd5e8aE05257CE51C473ddd1"; |
| 212 | + |
| 213 | +// USDC address on Ethereum Sepolia |
| 214 | +const usdcAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"; |
| 215 | + |
| 216 | +// 0.00001 ETH in WEI format |
| 217 | +const amount = 10000000000000n; |
| 218 | + |
| 219 | +// 10 USDC in WEI format. Since USDC has 6 decimals, 10 * 10^6 |
| 220 | +const approvalAmount = 10000000n; |
| 221 | + |
| 222 | +const userOpHash = await bundlerClient.sendUserOperation({ |
| 223 | + account: smartAccount, |
| 224 | + calls: [ |
| 225 | + // Approve USDC on Sepolia chain for Pimlico's ERC 20 Paymaster |
| 226 | + { |
| 227 | + to: usdcAddress, |
| 228 | + abi: parseAbi(["function approve(address,uint)"]), |
| 229 | + functionName: "approve", |
| 230 | + args: [pimlicoPaymasterAddress, approvalAmount], |
| 231 | + }, |
| 232 | + { |
| 233 | + to: "DESTINATION_ADDRESS", |
| 234 | + value: amount, |
| 235 | + data: "0x", |
| 236 | + }, |
| 237 | + { |
| 238 | + to: "DESTINATION_ADDRESS", |
| 239 | + value: amount, |
| 240 | + data: "0x", |
| 241 | + }, |
| 242 | + ], |
| 243 | +}); |
| 244 | + |
| 245 | +// Retrieve user operation receipt |
| 246 | +const receipt = await bundlerClient.waitForUserOperationReceipt({ |
| 247 | + hash: userOpHash, |
| 248 | +}); |
| 249 | + |
| 250 | +const transactionHash = receipt.receipt.transactionHash; |
| 251 | +``` |
0 commit comments