执行跨链路由

executeRoute 函数用于执行一条路由,并可选支持交易模拟、注入自定义 Cosmos 消息以及跟踪交易状态。 你必须提供一组用户地址(route.requiredChainAddresses 中每条链各一个),以及由 route(/v2/fungible/route)返回的路由对象。
此函数会处理地址与 gas 余额校验,准备路由消息,并按顺序在 Cosmos、Evm 和 Svm 链上执行交易。
async executeRoute(options: ExecuteRouteOptions): Promise<void>

必填字段

route 一个包含兑换或转账操作的 RouteResponse。 userAddresses 每条链对应一个用户地址,顺序必须与 route.requiredChainAddresses 保持一致。
所有用户地址都必须与路由中预期的链 ID 匹配,并且对相应链类型(Cosmos、Evm 或 Svm)而言是合法地址。如果地址不匹配或格式错误,将抛出错误。
getCosmosSigner 接收一个 chainId 并返回 Promise<OfflineSigner> 的函数 getEvmSigner 接收一个 chainId 并返回 Promise<WalletClient> 的函数 getSvmSigner 返回 Promise<OfflineSigner> 的函数

可选字段

slippageTolerancePercent 设置路由允许的最大滑点容忍度(默认为 "1")。 simulate 是否在执行前模拟交易(默认为 true)。 batchSimulate 如果为 true,会在执行前批量模拟所有交易;如果为 false,则逐笔模拟交易。(默认为 true)。 batchSignTxs 如果为 true,多笔交易路由中的所有交易都会在广播前一次性完成签名;如果为 false,则每笔交易都会在广播前单独签名。(默认为 true)。 beforeMsg / afterMsg 可选的 Cosmos 消息,可注入到路由执行的开始或结束位置。 useUnlimitedApproval 如果为 true,会将 Evm 代币授权额度设置为 MAX_UINT256。(默认为 false)。 bypassApprovalCheck 如果为 true,会跳过 Evm 上的代币授权检查。(默认为 false)。 timeoutSeconds 消息准备阶段超时前等待的秒数。 getGasPrice 按链覆盖 gas price。 getFallbackGasAmount 当模拟失败时使用的回退 gas 值。 gasAmountMultiplier 覆盖默认的模拟倍数(默认值为 1.5)。 getCosmosPriorityFeeDenom 接收一个 chainId 并返回 Promise<string> 的函数,用于指定 Cosmos 交易使用的优先费 denom。 示例:
await executeRoute({
  // ...executeRoute params
  getCosmosPriorityFeeDenom: async (chainId) => {
      // this will set the priority fee as ATOM for noble-1 chain
      if (chainId === "noble-1")
        return "ibc/EF48E6B1A1A19F47ECAEA62F5670C37C0580E86A9E88498B7E393EB6F49F33C0";
      return undefined;
    },
});

回调

你还可以按需提供以下回调: onTransactionSignRequested 当交易已准备好由钱包签名时调用。 onTransactionBroadcast 每笔交易广播后调用。 onTransactionCompleted 每笔交易确认后调用。 onTransactionTracked 在确认轮询期间调用,适用于进度跟踪 UI。 onValidateGasBalance 在计算 gas 并进行余额校验时调用。 onApproveAllownace 当授权交易正在执行时调用(仅适用于 EVM 交易)。
await executeRoute({
  route,
  userAddresses: [
    { chainId: "osmosis-1", address: osmoAddress },
    { chainId: "ethereum", address: evmAddress },
  ],
  simulate: true,
  batchSignTxs: true,
  slippageTolerancePercent: "0.5",
  beforeMsg: cosmosMsg1,
  afterMsg: cosmosMsg2,
  getCosmosSigner: (chainId) => {},
  getEvmSigner: (chainId) => {},
  getSvmSigner: () => {},
  onTransactionBroadcast: ({ chainId, txHash }) => {
    console.log(`Broadcasted on ${chainId}: ${txHash}`);
  },
  onTransactionCompleted: ({ chainId, txHash, status }) => {
    console.log(`Completed on ${chainId}: ${txHash} (Status: ${status})`);
  },
  onTransactionTracked: ({ chainId, txHash, status }) => {
    console.log(`Tracking ${chainId}: ${txHash} (Status: ${status})`);
  },
  onTransactionSignRequested: ({ chainId, signerAddress }) => {
    console.log(`Sign requested for ${chainId}`, signerAddress);
  },
});

Executing a cross-chain route

The executeRoute function is used to execute a route, including optional support for simulating transactions, injecting custom Cosmos messages, and tracking transaction status. You must provide a list of user addresses (one per chain in route.requiredChainAddresses), along with the route object returned from route (/v2/fungible/route).
This function handles validation of addresses and gas balances, prepares the route messages, and executes transactions in order across Cosmos, Evm, and Svm chains.
async executeRoute(options: ExecuteRouteOptions): Promise<void>

Required fields

route A RouteResponse containing swap or transfer operations. userAddresses One user address per chain in the same order as route.requiredChainAddresses.
All user addresses must match the chain ids expected in the route, and must be valid for the corresponding chain type (Cosmos, Evm, or Svm). An error will be thrown if the addresses are mismatched or malformed.
getCosmosSigner Function that takes a chainId and returns a Promise<OfflineSigner> getEvmSigner Function that takes a chainId and returns a Promise<WalletClient> getSvmSigner Function that returns a Promise<OfflineSigner>

Optional fields

slippageTolerancePercent Set the maximum slippage tolerance for the route (defaults to “1”). simulate Whether to simulate transactions before executing (defaults to true). batchSimulate If true, simulate all transactions in a batch before execution; if false, simulate each transaction individually. (defaults to true). batchSignTxs If true, all transactions in a multi-transaction route will be signed upfront before broadcasting; if false, each transaction will be signed individually just before broadcasting. (defaults to true). beforeMsg / afterMsg Optional Cosmos messages to inject at the beginning or end of the route execution. useUnlimitedApproval If true, sets Evm token allowances to MAX_UINT256. (defaults to false). bypassApprovalCheck If true, skips token approval checks on Evm. (defaults to false). timeoutSeconds Time in seconds to wait for message preparation before timing out. getGasPrice Override the gas price per chain. getFallbackGasAmount Fallback gas to use if simulation fails. gasAmountMultiplier Overrides the default simulation multiplier (default is 1.5). getCosmosPriorityFeeDenom function that takes a chainId and returns a Promise<string> for the priority fee denom to use for Cosmos transactions. example:
await executeRoute({
  // ...executeRoute params
  getCosmosPriorityFeeDenom: async (chainId) => {
      // this will set the priority fee as ATOM for noble-1 chain
      if (chainId === "noble-1")
        return "ibc/EF48E6B1A1A19F47ECAEA62F5670C37C0580E86A9E88498B7E393EB6F49F33C0";
      return undefined;
    },
});

Callbacks

You can optionally provide the following callbacks: onTransactionSignRequested Called when a transaction is ready to be signed on the wallet. onTransactionBroadcast Called after each transaction is broadcasted. onTransactionCompleted Called after each transaction is confirmed. onTransactionTracked Called during confirmation polling, useful for progress tracking uis. onValidateGasBalance Called while the transaction is calculating the gas and balance validation onApproveAllownace Called when the allowance tx is being executed (only in evm txs).
await executeRoute({
  route,
  userAddresses: [
    { chainId: "osmosis-1", address: osmoAddress },
    { chainId: "ethereum", address: evmAddress },
  ],
  simulate: true,
  batchSignTxs: true,
  slippageTolerancePercent: "0.5",
  beforeMsg: cosmosMsg1,
  afterMsg: cosmosMsg2,
  getCosmosSigner: (chainId) => {},
  getEvmSigner: (chainId) => {},
  getSvmSigner: () => {},
  onTransactionBroadcast: ({ chainId, txHash }) => {
    console.log(`Broadcasted on ${chainId}: ${txHash}`);
  },
  onTransactionCompleted: ({ chainId, txHash, status }) => {
    console.log(`Completed on ${chainId}: ${txHash} (Status: ${status})`);
  },
  onTransactionTracked: ({ chainId, txHash, status }) => {
    console.log(`Tracking ${chainId}: ${txHash} (Status: ${status})`);
  },
  onTransactionSignRequested: ({ chainId, signerAddress }) => {
    console.log(`Sign requested for ${chainId}`, signerAddress);
  },
});