@anywalletkit/protocol-swap2-core

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

The swap2 engine for Any Wallet Kit: one interface for swaps and bridges, quote binding, approvals, review gate, fee caps and status resume; providers plug in as separate packages.

原始使用文档

@anywalletkit/protocol-swap2-core

npm

The swap2 engine: one interface for swaps and bridges ("swap2"), and the execution safety every swap2 provider package relies on.

Install

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

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

  • BaseSwap2Protocol / SingleProviderSwap2 — a Swap2Protocol (from @anywalletkit/core) over a provider. It quotes, binds the execution to the quote, runs approvals, sends, and resumes status by id (getSwap2Status, claimSwap2 for two-phase routes).
  • Safety checks. Quote validation, a minimum-output floor, fee caps, a review gate before signing, and refusals that name their reason. Status ids look like awk1.<provider>.<intent>[.<hash>] (SWAP2_ID_PREFIX).
  • remoteProvider — runs a provider on your server (API keys, shared state) while the app keeps the keys and signs. The backend is not the authority over what the wallet signs: spread the provider package's pins into the stub (remoteProvider({name, baseUrl, ...lifiPins}), fyndPins, uniswapPins, cctpPins…). The engine then lets an EVM route call only the pinned callTargets, approve only the pinned spenders, and runs the provider's own verifyQuote (Fynd: checkFyndTransaction) on the client; a stub without pins gets no EVM calldata route at all (the router drops it with the reason), and neither does a stub whose pins name call targets but carry no check of the call itself — verifyQuote, or a verifyBuild the app supplies — since a pinned contract executes whatever its arguments say. Every shipped EVM pins export carries one. A route that pays an address the backend names — a deposit address, or a plain value send — runs only on a stub the app opened with depositAddressRoutes: true (nearIntentsPins; thorchainPins() and chainflipPins for their Tron vaults), on every chain. Every token call in a swap's calldata, from any provider, must move the source token, at most the swap amount; operator and Permit2 approvals are never a swap's main call. A typed (gasless) order is signed only when it is the swap's own Limit Order Protocol Order — verifying contract = the approved settlement contract, maker = this account, the source token, exactly the input — never a Permit* or another typed message; a provider's own check of the build can run on the client too (verifyBuild), and a remote stub's typed order is signed only when it has one (Omniston: omnistonVerifyBuild opens the order with the orderExtension the build carries). A TON cell-body deposit (bodyBoc) is bound too: it attaches the input plus at most tonGasCeiling of gas (default 1 TON; a jetton source: the ceiling alone), a jetton source's body must be a TEP-74 transfer of exactly the input from this account's own jetton wallet (getJettonWalletAddress) with the excess returning here, the decoded body reaches the review (cellBodyDecoded), and a remote stub's cell body needs the app's own validator (verifyBuild). An absolute baseUrl must be https: (a same-origin path is fine; allowInsecureBaseUrl for local development). A provider with a claim step (CCTP) also takes claimTargets from the app: claimSwap2 calls only those contracts.
  • createSwap2Handler (@anywalletkit/protocol-swap2-core/server, server-only) — the other end of remoteProvider: validates every request, HMAC-seals only quotes that pass the engine's own checks (validQuote: provider, input, floor, slippage, expiry; else 502) and builds only sealed, unexpired ones, puts an intent token on every swap id (tracking, claims and notices without it are refused), passes refusals through with URLs redacted; you supply guard (auth + rate limits, it sees the validated request) and, with per-user sign-in, subject (quotes and swap ids bound to the user). A reference server: examples/server.
  • Tokens outside the catalog — allowUnlistedTokens: true on the engine (and on createSwap2Handler for a remote engine) lets a swap name a token the catalog does not list by its EVM contract or Solana mint: the user's own tokens. Off by default. The engine reads the token's decimals with the account's getTokenInfo and sends them as sourceDecimals; providers that trade any token resolve the source with sourceAssetOf(params), the rest refuse it. Approvals and value binding are the same as for catalog tokens. A bridge to an unlisted destination token takes its decimals from the caller's toTokenDecimals (read on the destination chain), never from the quote alone: a quote whose destDecimals disagree is refused, and without them a Mayan leg out of Solana has no reviewed minimum and is refused at signing.
  • Errors — Swap2Error and its subclasses (quote, validation, execution, fee limit, status…).
  • Activity helpers — what describeActivity implementations share.

Providers live in their own packages: @anywalletkit/protocol-swap2-near-intents, -lifi, -thorchain, -omniston, -cctp, -usdt0, -chainflip, -jupiter, -uniswap, -balancer, -wrap. @anywalletkit/protocol-swap2-router auctions several of them.

Docs: Swaps

Apache-2.0

版本与发布记录

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

版本发布日期距今
0.10.1latest2026/11/05今天
0.10.02026/11/033 天前
0.9.02026/10/306 天前
0.8.02026/10/307 天前
0.7.02026/10/298 天前
0.6.12026/10/289 天前

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

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

The swap2 engine for Any Wallet Kit: one interface for swaps and bridges, quote binding, approvals, review gate, fee caps and status resume; providers plug in as separate packages.

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

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

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

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

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

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

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

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

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

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

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

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

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

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