How to Pay Gas in ERC-20 Tokens | EIP-7702
Smart EOAs via EIP-7702
Offer the ability for users to pay gas in ERC-20s using any of the supported ERC-20 Tokens on Candide Paymaster.
- Create a new app on the dashboard and copy the Paymaster RPC URL into your
.env
file. - Select an ERC-20 token to pay gas with and add its address to the
.env
file. We use CTT in this example.
- index.ts
- .env
import {
...
CandidePaymaster,
} from "abstractionkit";
const paymasterRPC = process.env.PAYMASTER_RPC as string;
const tokenAddress = process.env.TOKEN_ADDRESS as string;
const paymaster = new CandidePaymaster(paymasterRPC)
const tokensSupported = await paymaster.fetchSupportedERC20TokensAndPaymasterMetadata();
const tokenSelected = tokensSupported.tokens.find(
token => token.address.toLocaleLowerCase() === tokenAddress.toLowerCase()
);
if (tokenSelected) {
userOperation = await paymaster.createTokenPaymasterUserOperation(
smartAccount, // = new Simple7702Account(eoaDelegator.address)
userOperation,
tokenSelected.address,
bundlerUrl,
)
const cost = await paymaster.calculateUserOperationErc20TokenMaxGasCost(
userOperation,
tokenSelected.address
)
console.log("This useroperation may cost upto : " + cost + " wei in CTT token")
console.log("Please fund the sender account: " + userOperation.sender +" with more than "+ cost + " wei CTT token")
}
PAYMASTER_RPC=https://api.candide.dev/paymaster/$version/$network/$apikey
TOKEN_ADDRESS=0xFa5854FBf9964330d761961F46565AB7326e5a3b # CTT address on sepolia
info
Don't forget to fund the account with ERC-20s to pay for gas. We are using Candide Test Tokens (CTT) for this example. Request some on our Discord.