Read functions
getPool
Copy function getPool(
address tokenA,
address tokenB,
uint24 fee,
).call()
Description: Get the liquidity address
Contract to call: Factory
Parameter Name
Type
Description
The contract address of one token
The contract address of the other token
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%
Copy const factory_v3 = await tronWeb.contract(V3factoryAbi, "TLJWAScHZ4Qmk1axyKMzrnoYuu2pSLer")
const pooladderss_ = await factory_v3.getPool("TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf", "TYsb")
console.log(tronWeb.address.fromHex(pooladderss_))
Copy TPypbNIrRZ6FX735BY2wtKLdpnkLPoGiAo
slot0
Copy function slot0().call()
Description: Get information about the pool
Contract to call: Pool contract (can be obtained through factory, see getPool())
Square root of the current price (Q64.96 format), needs to be decoded to get the real price
Current tick index (related to price)
Current observation data index, used for time-weighted average price (TWAP) calculation
Current number of available observation points
observationCardinalityNext
Expected number of observation points in the future (used for capacity expansion)
The pool's fee configuration (protocol level)
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
Copy const contract_pool = await tronWeb.contract(poolAbi, "TSUUVjysXV8YqHytSNjfkNXnnB49QDvZpx")
const slot0_ = await contract_pool.slot0().call()
console.log(slot0_)
Copy [
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
Copy function tokenOfOwnerByIndex(
address owner,
uint256 index,
).call()
Description: Get the tokenId corresponding to the user's index
Contract to call: NonfungiblePositionManager contract
Parameter Name
Type
Description
The user's nth liquidity position
Copy const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, 'TLSWrv7eC1AZCXkRjpqMZUmvgd99cj7pPF')
const tokenId_ = await contract_Nonfungible.tokenOfOwnerByIndex('TXXXXXXXXXXXXXXXXXXXXXX',0).call();
console.log(tokenId_)
Copy BigNumber { _hex: '0x0207', _isBigNumber: true }
positions
Copy function positions(
uint256 tokenId,
).call()
Description: Get the liquidity corresponding to the user's tokenId
Contract to call: NonfungiblePositionManager contract
Parameter Name
Type
Description
Address authorized for the tokenId
Address of token0 in the pool
Address of token1 in the pool
Minimum value of the selected position
Maximum value of the selected position
Fee growth of the total position up to the last operation on a single position
How many uncollected tokens the position owes as of the last calculation
Copy const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, 'TLSWrv7eC1AZCXkRjpqMZUmvgd99cj7pPF')
const pos = await contract_Nonfungible.positions(1).call()
console.log(pos_)
Copy [
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
Copy function exactInput(
ExactInputParams,
).send()
Description: Execute an exchange transaction
Contract to call: SwapRouter
Copy struct ExactInputParams {
bytes path; // Encoded transaction path (e.g., tokenIn -> fee -> tokenOut)
address recipient; // Recipient address
uint256 deadline; // Transaction deadline
}
Return value: Transaction hash
Copy 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
Copy function increaseLiquidity(
IncreaseLiquidityParams,
).send()
Description: Used to add liquidity
Contract to call: NonfungiblePositionManager
Copy 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
Copy 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
Copy function decreaseLiquidity(
DecreaseLiquidityParams,
).send()
Description: Used to remove liquidity
Contract to call: NonfungiblePositionManager
Copy 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
Copy 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
Copy function collect(
CollectParams,
).send()
Description: Used to collect rewards
Contract to call: NonfungiblePositionManager
Copy 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
Copy const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, config.nile.NonfungiblePositionManager)
const collect_t = await contract_Nonfungible.collect([1, 'TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'])
console.log(collect_t)