Skip to content

Instantly share code, notes, and snippets.

@mgild
Created November 5, 2025 00:03
Show Gist options
  • Save mgild/401868a8783e9d5e4e38b08b82240c25 to your computer and use it in GitHub Desktop.
Save mgild/401868a8783e9d5e4e38b08b82240c25 to your computer and use it in GitHub Desktop.
import { CrossbarClient, OracleJob } from "@switchboard-xyz/common";
/**
* Creates an Oracle Job definition for hyloUSD
* HyloTask converts 1 hyUSD to jitoSOL
* hyUSD is a stablecoin with NAV pegged to $1.00 USD
* @returns OracleJob configured to fetch hyUSD to jitoSOL conversion rate
*/
function getHyloUSDJob(): OracleJob {
const job = OracleJob.fromObject({
tasks: [
{
hyloTask: {
token: OracleJob.HyloTask.Token.HyloUSD
}
},
],
});
return job;
}
/**
* Main function to fetch hyloUSD to jitoSOL conversion rate using Switchboard oracle
*/
(async function main() {
// Initialize Crossbar client (chain-agnostic)
const crossbarClient = new CrossbarClient("https://crossbar.switchboardlabs.xyz");
console.log("\nπŸ’° Fetching hyUSD to jitoSOL conversion rate...\n");
// Build oracle job for hyloUSD
const job = getHyloUSDJob();
// Create an OracleFeed with the job
const feed = {
name: "hyUSD to jitoSOL Conversion Rate",
jobs: [job],
};
try {
const result = await crossbarClient.simulateFeed(
feed,
false, // includeReceipts
);
console.log("βœ… Successfully fetched conversion rate!\n");
// Parse and display results
const conversionRate = result.results?.[0];
if (conversionRate === undefined || conversionRate === null) {
throw new Error(
"Failed to fetch conversion rate from oracle. Response: " +
JSON.stringify(result, null, 2),
);
}
// Convert to number
const conversionRateNum = Number(conversionRate);
console.log("═══════════════════════════════════════");
console.log(" hyUSD to jitoSOL Conversion");
console.log("───────────────────────────────────────");
console.log(` 1 hyUSD = ${conversionRateNum.toFixed(6)} jitoSOL`);
console.log("═══════════════════════════════════════\n");
// Display additional oracle metadata
console.log("πŸ“Š Oracle Response Metadata:");
console.log(` Raw Value: ${conversionRate}`);
console.log(` Timestamp: ${new Date().toISOString()}`);
console.log(` Feed Name: ${feed.name}\n`);
// Display full response for debugging
console.log("πŸ” Full Oracle Response:");
console.log(JSON.stringify(result, null, 2));
console.log();
} catch (error: any) {
console.error("\n❌ Error fetching hyUSD conversion rate:");
console.error(error.message || String(error));
if (error.message?.includes("network")) {
console.error(
"\nπŸ’‘ Tip: Check your network connection and try again.\n",
);
} else if (error.message?.includes("timeout")) {
console.error("\nπŸ’‘ Tip: Request timed out. Try again in a moment.\n");
}
process.exit(1);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment