How can I run swap operation which includes routing from my contract? I want to swap eth-tokens in my contract by using kyberswap.(I mean tokens or eth in the contract will be swapped, not in my wallet) Normally I found the 'encodedSwapData' by using aggregator-api, and send it as data in tx (sendRawTransaction), and the swap operation is done for my wallet. But I need to do it from my own contract.

async function swapETH() {
  const encodedSwapData = await getEncodedSwapData(ethAddr, usdtAddr, "20000000000000000000", true)
  const tx = {
    from: hrdhatAccountPublic,
    to: kyberRouter,
    gasLimit: 1000000,
    value: "20000000000000000000",
    data: encodedSwapData

  const sentTxResponse = await walletForKyber.sendTransaction(tx)

async function getEncodedSwapData(tokenIn, tokenOut, amountIn, lowestGas) {
  let url = 'https://aggregator-api.kyberswap.com/ethereum/route/encode'
  const _params = {
    tokenIn: tokenIn,
    tokenOut: tokenOut,
    amountIn: amountIn,//"20000000000000000000"
    to: hrdhatAccountPublic,
    saveGas: lowestGas ? "1" : "0",
    gasInclude: lowestGas ? "1" : "0",
    // clientData: 

  const res = await axios.get(url, { params: _params })
  // console.log(res.data.encodedSwapData);
  return res.data.encodedSwapData

This works for hardhat forking account. But I need to run this operation inside a contract. For this they say that I need to use the function swapExactInputSingle(ExactInputSingleParams params) inside the router contract.

Thats why I added an interface in my contract for router including this function and struct which is parameter in it.

interface IKyberswapRouter {
    struct ExactInputSingleParams {
        address tokenIn; // source token to swap
        address tokenOut; // dest token to receive
        uint16 fee; // fee of the pool to swap
        address recipient; // the recipient of tokenOut
        uint256 deadline; // deadline for the transaction
        uint256 amountIn; // the amount of tokenIn to swap
        uint256 minAmountOut; // min acceptable amount of tokenOut
        uint160 limitSqrtP; // the limit of sqrt price, partial swap if price reaches the limitation

    function swapExactInputSingle(ExactInputSingleParams memory params) external payable;

I've also added the the function in my contract to trigger it swapping ETH in it to the ERC20 token.

function kyberswapEthToErc20(
        address _tokenIn,
        address _tokenOut,
        uint256 amountIn,
        address routerr
    ) public {
        uint16 poolFee = 3000;

        IKyberswapRouter.ExactInputSingleParams memory params = IKyberswapRouter
                tokenIn: _tokenIn,//ETH or WETH?   ETH=0xEeeeeEe....
                tokenOut: _tokenOut,//USDT
                fee: poolFee,
                recipient: address(this),
                deadline: block.timestamp + 300,// '+300' causes any error?
                amountIn: amountIn,
                minAmountOut: 0,
                limitSqrtP: 0
        IKyberswapRouter(routerr).swapExactInputSingle{value: amountIn}(params);

I was planning to add two more function for swapping ERC20->ERC20 or ERC20->ETH. Clean code is not important for now. This function in my contract is not working. I don't know where the mistake is, parameter types/values or 'routerr' address. https://docs.kyberswap.com/contract/deployment

In the kyberdocs, router address is '0xC1e7dFE73E1598E3910EF4C7845B68A9Ab6F4c83'

In the aggregatorApi, router address is '0x6131B5fae19EA4f9D964eAc0408E4408b66337b5' and it can change.

Thanks for your help...

2 Answers 2


I'll try to help you:

The deadline is in seconds, does block.timestamp is comming in seconds or milliseconds? If i were you i wouldn't sum the timestamp with 300 on solidity, i would pass the deadline as it should be while calling the contract.

  • block.timestamp is a kind of unix time and returns you something like this 1674568286. If you convert it from link you can get GMT Tue Jan 24 2023 13:51:26 GMT+0000 Jan 24, 2023 at 13:52

I'm researching the fee structure of aggregators like Kyberswap, Paraswap, Totle, Matcha, Li.Fi, 1inch, etc, but I'm having problems finding information. My objective is to create my own DEX aggregator. Do you know how to get this information from?

  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Adam Boudj
    Apr 7, 2023 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.