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 (read more in "What tokens are supported?").
This API returns liquidity—prices and 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 required if you want a third-party to move funds on your behalf. In short, you are allowing them to move your tokens. In our case, we would like the 0x Exchange Proxy smart contract to trade our ERC20 tokens for us, so we need to write to the ERC20 contract of that token, and approve an allowance for 0x Exchange Proxy (i.e. allow it to move a certain amount of tokens on our behalf us).
For tokens that do not support gasless approvals, we needed to set the token allowance via the messages 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.
Meta Transactions​
Meta Transactions are messages that authorize smart contracts to perform actions on your behalf.
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.
Meta Transaction V2​
This represents one of two types of gasless transactions returned by the Gasless API /quote
endpoint. Meta Transaction V2 leverages the Swap API to fetch indicative pricing and quotes. 0x submits these transactions to the blockchain on behalf of the user.
OTC​
This represents the other type of transaction returned by the Gasless API /quote
endpoint. OTC transactions allow market makers to provide indicative pricing and quotes. In these transactions, a market maker submits the transaction to the blockchain on behalf of the user.
Technical Flow Charts​
The Gasless API can be combined with the Swap API to provide flexible execution options. This allows for a seamless user experience where advanced users can choose to enable or disable Gasless execution without compromising their experience.
For instance, you can offer "standard" swaps via Swap API for tokens that are not supported by the Gasless API, such as native tokens. Alternatively, you can design an app that uses Gasless by default, and automatically falls back to the Swap API in cases Gasless does not support (i.e. native tokens) or there is not enough token to cover gas.
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/