Skip to content
docs

@anywalletkit/protocol-swap2-layerswap

Layerswap for the swap2 engine: cross-chain transfers from EVM, Solana, TON, Tron and Bitcoin to any chain Layerswap serves, talking to Layerswap’s v2 API, with every swap read back and every deposit checked on the device before it is signed.

Layerswap is a solver bridge. One source transaction funds a swap Layerswap keeps under an id; Layerswap then pays the recipient on the destination chain from its own liquidity (or chains CCTP, a native rollup bridge or a DEX hop behind it), or refunds the source address. The deposit names the swap and nothing else — not the recipient, not the destination, not the minimum. Those live in Layerswap’s record of the swap. This module holds that record to the request field by field when the swap is created, again before the build and again right before the signature, and rebuilds or reads the deposit itself.

  • Exact input; same token or another (Layerswap prices the route; swaps through a DEX hop carry Layerswap’s slippage, which this module sets from yours).
  • Sources: 45 EVM chains with the Layerswap Depository, Solana, TON, Tron, Bitcoin. Destinations: every chain on Layerswap’s list this module names (EVM by chain id, plus Solana, TON, Tron, Bitcoin).
  • No provider SDK: viem for the Depository ABI, @noble/curves + @noble/hashes to derive Solana token accounts.
  • Read endpoints need no key. apiKey (Partner Dashboard → app → key) attributes swaps to your app; baseUrl can point at your own proxy that adds it.
import {layerswapProvider} from "@anywalletkit/protocol-swap2-layerswap";
import {CrossChainRouter} from "@anywalletkit/protocol-swap2-router";
const layerswap = layerswapProvider({baseUrl: "https://your-backend.example/layerswap"}); // or nothing: https://api.layerswap.io/api/v2
const router = new CrossChainRouter([layerswap /*, …other providers */]);

Standalone: new LayerswapSwap2Protocol(account, {fromChain: "ton:-239"}).

option
baseUrl Layerswap’s API (default) or your proxy in front of it.
apiKey Sent as X-LS-APIKEY; the key picks mainnet or testnet. Prefer a proxy: a key shipped in an app is public.
fetchImpl Your own fetch.
enabled Default true.
source funding what the account signs
EVM (45 chains) Layerswap Depository (use_depository) an exact approval of the input to the Depository (ERC-20), then depositNative(id, receiver) / depositERC20(id, token, receiver, amount)
Solana wallet transfer Layerswap’s prepared legacy message: one transfer of the input with the swap’s memo (sent unsigned; handed to the wallet behind one empty signature slot, the message bytes unchanged)
TON wallet transfer a TON transfer or a jetton transfer to Layerswap’s address with the swap’s comment
Tron wallet transfer a TRX / TRC-20 transfer to Layerswap’s address with the memo in raw_data.data
Bitcoin deposit address (use_deposit_address) a send of the input to the address minted for the swap

Why these: on Solana and Tron Layerswap’s deposit-address flow costs a sweep (≈ $0.36 on 100 USDC from Solana, ≈ $4 from Tron, 2026-12-01), so the memo’d wallet transfer is used; TON has only the wallet transfer; on Bitcoin the wallet transfer needs an OP_RETURN output the Bitcoin account does not write, and the deposit address costs ≈ 0.03 %.

A refusal reads layerswap: <what was found> and reaches onRefusal with a Swap2RefusalReason (recipient_mismatch, vault_mismatch, policy_refused, quote_expired, …); the thrown LayerswapRefusal also names its check.

The swap is the one asked for (checkSwapRecord; at creation, before the build, before the signature)

  • source and destination networks, source and destination tokens (by contract, in each chain’s spelling: EVM case, TON bounceable or not);
  • the recipient; the amount (a JSON double, held within 1e-9 — the exact one is on the deposit);
  • the source and refund addresses are sent as the sending account (a request naming another refund address is refused before anything is created). Layerswap’s record echoes neither (2026-12-01); a record that does name a source address must name this account. The deposit is the account’s own transfer or its own signature either way;
  • no integrator fee split (integrator_fee_splits empty, on the record and on the quote);
  • the funding method; a well-formed id and sequence number; status still user_transfer_pending at build and signature.

The quote — Layerswap prices again when the swap is created; that quote is the one reviewed. An integrator fee or a gas top-up (refuel) this module did not ask for is refused. Amounts come back as JSON numbers and are read down to base units; amounts go out as exact decimal literals, and an input with more than 15 significant digits (what a double keeps) is refused before any request — round it.

The deposit, per family (verify.ts)

  • one action required; Layerswap’s own approval step is never used, a signing step (gasless, Permit2) never taken;
  • the action’s network and token are the swap’s input;
  • EVM: the target is the pinned Depository; the call is rebuilt here from the swap’s sequence number, the input token and amount and a pinned receiver, and must equal Layerswap’s byte for byte; native value only for a native input; the approval spender is the Depository, for exactly the input;
  • Solana: a legacy message signed by this account alone, sent with no signature slot or one empty one; only the system, token, associated-token, compute-budget and memo programs; at most one compute-unit limit (≤ 400 000) and one price (≤ 5 000 000 micro-lamports, so ≤ 0.002 SOL in priority fees); exactly one top-level memo whose bytes are the swap’s sequence number; exactly one transfer of exactly the input — SOL to the deposit address, or SPL from this account’s associated token account to the deposit address’s (both derived here) — and at most one account creation (CreateIdempotent, optionally listing the rent sysvar), of this account’s own token account or the deposit address’s, under the transfer’s own token program; the wallet then simulates it and holds balances to the input;
  • TON: the comment is the swap’s sequence number (on Layerswap’s shared address it is what credits the deposit to this swap; seen on a live swap 2026-12-01); the jetton amount in the payload (the action’s own amount reads 0 for a jetton) or the TON value is exactly the input;
  • Tron: a Tron address, the memo the swap’s sequence number, exactly the input;
  • Bitcoin: a manual_transfer to a mainnet address, no memo output, exactly the input.

The whole deposit check runs again right before the signature, against the swap as Layerswap holds it then (GET /swaps/{id} for the record, GET /swaps/{id}/deposit_actions?source_address= for the deposit — the record alone lists a Solana deposit as a manual transfer without its transaction), and the transaction about to be signed must be that deposit. One deposit per swap from a provider instance. The Depository call goes out with the account’s own gas estimate of exactly that call (ownGasEstimate).

On 2026-12-01 two unfunded swaps were created from burner addresses with no key (Solana USDC → Base USDC, TON USDT → Arbitrum USDT0); the answers are in fixtures/api/real-2026-12-01.json and replayed through the engine in tests/real-api.test.ts. Requests name networks by Layerswap’s name, as those creates did.

The same day three funded runs went through this module and the SDK wallets, keyless, and all completed on Arbitrum within about 15 seconds of the deposit: 0.10 USDC from Base through the Depository, 0.10 USDC from Solana, 0.20 USDT from TON. Their answers are in fixtures/api/real-funded-2026-12-01.json, replayed in tests/real-funded.test.ts; the wallet’s addresses, the swap ids and sequence numbers, the times and the Solana blockhashes there are stand-ins (consistent within each run), and the on-chain hashes are left out. What they showed:

  • The EVM deposit is one transfer action to the Depository with amount_in_base_units: "0" (the value of an ERC-20 deposit) and depositERC20(sequence_number, token, receiver, amount) in call_data, plus gas_limit and encoded_args. The call rebuilt here was byte for byte Layerswap’s. No approval action comes with it: the engine sends the exact approval.
  • Every deposit_actions read answers the Solana message with a fresh blockhash; the quoted message is the one signed and the read-backs are held to it.
  • GET /networks was 2.66 MB (hundreds of tokens on each big EVM chain), so it alone may be up to 8 MB; every other answer is held to 1 MB. It is cached for 15 minutes.
  • A Solana account takes the engine’s transaction through sendSerialized (map sendSerializedTransaction: (tx, expect) => account.sendSerialized(tx, serializedGuardFromExecution(expect))); its guard passed Layerswap’s message with the bridge expectation.

Still read from the docs only: Tron’s memo and Bitcoin’s deposit address.

Where Layerswap pays out is its own record. The read-back holds that record to the request, so a swap created for another recipient is refused — but an API (or a proxy) that answers one record and pays from another is trusted, as with every deposit-address venue. The Depository only proves the input reaches a solver address Layerswap’s owner whitelisted (an account, not a Safe, on 2026-12-01) and that this module pinned.

Funds are in Layerswap’s hands between the deposit and the payout. If Layerswap cannot fill the swap it refunds the source address in the source token, minus the refund’s network fee (pending_refund → refunded); a deposit it never matches stays with Layerswap until its support resolves it — keep the swap id (layerswap:<uuid>) and the source hash.

  • Depository 0xE226E4825CB215aBaFAd98fdd400583eAb6a594f, the same address and runtime code on 45 EVM chains (LAYERSWAP_DEPOSITORY_CHAINS), owner 0xB6103502673173ab5Df6A57Ac3017Db4e16af399, not paused. Hexens audit (March 2026). A second deployment at 0x8f00…B038 is not Layerswap’s live one and is never a target.
  • Receivers 0x2fc617e933a52713247ce25730f6695920b3befe, 0x08b00ceee2fb66029b53d76110b19eeaabfd1e65 (getWhitelistedAddresses() on every chain).
  • watch.json follows the network list, the OpenAPI document, the contract source, the code and whitelist on six chains, and the funding guides.

trackIntent reads GET /swaps/{id}: user_transfer_pending → pending (the source hash is handed to deposit_speedup again while unmatched), ls_transfer_pending → pending with the deposit received, completed (with the payout hash), pending_refund → refund-pending, refunded, failed, expired.

layerswapPins for remoteProvider: EVM builds may call and approve only the Depository; Solana / TON / Tron / Bitcoin legs pay an address Layerswap names per swap, so the stub is opened to deposit-address routes. Its verifyQuote re-runs the deposit checks on the device against the app’s own request (the Depository call rebuilt, the Solana transaction read, the memo held to the swap’s sequence number); which swap the deposit names, and Layerswap’s record of it, remain the backend’s word.

Renames and re-exports LayerswapSwap2Protocol