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.
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);