@anywalletkit/protocol-swap2-router

中文文档
0.5.3发布于 2026/11/07Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 2300

Optional swap2 aggregator for Any Wallet Kit: auctions whichever swap2 providers you install and pass in.

原始使用文档
文档目录4 节
  1. Install
  2. Three ways to offer cross-chain swaps
  3. How the winner is chosen
  4. Activity

@anywalletkit/protocol-swap2-router

npm

The optional swap2 aggregator. Every provider module (-lifi, -near-intents, -thorchain, -omniston, -cctp, -uniswap, -balancer, -wrap) works on its own; install this package when you want one protocol that auctions a route across the providers you pass in. It bundles none.

Install

npm install @anywalletkit/protocol-swap2-router @anywalletkit/core
# or: pnpm add @anywalletkit/protocol-swap2-router @anywalletkit/core  ·  yarn add @anywalletkit/protocol-swap2-router @anywalletkit/core

On npm: @anywalletkit/protocol-swap2-router, published from the release workflow with provenance.

Three ways to offer cross-chain swaps

1. One protocol, no aggregator. Install a single provider and use its own protocol class.

import {LifiSwap2Protocol} from "@anywalletkit/protocol-swap2-lifi";
const swap2 = new LifiSwap2Protocol(account, {fromChain: "eip155:1"});

2. Aggregator picks for the user. Quotes every provider in parallel, validates each quote the same way and executes the winner.

import {RouterSwap2Protocol, usdValue} from "@anywalletkit/protocol-swap2-router";

const swap2 = new RouterSwap2Protocol(account, {
  fromChain: "eip155:1",
  providers: [lifiProvider({}), nearIntentsProvider({}), cctpProvider({rpcUrls})],
  valueUsd: usdValue(prices),   // optional: rank on net USD (see below)
  prefer: "best",               // or "fastest"
});
await swap2.swap2({fromToken: "USDC", toToken, toChain: "eip155:8453", fromTokenAmount, minAmountOut});

3. Aggregator with a route picker. List every route with the best and the fastest tagged, let the user choose, then execute exactly that provider.

const routes = await swap2.quoteSwap2Routes(options, {
  // Called each time another provider answers: show the first price at once,
  // a "searching…" hint while `pending` is non-empty.
  onUpdate: (routes, pending) => render(routes, pending),
});
// routes[i] = {provider, quote, netValueUsd, relativeValue, tags: ["best"] | ["fastest"] | …}
await swap2.using(picked.provider).swap2({...options, minAmountOut: picked.quote.toTokenAmountMin});

The same is available one level down on CrossChainRouter#getQuotes(params, {onUpdate}).

How the winner is chosen

  1. Value. With valueUsd (e.g. usdValue(prices, {gasUsd})) each quote is worth its guaranteed output in USD minus what the route charges on top: a native bridge fee and, when you can say it, the source gas. Without prices for every quote the auction compares guaranteed outputs.
  2. Price first. Quotes within tieBand (default 0.2%) of the best value are the same price.
  3. Speed and reliability inside the tie. Among equal prices the faster, then the more reliable, route wins (weights, default price:0.6,speed:0.3,reliability:0.1). A sliver of extra output no longer beats a route that lands twenty times faster; a real price gap still wins.

fastest is always the lowest ETA. Every quote passes the engine's checks before it is ranked, and the executed route is re-validated against its sealed quote before anything is signed.

Activity

The router executes through whichever provider wins, so it has no transactions of its own: describeActivity(tx, ctx) asks every installed provider in order and returns the first description (null when none claims the transaction, or when the router holds no providers). Each provider package documents what it recognizes (@anywalletkit/protocol-swap2-lifi → LI.FI Diamond calls, -thorchain → router depositWithExpiry / transferOut, -cctp → burn and claim, -uniswap / -balancer / -wrap → the DEX router / Vault / WETH9 calls, -omniston → STON.fi router and Omniston escrow on TON, -near-intents → the intents.near legs). The conformance fixture here is a public WETH9 deposit recognized by an in-test provider, proving the delegation; the negative is a LI.FI swap no installed provider claims.

版本与发布记录

@anywalletkit/protocol-swap2-router 共发布 15 个版本,首次发布于 2026/10/25;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.3latest2026/11/07今天
0.5.22026/11/052 天前
0.5.12026/11/035 天前
0.5.02026/10/308 天前
0.4.02026/10/309 天前
0.3.02026/10/2910 天前

关于 @anywalletkit/protocol-swap2-router 的常见问题

@anywalletkit/protocol-swap2-router 是什么?

Optional swap2 aggregator for Any Wallet Kit: auctions whichever swap2 providers you install and pass in.

如何安装 @anywalletkit/protocol-swap2-router?

在项目目录运行 npm install @anywalletkit/protocol-swap2-router,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@anywalletkit/core(0.8.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@anywalletkit/protocol-swap2-router 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anywalletkit/protocol-swap2-router 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@anywalletkit/protocol-swap2-router 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@anywalletkit/protocol-swap2-router 可以商用吗?

@anywalletkit/protocol-swap2-router 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@anywalletkit/protocol-swap2-router 的下载量有多少?

根据 npm 官方下载统计,@anywalletkit/protocol-swap2-router 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,300 次,日均约 77 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anywalletkit/protocol-swap2-router 还在维护吗?

最新版本 0.5.3 发布于 2026/11/07(今天);至今共发布 15 个版本,其中过去 12 个月发布了 15 个;首次发布于 2026/10/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。