Pay for gas with ERC20 tokens using MultiTokenPaymaster
Using token paymasters we have the ability to pay for gas
with whatever ERC20 tokens the paymaster supports.
We can do this in two steps:
Fetch the paymaster address and see if the MultiTokenPaymaster supports the particular token.
Approve the tokens we wish to pay for gas with if exists and when estimating the transaction include the Arka and ERC20 parameters and send it to the bundler as a single batch transaction.
We can do this like so:
From the above list of multiTokenPaymasters, check if your token is supported. If so, continue to the next step after taking the paymaster address associated with your token address.
Note: ERC20_ABI is the ERC20 ABI for the token you want to pay for gas with. You can copy from here
Copy
const erc20Contract = new ethers.Contract(TOKEN_ADDRESS, ERC20_ABI)const encodedData = erc20Contract.interface.encodeFunctionData('approve', [paymasterAddress, ethers.utils.parseEther('1')]) // Approval for 1 ETHawait primeSdk.addUserOpsToBatch({ to: TOKEN_ADDRESS, data: encodedData });
Once the tokens are approved we can create whatever transactions we want,
then estimate them before sending like this: