nexus-sdk-swap-flows

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Swap Flows

兑换流程

Call swapWithExactIn(input, options?)

调用swapWithExactIn(input, options?)

  • Use when input amount is fixed and sources are known.
  • Signature:
    • sdk.swapWithExactIn(input, { onEvent? })
  • Params (
    ExactInSwapInput
    ):
    • from: { chainId: number; amount: bigint; tokenAddress: Hex }[]
    • toChainId: number
    • toTokenAddress: Hex
  • Notes:
    • Ensure
      from
      amounts are in smallest units.
    • Use chain-specific token addresses (see
      TOKEN_CONTRACT_ADDRESSES
      ).
  • Result (
    SwapResult
    ):
    • { success: true; result: SuccessfulSwapResult }
  • 适用场景:输入金额固定且来源已知时。
  • 签名:
    • sdk.swapWithExactIn(input, { onEvent? })
  • 参数(
    ExactInSwapInput
    ):
    • from: { chainId: number; amount: bigint; tokenAddress: Hex }[]
    • toChainId: number
    • toTokenAddress: Hex
  • 注意事项:
    • 确保
      from
      中的金额为最小单位。
    • 使用链专属的代币地址(参考
      TOKEN_CONTRACT_ADDRESSES
      )。
  • 返回结果(
    SwapResult
    ):
    • { success: true; result: SuccessfulSwapResult }

Call swapWithExactOut(input, options?)

调用swapWithExactOut(input, options?)

  • Use when desired output amount is fixed.
  • Signature:
    • sdk.swapWithExactOut(input, { onEvent? })
  • Params (
    ExactOutSwapInput
    ):
    • toChainId: number
    • toTokenAddress: Hex
    • toAmount: bigint
    • toNativeAmount?: bigint
      (optional native token output)
    • fromSources?: { chainId: number; tokenAddress: Hex }[]
      (optional)
  • Notes:
    • If
      fromSources
      is omitted, SDK auto-selects sources.
  • 适用场景:期望输出金额固定时。
  • 签名:
    • sdk.swapWithExactOut(input, { onEvent? })
  • 参数(
    ExactOutSwapInput
    ):
    • toChainId: number
    • toTokenAddress: Hex
    • toAmount: bigint
    • toNativeAmount?: bigint
      (可选的原生代币输出)
    • fromSources?: { chainId: number; tokenAddress: Hex }[]
      (可选)
  • 注意事项:
    • 如果省略
      fromSources
      ,SDK将自动选择来源。

Call swapAndExecute(input, options?)

调用swapAndExecute(input, options?)

  • Use to perform swap (if needed) and then execute a contract call.
  • Signature:
    • sdk.swapAndExecute(input, { onEvent? })
  • Params (
    SwapAndExecuteParams
    ):
    • toChainId: number
    • toTokenAddress: Hex
    • toAmount: bigint
    • fromSources?: { chainId: number; tokenAddress: Hex }[]
    • execute: SwapExecuteParams
  • SwapExecuteParams
    :
    • to: Hex
    • data?: Hex
    • value?: bigint
    • gas: bigint
    • gasPrice?: 'low' | 'medium' | 'high'
    • tokenApproval?: { token: Hex; amount: bigint; spender: Hex }
  • Result (
    SwapAndExecuteResult
    ):
    • swapResult: SuccessfulSwapResult | null
      (null if swap skipped)
  • 适用场景:需要执行兑换(若有必要)并随后调用合约时。
  • 签名:
    • sdk.swapAndExecute(input, { onEvent? })
  • 参数(
    SwapAndExecuteParams
    ):
    • toChainId: number
    • toTokenAddress: Hex
    • toAmount: bigint
    • fromSources?: { chainId: number; tokenAddress: Hex }[]
    • execute: SwapExecuteParams
  • SwapExecuteParams
    • to: Hex
    • data?: Hex
    • value?: bigint
    • gas: bigint
    • gasPrice?: 'low' | 'medium' | 'high'
    • tokenApproval?: { token: Hex; amount: bigint; spender: Hex }
  • 返回结果(
    SwapAndExecuteResult
    ):
    • swapResult: SuccessfulSwapResult | null
      (若跳过兑换则为null)

Use token addresses and chains

使用代币地址和链信息

  • Use SDK constants for addresses and chain IDs:
    • SUPPORTED_CHAINS
    • TOKEN_CONTRACT_ADDRESSES
  • 使用SDK常量获取地址和链ID:
    • SUPPORTED_CHAINS
    • TOKEN_CONTRACT_ADDRESSES

Attach swap intent hook

绑定兑换意图钩子

  • Set
    sdk.setOnSwapIntentHook(...)
    and call
    allow()
    to proceed.
  • If the hook is not set, the SDK auto-approves.
  • 设置
    sdk.setOnSwapIntentHook(...)
    并调用
    allow()
    以继续执行。
  • 若未设置钩子,SDK将自动批准。

Stream swap events

监听兑换事件

  • Listen for
    NEXUS_EVENTS.SWAP_STEP_COMPLETE
    .
  • Handle special cases like swap skipped (e.g., already on destination token).
  • 监听
    NEXUS_EVENTS.SWAP_STEP_COMPLETE
    事件。
  • 处理特殊情况,比如兑换被跳过(例如已持有目标代币)。

Convert amounts to bigint

将金额转换为bigint类型

  • Use
    sdk.utils.parseUnits(value, decimals)
    or
  • sdk.convertTokenReadableAmountToBigInt(value, tokenSymbol, chainId)
    when decimals vary by chain.
  • 使用
    sdk.utils.parseUnits(value, decimals)
    ,或者
  • 当代币小数位数因链而异时,使用
    sdk.convertTokenReadableAmountToBigInt(value, tokenSymbol, chainId)