@switchboard-xyz/evm.js
A Typescript client to interact with Switchboard V2 on EVM chains.
Install
npm i --save @switchboard-xyz/evm.js
Usage
Create a Data Feed
import { AggregatorAccount, OracleJob } from "@switchboard-xyz/evm.js";
import { BigNumber, Wallet } from "ethers";
const authorityAddress = "0x1";
const queueAddress = "0x83Fb069B10426056Ef8Ca54750cB9bB552a59e7D";
const switchboardAddress = "0xe9F5Ecb00BC437F061DF59d899F00f260740dC48";
const batchSize = 1;
const minUpdateDelaySeconds = 30;
const minOracleResults = 1;
const ipfsJobsAddress = "";
const varianceThreshold = 0;
const minJobResults = 1;
const forceReportPeriod = 0;
const tx = await switchboard.createAggregator(
"My BTC Feed",
authorityAddress,
oracleRequestBatchSize,
minUpdateDelaySeconds,
minOracleResults,
ipfsJobsAddress,
queueAddress,
varianceThreshold,
minJobResults,
forceReportPeriod,
false,
{
value: BigNumber.from(
new Big(params.initialLoadAmount).mul(WEI_PER_ETH.toString()).toString()
),
}
);
const aggregatorAddress = tx.wait().then((logs) => {
const log = logs.logs[0];
const sbLog = switchboard.interface.parseLog(log);
return sbLog.args.accountAddress as string;
});
Creating Jobs on IPFS
A set of jobs must be encoded with the
following structure.
This example uses web3.storage.
const jobs = [
{
name: job.name,
weight: job.weight,
data: Buffer.from(
sb.OracleJob.encodeDelimited({
tasks: [
{
httpTask: {
url: "https://api.coinbase.com/v2/prices/USDC-USD/spot",
},
},
{
jsonParseTask: {
path: "$.data.amount",
},
},
{
boundTask: {
lowerBoundValue: "0.98",
upperBoundValue: "1.02",
},
},
],
}).finish()
).toString("base64"),
},
];
const client = new Web3Storage({
token: "<TOKEN_GOES_HERE>",
});
const content = new File([JSON.stringify(jobs)], "", {
type: "application/json",
});
const cid = await client.put([content], {
wrapWithDirectory: false,
});