SUN.io 中文文档
SUN.ioSunPump
  • SUN.io 简介
    • 入门指南(TRON)
      • 创建钱包
      • TRON 网络代币标准
      • 连接钱包
    • 风险
    • 白皮书
    • 服务条款
    • 隐私协议
    • 最新公告
  • 开始使用
    • 兑换
      • 代币兑换
        • 如何给token授权?
        • 如何兑换token?
        • 如何查看我最近的交易记录?
        • 如何查看交易对价格走势?
      • 稳定币兑换
        • 稳定币池介绍
        • 如何进行稳定币交易?
        • 如何提供稳定币池的流动性?
        • 如何提取稳定币池流动性?
        • 如何查看自己提供的流动性?
      • 智能路由
      • 交易手续费
        • 收取手续费奖励
      • 流动性资金池
        • 如何创建交易对
        • 如何增加流动性?
        • 如何删除流动性?
        • 增加/删除资金池的token比例是如何计算的?
        • 如何录入一个新的token?
        • 创建资金池初始价格是多少?
      • 代币列表
    • 矿池
      • SUN.io 平台挖矿细则
      • 如何参与LP挖矿?
      • 如何获取LP挖矿收益?
      • 如何退出LP挖矿?
      • 如何计算LP矿池加速倍数?
      • 如何锁定SUN?
      • 如何投票?
      • 如何重置投票?
      • 智能矿池
        • 如何参与 LP 矿池的定期质押?
        • 如何在 LP 矿池中追加定期质押?
        • 如何中途延长 LP 矿池定期质押锁定期?
        • 如何提前解锁 LP 矿池中定期质押的资产?
        • 如何领取 LP 矿池的定期质押奖励?
      • Farm
      • 历史矿池
    • SunPump
      • 🌞 怎么运行的?
      • 🚀 如何启动?
      • 💡代币详情
      • 👤 个人主页
      • 🏦 服务费
      • SunPump DLive Streaming 功能使用教程
    • SunPump Referral
      • 参与方式
      • 活动规则
      • FAQ
    • PSM
      • 如何使用 PSM 在 USDD 和其他稳定币之间进行 1:1 兑换?
    • 代币经济
      • SUN 代币经济模型
        • SUN 回购及销毁
      • veSUN
      • 空投
  • 治理
    • SUN DAO 治理
    • 参与治理
      • 提案
        • SUN DAO 论坛
        • 获得选票
        • 发起提案
        • 进行投票
        • SUN DAO 提案查看
      • DAO 权益
        • 矿池权重
        • veSUN
  • 开发者
    • 兑换
      • 稳定币兑换简介
      • SunSwap兑换简介
      • SunSwap v3兑换简介
        • 合约
        • Functions
      • 智能路由
        • 智能路由合约
        • 智能路由解算服务
        • 智能路由兑换函数
        • 智能路由兑换开发详细步骤
    • 挖矿
      • 智能挖矿V1
      • 智能挖矿V2
      • 治理挖矿
    • Sunpump
      • Sunpump Contracts
    • Github
    • 相关合约及ABIs
  • FAQ
    • 如何在手机 TronLink APP中使用SUN.io?
    • 价格是由什么决定的?
    • 兑换支持哪些Token?
    • 为什么我的兑换会失败?
    • 关于 SUN.io 平台能量补贴的详细说明
Powered by GitBook
On this page
  • swapExactInput
  • 代码实现
  • 完整代码
  1. 开发者
  2. 兑换
  3. 智能路由

智能路由兑换函数

智能路由当前只有一个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,usdd202pool,2pooltusdusdt,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);
Previous智能路由解算服务Next智能路由兑换开发详细步骤

Last updated 4 days ago