New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@switchboard-xyz/evm.js

Package Overview
Dependencies
Maintainers
5
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@switchboard-xyz/evm.js

API wrapper for intergating with the Switchboardv2 program on our EVM implementation

  • 3.9.17
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-60%
Maintainers
5
Weekly downloads
 
Created
Source

Switchboard Logo

@switchboard-xyz/evm.js

A Typescript client to interact with Switchboard on NEAR.

NPM Badge

Install

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

Usage

Directory

Create a Data Feed

import { AggregatorAccount, OracleJob } from "@switchboard-xyz/evm.js";
import { BigNumber, Wallet } from "ethers";

const authorityAddress = "0x1"; // Add your address here
const queueAddress = "0x83Fb069B10426056Ef8Ca54750cB9bB552a59e7D"; // Permissionless queue address
const switchboardAddress = "0xe9F5Ecb00BC437F061DF59d899F00f260740dC48";

const batchSize = 1; // 1 oracle will resolve a feed
const minUpdateDelaySeconds = 30; // update every 30 seconds
const minOracleResults = 1; // it should only take 1 oracle to resolve the feed
const ipfsJobsAddress = ""; // OracleJobs stored in IPFS
const varianceThreshold = 0; // Always update - but this is stored as a fixed scale factor decimal 18 digits represent the decimal portion of each num
const minJobResults = 1; // It should only take 1 job to resolve a feed
const forceReportPeriod = 0; // Force a result is off

const tx = await switchboard.createAggregator(
  "My BTC Feed",
  authorityAddress,
  oracleRequestBatchSize,
  minUpdateDelaySeconds,
  minOracleResults,
  ipfsJobsAddress,
  queueAddress,

  /// Response Configs (not used on-chain)
  varianceThreshold,
  minJobResults,
  forceReportPeriod,

  false, // Aggregator V3 Inteface is off (makes updates more expensive)
  {
    value: BigNumber.from(
      new Big(params.initialLoadAmount).mul(WEI_PER_ETH.toString()).toString()
    ),
  }
);

// get aggregator address from receipt
const aggregatorAddress = tx.wait().then((logs) => {
  const log = logs.logs[0];
  const sbLog = switchboard.interface.parseLog(log);
  return sbLog.args.accountAddress as string;
});

Create a Job 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>",
});

// get jobs from validation
const content = new File([JSON.stringify(jobs)], "", {
  type: "application/json",
});

// get content ID - fetchable immediately via ipfs
const cid = await client.put([content], {
  wrapWithDirectory: false,
});

FAQs

Package last updated on 17 May 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc