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:

/v1/{chainId}/quote/single

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:

/v1/{chainId}/swap/single

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:

{
  "data": "0xbfaa0506000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000e6b9bb7257b7c0801794f7f37b51390e3d5156950000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000be4a43f000000000000000000000000000000000000000000000000000000000bf3f0f8900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000a554e495a454e2d434c49000000000000000000000000000000000000000000000000000000000000000000000000..."
}

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:

(bool success, ) = UnizenContractAddress.call{value: swapAmount}(data);
require(success, "Swap failed");
  • swapAmount: The amount of the native token (e.g., ETH) to swap.

  • data: The data 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:

ERC20(sellToken).safeApprove(UnizenContractAddress, swapAmount);
(bool success, ) = UnizenContractAddress.call{value: 0}(data);
require(success, "Swap failed");
ERC20(sellToken).safeApprove(UnizenContractAddress, 0);  // Reset allowance
  • 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