Skip to main content
Version: V2

Example

Let's take an example of how KyberSwaps Aggregator API and SDK will be utilized on the BSC chain.

Introduction

Lets say a user wants to swap 0.1 BNB to KNC, and the configuration is as follows:

  • User settings:
    • Slippage is 0.5%
    • Transaction Deadline is 20 minutes
  • System settings:
    • Charge a fee of 0.08% from the input token (in this example, it would be BNB)

Swap

Calling Aggregator API

As mentioned in the overview, KyberSwaps Aggregator API will first need to be invoked.

In the above example on BSC chain, the Aggregator API call will be as follows:

https://aggregator-api.kyberswap.com/bsc/route?tokenIn=0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c&tokenOut=0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b&amountIn=99920000000000000

Here’s a description of the parameters in the above API call:

Param NameValueDescription
tokenIn“0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c”The address of input currency. In case of native token (ETH, BNB, etc.), use its wrapped token address. Example: BNB → WBNB = “0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c”
tokenOut“0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b”The address of output currency. In case of native token (ETH, BNB, etc.), use its wrapped token address. Example: BNB → WBNB = “0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c”
amountIn“99920000000000000”The amount user wants to swap (after subtracting fee and multiplied by 10 power WBNB’s decimals, which is 18). Explanation: 99920000000000000 = 0.1 (1 - 0.08%) 10^18

Aggregator API response

Below is an example of the response you will receive from KyberSwaps Aggregator API.

{
inputAmount: "99920000000000000",
outputAmount: "32606311681146930364",
totalGas: 190000,
gasPriceGwei: "5",
gasUsd: 0.4282290225428444,
amountInUsd: 45.040677823664225,
amountOutUsd: 44.85124101763668,
receivedUsd: 44.42301199509384,
swaps: [
[
{
pool: "0x6170b6d96167346896169b35e1e9585feab873bb",
tokenIn: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
tokenOut: "0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b",
swapAmount: "99920000000000000",
amountOut: "32606311681146930364",
limitReturnAmount: "0",
maxPrice: "115792089237316195423570985008687907853269984665640564039457584007913129639935",
exchange: "kyberswap",
poolLength: 2,
poolType: "dmm"
}
]
],
tokens: {
0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c: {
address: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
symbol: "WBNB",
name: "Wrapped BNB",
price: 450.7673921503625,
decimals: 18
},
0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b: {
address: "0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b",
symbol: "KNC",
name: "Kyber Network Crystal",
price: 1.3755386213635996,
decimals: 18
}
}
}

Passing Swap Parameters

Based on the Aggregator APIs response in the previous step, some of the output data will become input parameters while calling getSwapParameters in Aggregator SDK:

Output in Aggregator API’s responseAPI ValueInput Parameter when Calling Aggregator SDKSDK Value
inputAmount"99920000000000000"amountIn“99920000000000000”
outputAmount"32606311681146930364"tradeConfig.minAmountOut“32444091225021821257” Explanation: 32444091225021821257 = 32606311681146930364 / (1 + 0.5%) 0.5% is allowed slippage.
swaps[ [ { pool: "0x6170b6d96167346896169b35e1e9585feab873bb", tokenIn: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c", tokenOut: "0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b", swapAmount: "99920000000000000", amountOut: "32606311681146930364", limitReturnAmount: "0", maxPrice: "115792089237316195423570985008687907853269984665640564039457584007913129639935", exchange: "kyberswap", poolLength: 2, poolType: "dmm" } ] ]customTradeRoute[[{"pool":"0x6170b6d96167346896169b35e1e9585feab873bb","tokenIn":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","tokenOut":"0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b","swapAmount":"99920000000000000","amountOut":"32606311681146930364","limitReturnAmount":"0","maxPrice":"115792089237316195423570985008687907853269984665640564039457584007913129639935","exchange":"kyberswap","poolLength":2,"poolType":"dmm"}]] ( use JSON.stringify )

Next, you need to invoke getSwapParameters using the new input parameters described above. Below is an example if you’re trying to swap BNB to KNC.

const swapParameters = await getSwapParameters({
chainId: 56, // Binance Smart Chain Mainnet.
currencyInAddress: ETHER_ADDRESS, // 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE.
currencyInDecimals: 18,
amountIn: '99920000000000000',
currencyOutAddress: '0xfe56d5892BDffC7BF58f2E84BE1b2C32D21C308b', // KNC
currencyOutDecimals: 18,
tradeConfig: {
// 32444091225021821257 = 32606311681146930364 / (1 + 0.5%)
// 32606311681146930364 is the outputAmount which come from API response.
// 0.5% is allowed slippage.
minAmountOut: '32444091225021821257',
recipient: '0x16368dD7e94f177B8C2c028Ef42289113D328121',
deadline: 1641804722938, // = Date.now() + 20 * 60 * 1000
},
feeConfig: {
isInBps: true,
feeAmount: '8', // 0.08%
feeReceiver: '0xDa0D8fF1bE1F78c5d349722A5800622EA31CD5dd',
chargeFeeBy: 'currency_in',
},
customTradeRoute: `[[{"pool":"0x6170b6d96167346896169b35e1e9585feab873bb","tokenIn":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","tokenOut":"0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b","swapAmount":"99920000000000000","amountOut":"32606311681146930364","limitReturnAmount":"0","maxPrice":"115792089237316195423570985008687907853269984665640564039457584007913129639935","exchange":"kyberswap","poolLength":2,"poolType":"dmm"}]]`,
});

Output from Swap Parameters

Below is the output of the above function getSwapParameters:

{
"methodNames": [
"swap"
],
"args": [
"0x0E4C34336bf64581cf4dFA658D13a7067888B5a1",
[
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
"0xfe56d5892BDffC7BF58f2E84BE1b2C32D21C308b",
[
"0xDa0D8fF1bE1F78c5d349722A5800622EA31CD5dd"
],
[
"80000000000000"
],
"0x16368dD7e94f177B8C2c028Ef42289113D328121",
"0x16345785d8a0000",
"0x1b83c5d1e7decbace",
"0x0",
"0x"
],
"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000fe56d5892bdffc7bf58f2e84be1b2c32d21c308b000000000000000000000000000000000000000000000001b83c5d1e7decbace00000000000000000000000016368dd7e94f177b8c2c028ef42289113d3281210000000000000000000000000000000000000000000000000000017e432ff2fa000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000006170b6d96167346896169b35e1e9585feab873bb000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000fe56d5892bdffc7bf58f2e84be1b2c32d21c308b00000000000000000000000016368dd7e94f177b8c2c028ef42289113d3281210000000000000000000000000000000000000000000000000162fcb5e9f5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
],
"value": "0x16345785d8a0000"
}

Invoking Swap

Now that we have the output from the getSwapParameters function, we need to invoke the swap function in the smart contract using the above output. To test this out, you can go to bscscan, use the above output in your code and invoke the swap function.

Below is an illustration of invoking the swap function on bscscan: BscScan

After the function runs successfully, we have a transaction receipt below:

Binance Transaction Hash (Txhash) Details | BscScan