Understanding Tx Relay API
This guide contains information about how Tx Relay 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).
Tx Relay API provides a “gasless” ERC-20 token swap experience for your end users.
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, Tx Relay 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 Tx Relay API to pay for the gas on behalf of the user with the trade's input token (sell token).
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.
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 are messages that authorize smart contracts to perform actions on your behalf.
Gasless approvals is possible when the user signs a EIP-712 message giving Tx Relay 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).
By integrating Tx Relay API, your app can support gasless transactions — in which the user signs a EIP-712 message giving Tx Relay API permission to submit a transaction on behalf of the user to the blockchain. This is possible with Meta Transactions. In the current architecture, Tx Relay 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 Tx Relay 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.
This represents the other type of transaction returned by the Tx Relay 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
Gasless Approval + Tx Relay API Flow
Below is an image showing the ideal path when implementing both gasless approvals and Tx Relay. Click here to expand the image.
Composing Gasless Approval + Tx Relay API + Swap API
Tx Relay API is composable with Swap API if you want to offer execution optionality. 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 Tx Relay + Swap API? Try out the flow in the Matcha Auto feature 👉 https://matcha.xyz/