Before you start
- The TypeScript project, plus
npm install @solana/web3.js bs58. - A Solana wallet with some SOL. Add its base58 private key as
SOLANA_PRIVATE_KEY, and a Solana RPC URL asSOLANA_RPC_URL, to.env.
Script
Save this assrc/swap-across-chains.ts and run npx tsx src/swap-across-chains.ts.
src/swap-across-chains.ts
Notes
fromTokenandtoTokenaccept a symbol such asUSDC, a token address, ornullfor the chain’s native token.- A wallet may add its own compute-budget instructions, but every instruction BRDG built must stay exactly as it is. Any other change is refused as
built_mismatch. - BRDG sets a recent blockhash on the transaction before returning it, so do not replace it. A Solana blockhash is only valid for about a minute, so sign and send promptly. If it expires, get a new quote.