Swapping
This guide will illustrate how to use the SDK for swaps.
Step 1. Import getSwapParameters
To start using the SDK in your application, use an import
or require
statement, depending on what your environment supports.
- ES6 (import)
import getSwapParameters from '@kyberswap/aggregator-sdk';
- CommonJS (require)
const getSwapParameters = require('@kyberswap/aggregator-sdk').default;
Step 2. Call getSwapParameters
with the following input parameters
Parameter Name | Type | Description | Example |
---|---|---|---|
currencyInAddress | string | The address of input currency. In case of native token (ETH, BNB, etc.), set it to ETHER_ADDRESS or `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`. | `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE` |
currencyInDecimals | number | The decimal of input currency | 18 |
amountIn | string | Swap amount (in token wei) | Swap 0.01 BNB ⇒ amountIn = `10000000000000000` |
currencyOutAddress | string | The address of output currency. In case of native token (ETH, BNB, etc.), set it to ETHER_ADDRESS or `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`. | `0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b` |
currencyOutDecimals | number | The decimal of output currency | 18 |
tradeConfig | object | Trade configuration | { minAmountOut: “31722332260833999566”, recipient: “0x16368dD7e94f177B8C2c028Ef42289113D328121”, deadline: 1641804722938 } |
tradeConfig.minAmountOut | string | Minimum amount out. Below this amount, the transaction will be reverted. It is calculated from the amount of output returned from Aggregator API, fee amount (if fees are taken in currency out), and slippage amount. | `31722332260833999566` |
tradeConfig.recipient | string | The address of the wallet to receive currencies after the swap is successful | `0x16368dD7e94f177B8C2c028Ef42289113D328121` |
tradeConfig.deadline | number | The timestamp when the transaction expires | 1641642437722 |
feeConfig | object | undefined | Fee configuration | { chargeFeeBy: “currency_in”, feeReceiver: “0xDa0D8fF1bE1F78c5d349722A5800622EA31CD5dd”, feeAmount: “8”, isInBps: true } |
feeConfig.chargeFeeBy | “currency_in” | “currency_out” | Charge fees in currencyIn or currencyOut | `currency_in` |
feeConfig.feeReceiver | string | The address of the wallet to receive fee after the swap is successful | `0xDa0D8fF1bE1F78c5d349722A5800622EA31CD5dd` |
feeConfig.feeAmount | string | The amount of fee. Unit is bps or token out depends on feeConfig.isInBps | `8` |
feeConfig.isInBps | boolean | If taking fee in percent, this is the percent in bps (BPS = 10,000, feeAmount = 100 means taking 1%). Otherwise, it is the amount of token to take as the fee. | true |
customTradeRoute | string | undefined | The trade route of this swap. Set it the output of JSON.stringify(swaps)* or set it to undefined so that the SDK finds the route on its own. *JSON.stringify(swaps): swap is what get from Aggregator API. | `[[{"pool":"0x6170b6d96167346896169b35e1e9585feab873bb","tokenIn":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","tokenOut":"0xfe56d5892bdffc7bf58f2e84be1b2c32d21c308b","swapAmount":"99920000000000000","amountOut":"32150597150476633010","limitReturnAmount":"0","maxPrice":"115792089237316195423570985008687907853269984665640564039457584007913129639935","exchange":"kyberswap","poolLength":2,"poolType":"dmm"}]]` |
Step 3. Get the output data from getSwapParameters
The output of getSwapParameters
is an object containing methodNames
, args
and values
as described in the table below.
Field Name | Type | Description | Example |
---|---|---|---|
methodNames | Array<string> | Swap function name | [”swap”] |
args | Array<string | Array<string | string[]> | Swap function arguments | [ "0x0E4C34336bf64581cf4dFA658D13a7067888B5a1", [ "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "0xfe56d5892BDffC7BF58f2E84BE1b2C32D21C308b", ["0xDa0D8fF1bE1F78c5d349722A5800622EA31CD5dd"], ["80000000000000"], "0x16368dD7e94f177B8C2c028Ef42289113D328121", "0x16345785d8a0000", "0x1b83c5d1e7decbace", "0x0", "0x" ], “0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000fe56d5892bdffc7bf58f2e84be1b2c32d21c308b000000000000000000000000000000000000000000000001b83c5d1e7decbace00000000000000000000000016368dd7e94f177b8c2c028ef42289113d3281210000000000000000000000000000000000000000000000000000017e432ff2fa000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000006170b6d96167346896169b35e1e9585feab873bb000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000fe56d5892bdffc7bf58f2e84be1b2c32d21c308b00000000000000000000000016368dd7e94f177b8c2c028ef42289113d3281210000000000000000000000000000000000000000000000000162fcb5e9f5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”] |
value | string | The amount of native token sent with the transaction (if swap from ETH, BNB, etc). | `0x16345785d8a0000` |
Step 4. Call swap function in smart contract
You can use the output in the previous step to call Kyberswaps Router Smart Contract* inside your code or directly test it in bscscan.
*Kyberswap’s Router Smart Contract’s address: 0xdf1a1b60f2d438842916c0adc43748768353ec25 (Same address on Ethereum, BSC, Polygon, Avalanche, Fantom, Cronos networks).