@anywalletkit/protocol-swap2-kyberswap
@anywalletkit/protocol-swap2-kyberswap
Section titled “@anywalletkit/protocol-swap2-kyberswap”KyberSwap Aggregator as a same-chain swap2 provider: exact-input swaps routed by KyberSwap and settled through MetaAggregationRouterV2 on 18 EVM chains — Ethereum, Base, BNB Smart Chain, Arbitrum, Optimism, Polygon, Avalanche, Linea, Sonic, Berachain, Unichain, HyperEVM, Plasma, Monad, MegaETH, Robinhood Chain, Arc and Elysium.
Keyless (KyberSwap’s public API) or with an API-gateway key. No integrator fee is charged.
import {kyberswapProvider} from '@anywalletkit/protocol-swap2-kyberswap'
const kyber = kyberswapProvider({ rpcUrls: {8453: ['https://…'], 999: ['https://…']}, // required to execute (simulation before signing) and for receipts enabledChains: [8453, 999], // default: every chain in KYBER_CHAINS clientId: 'my-wallet', // x-client-id and the build's `source` (written on-chain)})// in the router: new RouterSwap2Protocol(account, {fromChain, providers: [kyber, …]})Standalone: new KyberswapSwap2Protocol(account, {fromChain: 'eip155:8453', rpcUrls}).
Behind a backend: remoteProvider({name: 'kyberswap', baseUrl, ...kyberPins}) in the app, so every quote the backend
returns is checked on the device against the pinned router and the app’s own request.
| Option | Default | |
|---|---|---|
rpcUrls |
— | per chain id; without one, execution quotes on that chain are refused |
enabledChains |
all of KYBER_CHAINS |
|
apiKey |
— | KyberSwap API-gateway key (X-Api-Key, https://api.kyberswap.com/swap); keep it on a server. With a key, x-client-id is not sent |
baseUrl |
public API | override the API base |
clientId |
anywalletkit |
x-client-id (keyless only) and the build’s source |
defaultSlippageBps |
50 | KyberSwap accepts under 2000 |
quoteTtlMs |
30 000 | how long a quote stays executable (the review fits; an approval mined meanwhile gets one re-quote) |
rfq |
"exclude" |
RFQ market makers (pmm-N, Bebop, Native, Hashflow, …); see RFQ and limit orders |
limitOrders |
"exclude" |
KyberSwap’s own limit orders (kyberswap-limit-order-v2); see RFQ and limit orders |
excludeRfqSources |
true |
deprecated: false = both of the above "include"; each of them, when set, wins for its own sources |
excludedSources |
— | KyberSwap source ids |
How a swap is checked
Section titled “How a swap is checked”- A dry quote is
GET /routes; an execution quote also callsPOST /route/build. The route summary is sent back untouched, with sender, recipient, slippage, a 20-minute deadline andsource— never fee fields,permit,ignoreCappedSlippageororigin. - KyberSwap’s build endpoint encodes whatever route summary it receives, fee entries and output amount included, so
nothing in its answer is trusted.
checkKyberTransactiondecodes the calldata strictly (re-encoded byte for byte) and refuses unless:- the target is MetaAggregationRouterV2 and the call is
swap()(swapGeneric,swapSimpleModeand router V3 are refused by name); - the executor is the pinned AggregationExecutorProxy and the router approves nothing itself;
- input token and amount are the request’s: a native input carries no split and
valueequal to the amount, a token input’s split adds up to the amount and sends no value; - output token and receiver are the request’s (never the zero address);
- the minimum is positive and at least the quote’s minimum and the caller’s floor;
- there are no fee receivers, no permit, and no flag but
0x200.
- the target is MetaAggregationRouterV2 and the call is
- The quote’s minimum is KyberSwap’s own:
floor(amountOut × (1 − slippage)) − 1(the build lands one unit under the plain floor on every chain).destAmountis KyberSwap’samountOut, the route’s ceiling: executed outputs come a few basis points under it, and KyberSwap keeps any output above the quote. - Every execution quote is simulated before the user pays for anything: a native input with a balance override; a
token input with its approval and the swap as one simulated block (
eth_simulateV1), or as the plain call when the allowance is already there — so a route that reverts never asks for an approval. A wallet without the input, or an RPC withouteth_simulateV1(Monad, Avalanche, HyperEVM on 2026-11-25), skips that early check (providerData.simulated: "none"). Every swap is simulated again from the account at the latest block right before signing (preflightDeposit). A revert, or no RPC answering, refuses the swap — nothing is sent unsimulated. The gas limit is built on the account’s own estimate of the exact call (ownGasEstimate):max(estimate, build gas) × 1.3 + 100 000, see “Gas” below. - Token inputs approve exactly the amount to the router (the engine default), spent in the same transaction.
- KyberSwap’s own USD values of the input and the output (
amountInUsd/amountOutUsd) reach the router’squoteValuesasproviderData.priceImpactBps— the value lost to pool impact and pool fees — so a near-total loss gets the “be careful” warning even when the app has no price for the token (2026-11-25 Unichain USDC → JUP: 7 824 bps). Information only, never a refusal; a token KyberSwap does not price carries none.
RFQ and limit orders
Section titled “RFQ and limit orders”Two kinds of KyberSwap liquidity are not pools: RFQ market makers (the sources KyberSwap tags “RFQ sources”:
pmm-N, Bebop, Native, Hashflow, Swaap, …), whose firm quote is signed when the route is built, and KyberSwap’s own
limit orders (kyberswap-limit-order-v2), makers’ signed orders kept off-chain and co-signed by KyberSwap when the
route is built. Both are left out by default. KyberSwap’s excludeRFQSources=true (0.2.0, and the default here)
leaves out both together; checked 2026-11-30 on Ethereum DRV → USDC: with it, no limit-order hop; with the RFQ sources
excluded by name instead, the limit order stays in the route.
kyberswapProvider({rpcUrls, limitOrders: 'include'}) // limit orders, no RFQ (recommended, below)kyberswapProvider({rpcUrls, rfq: 'include', limitOrders: 'include'}) // everything KyberSwap offers this clientrfq |
limitOrders |
GET /routes asks |
|---|---|---|
| exclude | exclude | excludeRFQSources=true — the 0.2.0 request, byte for byte |
| exclude | include | excludedSources= the chain’s RFQ sources (KYBER_RFQ_SOURCES, read from KyberSwap’s source list 2026-11-30) |
| include | exclude | excludedSources=kyberswap-limit-order-v2 |
| include | include | no source filter |
The app’s own excludedSources are added to the list. The execution quote asks /routes with the same filters as
the dry one, so the route reviewed and the route built draw on the same liquidity.
What changes for a route that uses either kind (kyberSourceKind reads each hop’s poolType — “orderbook” for an
RFQ maker, “limit-order” — or its source id; a new pmm-N KyberSwap adds after the list was read is still
recognised):
providerData.offchainis"rfq","limit-order"or"rfq,limit-order".validUntilMsis at mostKYBER_RFQ_TTL_MS(10 s) with an RFQ hop,KYBER_LIMIT_ORDER_TTL_MS(30 s) with a limit order, and never more thanquoteTtlMs. The API states no expiry for either; the figures are the measurement below. An approval mined after the quote ran out gets the engine’s one re-quote, on the same allowance (no new approval).- The execution quote’s
destAmountis the build’samountOutwhen that is lower than the route’s (the maker signs, or KyberSwap falls back to the pool it names after the maker —pmm-19/uniswapv3— only at build); a build under the quote’s minimum is refused (price_check_failed). - Nothing else is relaxed: the same
swap(), router, executor, flags, input split, receiver and minimum checks (every RFQ and limit-order build measured passedcheckKyberTransactionunchanged), the same simulation before any approval and again before signing,exactAllowanceOnlyon the generic-build routers. - A swap whose maker quote or limit order no longer fills reverts in the pre-signing simulation and is refused as
quote_expired(“the limit order in this route no longer fills (expired or taken). Nothing was sent; refresh the quote.”); the router’s minimum check staysprice_check_failed. Recorded:fixtures/offchain.
What this client gets: keyless, and with an API key, a route of RFQ sources alone is refused (40011 filtered liquidity sources) on Ethereum, Base, BNB Smart Chain and Arbitrum; RFQ makers show up only as hops inside mixed
routes, and not every minute (on 2026-11-30 they were in 8 of the 20 routes below at 11:38–11:44 UTC, and in none
of six of those pairs, asked every 9 s for two minutes, at 11:53).
Measured (2026-11-30)
Section titled “Measured (2026-11-30)”Price: 20 pairs on Ethereum, Base, Arbitrum and BNB Smart Chain, GET /routes in the four settings one after the
other (the first to the last 1–10 s apart, median 3 s: four pairs at a time shared the public API’s 3 requests per
second), each against today’s route (both excluded), in bps of amountOut. Differences of about 1 bps are those
seconds (proprietary AMMs reprice every block), not the setting.
| Chain | Swap | Size | Limit orders only | RFQ only | Both | Off-chain hops in “both” |
|---|---|---|---|---|---|---|
| Ethereum | ETH → USDC | $1,661 | +0.1 | +0.0 | +0.1 | — |
| Ethereum | ETH → USDC | $83,070 | −0.4 | −0.4 | +0.5 | — |
| Ethereum | USDC → ETH | $199,976 | −0.2 | −0.4 | −0.3 | — |
| Ethereum | USDT → USDC | $499,742 | +0.0 | +0.0 | +0.0 | — |
| Ethereum | WBTC → USDC | $178,882 | +0.0 | +0.0 | +0.0 | — |
| Ethereum | PEPE → ETH | $3,506 | +0.0 | +0.0 | +0.0 | — |
| Ethereum | DRV → USDC | $23,806 | +15.1 | +0.0 | +15.1 | limit order |
| Base | ETH → USDC | $49,819 | +0.8 | +0.6 | +0.6 | Bebop |
| Base | USDC → ETH | $995 | +0.7 | +0.5 | +0.5 | — |
| Base | USDC → cbBTC | $99,456 | +0.2 | +0.0 | +0.1 | — |
| Base | AERO → USDC | $19,012 | +0.1 | −0.3 | +0.1 | — |
| Base | USDC → VIRTUAL | $19,891 | +0.9 | −2.0 | +7.3 | Bebop, pmm-8, pmm-19, pmm-21 |
| Arbitrum | ETH → USDC | $49,853 | +0.0 | +0.0 | −0.9 | Native |
| Arbitrum | USDC → WBTC | $99,997 | +0.7 | +0.0 | +0.0 | pmm-13 |
| Arbitrum | USDT0 → USDC | $199,757 | +0.0 | +0.0 | +0.0 | pmm-13 |
| Arbitrum | ARB → USDC | $10,898 | +0.0 | +0.0 | +0.2 | pmm-13 |
| BNB Smart Chain | BNB → USDT | $55,213 | +0.0 | +0.0 | −0.3 | — |
| BNB Smart Chain | USDT → BNB | $49,950 | −0.0 | +0.1 | +0.5 | — |
| BNB Smart Chain | CAKE → USDT | $30,781 | +0.1 | +0.0 | +1.4 | pmm-19 |
| BNB Smart Chain | USDT → BTCB | $49,885 | −0.1 | −0.7 | −1.0 | pmm-13 |
The same DRV → USDC swap read +46 and +59 bps through a limit order earlier that day (reported first by cp0x: another
KyberSwap interface 0.17 % over this module on 64 000 DRV, its route through kyberswap-limit-order-v2); KNC → USDC
routes through a limit order too. Limit orders pay where makers post orders above the pools, mostly long-tail tokens; on
majors neither kind moved the price by more than the noise.
Execution: for every route with an off-chain hop (and today’s route as a control), POST /route/build right after the
quote for a throwaway sender = recipient, and that one calldata run with eth_simulateV1 (sender funded by a state
override, a token input approved exactly in the same simulated block; nothing sent) 0, 10, 30 and 60 s after the
quote; plus the quote’s route built afresh 10, 30 and 60 s after it and run at once. Arbitrum USDC → WBTC is left out:
today’s route reverted there too, so its reverts are not the RFQ maker’s.
| Route | Calldata from the quote runs at 0 / 10 / 30 / 60 s | Built afresh at 10 / 30 / 60 s, runs |
|---|---|---|
| pools only (control) | 18/18 at 0 s, 18/18 at 60 s | — |
| with a limit order | 2/2, 2/2, 2/2, 2/2 | 1/1, 1/1, 1/1 |
| with an RFQ maker | 5/7, 5/7, 4/7, 4/7 | 5/7, 5/7, 5/7 |
- Limit orders: one DRV → USDC calldata followed to 300 s ran up to 91 s, paying 0.17 % less at 61 s and 0.38 % less
at 91 s (parts of the orders gone, still above the minimum), and reverted (“Call failed”) from 121 s — the recording
in
fixtures/offchain. - RFQ: two of the seven RFQ routes never ran (Base USDC → VIRTUAL through five makers; Arbitrum ARB → USDC through
pmm-13), not from the quote and not built afresh: the simulation right after the build refuses them before any approval. One (BNB Smart Chain CAKE → USDT throughpmm-19) ran at 11 s and reverted at 30 s: with an approval in between, that swap fails after the user paid for the approval — hence the 10-second life, which sends such a swap to a re-quote after the approval instead. One fresh build failed outright (4222 quoted amount is smaller than estimated): a refusal, as for any route.
Which setting
Section titled “Which setting”For an app with an approval between the quote and the signature (30–60 s): limitOrders: "include", rfq: "exclude". Limit orders are where the price is (+15 to +59 bps on the long tail, nothing lost on majors), and their
fills lasted well past the 30 s quote life in every measurement. RFQ added at most 7 bps on 2 of 20 pairs, two of
seven RFQ routes did not execute at all, and one expired inside an approval’s time; with RFQ included the 10-second
life also means a review longer than 10 s ends in “quote expired” before anything is signed.
RFQ for listing only (dry quotes with rfq: "include", execution without) is not offered: the list would show a price
the execution quote, asking without RFQ, may not reach.
Gas: gasEstimate
Section titled “Gas: gasEstimate”Every quote, dry or for execution, carries gasEstimate = KyberSwap’s routeSummary.gas from GET /routes (asked
with gasInclude=true), gasEstimateSource: "venue". It is there to compare routes (see protocol-swap2-core, “Gas:
gasEstimate vs gasLimit”): a dry quote has no transaction, so before this the router could only assume a gas limit
for it. It counts the swap call only, no approval. A route that states no gas (or 0) gives a dry quote without one;
an execution quote then takes the build’s gas instead, when that is a valid figure.
Gas: the limit the swap is sent with
Section titled “Gas: the limit the swap is sent with”The execution quote carries the build’s gas as txRequest.venueGas (a decimal string from 21 000 to
10 000 000; anything else is left out, never refused), and the transaction carries no gasLimit. Right before the
broadcast the engine dry-runs the exact call from the account (ownGasEstimate) and sends it with
max(the account's estimate, venueGas) × 1.3 + 100 000, at most 10 000 000 (30 000 000 on Monad; above it the swap is refused,
policy_refused); the native-gas check prices that limit. KyberSwap’s figure is a floor, never a ceiling: it can
raise the limit, never lower it below the account’s estimate. An account that does not report its estimate
(quoteSendTransaction without gas) sends with its own estimate, as before. A remote stub with kyberPins declares
ownGasEstimate too, and a swap2 server carries venueGas in the sealed quote.
Why (2026-12-04, Ethereum, 19 WETH → USDC through MetaAggregationRouterV2, three execution quotes in a row): the routes went through many oracle-priced PMM pools, whose gas moves between the estimate and inclusion, and the router’s catch turns an out-of-gas inside the executor into a plain “Call failed”.
| Transaction | Sent with (the wallet’s estimate × 1.2) | Gas used | Result | The new rule from that estimate alone |
|---|---|---|---|---|
| 0x4da3fd71… | 1 187 136 (989 280) | 1 152 501 | out of gas | 1 386 064 |
| 0x05151ebb… | 2 882 809 (2 402 341) | 2 762 524 | out of gas | 3 223 044 |
| 0x6fc9f8ac… | 4 316 655 (3 597 212) | 2 864 620 | success | 4 776 376 |
For the first one the build’s gas decides: the new limit reaches the 2 864 620 the successful route used when that
figure is at least 2 126 631. A fresh build of the same swap (19 WETH → USDC, 2026-12-04, a public WETH holder as
sender) stated 1 949 706 (RFQ excluded, as the module asks; limit 2 634 618) and 2 241 582 (all sources; limit
3 014 057). The first route’s own figure is not recoverable and its calldata was about half the size of the
successful one (7.5 kB against 15.8 kB), so what it needed is not known.
What KyberSwap’s figure is: its own model of the route’s gas, not a simulation. The build answers the same number
(gas of POST /route/build equalled routeSummary.gas in all 16 recordings), and routine routes land on round model
values (330 498 for one Uniswap v3 or PancakeSwap v3 pool, 956 331 on two different chains). Against the gas the built
call used — 16 live routes recorded 2026-11-28 (fixtures/quotes) on Ethereum, Base, Arbitrum, Optimism, Polygon, BNB
Smart Chain, Linea, Sonic and Unichain for a throwaway sender = recipient, each built call run with eth_simulateV1
right after, at the chain’s gas price (sender funded by a state override, a token input approved exactly in the same
simulated block; nothing sent):
Gas used ÷ routeSummary.gas |
|
|---|---|
| 16 routes | 0.58–1.85, median 0.73 |
| above the figure | 4 of 16: Linea Etherex CL 1.01, Polygon Ramses v3 + DODO 1.07, Sonic Metropolis LB 1.44, Base kipseli-prop 1.85 |
So it mostly runs high (the call used about three quarters of it at the median), and a proprietary-AMM route can run well over (how much gas such a pool burns in a simulation need not be what it burns on-chain). The receiver held none of the output, so writing its first balance is in these figures. On Arbitrum they are L2 execution gas without the L1 data component, which the router leaves out too.
Router owner
Section titled “Router owner”KYBER_CHAINS[id].build names the router build on each chain, read on-chain:
generic(Ethereum, Base, BNB Smart Chain, Arbitrum, Optimism, Polygon, Avalanche, Linea): the router hasswapGenericand an owner-managed whitelist, so its owner (a single key on all but Linea) could make it spend an allowance left on it. None is left: the approval is exact and spent by the swap itself, and revoked if the swap does not go out. The engine’s two approval options (protocol-swap2-core, “Approvals”) would change that —revokeUnspentApproval: falseleaves an unspent approval in place,allowanceReuse: "sufficient"swaps from a larger allowance the account already gave the router, which then stays — so the provider declaresexactAllowanceOnlyfor these chains (kyberExactAllowanceOnly, also inkyberPinsfor a remote stub): the engine holds KyberSwap routes here to the defaults whatever the app set, the allowance exact and revoked when unspent. A chain this module does not list counts asgeneric(fail closed).lite(every other chain): noswapGeneric, no whitelist. The approval options apply there as on any provider.
Limits
Section titled “Limits”- Exact input only; same chain only; native ↔ wrapped is the Wrap provider’s (not routed here).
- On Arc the native coin is refused (USDC with 18-decimal native accounting); swap its ERC-20.
- The public API allows 3 requests per second per IP, shared by everything behind that IP; dry routes for the same
request are reused for 5 seconds. A 429 is a
rate_limitedrefusal and the router carries on without KyberSwap. - Smart-account / EIP-7702 senders, fee-on-transfer and rebasing tokens are not supported.
Checked on mainnet
Section titled “Checked on mainnet”Base, 2026-11-24, one test wallet, through the engine (review → approval → preflight → send):
| Swap | Quoted | Received | Gas | Transaction |
|---|---|---|---|---|
| 0.000079 ETH → USDC (native value, no approval) | 0.133161 USDC (min 0.132494) | 0.133160 USDC | 239,155 | 0x989779e9…148fe7 |
| 1 USDC → ETH (exact approval, spent in the swap) | 0.000595564 ETH (min 0.000592587) | 0.000595540 ETH (−0.4 bps) | 305,229 + approval | 0xda5f2326…ba6dac |
The router’s USDC allowance was 0 after the token swap. scripts/verify-mainnet.mjs repeats it (--send native|token).
scripts/verify-live.mjs checks the contracts on every chain and builds (unsigned) one corridor each way per chain.
blockNumber
Section titled “blockNumber”QuoteParams.blockNumber (one block for a comparison round) is not supported: the price comes from the KyberSwap Aggregator API, so the quote names no block (Quote.blockNumber absent).
Classes
Section titled “Classes”Interfaces
Section titled “Interfaces”Type Aliases
Section titled “Type Aliases”Variables
Section titled “Variables”- KYBER_CHAINS
- KYBER_DEFAULT_SLIPPAGE_BPS
- KYBER_EXECUTOR
- KYBER_FLAGS_ALLOWED
- KYBER_LIMIT_ORDER_SOURCES
- KYBER_LIMIT_ORDER_TTL_MS
- KYBER_NATIVE
- KYBER_REFUSED_SELECTORS
- KYBER_RFQ_SOURCES
- KYBER_RFQ_TTL_MS
- KYBER_ROUTER_ABI
- KYBER_ROUTER_V2
- KYBER_ROUTER_V3
- KYBER_SWAP_SELECTOR
- kyberPins
- KYBERSWAP_MODULE
Functions
Section titled “Functions”- checkKyberTransaction
- describeKyberActivity
- kyberApiSlippage
- kyberChain
- kyberExactAllowanceOnly
- kyberGasEstimate
- kyberMinimum
- kyberNativeSwaps
- kyberSourceKind
- kyberswapProvider
- kyberValueLossBps
- kyberVerifyQuote
References
Section titled “References”default
Section titled “default”Renames and re-exports KyberswapSwap2Protocol