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:

  1. Fetch the paymaster address and approve the tokens we wish to pay for gas with.
  2. When estimating the transaction include the Arka and ERC20 parameters.

The fetching of pimlico erc20 paymaster address is only required for the first time for each specified gas token since we need to approve the tokens to spend from the paymaster address on behalf of you.

We can do this like so:

const returnedValue = await fetch(`${arka_url}/pimlicoAddress`, {
method: 'POST',
headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
},
body: JSON.stringify({ "params": [entryPointAddress, { token: "USDC" }, Number(process.env.CHAIN_ID), arka_api_key] })
})
.then((res) => {
    return res.json()
}).catch((err) => {
    console.log(err);
});
const paymasterAddress = returnedValue.message;

const erc20Contract = new ethers.Contract(tokenAddress, ERC20_ABI)
const encodedData = erc20Contract.interface.encodeFunctionData('approve', [paymasterAddress, ethers.constants.MaxUint256]) // Infinite Approval
await primeSdk.addUserOpsToBatch({ to: tokenAddress, data: encodedData });
const approveOp = await primeSdk.estimate();
await primeSdk.send(approveOp);

Once the tokens are approved we can create whatever transactions we want, then estimate them before sending like this:

const op = await primeSdk.estimate(
{ 
    url: "https://arka.etherspot.io?apiKey=arka_public_key&chainId=80001",
    context: { token: "USDC", mode: 'erc20' } 
 });