Integration with Unizen Contracts for Token Swapping
By following these steps, you can seamlessly interact with Unizen’s API and integrate token swapping functionality into your smart contract. This guide provides a comprehensive approach, ensuring both
Step-by-Step Integration with Unizen Contracts for Token Swapping
Integrating with the Unizen platform for token swapping within your smart contracts requires a structured approach. Here’s a detailed guide to help you perform token swaps using Unizen’s APIs.
Step 1: Retrieve a Quote
The first step in this integration is to obtain a quote for the token swap.
Endpoint:
Replace {chainId}
with the appropriate blockchain ID (e.g., Ethereum, BSC). This request to the Unizen API returns a quote, which includes essential details such as the estimated gas cost and the token amounts necessary to perform the swap.
Step 2: Call the Unizen Swap API
Once you have a quote, you can initiate the token swap by calling the Unizen Swap API.
Endpoint:
Parameters:
account
: This is the contract address of your integrator contract interacting with Unizen.receiver
: Specify the receiver of the swapped tokens:For direct delivery to a user's address, enter the user's address.
If the tokens should return to your contract, set this parameter to the
account
address.
Example Response:
This response contains data necessary to proceed with the swap transaction.
Note: If you already know the receiver address at the time of obtaining the quote, you can skip the call to the swap endpoint, simplifying the integration process. Assuming you also know the account address of the smart contract that will interact with the Unizen contract, you can pass the receiver address directly in the quote request. Additionally, by setting the disableEstimate
parameter to true
, the response will include both a gas estimation and the necessary transaction data, allowing you to proceed directly to transaction execution without the intermediate swap API call. This can streamline the process, saving resources and reducing the number of API interactions required.
Executing the Swap in Solidity
After receiving the response data from Unizen, you can execute the token swap in your Solidity smart contract. Follow one of these methods based on the token type being swapped.
For Native Token Swaps
If swapping from a native token like ETH, use the following code:
swapAmount
: The amount of the native token (e.g., ETH) to swap.data
: Thedata
from the Unizen API response.
For ERC20 Token Swaps
When swapping an ERC20 token, make sure to approve the Unizen contract to spend the tokens on cridyour behalf before executing the swap. Then use the following code:
sellToken
: The ERC20 token address you want to swap.swapAmount
: The amount of the ERC20 token to be swapped.data
: The response data from the Unizen API.
Security Note: Resetting the token allowance back to zero (safeApprove(UnizenContractAddress, 0)
) after the swap is a best practice for security, as it prevents excessive approvals that could pose risks if the contract’s security is compromised.
Last updated