-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update PythConnection to subscribe to single accounts and add new ws …
…example with SOL/USDC feed (#64)
- Loading branch information
Showing
4 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Connection, PublicKey } from '@solana/web3.js' | ||
import { PythConnection } from './PythConnection' | ||
import { getPythClusterApiUrl, getPythProgramKeyForCluster, PythCluster, PriceStatus } from '.' | ||
|
||
const PYTHNET_CLUSTER_NAME: PythCluster = 'pythnet' | ||
const connection = new Connection(getPythClusterApiUrl(PYTHNET_CLUSTER_NAME)) | ||
const pythPublicKey = getPythProgramKeyForCluster(PYTHNET_CLUSTER_NAME) | ||
// This feed ID comes from this list: https://pyth.network/developers/price-feed-ids#solana-mainnet-beta | ||
// This example shows Crypto.SOL/USD | ||
const feeds = [new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG')] | ||
|
||
const pythConnection = new PythConnection(connection, pythPublicKey, "confirmed", feeds) | ||
pythConnection.onPriceChangeVerbose((productAccount, priceAccount) => { | ||
// The arguments to the callback include solana account information / the update slot if you need it. | ||
const product = productAccount.accountInfo.data.product | ||
const price = priceAccount.accountInfo.data | ||
// sample output: | ||
// SOL/USD: $14.627930000000001 ±$0.01551797 | ||
if (price.price && price.confidence) { | ||
// tslint:disable-next-line:no-console | ||
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`) | ||
} else { | ||
// tslint:disable-next-line:no-console | ||
console.log(`${product.symbol}: price currently unavailable. status is ${PriceStatus[price.status]}`) | ||
} | ||
}) | ||
|
||
// tslint:disable-next-line:no-console | ||
console.log('Reading from Pyth price feed...') | ||
pythConnection.start() |