npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pythnetwork/client

v2.21.0

Published

Client for consuming Pyth price data

Downloads

82,764

Readme

@pythnetwork/client

A library for reading 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 reads on-chain Pyth data from @solana/web3.js and returns JavaScript-friendly objects.

Installation

npm

$ npm install --save @pythnetwork/client

Yarn

$ yarn add @pythnetwork/client

Example Usage

This library lets you consume prices in two different ways: you can either get continuously-streaming price updates via a websocket connection, or send one-off requests every time you want the current price.

Streaming updates

The websocket connection provides a subscription model for consuming price updates:

const pythConnection = new PythConnection(solanaWeb3Connection, getPythProgramKeyForCluster(solanaClusterName))
pythConnection.onPriceChange((product, price) => {
  // sample output:
  // Crypto.SRM/USD: $8.68725 ±$0.0131 Status: Trading
  console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence} Status: ${PriceStatus[price.status]}`)
})

// Start listening for price change events.
pythConnection.start()

The onPriceChange callback will be invoked every time a Pyth price gets updated. This callback gets two arguments:

  • price contains the official Pyth price and confidence, along with the component prices that were combined to produce this result.
  • product contains metadata about the price feed, such as the symbol (e.g., "BTC/USD") and the number of decimal points.

See src/example_ws_usage.ts for a runnable example of the above usage. You can run this example with npm run ws_example.

You may also register to specific account updates using connection.onAccountChange in the solana web3 API, then use the methods in index.ts to parse the on-chain data structures into Javascript-friendly objects.

Request an update

The request model allows you to send one-off HTTP requests to get the current price without subscribing to ongoing updates:

const pythClient = new PythHttpClient(connection, pythPublicKey);
const data = await pythClient.getData();

for (let symbol of data.symbols) {
  const price = data.productPrice.get(symbol)!;
  // Sample output:
  // Crypto.SRM/USD: $8.68725 ±$0.0131 Status: Trading
  console.log(`${symbol}: $${price.price} \xB1$${price.confidence} Status: ${PriceStatus[price.status]}`)
}

The getData function will fetch all information about every product listed on Pyth. This includes the current price as well as metadata, such as the base and quote currencies. See src/example_http_usage.ts for a runnable example of the above usage. You can run this example with npm run http_example.

Releases

In order to release a new version of this library and publish it to npm, follow these steps:

  1. Update CHANGELOG.md with a description of the changes in this version.
  2. Run npm version <new version number>. This command will update the version of the package, tag the branch in git, and push your changes to github.
  3. Once your change is merged into main, create a release, and a github action will automatically publish a new version of the package to npm.