Created
August 26, 2025 13:30
-
-
Save mgild/4f50ea6719978df6989a4028ee75e222 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as sb from "@switchboard-xyz/on-demand"; | |
import { | |
Connection, | |
Transaction, | |
sendAndConfirmTransaction, | |
Keypair, | |
PublicKey, | |
} from "@solana/web3.js"; | |
import { TX_CONFIG } from "./utils"; | |
/** | |
* Test script for PullFeed transaction simulation | |
* | |
* This script replicates the exact transaction flow from the user's frontend code | |
* to help debug transaction simulation errors with PullFeed operations. | |
*/ | |
async function main() { | |
try { | |
console.log("π Loading environment..."); | |
// Load Solana environment (keypair, connection, program) | |
const { keypair, connection, program } = await sb.AnchorUtils.loadEnv(); | |
console.log("β Environment loaded"); | |
console.log("RPC:", connection.rpcEndpoint); | |
console.log("Wallet:", keypair.publicKey.toBase58()); | |
// Load Switchboard program from connection (matching user's code) | |
console.log("π Loading Switchboard program..."); | |
const swbProgram = await sb.AnchorUtils.loadProgramFromConnection(connection); | |
console.log("β Switchboard program loaded:", swbProgram.programId.toBase58()); | |
// Create PullFeed instances (matching user's code) | |
console.log("π Creating PullFeed instances..."); | |
const pullFeedInstances = [ | |
new sb.PullFeed(swbProgram, new PublicKey("HX5WM3qzogAfRCjBUWwnniLByMfFrjm1b5yo4KoWGR27")), | |
]; | |
console.log(`β Created ${pullFeedInstances.length} PullFeed instances`); | |
// Fetch gateway URL from the first feed | |
console.log("π Fetching gateway URL..."); | |
const gateway = await pullFeedInstances[0].fetchGatewayUrl(); | |
console.log("β Gateway URL fetched:", gateway); | |
// Fetch update instructions and lookup tables (matching user's code) | |
console.log("π Fetching update instructions..."); | |
const [pullIx, luts] = await sb.PullFeed.fetchUpdateManyIx(swbProgram, { | |
feeds: pullFeedInstances, | |
gateway, | |
numSignatures: 1, | |
payer: keypair.publicKey, | |
}); | |
console.log("β Instructions fetched"); | |
console.log("Instructions count:", pullIx.length); | |
console.log("Lookup tables count:", luts.length); | |
// Create transaction (matching user's code exactly) | |
console.log("π Creating transaction..."); | |
const transaction = new Transaction(); | |
pullIx.forEach((ix) => transaction.add(ix)); | |
// Get latest blockhash (matching user's code) | |
console.log("π Getting latest blockhash..."); | |
const { blockhash } = await connection.getLatestBlockhash(); | |
transaction.recentBlockhash = blockhash; | |
transaction.feePayer = keypair.publicKey; | |
console.log("β Transaction prepared"); | |
// Simulate transaction before sending | |
console.log("π Simulating transaction..."); | |
try { | |
const simulation = await connection.simulateTransaction(transaction); | |
if (simulation.value.err) { | |
console.error("β Simulation failed:", simulation.value.err); | |
console.error("Simulation logs:", simulation.value.logs); | |
return; | |
} | |
console.log("β Simulation successful"); | |
console.log("Compute units used:", simulation.value.unitsConsumed); | |
if (simulation.value.logs) { | |
console.log("Simulation logs:", simulation.value.logs.slice(-5)); // Show last 5 logs | |
} | |
} catch (simError) { | |
console.error("β Simulation error:", simError); | |
return; | |
} | |
// Sign and send transaction (matching user's code) | |
// console.log("π Sending transaction..."); | |
// try { | |
// const signature = await sendAndConfirmTransaction( | |
// connection, | |
// transaction, | |
// [keypair], | |
// { | |
// commitment: "confirmed", | |
// maxRetries: 3, | |
// } | |
// ); | |
// | |
// console.log("β Transaction successful!"); | |
// console.log("Signature:", signature); | |
// console.log("Explorer:", `https://explorer.solana.com/tx/${signature}?cluster=devnet`); | |
// | |
// } catch (error) { | |
// console.error("β Error sending transaction:", error); | |
// if (error instanceof Error) { | |
// console.error("Error message:", error.message); | |
// | |
// // Enhanced error information | |
// if (error.message.includes("0x1")) { | |
// console.error("π‘ This might be an account not found error. Check oracle addresses."); | |
// } | |
// if (error.message.includes("0x0")) { | |
// console.error("π‘ This might be an insufficient funds error. Check wallet balance."); | |
// } | |
// } | |
// } | |
} catch (error) { | |
console.error("π₯ Script error:", error); | |
if (error instanceof Error) { | |
console.error("Error message:", error.message); | |
console.error("Stack:", error.stack); | |
} | |
} | |
} | |
// Handle process termination gracefully | |
process.on('SIGINT', () => { | |
console.log('\nπ Script terminated by user'); | |
process.exit(0); | |
}); | |
process.on('unhandledRejection', (reason, promise) => { | |
console.error('π¨ Unhandled Rejection at:', promise, 'reason:', reason); | |
process.exit(1); | |
}); | |
// Run the script | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment