@anywalletkit/protocol-swap2-core
@anywalletkit/protocol-swap2-core
Section titled “@anywalletkit/protocol-swap2-core”The swap2 engine: one interface for swaps and bridges (“swap2”), and the execution safety every swap2 provider package relies on.
Install
Section titled “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/coreOn npm: @anywalletkit/protocol-swap2-core, published from the release workflow with provenance.
BaseSwap2Protocol/SingleProviderSwap2— aSwap2Protocol(from@anywalletkit/core) over a provider. It quotes, binds the execution to the quote, runs approvals, sends, and resumes status by id (getSwap2Status,claimSwap2for 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 pinnedcallTargets, approve only the pinnedspenders, and runs the provider’s ownverifyQuote(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 averifyBuildthe 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 withdepositAddressRoutes: true(nearIntentsPins;thorchainPins()andchainflipPinsfor 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 ProtocolOrder— verifying contract = the approved settlement contract, maker = this account, the source token, exactly the input — never aPermit*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:omnistonVerifyBuildopens the order with theorderExtensionthe build carries). A TON cell-body deposit (bodyBoc) is bound too: it attaches the input plus at mosttonGasCeilingof 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 absolutebaseUrlmust behttps:(a same-origin path is fine;allowInsecureBaseUrlfor local development). A provider with a claim step (CCTP) also takesclaimTargetsfrom the app:claimSwap2calls only those contracts.createSwap2Handler(@anywalletkit/protocol-swap2-core/server, server-only) — the other end ofremoteProvider: 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 supplyguard(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: trueon the engine (and oncreateSwap2Handlerfor 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’sgetTokenInfoand sends them assourceDecimals; providers that trade any token resolve the source withsourceAssetOf(params), the rest refuse it. Approvals and value binding are the same as for catalog tokens. - Errors —
Swap2Errorand its subclasses (quote, validation, execution, fee limit, status…). - Activity helpers — what
describeActivityimplementations 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
Classes
Section titled “Classes”- BaseSwap2Protocol
- SingleProviderSwap2
- Swap2ConfigurationError
- Swap2Error
- Swap2ExecutionError
- Swap2FeeLimitError
- Swap2InFlightError
- Swap2InsufficientGasError
- Swap2MaybeSentError
- Swap2QuoteError
- Swap2StatusError
- Swap2UnsupportedOperationError
- Swap2ValidationError
Interfaces
Section titled “Interfaces”- Asset
- CrossChainProvider
- IntentRef
- IntentStatus
- Quote
- QuoteParams
- RemotePins
- RemoteProviderOptions
- RoutedQuote
- ScoreWeights
- SerializedExecutionExpectation
- SlippageContext
- SlippageOptions
- SlippageSuggestion
- Swap2EngineConfig
- Swap2ExecutionPhaseDetail
- Swap2ExecutionPlan
- SwapActivityOptions
- TonBodySummary
- TxRequest
- UnsignedTx
Type Aliases
Section titled “Type Aliases”Variables
Section titled “Variables”Functions
Section titled “Functions”- assetLabel
- assetMetaFor
- calledContract
- callsContract
- callSelector
- chainLabel
- describeSwapLike
- evmRef
- isEvm
- matchesCall
- namespace
- nativeDecimals
- nativeUnits
- ownerAssets
- redactUrls
- remotePinRefusal
- remoteProvider
- shortAddress
- slippageClassOf
- sourceAssetOf
- suggestSlippage
- swap2ChainId
- tonRawAddress
- validQuote