Execute A Swap With The Aggregator API

Interacting With KyberSwap Aggregator Router Contract

Overview

As the KyberSwap Aggregator has been deployed on Solana, KyberSwap maintains 2 different API specifications:

This guide focuses on calling the Aggregator APIs for EVM chains but the same principles apply to the Solana variant.

To execute a swap, the router (MetaAggregationRouterV2) contract requires the encoded swap data to be included as part of the transaction. This encoded swap data as well as other swap metadata is returned as part of the API response. As such, developers are expected to call the swap API prior to sending a transaction to the router contract.

Backwards compatibility

Following feedback on the initial [V1] API, KyberSwap has implemented a more performant [V2] API which improves the response time for getting a route via offloading encoding requirements to the post method.

Please refer to Upgrading From APIv1 To APIv2 for further details on the motivation behind the upgrade as well as the relevant changes to swap flow and parameters.

Please use the [V2]GET API for more efficient route queries. The returned route can then be reused in the [V2]POST body to get the encoded swap data. The [V1]GET and [V2]GET remains backwards compatible with the main change being the queried path.

Web3.js example

Swap Params Response

Parameter NameTypeRequiredDescription

inputAmount

string

hex string represents the input amount

routerAddress

string

ERC20 contract address of the router

encodedSwapData

string

hex data to be used in transaction

...

The response parameters have been slightly altered in the [V2]POST API. The relevant parameter keys are provided below ([V1] -> [V2]):

  • inputAmount -> amountIn

  • encodedSwapData -> data

The Encoded Swap API response includes inputAmount and encodedSwapData above to be used as data to interact with our smart contract. By using the encodedSwapData, you don't have to care about the logic behind the encoding process. The next section will explain client usage

Web3js Client Integration

Web3.js integration is relatively straightforward but to avoid transaction failure due to gas, we recommend performing a gas estimation call.

Initial web3 context

const { account, chainId, library } = useActiveWeb3React()

Estimate gas

Note: The value of the transaction is the inputAmount if the input token is a native token, and 0 otherwise

const amountIn = tokenIn == ETHER ? response.InputAmount : 0

const estimateGasOption = {
        from: account,
        to: trade.routerAddress,
        data: trade.encodedSwapData,
        value: BigNumber.from(amountIn),
      }
      
const gasEstimate = await library
        .getSigner()
        .estimateGas(estimateGasOption)
        .then(response => {
          return response
        })

Execute transaction call

Using the gasEstimate function above to combine with the transaction object

Note: The value of the transaction is the inputAmount if the input token is a native token, and 0 otherwise

const amountIn = tokenIn == ETHER ? response.InputAmount : 0

const sendTransactionOption = {
    from: account,
    to: api.routerAddress,
    data: api.encodedSwapData,
    gasLimit: gasEstimate,
    gasPrice: gasPrice,
    ...(trade.inputAmount.currency instanceof Token
          ? {}
          : { value: BigNumber.from(inputAmount) }),
}

library
    .getSigner()
    .sendTransaction(sendTransactionOption)

Last updated

Change request #477: Elastic fee tier and token launches