V3 Functions

Read functions

getPool

function getPool(
    address tokenA,
    address tokenB,
    uint24 fee,
).call()
  • Description: Get the liquidity address

  • Contract to call: Factory

  • Parameters:

Parameter Name
Type
Description

tokenA

address

The contract address of one token

tokenB

address

The contract address of the other token

fee

uint24

The pool's liquidity fee rate multiplied by 1,000,000

  • Return value: The address of the pool

  • Example: Find the V3 liquidity address for TRX/USDT with a fee rate of 0.05%

const factory_v3 = await tronWeb.contract(V3factoryAbi, "TLJWAScHZ4Qmk1axyKMzrnoYuu2pSLer")
const pooladderss_ = await factory_v3.getPool("TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf", "TYsb")
console.log(tronWeb.address.fromHex(pooladderss_))
  • Return

TPypbNIrRZ6FX735BY2wtKLdpnkLPoGiAo

slot0

function slot0().call()
  • Description: Get information about the pool

  • Contract to call: Pool contract (can be obtained through factory, see getPool())

  • Parameters: None

  • Return value:

Field Name
Description

sqrtPriceX96

Square root of the current price (Q64.96 format), needs to be decoded to get the real price

tick

Current tick index (related to price)

observationIndex

Current observation data index, used for time-weighted average price (TWAP) calculation

observationCardinality

Current number of available observation points

observationCardinalityNext

Expected number of observation points in the future (used for capacity expansion)

feeProtocol

The pool's fee configuration (protocol level)

unlocked

Indicates whether the pool is locked (used to prevent re-entrancy attacks)

  • Example: Query information about the TRX/USDT-0.05 pool on the main network

const contract_pool = await tronWeb.contract(poolAbi, "TSUUVjysXV8YqHytSNjfkNXnnB49QDvZpx")
const slot0_ = await contract_pool.slot0().call()
console.log(slot0_)
  • Return

[
  BigNumber { _hex: '0x84b4ff29997c28cc5848ed8d', _isBigNumber: true },
  -13142,
  0,
  1,
  1,
  0,
  true,
  sqrtPriceX96: BigNumber { _hex: '0x84b4ff29997c28cc5848ed8d', _isBigNumber: true },
  tick: -13142,
  observationIndex: 0,
  observationCardinality: 1,
  observationCardinalityNext: 1,
  feeProtocol: 0,
  unlocked: true
]

tokenOfOwnerByIndex

function tokenOfOwnerByIndex(
    address owner,
    uint256 index,
).call()
  • Description: Get the tokenId corresponding to the user's index

  • Contract to call: NonfungiblePositionManager contract

  • Parameters:

Parameter Name
Type
Description

owner

address

User's address

index

uint256

The user's nth liquidity position

  • Return value: tokenId

  • Example

const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, 'TLSWrv7eC1AZCXkRjpqMZUmvgd99cj7pPF')
const tokenId_ = await contract_Nonfungible.tokenOfOwnerByIndex('TXXXXXXXXXXXXXXXXXXXXXX',0).call();
console.log(tokenId_)
  • Return

BigNumber { _hex: '0x0207', _isBigNumber: true }

positions

function positions(
    uint256 tokenId,
).call()
  • Description: Get the liquidity corresponding to the user's tokenId

  • Contract to call: NonfungiblePositionManager contract

  • Parameters:

Parameter Name
Type
Description

tokenId

uint256

The user's token ID

  • Return value:

Field Name
Description

nonce

operator

Address authorized for the tokenId

token0

Address of token0 in the pool

token1

Address of token1 in the pool

fee

Pool fee rate

tickLower

Minimum value of the selected position

tickUpper

Maximum value of the selected position

liquidity

Liquidity

feeGrowthInside0LastX128

feeGrowthInside1LastX128

Fee growth of the total position up to the last operation on a single position

tokensOwed0

tokensOwed1

How many uncollected tokens the position owes as of the last calculation

  • Example

const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, 'TLSWrv7eC1AZCXkRjpqMZUmvgd99cj7pPF')
const pos = await contract_Nonfungible.positions(1).call()
console.log(pos_)
  • Return

[
  BigNumber { _hex: '0x00', _isBigNumber: true },
  '410000000000000000000000000000000000000000',
  '4138d5a8e87a4bcc2f822786a9b1134a3b7836e382',
  '41646dc03a0884cd4b5254297a04a58a56499f242d',
  3000,
  -283380,
  -269520,
  BigNumber { _hex: '0x134c8d6d899dd330', _isBigNumber: true },
  BigNumber { _hex: '0x00', _isBigNumber: true },
  BigNumber { _hex: '0x01bf24ecb68662e15f15eebe', _isBigNumber: true },
  BigNumber { _hex: '0x00', _isBigNumber: true },
  BigNumber { _hex: '0x00', _isBigNumber: true },
  nonce: BigNumber { _hex: '0x00', _isBigNumber: true },
  operator: '410000000000000000000000000000000000000000',
  token0: '4138d5a8e87a4bcc2f822786a9b1134a3b7836e382',
  token1: '41646dc03a0884cd4b5254297a04a58a56499f242d',
  fee: 3000,
  tickLower: -283380,
  tickUpper: -269520,
  liquidity: BigNumber { _hex: '0x134c8d6d899dd330', _isBigNumber: true },
  feeGrowthInside0LastX128: BigNumber { _hex: '0x00', _isBigNumber: true },
  feeGrowthInside1LastX128: BigNumber { _hex: '0x01bf24ecb68662e15f15eebe', _isBigNumber: true },
  tokensOwed0: BigNumber { _hex: '0x00', _isBigNumber: true },
  tokensOwed1: BigNumber { _hex: '0x00', _isBigNumber: true }
]

Write functions

exactInput

function exactInput(
    ExactInputParams,
).send()
  • Description: Execute an exchange transaction

  • Contract to call: SwapRouter

  • Parameters:

struct ExactInputParams {
    bytes path;          // Encoded transaction path (e.g., tokenIn -> fee -> tokenOut)
    address recipient;   // Recipient address
    uint256 deadline;    // Transaction deadline
}
  • Return value: Transaction hash

  • Example

const contract_swap = await tronWeb.contract(V3swaprouterAbi, config.nile.routerV3)
const exactInput_res = await contract_swap.exactInput(['0xe518c608a37e2a262050e10be0c9d03'])
console.log(exactInput_res)

Note: The encoding of the transaction path is too complex, it is recommended that developers use smart routing for exchange transactions.

increaseLiquidity

function increaseLiquidity(
    IncreaseLiquidityParams,
).send()
  • Description: Used to add liquidity

  • Contract to call: NonfungiblePositionManager

  • Parameters:

struct IncreaseLiquidityParams {
    uint256 tokenId;           // NFT ID of an existing position
    uint256 amount0Desired;    // Desired amount of token0 to invest
    uint256 amount1Desired;    // Desired amount of token1 to invest
    uint256 amount0Min;        // Minimum acceptable amount of token0 (for slippage protection)
    uint256 amount1Min;        // Minimum acceptable amount of token1 (for slippage protection)
    uint256 deadline;          // Deadline
}
  • Return value: Transaction hash

  • Example

const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, config.nile.NonfungiblePositionManager)
const increase_L = await contract_Nonfungible.increaseLiquidity([1, 10, 10, 0, 0, Math.floor(Date.now() / 1000) + 60 * 20])
console.log(increase_L)

decreaseLiquidity

function decreaseLiquidity(
    DecreaseLiquidityParams,
).send()
  • Description: Used to remove liquidity

  • Contract to call: NonfungiblePositionManager

  • Parameters:

struct DecreaseLiquidityParams {
    uint256 tokenId;        // NFT ID of the position
    uint128 liquidity;      // Amount of liquidity to remove
    uint256 amount0Min;     // Minimum acceptable amount of token0 (for slippage protection)
    uint256 amount1Min;     // Minimum acceptable amount of token1 (for slippage protection)
    uint256 deadline;       // Deadline
}
  • Return value: Transaction hash

  • Example

const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, config.nile.NonfungiblePositionManager)
const derease_L = await contract_Nonfungible.decreaseLiquidity([1, 5228703, 0, 0, Math.floor(Date.now() / 1000) + 60 * 20])
console.log(derease_L)

collect

function collect(
    CollectParams,
).send()
  • Description: Used to collect rewards

  • Contract to call: NonfungiblePositionManager

  • Parameters:

struct CollectParams {
    uint256 tokenId;       // NFT ID of the position
    address recipient;     // Reward receiving address
    uint128 amount0Max;    // Maximum amount of token0 to extract (usually set to maximum value)
    uint128 amount1Max;    // Maximum amount of token1 to extract (usually set to maximum value)
}
  • Return value: Transaction hash

  • Example

const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, config.nile.NonfungiblePositionManager)
const collect_t = await contract_Nonfungible.collect([1, 'TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'])
console.log(collect_t)

Last updated