SUN.io Docs
SUN.ioSunPump
  • SUN.io Overview
    • Get Started(TRON)
      • Create a Wallet
      • TRON Network Token Standards
      • Connect Your Wallet to SUN.io
    • Risks
    • Whitepaper
    • Terms of Service
    • Privacy Policy
    • Announcement
  • GET START
    • Exchange
      • Token Swaps
        • How to authorize a token?
        • How to exchange tokens?
        • How to check my recent transaction history?
        • How to view 24h price trend of a pair ?
      • Stablecoin Pool
        • Stablecoin pool overview
        • How to swap stablecoins?
        • How to provide liquidity for a stablecoin pool?
        • How to withdraw liquidity from a stablecoin pool?
        • How to check the liquidity you have provided?
      • Smart Router
      • Trading Fee
        • Claim Fee Rewards
      • Liquidity Pool
        • How to create a trading pair?
        • How to add liquidity?
        • How to remove liquidity?
        • How to calculate the ratio of tokens to be added to/removed from the pool?
        • How to record a new token?
        • What's the initial price for creating a fund pool?
      • Token Lists
    • Mining Pool
      • Mining Rules of SUN.io
      • How to participate in liquidity mining?
      • How to earn liquidity mining rewards?
      • How to exit liquidity mining?
      • How to calculate the boost rate of LP mining pool?
      • How to lock SUN?
      • How to vote?
      • How to reset votes?
      • Intelligent Boost Mining Pool
        • How to participate in Fixed Staking?
        • How to stake more assets in Fixed Staking?
        • How to extend duration in Fixed Staking?
        • How to make an early withdrawal in Fixed Staking?
        • How to claim rewards in Fixed Staking?
      • Farm
      • Ended Farming Pools
    • SunPump
      • 🌞 How to Participate?
      • 🚀 How to Launch?
      • 💡 Token Details
      • 👤 Personal Profile
      • 🏦 Service Fees
      • How to Use DLive Streaming on SunPump
    • SunPump Referral
      • How to Join
      • Rules
      • FAQ
    • PSM
      • How to swap between USDD and other stablecoins at 1:1 ratio in PSM?
    • Tokenomics
      • SUN Tokenomics
        • Buyback & Burning of the SUN Token
      • veSUN
      • Airdrop
  • GOVERNANCE
    • SUN DAO Governance
    • Participating Governance
      • Proposal
        • SUN DAO Forum
        • How to get more votes?
        • Create Proposal
        • How to vote on the SUN DAO?
        • SUN DAO proposal
      • Governance Rights
        • Weight
        • veSUN
  • DEVELOPERS
    • Swap
      • StableSwap Overview
      • SunSwap Overview
      • SunSwap V3 Overview
      • Smart Router
        • Contract
        • Calculation Service
        • Exchange Functions
        • Detailed Development Steps
    • Mining
      • Smart Mining V1
      • Smart Mining V2
      • Governance Mining
    • Sunpump
      • Sunpump Contracts
    • Github
    • Contracts and ABIs
  • FAQ
    • How to use Sun.io in TronLink Mobile?
    • How is price determined?
    • What tokens are supported for swap?
    • Why does my exchange fail?
    • Detailed Explanation of SUN.io Platform Energy Subsidies
Powered by GitBook
On this page
  • swapExactInput
  • Code Implementation
  • Complete Code
  1. DEVELOPERS
  2. Swap
  3. Smart Router

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"
  }
];
PreviousCalculation ServiceNextDetailed Development Steps

Last updated 1 day ago