nexus-sdk-swap-flows
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSwap Flows
交换流程
Call swapWithExactIn(input, options?)
调用swapWithExactIn(input, options?)
- Use when input amount is fixed and sources are known.
- Signature:
sdk.swapWithExactIn(input, { onEvent? })
- Params ():
ExactInSwapInputfrom: { chainId: number; amount: bigint; tokenAddress: Hex }[]toChainId: numbertoTokenAddress: Hex
- Notes:
- Ensure amounts are in smallest units.
from - Use chain-specific token addresses (see ).
TOKEN_CONTRACT_ADDRESSES
- Ensure
- Result ():
SwapResult{ success: true; result: SuccessfulSwapResult }
- 适用于输入金额固定且来源已知的场景。
- 签名:
sdk.swapWithExactIn(input, { onEvent? })
- 参数 ():
ExactInSwapInputfrom: { chainId: number; amount: bigint; tokenAddress: Hex }[]toChainId: numbertoTokenAddress: 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 ():
ExactOutSwapInputtoChainId: numbertoTokenAddress: HextoAmount: bigint- (optional native token output)
toNativeAmount?: bigint - (optional)
fromSources?: { chainId: number; tokenAddress: Hex }[]
- Notes:
- If is omitted, SDK auto-selects sources.
fromSources
- If
- 适用于预期输出金额固定的场景。
- 签名:
sdk.swapWithExactOut(input, { onEvent? })
- 参数 ():
ExactOutSwapInputtoChainId: numbertoTokenAddress: HextoAmount: bigint- (可选,原生代币输出)
toNativeAmount?: bigint - (可选)
fromSources?: { chainId: number; tokenAddress: Hex }[]
- 注意事项:
- 如果省略,SDK会自动选择来源。
fromSources
- 如果省略
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 ():
SwapAndExecuteParamstoChainId: numbertoTokenAddress: HextoAmount: bigintfromSources?: { chainId: number; tokenAddress: Hex }[]execute: SwapExecuteParams
- :
SwapExecuteParamsto: Hexdata?: Hexvalue?: bigintgas: bigintgasPrice?: 'low' | 'medium' | 'high'tokenApproval?: { token: Hex; amount: bigint; spender: Hex }
- Result ():
SwapAndExecuteResult- (null if swap skipped)
swapResult: SuccessfulSwapResult | null
- 用于执行必要的交换后再调用合约的场景。
- 签名:
sdk.swapAndExecute(input, { onEvent? })
- 参数 ():
SwapAndExecuteParamstoChainId: numbertoTokenAddress: HextoAmount: bigintfromSources?: { chainId: number; tokenAddress: Hex }[]execute: SwapExecuteParams
- 说明:
SwapExecuteParamsto: Hexdata?: Hexvalue?: bigintgas: bigintgasPrice?: 'low' | 'medium' | 'high'tokenApproval?: { token: Hex; amount: bigint; spender: Hex }
- 返回值 ():
SwapAndExecuteResult- (跳过交换时为null)
swapResult: SuccessfulSwapResult | null
Use token addresses and chains
代币地址与链使用
- Use SDK constants for addresses and chain IDs:
SUPPORTED_CHAINSTOKEN_CONTRACT_ADDRESSES
- 使用SDK提供的常量获取地址和链ID:
SUPPORTED_CHAINSTOKEN_CONTRACT_ADDRESSES
Attach swap intent hook
绑定交换意图钩子
- Set and call
sdk.setOnSwapIntentHook(...)to proceed.allow() - 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 or
sdk.utils.parseUnits(value, decimals) - when decimals vary by chain.
sdk.convertTokenReadableAmountToBigInt(value, tokenSymbol, chainId)
- 使用方法,或
sdk.utils.parseUnits(value, decimals) - 当不同链的代币小数位数不同时,使用方法。
sdk.convertTokenReadableAmountToBigInt(value, tokenSymbol, chainId)