Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@pythnetwork/client

pyth-network74kApache-2.02.22.0TypeScript support: included

Client for consuming Pyth price data

pyth, solana, oracle

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.

changelog

Changelog

2.20.0

  • Add messageSent field to PriceData
  • Add maxLatency field to PriceData

2.19.0

  • Upgrade @coral-xyz/anchor to ^0.28.1-beta.1
  • Upgrade @coral-xyz/borsh to ^0.28.0

2.18.0

  • Optimize Solana RPC calls

2.17.0

  • Rename initPrice to setExponent for clarity

2.16.0

  • Remove pythtest and add pythtest-crosschain and pythtest-conformance. There are now two oracle instances.

2.15.1

  • Fix import of @coral-xyz/borsh

2.13.1

  • Export PythCluster and getPythClusterApiUrl globally

2.13.0

  • Add method to fetch individual prices specified by user

2.12.0

  • Add PriceStatus.Ignored

2.11.0

  • Add @coral-xyz/anchor to deps
  • ProductData.priceAccountKeywasPublicKeybecomesPublicKey | null` if the product account doesn't yet have a price account
  • Anchor client now support updProduct

2.10.0

  • Add localnet to PythCluster
  • Add Permission to AccountType
  • Fix error in idl and make it support permissionsAccount

2.9.0

  • Add Anchor client

    2.8.0

  • Added onPriceChangeVerbose callback to PythConnection to support getting account keys and slots on each price update.

2.7.2

Changed

  • Added pythtest-conformance program key and cluster url
  • Updated examples to work with pythtest-conformance

2.7.1

Moved solana/web3 to peerDependencies

2.7.0

Added timestamp fields (overwriting existing reserved space)

2.6.3

Changed

  • Renamed websocket usage example to example_ws_usage.ts
  • Updated documentation to describe usage for PythHttpClient

Added

  • Example code for using the http client in example_http_usage.ts

2.6.1

Changed

  • Changed TWAP to EMAP and TWAC to EMAC

2.6.0

Added

  • Checks current slot when parsing a Price account and set status to unknown if price is stale. Being stale means it is not updated in MAX_SLOT_DIFFERENCE slots (currently 25).
  • Adds status field in PriceData to access the current status (considering price getting stale) easier.

Changed

  • Converts Some type/status structs to enums to be able to use them in a cleaner way. It's backward compatible.

2.5.3

Fixed

Updated tests for Pyth symbology change

2.5.1

Changed

Improved error message when passing an invalid cluster name to pythProgramKeyForCluster

2.5.0

Changed

Restructure drv2 field in PriceData to minPublishers and other future drv values

2.4.0

Changed

Product only define price and confidence fields if it currently has a valid price

Fixed

Memory leak in an underlying library

2.3.2

Added

PythConnection

2.2.0

Added

numQuoters

2.1.0

Added

Support new twap and twac

2.0.1

Added

Types

2.0.0

Added

Support for v2 account structure

Removed

Support for v1 account structure

1.0.7

Changed

allow nextPriceAccountKey to be null

1.0.6

Changed

Updated README.md

1.0.5

Fixed

Respect size when parsing product metadata

1.0.4

Fixed

Gatsby support

1.0.3

Fixed

Do not rely on Buffer's readBig(U)Int64LE

1.0.2

Changed

Repository url

1.0.1

Fixed

Parse price as BigInt64

1.0.0

Initial release