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 V1
          • V1 Contract
          • V1 Functions
        • SunSwap V2
          • V2 Contract
          • V2 Functions
      • SunSwap V3 Overview
        • V3 Contract
        • V3 Functions
      • 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
  • Get Liquidity Information
  • getPair
  • Add/Remove Liquidity
  • addLiquidity
  • addLiquidityETH
  • removeLiquidity
  • removeLiquidityETH
  • Execute Swap
  • swapExactTokensForTokens
  • swapExactETHForTokens
  • swapExactTokensForETH
  1. DEVELOPERS
  2. Swap
  3. SunSwap Overview
  4. SunSwap V2

V2 Functions

Get Liquidity Information

getPair

function getPair(
    address tokenA,
    address tokenB,
).call()
  • Description: Get the liquidity pool address

  • Contract called: Factory

  • Parameters:

Parameter Name
Type
Description

tokenA

address

The address of tokenA

tokenB

address

The address of tokenB

  • Return value: Pool address

  • Example: Query the pool address for TRX/USDT

const factory_v2 = await tronWeb.contract(V2factoryAbi, config.nile.v2Factory)
const pairaddress_ = await factory_v2.getPair(config.nile.wtrxToken, config.nile.usdtToken).call()
console.log(tronWeb.address.fromHex(pairaddress_))
  • Return

TKioHQsGLkaEWwBwkUB2T4Rm6nwGATtJyh

Add/Remove Liquidity

addLiquidity

function addLiquidity(
    address tokenA,
    address tokenB,
    uint256 amountADesired,
    uint256 amountBDesired,
    uint256 amountAMin,
    uint256 amountBMin,
    address to,
    uint256 deadline,
).send()
  • Description: Add liquidity (tokenA and tokenB do not include TRX)

  • Contract called: Router

  • Parameters:

Parameter Name
Type
Description

tokenA

address

The address of tokenA

tokenB

address

The address of tokenB

amountADesired

uint256

The desired amount of tokenA to add

amountBDesired

uint256

The desired amount of tokenB to add

amountAMin

uint256

The minimum amount of tokenA to add

amountBMin

uint256

The minimum amount of tokenB to add

to

address

The recipient of the liquidity tokens

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Add liquidity to the TUSD/USDT pool

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const addLiquidity_t = await swap_v2.addLiquidity(config.nile.usdtToken, config.nile.tusdToken, 25000000, 1000000000000000, 1, 1, "TXXXXXXXXXXXXXXXXXXXX",Math.floor(Date.now() / 1000 + 86400)).send()
console.log(addLiquidity_t)

addLiquidityETH

function addLiquidityETH(
    address token,
    uint256 amountTokenDesired,
    uint256 amountTokenMin,
    uint256 amountETHMin,
    address to,
    uint256 deadline,
).send()
  • Description: Add liquidity (One of the tokens is TRX)

  • Contract called: Router

  • Parameters:

Parameter Name
Type
Description

token

address

The address of the token to add liquidity for

amountTokenDesired

uint256

The desired amount of token to add

amountTokenMin

uint256

The minimum amount of token to add

amountETHMin

uint256

The minimum amount of TRX to add

to

address

The recipient of the liquidity tokens

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Add liquidity to the TRX/USDT pool

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const addLiquidityETH_t = await swap_v2.addLiquidityETH(config.nile.usdtToken, 100, 1, 1, "TXXXXXXXXXXXXXXXXXXX",Math.floor(Date.now() / 1000 + 86400)).send({callValue:1000000})
console.log(addLiquidityETH_t)

removeLiquidity

function removeLiquidity(
    address tokenA,
    address tokenB,
    uint256 liquidity,
    uint256 amountAMin,
    uint256 amountBMin,
    address to,
    uint256 deadline,
).send()
  • Description: Remove liquidity (tokenA and tokenB do not include TRX)

  • Contract called: Router

  • Parameters:

Parameter Name
Type
Description

tokenA

address

The address of tokenA

tokenB

address

The address of tokenB

liquidity

uint256

The amount of liquidity tokens to remove

amountAMin

uint256

The minimum amount of tokenA to receive

amountBMin

uint256

The minimum amount of tokenB to receive

to

address

The recipient of the tokens

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Remove liquidity from the TUSD/USDT pool

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const removeLiquidity_t = await swap_v2.removeLiquidity(config.nile.usdtToken,config.nile.tusdToken, 1, 1, 1, "TXXXXXXXXXXXXXXXXXXXX",Math.floor(Date.now() / 1000 + 86400)).send()
console.log(removeLiquidity_t)

removeLiquidityETH

function removeLiquidityETH(
    address token,
    uint256 liquidity,
    uint256 amountTokenMin,
    uint256 amountETHMin,
    address to,
    uint256 deadline,
).send()
  • Description: Remove liquidity (One of the tokens is TRX)

  • Contract called: Router

  • Parameters:

Parameter Name
Type
Description

token

address

The address of the token to remove liquidity for

liquidity

uint256

The amount of liquidity tokens to remove

amountTokenMin

uint256

The minimum amount of token to receive

amountETHMin

uint256

The minimum amount of TRX to receive

to

address

The recipient of the tokens

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Remove liquidity from the TRX/USDT pool

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const removeLiquidityETH_t = await swap_v2.removeLiquidityETH(config.nile.usdtToken,1, 1, 1, "TXXXXXXXXXXXXXXXXXXXX",Math.floor(Date.now() / 1000 + 86400)).send()
console.log(removeLiquidityETH_t)

Execute Swap

swapExactTokensForTokens

function swapExactTokensForTokens(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] path,
    address to,
    uint256 deadline,
).send()
  • Description: Sell Token to Buy Token

  • Contract Called: Router

  • Parameters:

Parameter Name
Type
Description

amountIn

uint256

The amount of input tokens to send

amountOutMin

uint256

The minimum amount of output tokens to receive

path

address[]

An array of token addresses, ordered from input to output

to

address

The recipient of the output tokens

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Sell USDT to Buy SUN

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const swapExactTokensForTokens_t = await swap_v2.swapExactTokensForTokens(20000000, 0, [config.nile.usdtToken, config.nile.sunToken], "TXXXXXXXXXXXXXXXXXXX", Math.floor(Date.now() / 1000 + 86400)).send()
console.log(swapExactTokensForTokens_t)

swapExactETHForTokens

function swapExactETHForTokens(
    uint256 amountOutMin,
    address[] path,
    address to,
    uint256 deadline,
).send()
  • Description: Sell TRX to Buy Token

  • Contract Called: Router

  • Parameters:

Parameter Name
Type
Description

amountOutMin

uint256

The minimum amount of output tokens to receive

path

address[]

An array of token addresses, ordered from input (WTRX) to output

to

address

The recipient of the output tokens

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Sell TRX to Buy USDT

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const swapExactETHForTokens_t = await swap_v2.swapExactETHForTokens(1, [config.nile.wtrxToken, config.nile.usdtToken], "TXXXXXXXXXXXXXXXXXXXX", Math.floor(Date.now() / 1000 + 86400)).send({callValue:1000000})
console.log(swapExactETHForTokens_t)

swapExactTokensForETH

function swapExactTokensForETH(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] path,
    address to,
    uint256 deadline,
).send()
  • Description: Sell Token to Buy TRX

  • Contract Called: Router

  • Parameters:

Parameter Name
Type
Description

amountIn

uint256

The amount of input tokens to send

amountOutMin

uint256

The minimum amount of TRX to receive

path

address[]

An array of token addresses, ordered from input to output (WTRX)

to

address

The recipient of the output TRX

deadline

uint256

Unix timestamp after which the transaction will revert

  • Return value: hash

  • Example: Sell USDT to Buy TRX

const swap_v2 = await tronWeb.contract(V2RouterAbi, config.nile.routerV2)
const swapExactTokensForETH_t = await swap_v2.swapExactTokensForETH(20000000, 0, [config.nile.usdtToken, config.nile.wtrxToken], "TXXXXXXXXXXXXXXXXXXXX", Math.floor(Date.now() / 1000 + 86400)).send()
console.log(swapExactTokensForETH_t)
PreviousV2 ContractNextSunSwap V3 Overview

Last updated 1 day ago