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)