Exchange Functions

The Smart Routing currently has only one function, swapExactInput, which users can call to trigger transactions. The Smart Router will process the transaction data internally and complete the transaction.

swapExactInput

  • Name: swapExactInput( address[], string[], uint256[], uint24[], SwapData)

  • Description: The swapExactInput function is the unified entry point for exchange operations.

  • Definition:

function swapExactInput(
        address[] calldata path,
        string[] calldata poolVersion,
        uint256[] calldata versionLen,
        uint24[] calldata fees,
        SwapData calldata data
) external nonReentrant payable returns (uint256[] memory amountsOut)

struct SwapData {
        uint256 amountIn;
        uint256 amountOutMin;
        address to;
        uint256 deadline;
}
  • Parameters:

    path: Array of token addresses (swap path)
    
    poolVersion: Array of pool versions (case-sensitive)
    
    versionLen: Array of lengths between adjacent pool versions; the first value should be incremented by 1
    
    fees: Array of fee rates. The length must match the path array. The last value must be zero; use zero for v1 and v2, and specify the actual fee for v3
    
    data: [amount to swap, minimum acceptable output amount, recipient address, deadline]
  • Example: Swap 1 TRX for USDJ

Name
Value
Description

path

[WTRX address, USDT address, USDJ address]

path of tokens

poolVersion

['v1', 'v2']

poolVersion can be v1, v2, v3, usdt20psm, usdd202pool, 2pooltusdusdt, usdc2pooltusdusdt, usd42pooltusdusdt, usdj2pooltusdusdt, oldusdcpool, old3pool

versionLen

[2, 1]

array of counts of token numbers in poolVersions, eg: if path = [A,B,C,D],poolVersion = ['v2','v2','v3'],that means A→B use 'V2', B→C use 'v2',C→D use 'V3',so the versionLen = ['3','1']. The number of first poolversion count must +1

fees

[0,0,0]

poolFees used to distinguish V3 pools; all other pools are set to 0.

data

[1000000, 1, receiver, deadline]

SwapData

Code Implementation

Mainly using TronWeb to interact with the contract. After initializing the TronWeb instance, you can easily interact with the online contract.

const router = await tronWeb.contract(smartrouterAbi, nile.smartRouter);
const trx = '0x0000000000000000000000000000000000000000';
let result = await router
  .swapExactInput(
    [trx, nile.usdtToken, nile.usdjToken],
    ['v1', 'v2'],//lowercase
    [2, 1],//first one must be +1
    [0,0,0], //last one is fixed at zero, only V3 has numbers, V1,V2 are all zero
    [1000000,1, //without precision
    'TXXXXXXXXXXXXXXXXXXXXXXX', //address to receive the exchanged amount
    Math.floor(Date.now() / 1000 + 86400)],//deadline in seconds
  )
  .send({ callValue: 1000000 , feeLimit: 10000 * 1e6 }); // if the first one is TRX, value must be filled, and consistent with amountIn
console.log(result);

Complete Code

import { initTronWeb } from './tron.mjs';
import config from './config.js';
const { mainnet, nile } = config;
import utils from 'web3-utils';
const { toBN, toWei } = utils;

const tronWeb = await initTronWeb(nile);
let smartrouterAbi = [
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "_old3pool",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_usdcPool",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_v2Router",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_v1Foctroy",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_usdt",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_usdj",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_tusd",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_usdc",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_psmUsdd",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_v3Router",
        "type": "address"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "constructor"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "owner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "pool",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "address[]",
        "name": "tokens",
        "type": "address[]"
      }
    ],
    "name": "AddPool",
    "type": "event",
    "stateMutability": "nonpayable"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "admin",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "pool",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "address[]",
        "name": "tokens",
        "type": "address[]"
      }
    ],
    "name": "ChangePool",
    "type": "event",
    "stateMutability": "nonpayable"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "buyer",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "uint256",
        "name": "amountIn",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256[]",
        "name": "amountsOut",
        "type": "uint256[]"
      }
    ],
    "name": "SwapExactETHForTokens",
    "type": "event",
    "stateMutability": "nonpayable"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "buyer",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "uint256",
        "name": "amountIn",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256[]",
        "name": "amountsOut",
        "type": "uint256[]"
      }
    ],
    "name": "SwapExactTokensForTokens",
    "type": "event",
    "stateMutability": "nonpayable"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "originOwner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "TransferAdminship",
    "type": "event",
    "stateMutability": "nonpayable"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "originOwner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "TransferOwnership",
    "type": "event",
    "stateMutability": "nonpayable"
  },
  {
    "stateMutability": "payable",
    "type": "fallback",
    "name": "fallback"
  }
];

Last updated