Coder Social home page Coder Social logo

cruston / pyth-client-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pyth-network/pyth-client-js

0.0 0.0 0.0 749 KB

Javascript API for on-chain pyth account structure

Home Page: https://pyth.network

License: Apache License 2.0

TypeScript 100.00%

pyth-client-js's Introduction

@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.

pyth-client-js's People

Contributors

guibescos avatar evan-gray avatar cctdaniel avatar jayantk avatar jacksturt avatar ali-bahjati avatar cryptopapi997 avatar admdebian avatar beautyfree avatar dr497 avatar jkbpvsc avatar arrowana avatar richardbrooks2000 avatar badass-commits avatar jeff-delonge avatar chalker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.