💱交易

通过 UTXOSwap SDK 组装您的交易

UTXOSwap 目前仅支持单跳交易。因此执行交易时,您需要首先确定交易池,然后构建交易意图,最后发送交易。

获取交易池

获取交易池信息是构建交易的第一步。

查询方法

  • 通过代币的 type hash 使用 searchKey 来查询相关的交易池,例如:

    • 传入全零 (0x0000000000000000000000000000000000000000000000000000000000000000) 来查询所有包含 CKB 的交易对。

    • 传入 (0x178fb47b597a56d48b549226aff59f750b4784250c7f40f781b64ef090a8a0a7) 来查询所有包含 Seal 的交易对。

  • 如果 searchKey 为空,则返回所有交易池列表。

示例代码

const { list: pools } = await client.getPoolsByToken({
  pageNo: 0,
  pageSize: 100,
  searchKey: "0x0000000000000000000000000000000000000000000000000000000000000000",
});

组装交易

设置交易参数

  • tokens: 包含两个资产的数组,数组第一个资产为支付资产,第二个资产为期望获得的资产。

  • ckbAddress: 交易地址。

  • poolInfo: 从交易池列表中获得的特定池信息。

配置 Pool 实例

const pool = new Pool({
  tokens,
  ckbAddress: address,
  collector,
  client,
  poolInfo,
});

const { output } = pool.current.calculateOutputAmountAndPriceImpactWithExactInput(inputValue);

发送交易

设置滑点 (slippage) 和手续费率(fee rate),然后调用签名函数发送交易。建议使用 CCC SDK 来作为交易签名器。

交易示例

const intentTxHash = await pool.swapWithExactInput(
  signTxFunc, // 交易签名函数, 建议使用 CCC-SDK 作为交易签名器
  slippage, // 默认 0.5%,范围 1-100
  5000 // CKB 手续费率,至少为 5000
);

常用函数

计算预期输出

计算使用 calculateOutputAmountAndPriceImpactWithExactInput 方法预期获得的 token 数量。

const { output, } = pool.current.calculateOutputAmountAndPriceImpactWithExactInput(inputValue);

Last updated