How to pay gas with any token
Learn how to enable gas payments with ERC-20 tokens.
Gas fees paid in the native gas token can feel foreign to users that primarily hold stablecoins or your app’s own token. With our smart wallet, you can enable your users to pay gas with ERC-20 tokens beyond the native gas token, like USDC or your own custom tokens, streamlining the user experience.
How it works: We front the gas using the network’s native gas token and transfer the ERC-20 tokens from the user’s wallet to a wallet you control. The equivalent USD amount and the admin fee is then added to your monthly invoice.
[Recommended] Use our SDK to create and use wallets. The SDK handles all complexity for you, making development faster and easier.
If you want to use APIs directly, follow these steps.
Steps
1. Get an API key
- Get you API Key by creating an app in your Alchemy Dashboard
- Make sure you enable the networks you are building on under the Networks tab
2. Create a Gas Manager policy
To enable your users to pay gas using an ERC-20 token, you need to create a “Pay gas with any token” Policy via the Gas Manager dashboard. You can customize the policy with the following:
- Receiving address: an address of your choosing where the users’ ERC20 tokens will be sent to as they pay for gas (this is orchestrated by the paymaster contract and happens automatically at the time of the transaction).
- Tokens: the tokens the user should be able to pay gas with. Learn more here.
- ERC-20 transfer mode: choose when the user’s token payment occurs.
- [Recommended] After: No upfront allowance is required. The user signs an approval inside the same user operation batch, and the paymaster pulls the token after the operation has executed. If that post-execution transfer fails, the entire user operation is reverted and you still pay the gas fee.
- Before: You (the developer) must ensure the paymaster already has sufficient allowance—either through a prior
approve()
transaction or a permit signature—before the UserOperation is submitted. If the required allowance isn’t in place when the user operation is submitted, it will be rejected upfront.
- Sponsorship expiry period: this is the period for which the Gas Manager signature and ERC-20 exchange rate will remain valid once generated.
Now you should have a Gas policy created with a policy id you can use to enable gas payments with ERC-20 tokens.
3. Get Gas Manager’s signature
When sending a userOperation, you can specify the paymaster
and paymasterData
fields in the userOp
object. These fields are related to the signature of the Gas Manager that enables the user to pay for gas with ERC-20 tokens.
You can get these fields through alchemy_requestGasAndPaymasterAndData
using your Gas Manager Policy id, the API key of the app associated with the policy, a userOperation, the address of the EntryPoint contract, and the address of the ERC-20 token. You can find an example script below.
4. Send the userOp
Once you get the paymaster
and paymasterData
fields, you can use them in your userOperation when you call eth_sendUserOperation
. You can find an example script below.