KyberSwap Docs
Search
K

Execute A Swap With The Aggregator API

Interacting With KyberSwap Aggregator Router Contract

Overview

KyberSwap maintains a single API specification for all EVM chains:

KyberSwap Aggregator APIv1

Following feedback on the initial non-versioned API, KyberSwap has implemented a more performant [V1] API which improves the response time for getting a route via offloading encoding requirements to the post method.
For integrators who have previously integrated with the non-versioned API, please refer to Upgrading To APIv1 for further details on the motivation behind the upgrade as well as the relevant changes to swap flow and parameters.
Please use the [V1]GET API for more efficient route queries. The returned route can then be reused in the [V1]POST body to get the encoded swap data. The non-versionedGET and [V1]GET remains backwards compatible with the main change being the queried path.
While both versions of the API remains backwards compatible, only the [V1] APIs will continue to receive updates and hence developers are highly encouraged to implement the latest [V1] APIs to avoid any disruptions as the non-versioned API is eventually deprecated.

Sequence diagram

APIv1 sequence diagram
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 are 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.

Web3.js example

Swap Params Response

Parameter Name
Type
Required
Description
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 [V1]POST API. The relevant parameter keys are provided below ( non-versioned -> [V1]):
  • 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)