智能路由兑换函数

智能路由当前只有一个swapExactInput函数,用户可以调用其来触发交易。智能路由器会在内部处理交易数据并完成交易。

swapExactInput

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

  • 描述:swapExactInput 函数是兑换操作的统一入口。

  • 定义:

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;
         }
  • 参数:

path:路径数组 

poolVersion:pool的版本数组。大小写敏感 

versionLen:相邻 pool 版本的长度数组。第一个要+1。 

fees:fee率数组。fee率数组长度跟path数组长度一致,最后一个固定为零;v1和v2都填零,v3根据池子的费率填写。 

data:[要兑换金额,兑换出的最小接受额,接受兑换金额地址,deadline] 
  • 示例:用1个TRX兑换为USDJ

Name
Value
Description

path

[WTRX address, USDT address, USDJ address]

path of tokens

poolVersion

['v1', 'v2']

poolVersion can be v1,V2,V3,usdt20psm,usdd202pool2pooltusdusdt,usdc2pooltusdusdt,usdd2pooltusdusdt,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

代码实现

主要利用 TronWeb 与合约交互, 初始化 TronWeb 实例后, 就能很方便的与线上合约交互。

const router = await tronWeb.contract(smartrouterAbi, nile.smartRouter);
const trx = '0x0000000000000000000000000000000000000000';
let result = await router
  .swapExactInput(
    [trx, nile.usdtToken, nile.usdjToken],
    ['v1', 'v2'],//小写
    [2, 1],//第一个要加1
    [0,0,0], //最后一个固定是零。只有V3有数字,V1,V2都是零
    [1000000,1, //不带精度
    'TXXXXXXXXXXXXXXXXXXXXXXXX', //接受兑换金额的地址自定义
    Math.floor(Date.now() / 1000 + 86400)],//deadline 秒
  )
  .send({ callValue: 1000000 , feeLimit: 10000 * 1e6 }); //如果第一个是TRX,则value必须填写,且与要兑换的金额一致。如果第一个不是TRX,则value不需要填写。
console.log(result);

完整代码

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"
  },
  {
    "inputs": [],
    "name": "admin",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "owner",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "psmUsdd",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "v1Factory",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "v2Router",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "v3Router",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "stateMutability": "payable",
    "type": "receive",
    "name": "receive"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "transferOwnership",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "newAdmin",
        "type": "address"
      }
    ],
    "name": "transferAdminship",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "token",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "retrieve",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "string",
        "name": "poolVersion",
        "type": "string"
      },
      {
        "internalType": "address",
        "name": "pool",
        "type": "address"
      },
      {
        "internalType": "address[]",
        "name": "tokens",
        "type": "address[]"
      }
    ],
    "name": "addPool",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "string",
        "name": "poolVersion",
        "type": "string"
      },
      {
        "internalType": "address",
        "name": "pool",
        "type": "address"
      },
      {
        "internalType": "address[]",
        "name": "tokens",
        "type": "address[]"
      }
    ],
    "name": "addUsdcPool",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "string",
        "name": "poolVersion",
        "type": "string"
      },
      {
        "internalType": "address",
        "name": "pool",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "gemJoin",
        "type": "address"
      },
      {
        "internalType": "address[]",
        "name": "tokens",
        "type": "address[]"
      }
    ],
    "name": "addPsmPool",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "string",
        "name": "poolVersion",
        "type": "string"
      }
    ],
    "name": "isUsdcPool",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "string",
        "name": "poolVersion",
        "type": "string"
      }
    ],
    "name": "isPsmPool",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "pool",
        "type": "address"
      },
      {
        "internalType": "address[]",
        "name": "tokens",
        "type": "address[]"
      }
    ],
    "name": "changePool",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address[]",
        "name": "path",
        "type": "address[]"
      },
      {
        "internalType": "string[]",
        "name": "poolVersion",
        "type": "string[]"
      },
      {
        "internalType": "uint256[]",
        "name": "versionLen",
        "type": "uint256[]"
      },
      {
        "internalType": "uint24[]",
        "name": "fees",
        "type": "uint24[]"
      },
      {
        "components": [
          {
            "internalType": "uint256",
            "name": "amountIn",
            "type": "uint256"
          },
          {
            "internalType": "uint256",
            "name": "amountOutMin",
            "type": "uint256"
          },
          {
            "internalType": "address",
            "name": "to",
            "type": "address"
          },
          {
            "internalType": "uint256",
            "name": "deadline",
            "type": "uint256"
          }
        ],
        "internalType": "struct SmartExchangeRouter.SwapData",
        "name": "data",
        "type": "tuple"
      }
    ],
    "name": "swapExactInput",
    "outputs": [
      {
        "internalType": "uint256[]",
        "name": "amountsOut",
        "type": "uint256[]"
      }
    ],
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amountMinimum",
        "type": "uint256"
      },
      {
        "internalType": "address",
        "name": "recipient",
        "type": "address"
      }
    ],
    "name": "unwrapWTRX",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amountMax",
        "type": "uint256"
      }
    ],
    "name": "warpWTRX",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  }
];
const router = await tronWeb.contract(smartrouterAbi, nile.smartRouter);
const trx = '0x0000000000000000000000000000000000000000';
let result = await router
  .swapExactInput(
    [trx, nile.usdtToken, nile.usdjToken],
    ['v1', 'v2'],//小写
    [2, 1],//第一个要加1
    [0,0,0], //最后一个固定是零。只有V3有数字,V1,V2都是零
    [1000000,1, //不带精度
    'THP6c4MTraQZatHbkxTP3pdL4AsgdeN9sj', //接受兑换金额的地址自定义
    Math.floor(Date.now() / 1000 + 86400)],//deadline 秒
  )
  .send({ callValue: 1000000 }); //如果第一个是TRX,则value必须填写,且与要兑换的金额一致。如果第一个不是TRX,则value不需要填写。
console.log(result);

Last updated