
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
@switchboard-xyz/solana.js
Advanced tools
npm i --save @switchboard-xyz/solana.js
Directory
import { Connection } from "@solana/web3.js";
import {
SwitchboardProgram,
TransactionObject,
} from "@switchboard-xyz/solana.js";
const program = await SwitchboardProgram.load(
"mainnet-beta",
new Connection("https://api.mainnet-beta.solana.com"),
payerKeypair /** Optional, READ-ONLY if not provided */
);
import { QueueAccount } from "@switchboard-xyz/solana.js";
const [queueAccount, txnSignature] = await QueueAccount.create(program, {
name: "My Queue",
metadata: "Top Secret",
queueSize: 100,
reward: 0.00001337,
minStake: 10,
oracleTimeout: 60,
slashingEnabled: false,
unpermissionedFeeds: true,
unpermissionedVrf: true,
enableBufferRelayers: false,
});
const queue = await queueAccount.loadData();
import { QueueAccount } from "@switchboard-xyz/solana.js";
const queueAccount = new QueueAccount(program, queuePubkey);
const [oracleAccount, oracleInitSignature] = await queueAccount.createOracle({
name: "My Oracle",
metadata: "Oracle #1",
stakeAmount: 10,
});
const oracle = await oracleAccount.loadData();
await oracleAccount.heartbeat();
import { QueueAccount } from "@switchboard-xyz/solana.js";
import { OracleJob } from "@switchboard-xyz/common";
const queueAccount = new QueueAccount(program, queuePubkey);
const [aggregatorAccount, aggregatorInitSignatures] =
await queueAccount.createFeed({
batchSize: 1,
minRequiredOracleResults: 1,
minRequiredJobResults: 1,
minUpdateDelaySeconds: 60,
fundAmount: 2.5, // deposit 2.5 wSOL into the leaseAccount escrow
jobs: [
{ pubkey: jobAccount.publicKey },
{
weight: 2,
data: OracleJob.encodeDelimited(
OracleJob.fromObject({
tasks: [
{
valueTask: {
value: 1,
},
},
],
})
).finish(),
},
],
});
const aggregator = await aggregatorAccount.loadData();
import { AggregatorAccount } from "@switchboard-xyz/solana.js";
const aggregatorAccount = new AggregatorAccount(program, aggregatorPubkey);
await aggregatorAccount.openRound();
After the oracles respond, read the feed result
import Big from "big.js";
import { AggregatorAccount } from "@switchboard-xyz/solana.js";
const aggregatorAccount = new AggregatorAccount(program, aggregatorPubkey);
const result: Big | null = await aggregatorAccount.fetchLatestValue();
if (result === null) {
throw new Error("Aggregator holds no value");
}
console.log(result.toString());
Optionally, add a history buffer to your feed to store the last N historical samples
import {
AggregatorAccount,
AggregatorHistoryBuffer,
} from "@switchboard-xyz/solana.js";
const aggregatorAccount = new AggregatorAccount(program, aggregatorPubkey);
const aggregator = await aggregatorAccount.loadData();
const [historyBuffer, addHistorySignature] =
await AggregatorHistoryBuffer.create(program, {
aggregatorAccount,
maxSamples: 10000,
});
const history = await historyBuffer.loadData();
Setup a websocket listener to invoke a callback whenever an aggregator is updated
import Big from "big.js";
import { AggregatorAccount } from "@switchboard-xyz/solana.js";
const aggregatorAccount = new AggregatorAccount(program, aggregatorPubkey);
const ws = aggregatorAccount.onChange((aggregator) => {
const result = AggregatorAccount.decodeLatestValue(aggregator);
if (result !== null) {
console.log(result.toString());
}
});
FAQs
A Typescript client to interact with Switchboard on Solana.
We found that @switchboard-xyz/solana.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.