Repository Public
1// Recorded Jupiter /build answers (v1 transactions) with the message, intent and mainnet simulation that went with them2// (scripts/sim-jupiter.mts --record), and the means to re-address one to another wallet for a test.3import { readFileSync } from 'node:fs';4import { associatedTokenAddress, compileSolV1Message, decodeSolV1Message, DEFAULT_POLICY, NETWORKS, solMessageHash, TOKEN_PROGRAM, tokenProgramOf, WSOL_MINT, type Intent, type SolIxInput, type SolV1Message } from '@spw/core';56export interface JupFixture { wallet: string; from: string; to: string; converted: boolean; intent: Intent; message: string; build: any;7 simulation: { fee: number; unitsConsumed: number; preBalances: number[]; postBalances: number[]; preTokenBalances: any[]; postTokenBalances: any[] }; effects: { spent: string; received: string; rentKept: string } }8export const jupFixture = (name: string): JupFixture => JSON.parse(readFileSync(new URL(`../../packages/core/test/fixtures/jupiter-exec/${name}.json`, import.meta.url), 'utf8'));9const N = NETWORKS.solana!;10export const solToken = (sym: string) => (sym === 'SOL' ? N.native : N.tokens.find((t) => t.symbol === sym)!);11export const solMint = (sym: string) => (sym === 'SOL' ? WSOL_MINT : solToken(sym).id);12export const solTokenProgram = (sym: string) => (sym === 'SOL' ? TOKEN_PROGRAM : tokenProgramOf(solToken(sym)));13export const solAta = (owner: string, sym: string) => associatedTokenAddress(owner, solMint(sym), solTokenProgram(sym));14export const ixInputs = (m: SolV1Message): SolIxInput[] => m.instructions.map((x) => ({ program: x.program, data: Uint8Array.from(x.data), accounts: x.accounts.map((k) => ({ pubkey: k, signer: k === m.payer, writable: m.keys.indexOf(k) < m.keys.length - m.numReadonlyUnsigned })) }));1516/** every address of the recorded wallet (itself and its token accounts of all registry assets) → the same for `me` */17export function addressMap(from: string, me: string): Record<string, string> {18 const map: Record<string, string> = { [from]: me };19 for (const t of [N.native, ...N.tokens]) map[solAta(from, t.symbol)] = solAta(me, t.symbol);20 return map;21}22/** the recorded /build answer as Jupiter would have given it to `me` */23export function buildFor(f: JupFixture, me: string): any {24 let s = JSON.stringify(f.build);25 for (const [a, b] of Object.entries(addressMap(f.wallet, me))) s = s.split(a).join(b);26 return JSON.parse(s);27}28/** the recorded message and intent, re-addressed to `me` (same instructions, same order, same account positions). The recording29 * carries the policy version in force on the day it was made; the re-addressed intent is one for the signer of TODAY, so it30 * names the default policy's current version (an intent for another version is refused: `policy version changed`). */31export function readdress(f: JupFixture, me: string, keyId: string, o: Partial<Intent> = {}): { intent: Intent; message: string; msg: Uint8Array } {32 const m = decodeSolV1Message(new Uint8Array(Buffer.from(f.message, 'base64'))), map = addressMap(f.wallet, me);33 const msg = compileSolV1Message({ payer: me, blockhash: m.blockhash, computeUnitLimit: m.computeUnitLimit, priorityFeeLamports: m.priorityFeeLamports, loadedAccountsDataSizeLimit: m.loadedAccountsDataSizeLimit,34 instructions: ixInputs(m).map((x) => ({ ...x, accounts: x.accounts.map((a) => ({ ...a, pubkey: map[a.pubkey] ?? a.pubkey })) })) });35 const intent: Intent = { ...f.intent, keyId, from: me, to: me, policyVersion: DEFAULT_POLICY.version, ...o, swap: { ...f.intent.swap!, dataHash: solMessageHash(msg) } };36 return { intent, message: Buffer.from(msg).toString('base64'), msg };37}38