V3 Functions
Read Functions
getPool
function getPool(
address tokenA,
address tokenB,
uint24 fee,
).call()
描述:获得流动性地址
调用合约:Factory
参数:
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
返回值:pool的地址
示例:查找 TRX/USDT 且费率为 0.05% 的 V3 流动性地址
const factory_v3 = await tronWeb.contract(V3factoryAbi, "TLJWAScHZ4Qmk1axyKMzrnoYuu2pSLer1F");
const pooladderss_ = await factory_v3.getPool("TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf", "TYsbWxNnyTgsZaTFaue9hqpxkU3Fkco94a", 500).call();
console.log(tronWeb.address.fromHex(pooladderss_))
返回
TPypbNrrRZ6FX735BY2wtKLdpnkLPoGrAo
slot0
function slot0().call()
描述:获得 pool 的信息
调用合约:pool 合约 (通过factory可以获得该合约地址,见getPool())
参数:None
返回值:
sqrtPriceX96
当前价格的平方根(Q64.96格式),需要解码成真实价格
tick
当前的 tick 索引(与价格相关)
observationIndex
当前观测数据索引,用于时间加权平均价(TWAP)计算
observationCardinality
当前可用的观测点数量
observationCardinalityNext
将来预期的观测点数量(用于扩容)
feeProtocol
池子的手续费配置(协议级)
unlocked
表示是否当前池子被锁定(用于防止重入攻击)
示例:查询主网 TRX/USDT-0.05 池子的信息
const contract_pool = await tronWeb.contract(poolAbi, "TSUUVjysXV8YqHytSNjfkNXnnB49QDvZpx")
const slot0_ = await contract_pool.slot0().call()
console.log(slot0_)
返回
[
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()
描述:获取用户index仓位对应的tokenId
调用合约:NonfungiblePositionManager 合约
参数:
owner
address
User's address
index
uint256
The user's nth liquidity position
返回值:tokenId
示例
const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, 'TLSWrv7eC1AZCXkRjpqMZUmvgd99cj7pPF')
const tokenId_ = await contract_Nonfungible.tokenOfOwnerByIndex('TXXXXXXXXXXXXXXXXXXXXXX',0).call();
console.log(tokenId_)
返回
BigNumber { _hex: '0x0207', _isBigNumber: true }
positions
function positions(
uint256 tokenId,
).call()
描述:获取用户tokenId对应流动性
调用合约:NonfungiblePositionManager 合约
参数:
tokenId
uint256
The user's token ID
返回值:
nonce
operator
tokenId授权地址
token0
池子中token0地址
token1
池子中token1地址
fee
池子fee率
tickLower
选择的position的最低值
tickUpper
选择的position的最高值
liquidity
流动性
feeGrowthInside0LastX128
feeGrowthInside1LastX128
截至对单个头寸的最后一次操作时总头寸的费用增长
tokensOwed0
tokensOwed1
截至上次计算,该头寸欠多少未收集的代币
示例
const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, 'TLSWrv7eC1AZCXkRjpqMZUmvgd99cj7pPF')
const pos_ = await contract_Nonfungible.positions(1).call()
console.log(pos_)
返回
[
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()
描述:执行兑换交易
调用合约:SwapRouter
参数:
struct ExactInputParams {
bytes path; // 已编码的交易路径(如 tokenIn -> fee -> tokenOut)
address recipient; // 收款人地址
uint256 deadline; // 交易截止时间
}
返回值:交易 hash
示例
const contract_swap = await tronWeb.contract(V3swaprouterAbi, config.nile.routerV3)
const exactInput_res = await contract_swap.exactInput(['0xe518c608a37e2a262050e10be0c9d03c7a0877f3000bb843c42f702b0a11565c46e34022aab677d7bd8ae3', self_address,Math.floor(Date.now() / 1000 + 86400)]).send()
console.log(exactInput_res)
注:对交易路径的编码过于复杂,建议开发者通过智能路由进行兑换交易。
increaseLiquidity
function increaseLiquidity(
IncreaseLiquidityParams,
).send()
描述:用于添加流动性
调用合约:NonfungiblePositionManager
参数:
struct IncreaseLiquidityParams {
uint256 tokenId; // 已存在的头寸的 NFT ID
uint256 amount0Desired; // 希望投入的 token0 数量
uint256 amount1Desired; // 希望投入的 token1 数量
uint256 amount0Min; // 可接受的最小 token0 数量(用于滑点保护)
uint256 amount1Min; // 可接受的最小 token1 数量(用于滑点保护)
uint256 deadline; // 截止时间
}
返回值:交易 hash
示例
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 + 86400)]).send({callValue:10})
console.log(increase_L)
decreaseLiquidity
function decreaseLiquidity(
DecreaseLiquidityParams,
).send()
描述:用于移除流动性
调用合约:NonfungiblePositionManager
参数:
struct DecreaseLiquidityParams {
uint256 tokenId; // 头寸的 NFT ID
uint128 liquidity; // 要移除的流动性数量
uint256 amount0Min; // 可接受的最小 token0 数量(用于滑点保护)
uint256 amount1Min; // 可接受的最小 token1 数量(用于滑点保护)
uint256 deadline; // 截止时间
}
返回值:交易 hash
示例
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 + 86400)]).send()
console.log(derease_L)
collect
function collect(
CollectParams,
).send()
描述:用于获取奖励
调用合约:NonfungiblePositionManager
参数:
struct CollectParams {
uint256 tokenId; // 头寸的 NFT ID
address recipient; // 奖励接收地址
uint128 amount0Max; // 最多提取的 token0 数量(通常设为最大值)
uint128 amount1Max; // 最多提取的 token1 数量(通常设为最大值)
}
返回值:交易 hash
示例
const contract_Nonfungible = await tronWeb.contract(NonfungiblePositionManagerAbi, config.nile.NonfungiblePositionManager)
const collect_t = await contract_Nonfungible.collect([1, 'TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 1000000000, 1000000000]).send()
console.log(collect_t)
Last updated