Skip to content

Commit 1547bcb

Browse files
authored
Merge pull request #3 from pyth-network/js-dev
initial js lib
2 parents 8f56b41 + d3c90bf commit 1547bcb

13 files changed

+16101
-0
lines changed

js/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
/lib

js/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"printWidth": 120,
3+
"trailingComma": "all",
4+
"semi": false,
5+
"singleQuote": true
6+
}

js/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pyth-client
2+
3+
## A library for parsing on-chain Pyth oracle data
4+
5+
[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.
6+
7+
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.
8+
9+
See our [examples repo](https://github.com/pyth-network/pyth-examples) for real-world usage examples.
10+
11+
## Example Usage
12+
13+
```
14+
import { Connection, PublicKey } from '@solana/web3.js'
15+
import { parseMappingData, parsePriceData, parseProductData } from '@pythnetwork/pyth-client'
16+
const connection = new Connection(SOLANA_CLUSTER_URL)
17+
const publicKey = new PublicKey(ORACLE_MAPPING_PUBLIC_KEY)
18+
connection.getAccountInfo(publicKey).then((accountInfo) => {
19+
const { productAccountKeys } = parseMappingData(accountInfo.data)
20+
connection.getAccountInfo(productAccountKeys[productAccountKeys.length - 1]).then((accountInfo) => {
21+
const { product, priceAccountKey } = parseProductData(accountInfo.data)
22+
connection.getAccountInfo(priceAccountKey).then((accountInfo) => {
23+
const { price, confidence } = parsePriceData(accountInfo.data)
24+
console.log(`${product.symbol}: $${price} \xB1$${confidence}`)
25+
// SRM/USD: $8.68725 ±$0.0131
26+
})
27+
})
28+
})
29+
```
30+
31+
To get streaming price updates, you may want to use `connection.onAccountChange`

js/jestconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"transform": {
3+
"^.+\\.(t|j)sx?$": "ts-jest"
4+
},
5+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
7+
}

0 commit comments

Comments
 (0)