LogoLogo
  • Introduction to Unizen
    • Unizen Overview
      • Unizen Liquidity Distribution Mechanism (ULDM)
        • ULDM Performance
      • Unizen Interoperability Protocol (UIP)
        • LayerZero
        • DeBridge
        • Stargate
        • Celer
        • Axelar
        • Thorchain
    • Unizen Dashboard
      • General
      • Portfolio
      • History
    • Unizen Trade
      • Fees
    • Unizen Explore
      • Unizen Omni-Chain Data Pool
    • Unizen Earn
  • ZCX
    • Token Utility
    • Tokenomics
  • API - Introduction
    • Introduction
    • Before you get started
      • Understanding Price Impact and Price Slippage in Token Swaps
      • Token Allowance Management for Non-updatable Allowance Tokens
      • Tokens with taxes
      • Wrapping and Unwrapping Native Tokens
      • Quote expiration deadline
    • Security Best Practices for Integrating Unizen
      • Why disable CORS
      • How to integrate with a reverse proxy
    • Version 2 of our smart contracts
      • Migration to smart contract v2
  • API - GET STARTED
    • QuickStart guide
    • Swagger
    • Information endpoints
      • GET /trade/v1/info/chains
      • GET /trade/v1/info/sources
      • GET/v1/info/cross-providers
      • GET /trade/v1/info/token/search
      • GET /v1/info/token/popular
      • GET /trade/v1/info/token/{chainId}/{tokenAddress}
      • GET /trade/v1/info/tokenLogo/{chainId}/{tokenAddress}
      • GET /info/thorchain-inbound-address
      • GET /trade/v1/info/tx/{txHash}
      • GET /trade/v1/info/trade/{chainId}/{txHash}
      • GET /trade/v1/info/trades
    • Approval
      • GET /trade/v1/{chainId}/approval/spender
      • GET /trade/v1/{chainId}/approval/transaction
      • GET /trade/v1/{chainId}/approval/allowance
    • Single-Chain Swap
      • GET /trade/v1/{chainId}/quote/single
      • GET /trade/v1/{chainId}/swap/single
      • Send transaction in evm chains
      • Send transaction in Solana
    • Cross-Chain Swap
      • GET /trade/v1/{chainId}/quote/cross
      • GET /trade/v1/{chainId}/swap/cross
      • Send transaction
    • Gasless orders
      • POST /trade/v1/gasless/typed-data
      • POST /v1/gasless/estimate
      • POST /v1/gasless/create
      • POST /v1/gasless/cancel
      • GET /trade/v1/gasless/status/{orderId}
      • GET /v1/gasless/orderByAddress/{address}
    • UTXO Assets and Cosmos Swap
      • GET /trade/v1/{chainId}/quote/cross 1
      • GET /trade/v1/{chainId}/swap/cross
      • Sending transactions
    • Efficient Quote Retrieval with Batch Processing
      • GET /trade/v1/{chainId}/batch_quote/single
    • Error Messages
  • GASLESS TRADES
    • Obtaining gasless quotes
    • Gas estimation
    • Executing the trade
    • Following the orders
  • On-Chain Contracts - Get Started
    • Integration with Unizen Contracts for Token Swapping
    • Registering Errors on Smart Contract Calls
  • PERMIT2
    • What is Permit2?
    • Usage in our api
  • WIDGET - Get Started
    • Embed the Unizen Widget
    • Playground
  • Other
    • Smart Contracts
    • Security Audits
    • Roadmap
  • links
    • Unizen
    • Marketing Website
    • Medium
    • Twitter
    • Discord
    • Telegram
Powered by GitBook
On this page

Was this helpful?

  1. On-Chain Contracts - Get Started

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.

PreviousFollowing the ordersNextRegistering Errors on Smart Contract Calls

Last updated 5 months ago

Was this helpful?