@pythnetwork/client
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -0,3 +1,5 @@ | ||
/// <reference types="node" /> | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { Buffer } from 'buffer'; | ||
/** Constants. This section must be kept in sync with the on-chain program. */ | ||
export declare const Magic = 2712847316; | ||
@@ -10,2 +12,5 @@ export declare const Version2 = 2; | ||
export declare const DeriveType: string[]; | ||
export declare const AccountType: string[]; | ||
/** Number of slots that can pass before a publisher's price is no longer included in the aggregate. */ | ||
export declare const MAX_SLOT_DIFFERENCE = 25; | ||
export interface Base { | ||
@@ -76,4 +81,6 @@ magic: number; | ||
} | ||
/** Parse data as a generic Pyth account. Use this method if you don't know the account type. */ | ||
export declare function parseBaseData(data: Buffer): Base | undefined; | ||
export declare const parseMappingData: (data: Buffer) => MappingData; | ||
export declare const parseProductData: (data: Buffer) => ProductData; | ||
export declare const parsePriceData: (data: Buffer) => PriceData; |
@@ -14,6 +14,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parsePriceData = exports.parseProductData = exports.parseMappingData = exports.DeriveType = exports.PriceType = exports.CorpAction = exports.PriceStatus = exports.Version = exports.Version2 = exports.Magic = void 0; | ||
exports.parsePriceData = exports.parseProductData = exports.parseMappingData = exports.parseBaseData = exports.MAX_SLOT_DIFFERENCE = exports.AccountType = exports.DeriveType = exports.PriceType = exports.CorpAction = exports.PriceStatus = exports.Version = exports.Version2 = exports.Magic = void 0; | ||
var web3_js_1 = require("@solana/web3.js"); | ||
var buffer_1 = require("buffer"); | ||
var readBig_1 = require("./readBig"); | ||
/** Constants. This section must be kept in sync with the on-chain program. */ | ||
exports.Magic = 0xa1b2c3d4; | ||
@@ -26,4 +27,28 @@ exports.Version2 = 2; | ||
exports.DeriveType = ['Unknown', 'TWAP', 'Volatility']; | ||
exports.AccountType = ['Unknown', 'Mapping', 'Product', 'Price', 'Test']; | ||
/** Number of slots that can pass before a publisher's price is no longer included in the aggregate. */ | ||
exports.MAX_SLOT_DIFFERENCE = 25; | ||
var empty32Buffer = buffer_1.Buffer.alloc(32); | ||
var PKorNull = function (data) { return (data.equals(empty32Buffer) ? null : new web3_js_1.PublicKey(data)); }; | ||
/** Parse data as a generic Pyth account. Use this method if you don't know the account type. */ | ||
function parseBaseData(data) { | ||
// data is too short to have the magic number. | ||
if (data.byteLength < 4) { | ||
return undefined; | ||
} | ||
var magic = data.readUInt32LE(0); | ||
if (magic === exports.Magic) { | ||
// program version | ||
var version = data.readUInt32LE(4); | ||
// account type | ||
var type = data.readUInt32LE(8); | ||
// account used size | ||
var size = data.readUInt32LE(12); | ||
return { magic: magic, version: version, type: type, size: size }; | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
exports.parseBaseData = parseBaseData; | ||
var parseMappingData = function (data) { | ||
@@ -30,0 +55,0 @@ // pyth magic number |
{ | ||
"name": "@pythnetwork/client", | ||
"version": "2.2.0", | ||
"description": "Pyth price oracle data structures", | ||
"version": "2.3.0", | ||
"description": "Client for consuming Pyth price data", | ||
"homepage": "https://pyth.network", | ||
@@ -21,3 +21,4 @@ "main": "lib/index.js", | ||
"version": "npm run format && git add -A src", | ||
"postversion": "git push && git push --tags" | ||
"postversion": "git push && git push --tags", | ||
"example": "npm run build && node lib/example_usage.js" | ||
}, | ||
@@ -24,0 +25,0 @@ "keywords": [ |
# @pythnetwork/client | ||
## A library for parsing on-chain Pyth oracle data | ||
## A library for reading on-chain Pyth oracle data | ||
[Pyth](https://pyth.network/) 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](https://www.npmjs.com/package/@solana/web3.js) and returns JavaScript-friendly objects. | ||
This library reads on-chain Pyth data from [@solana/web3.js](https://www.npmjs.com/package/@solana/web3.js) and returns JavaScript-friendly objects. | ||
@@ -27,22 +27,25 @@ See our [examples repo](https://github.com/pyth-network/pyth-examples) for real-world usage examples. | ||
This library provides a subscription model for consuming price updates: | ||
```javascript | ||
import { Connection, PublicKey } from '@solana/web3.js' | ||
import { parseMappingData, parsePriceData, parseProductData } from '@pythnetwork/client' | ||
const pythConnection = new PythConnection(solanaWeb3Connection, getPythProgramKeyForCluster(solanaClusterName)) | ||
pythConnection.onPriceChange((product, price) => { | ||
// sample output: | ||
// SRM/USD: $8.68725 ±$0.0131 | ||
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`) | ||
}) | ||
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 | ||
}) | ||
}) | ||
}) | ||
// Start listening for price change events. | ||
pythConnection.start() | ||
``` | ||
To get streaming price updates, you may want to use `connection.onAccountChange` | ||
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_usage.ts` for a runnable example of the above usage. | ||
You can run this example with `npm run 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. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
41289
13
597
50
1