SPB Git

Repository Public

record-swap-jupiter-fixtures.mts 2.9 KB 39 lines typescript

1// Re-records the Jupiter test fixtures from the LIVE API (read-only GETs, keyless, paced under the 0.5 request/s limit).2// Each file is the provider's answer verbatim: { recordedAt, url, status, body }. Nothing is signed, built or sent.3// Run: npx tsx scripts/record-swap-jupiter-fixtures.mts     (then re-run the tests: amounts in jupiter.test.ts are read from the fixtures)4import { mkdirSync, writeFileSync } from 'node:fs';5import { NETWORKS } from '@spw/core';6import { JUPITER_API_BASE, WSOL_MINT } from '../packages/swap/src/providers/jupiter.ts';78const dir = new URL('../packages/swap/test/fixtures/jupiter/', import.meta.url);9mkdirSync(dir, { recursive: true });10const mint = (symbol: string) => (symbol === 'SOL' ? WSOL_MINT : NETWORKS.solana!.tokens.find((t) => t.symbol === symbol)!.id);11/** the example wallet of Jupiter's own API reference (GET /build, parameter `taker`): a public address, not ours */12const TAKER = 'GkwFnmMDvn3HGMpJpWBg8tgJxr3NxNvg3AXxvXVPbRGJ';13const build = (from: string, to: string, amount: bigint, o: { taker?: string; slippageBps?: number } = {}) =>14  `${JUPITER_API_BASE}/build?${new URLSearchParams({ inputMint: mint(from), outputMint: mint(to), amount: amount.toString(), taker: o.taker ?? TAKER, slippageBps: String(o.slippageBps ?? 50) })}`;1516const cases: [name: string, url: string][] = [17  ['sol-usdc', build('SOL', 'USDC', 300_000_000n)], // 0.3 SOL: wrap + swap18  ['usdc-sol', build('USDC', 'SOL', 25_000_000n)], // 25 USDC: swap + unwrap19  ['sol-pyusd', build('SOL', 'PYUSD', 300_000_000n)], // Token-2022 output20  ['pyusd-sol', build('PYUSD', 'SOL', 25_000_000n, { slippageBps: 100 })], // Token-2022 input, 1 % slippage21  ['usdg-usdc', build('USDG', 'USDC', 25_000_000n)], // Token-2022 input22  ['usdc-usdg', build('USDC', 'USDG', 25_000_000n, { slippageBps: 0 })], // Token-2022 output, zero slippage23  ['usdc-wbtc', build('USDC', 'WBTC', 25_000_000n)], // 8 decimals, multi-hop through mints outside the registry24  ['usdc-bonk', build('USDC', 'BONK', 25_000_000n)], // 5 decimals25  ['wbtc-usdc-high-impact', build('WBTC', 'USDC', 2_500_000_000n)], // 25 WBTC into a thin pool: price impact close to 100 %26  ['no-route-dust', build('BONK', 'USDC', 1n)], // 400 No routes found27  ['no-route-liquidity', build('USDG', 'WBTC', 10n ** 18n)], // 400 Insufficient liquidity28  ['error-invalid-taker', build('SOL', 'USDC', 300_000_000n, { taker: 'notanaddress' })], // 400 Invalid taker29  ['program-id-to-label', `${JUPITER_API_BASE}/program-id-to-label`],30];31for (const [name, url] of cases) {32  const t0 = Date.now();33  const r = await fetch(url, { headers: { accept: 'application/json' } });34  const body: unknown = JSON.parse(await r.text());35  writeFileSync(new URL(`${name}.json`, dir), `${JSON.stringify({ recordedAt: new Date(t0).toISOString(), url, status: r.status, body })}\n`);36  console.log(name.padEnd(24), r.status, `${Date.now() - t0} ms`, `remaining=${r.headers.get('x-ratelimit-remaining')}`);37  await new Promise((res) => setTimeout(res, 2600));38}39
Files main
Menu

Theme