Usage in our api
Using permitData
to Enhance Permit2 in the Trading API
permitData
to Enhance Permit2 in the Trading APIThis document explains how to leverage the permitData
field with the Permit2 mechanism in the trading API for efficient and secure token approvals. The endpoint in focus is:
Endpoint: GET /single-chain/swap
What is permitData
?
permitData
?permitData
is an advanced feature provided by the API to facilitate gas-efficient and user-friendly token approvals. By utilizing Permit2, permitData
enables off-chain signatures for token allowances, which can be submitted on-chain without requiring the user to send a separate approval transaction.
This eliminates the need for traditional ERC-20 approve
calls, streamlining the trading experience while saving gas costs and improving security.
How permitData
Works
permitData
WorksOff-Chain Signature: The user signs a
permit
message off-chain authorizing the trading contract to spend their tokens.On-Chain Execution: The signed
permitData
is sent along with the swap request to the/single-chain/swap
endpoint.Token Allowance: The contract uses Permit2 to verify the signature and execute the swap, avoiding a separate on-chain approval step.
Workflow to Use permitData
permitData
1. Retrieve permitData
Structure
To generate the permitData
that the user needs to sign, follow these steps:
Prepare the Request Payload:
Prepare the data required for Permit2 signed typed data, including
token
,amount
,spender
,nonce
, anddeadline
. Generate the typed data for signing using either@uniswap/permit2-sdk
or any method of your choice.Ensure the token being approved supports Permit2.
Ensure that you sent version=v2 on the quote request endpoint, as this feature it's only available in v2 version of our smart contract.
User Signs the Data:
Obtain the user’s signature on the
permitData
using a wallet (e.g., MetaMask or hardware wallet). (for example: https://viem.sh/docs/actions/wallet/signTypedData.html)with the signature after sign, this is the permitData format:
2. Call the Swap Endpoint with permitData
Send the signed permitData
to the /single-chain/swap
endpoint along with the other required parameters:
Request Example:
Response Example:
Benefits of Using permitData
permitData
Gas Savings:
Eliminates the need for a separate approval transaction, reducing gas costs.
Enhanced Security:
Users can provide time-limited and amount-specific allowances, minimizing exposure to potential exploits.
Streamlined UX:
Allows users to sign once off-chain and execute swaps seamlessly without additional approval steps.
Batch Approvals:
Permit2 enables batch approvals and allowances for multiple tokens, optimizing multi-token swaps.
Best Practices
Validate
permitData
Before Execution:Ensure the
permitData
signature is valid and corresponds to the intended swap parameters.
Handle Expiry Gracefully:
Monitor the
deadline
field in thepermitData
to ensure it hasn’t expired before sending the transaction.
Test with Supported Tokens:
Verify that the tokens involved in the swap support Permit2. Tokens without Permit2 support will require traditional approvals.
Key Notes
Permit2 is only supported on compatible tokens and networks.
Ensure that the
spender
address matches the contract that will execute the swap.Keep the signed
permitData
secure and ensure it is only used for the intended transaction.
By incorporating permitData
and Permit2 into your trading workflow, you can enhance user experience, save gas, and improve transaction efficiency. For additional details, refer to the full API documentation at https://api.zcx.com/trade/docs#/Single%20chain%20methods/SwapSingleController_getSingleSwap.
Last updated