Pay gas in ERC20 tokens
Overview
This tutorial demonstrates how to use the Biconomy Smart Account to perform a transaction with ERC-20 token payment. The provided code includes creating a Biconomy Smart Account, encoding a function call, building a transaction, and sending it with ERC-20 token payment.
Prerequisites
- Node.js installed on your machine
- A Biconomy API key and Bundler url, get from dashboard
For this tutorial we are going to mint and NFT on Amoy, this means we can use the following:
- bundlerUrl -->
https://bundler.biconomy.io/api/v2/80002/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44
- preferred ERC20 token for gas payment --> 0xf555F8d9Cf90f9d95D34488e6C852796D9acBd31 (USDC)
- nft address --> 0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e
Step 1: Create Biconomy Smart Account and Encode Function Call
const smartWallet = await createSmartAccountClient({
signer, // can be viem client or ethers signer
bundlerUrl,
biconomyPaymasterApiKey,
});
const encodedCall = encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [recipient],
});
Create a Biconomy Smart Account instance and encode the function call for the transaction.