Skip to content

Instantly share code, notes, and snippets.

@mgild
Created August 26, 2025 13:30
Show Gist options
  • Save mgild/4f50ea6719978df6989a4028ee75e222 to your computer and use it in GitHub Desktop.
Save mgild/4f50ea6719978df6989a4028ee75e222 to your computer and use it in GitHub Desktop.
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