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)

Last updated