Repository Public
1// Records real answers of the KyberSwap Aggregator API (GET /routes only: price discovery, nothing is built or signed) into2// packages/swap/test/fixtures/kyberswap/. Each file keeps the request URL, the HTTP status, the time and the untouched body.3// Run: npx tsx scripts/record-swap-kyberswap-fixtures.mts (about one request per second; the public limit is 3 rps)4import { mkdirSync, writeFileSync } from 'node:fs';5import { NETWORKS } from '@spw/core';67const BASE = 'https://aggregator-api.kyberswap.com';8const NATIVE = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';9const dir = new URL('../packages/swap/test/fixtures/kyberswap/', import.meta.url);10mkdirSync(dir, { recursive: true });11const tok = (net: string, sym: string) => NETWORKS[net]!.tokens.find((t) => t.symbol === sym)!.id;12const cases: [name: string, chain: string, tokenIn: string, tokenOut: string, amountIn: string][] = [13 ['ethereum-eth-usdc', 'ethereum', NATIVE, tok('ethereum', 'USDC'), '10000000000000000'],14 ['ethereum-usdc-wbtc', 'ethereum', tok('ethereum', 'USDC'), tok('ethereum', 'WBTC'), '50000000'],15 ['base-usdc-eth', 'base', tok('base', 'USDC'), NATIVE, '25000000'],16 ['arbitrum-usdc-link', 'arbitrum', tok('arbitrum', 'USDC'), tok('arbitrum', 'LINK'), '30000000'],17 ['polygon-pol-usdc', 'polygon', NATIVE, tok('polygon', 'USDC'), '100000000000000000000'],18 ['optimism-eth-usdc', 'optimism', NATIVE, tok('optimism', 'USDC'), '10000000000000000'],19 ['bsc-bnb-fdusd', 'bsc', NATIVE, tok('bsc', 'FDUSD'), '40000000000000000'],20 ['avalanche-usdc-usdt', 'avalanche', tok('avalanche', 'USDC'), tok('avalanche', 'USDT'), '20000000'],21 ['hyperevm-hype-usdc', 'hyperevm', NATIVE, tok('hyperevm', 'USDC'), '1000000000000000000'],22 ['no-route-1-wei', 'ethereum', NATIVE, tok('ethereum', 'USDC'), '1'],23 ['error-unknown-token', 'ethereum', NATIVE, '0x000000000000000000000000000000000000dEaD', '10000000000000000'],24 ['error-malformed-token', 'ethereum', NATIVE, 'nothex', '10000000000000000'],25 ['error-amount-too-large', 'ethereum', NATIVE, tok('ethereum', 'USDC'), '1000000000000000000000000000000000'],26 ['error-unknown-chain', 'notachain', NATIVE, tok('ethereum', 'USDC'), '10000000000000000'],27];28for (const [name, chain, tokenIn, tokenOut, amountIn] of cases) {29 const url = `${BASE}/${chain}/api/v1/routes?tokenIn=${tokenIn}&tokenOut=${tokenOut}&amountIn=${amountIn}`;30 const t0 = Date.now();31 const r = await fetch(url, { headers: { accept: 'application/json', 'x-client-id': process.env.KYBERSWAP_CLIENT_ID ?? 'swap-router' } });32 const text = await r.text();33 let body: unknown; try { body = JSON.parse(text); } catch { body = text; }34 writeFileSync(new URL(`${name}.json`, dir), JSON.stringify({ recordedAt: new Date(t0).toISOString(), url, status: r.status, body }, null, 1) + '\n');35 console.log(name.padEnd(26), r.status, `${Date.now() - t0} ms`, text.slice(0, 150).replace(/\s+/g, ' '));36 await new Promise((res) => setTimeout(res, 1000));37}38