Understanding Gasless API
This guide contains information about how Gasless API works, key terms, and technical flow charts for how to implement in-app.
How does it work?​
A major source of friction in web3 is the need for users to hold a small amount of the chain's native token to pay for token approvals and subsequent transactions (aka paying "gas" fees).
Gasless API can provide a gasless approval and gasless swap experience for supported ERC-20 token swaps.
This API returns indicative prices and firm quotes to end users. If a user is happy with a quote the API has provided, they can sign an EIP-712 message for the approval and trade. Then, Gasless API can submit the transaction to the blockchain on behalf of the user, eliminating the need for the end user to pay for gas manually.
The term "gasless" refers to the ability for users to trade without needing to pay the gas cost at that moment. Instead, a third party like 0x can submit the transaction(s) to the blockchain and pay the gas fee. So, in practice, there is still gas involved—it's just being paid by someone else. The user doesn't have to worry about holding the chain's native token in order to pay for gas cost upfront when submitting a transaction. A suggested implementation is to use Gasless API to pay for the gas on behalf of the user with the trade's input token (sell token).
Key Terms​
Token Allowances​
A token allowances is needed for a third-party to move your funds. For our case, we need to approve the Permit2 contract to trade our ERC20 tokens. This requires writing to the ERC20 contract and approving an allowance for Permit2 to move a specified amount of tokens on our behalf.
For tokens that do not support gasless approvals, we needed to set the token allowance via the steps listed here.
EIP-712: Human Readable Messages​
EIP-712 is the standard for typed message signing. This EIP allows wallets to display data in signing prompts in a human readable and not just a blind hash.
Gasless leverages EIP-712 messages to enable gasless approvals and gasless transactions.
EIP-2612: Gasless Approvals​
EIP-2612 unlocked gasless token approvals by introducting a new Permit function to ERC-20 tokens. The Permit function enables enables users to grant permission to others to spend their tokens in a single transaction which offer more efficient token approvals and enabled gasless transactions.
Gasless Approvals​
Gasless approvals is possible when the user signs a EIP-712 message giving Gasless API permission to set an allowance on the token required for approval. In the current architecture 0x or the market maker pays for this gas fee, and the user has a “gasless” experience. This is possible for tokens that support the Permit Extension (EIP-2612).
Gasless Transactions​
By integrating Gasless API, your app can support gasless transactions — in which the user signs a EIP-712 message giving Gasless API permission to submit a transaction on behalf of the user to the blockchain. This is possible with Meta Transactions. In the current architecture, Gasless pays for the gas of the transaction using some of the user’s sell token.
Settler Metatransaction​
Metatransactions are messages that authorize smart contracts to perform actions on your behalf.
Settler Metatransactions, settler_metatransaction
, represents a type of gasless transactions returned by the Gasless API /gasless/quote
endpoint. Settler Metatransaction leverages the Swap API to fetch indicative pricing and quotes. 0x submits these transactions to the blockchain on behalf of the user.
Technical Flow Charts​
Gasless API Flow​
Below is an image showing the ideal path when implementing both gasless approvals and gasless swaps using the Gasless API. Click here to expand the image.
Composing Gasless API + Swap API​
Gasless API is composable with Swap API if you want to offer execution optionality (e.g. offering "vanilla" swaps for tokens that Gasless API does not support). Devs can design a UX where advanced users can opt in and opt out of gasless without sacrificing experience. Click here to expand the image
Want to see a live implementation of Gasless API + Swap API?
👉 Try out the flow in the Matcha Auto feature: https://matcha.xyz/
👉 Learn more about Matcha Auto: https://youtu.be/ziV3O9QLE5U?si=orPciAj00iOWKxd4