sbv2-solana/javascript/solana.js
Conner Gallagher 33ce0a9ebd chore: lockfile 2023-08-11 10:01:52 -06:00
..
idl chore: pull monorepo changes (#111) 2023-07-19 09:23:43 -06:00
scripts chore: pull monorepo changes (#116) 2023-08-11 09:53:19 -06:00
src chore: pull monorepo changes (#116) 2023-08-11 09:53:19 -06:00
test chore: pull monorepo changes (#111) 2023-07-19 09:23:43 -06:00
.editorconfig added solana.js 2022-11-28 16:50:18 -07:00
.gitignore Add V3 SGX Program to solana.js (#102) 2023-06-12 12:19:25 -04:00
.mocharc.json V3 Program SDK Updates (#104) 2023-06-28 12:07:33 -06:00
README.md chore: pull monorepo changes (#111) 2023-07-19 09:23:43 -06:00
esbuild.js chore: pull monorepo changes (#116) 2023-08-11 09:53:19 -06:00
package.json chore: pull monorepo changes (#116) 2023-08-11 09:53:19 -06:00
tsconfig.base.json chore: lockfile 2023-08-11 10:01:52 -06:00
tsconfig.cjs.json solana.js now uses ESM with import resolutions 2023-06-01 11:58:42 -06:00
tsconfig.json chore: lockfile 2023-08-11 10:01:52 -06:00

README.md

Switchboard Logo

@switchboard-xyz/solana.js

A Typescript client to interact with Switchboard V2 on Solana.

Test Status Anchor Test Status

NPM Badge Types Badge

Install

npm i --save @switchboard-xyz/solana.js

Usage

Directory

Load Switchboard Program

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 */
);

Create a Queue

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();

Add an Oracle

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();

Create a Data Feed

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();

Request a New Value

import { AggregatorAccount } from "@switchboard-xyz/solana.js";

const aggregatorAccount = new AggregatorAccount(program, aggregatorPubkey);

await aggregatorAccount.openRound();

Read a Data Feed

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());

Add a History Buffer

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();

Watch Data Feed

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());
  }
});