Execute A Swap With The Aggregator API
Interacting With KyberSwap Aggregator Router Contract
Last updated
Was this helpful?
Interacting With KyberSwap Aggregator Router Contract
Last updated
Was this helpful?
As the KyberSwap Aggregator has been deployed on Solana, KyberSwap maintains 2 different API specifications:
This guide focuses on calling the Aggregator APIs for but the same principles apply to the 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.
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 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.
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 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
Web3.js integration is relatively straightforward but to avoid transaction failure due to gas, we recommend performing a gas estimation call.
Note: The value of the transaction is the inputAmount
if the input token is a native token, and 0 otherwise
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