You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@pythnetwork/client

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pythnetwork/client

Pyth price oracle data structures

1.0.7
Source
npm
Version published
Weekly downloads
27K
-4.25%
Maintainers
2
Weekly downloads
 
Created
Source

@pythnetwork/client

A library for parsing on-chain Pyth oracle data

Pyth is building a way to deliver a decentralized, cross-chain market of verifiable data from high-quality nodes to any smart contract, anywhere.

This library consumes on-chain Pyth accountInfo.data from @solana/web3.js and returns JavaScript-friendly objects.

See our examples repo for real-world usage examples.

Installation

npm

$ npm install --save @pythnetwork/client

Yarn

$ yarn add @pythnetwork/client

Example Usage

import { Connection, PublicKey } from '@solana/web3.js'
import { parseMappingData, parsePriceData, parseProductData } from '@pythnetwork/client'

const connection = new Connection(SOLANA_CLUSTER_URL)
const publicKey = new PublicKey(ORACLE_MAPPING_PUBLIC_KEY)

connection.getAccountInfo(publicKey).then((accountInfo) => {
  const { productAccountKeys } = parseMappingData(accountInfo.data)
  connection.getAccountInfo(productAccountKeys[productAccountKeys.length - 1]).then((accountInfo) => {
    const { product, priceAccountKey } = parseProductData(accountInfo.data)
    connection.getAccountInfo(priceAccountKey).then((accountInfo) => {
      const { price, confidence } = parsePriceData(accountInfo.data)
      console.log(`${product.symbol}: $${price} \xB1$${confidence}`)
      // SRM/USD: $8.68725 ±$0.0131
    })
  })
})

To get streaming price updates, you may want to use connection.onAccountChange

Keywords

pyth

FAQs

Package last updated on 07 Jun 2021

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