Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 492e2c9

Browse files
authored
Add websocket: Add docs + update examples for evm and terra packages (#14)
* Add docs + update examples * Address Jayant comments
1 parent 4b78c8c commit 492e2c9

File tree

11 files changed

+301
-89
lines changed

11 files changed

+301
-89
lines changed

pyth-common-js/src/examples/PriceServiceClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const argv = yargs(hideBin(process.argv))
2222
})
2323
.option("price-ids", {
2424
description:
25-
"Space separated Price Feed Ids (in hex without leading 0x) to fetch." +
25+
"Space separated price feed ids (in hex without leading 0x) to fetch." +
2626
" e.g: f9c0172ba10dfa4d19088d...",
2727
type: "array",
2828
required: true,

pyth-evm-js/README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ providing a way to fetch these prices directly in your code. The following examp
2525
Pyth prices and submit them to the network:
2626

2727
```typescript
28-
const connection = new EvmPriceServiceConnection({
29-
httpEndpoint: "https://prices-testnet.pyth.network", // See Price Service Endpoints section below for other endpoints
30-
});
28+
const connection = new EvmPriceServiceConnection(
29+
"https://prices-testnet.pyth.network"
30+
); // See Price Service endpoints section below for other endpoints
3131

3232
const priceIds = [
3333
// You can find the ids of prices at https://pyth.network/developers/price-feeds#binance-smart-chain-testnet
@@ -41,6 +41,17 @@ const priceFeeds = connection.getPythLatestPriceFeeds(priceIds);
4141
console.log(priceFeeds[0].getCurrentPrice()); // Price { conf: '1234', expo: -8, price: '12345678' }
4242
console.log(priceFeeds[1].getEmaPrice()); // Exponentially-weighted moving average price
4343

44+
// By subscribing to the price feeds you can get their updates in real-time.
45+
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
46+
console.log("Received a new price feed update!");
47+
console.log(priceFeed.getCurrentPrice());
48+
});
49+
50+
// When using subscription, make sure to close the websocket upon termination to finish the process gracefully.
51+
setTimeout(() => {
52+
connection.closeWebSocket();
53+
}, 60000);
54+
4455
// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target
4556
// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should
4657
// call the Pyth Contract with this data.
@@ -83,10 +94,10 @@ There are two examples in [examples](./src/examples/).
8394

8495
#### EvmPriceServiceClient
8596

86-
[This example](./src/examples/EvmPriceServiceClient.ts) fetches a `PriceFeed` for each given price id and prints them. You can run it with `npm run example-client`. A full command that prints BTC and ETH Price Feeds, in the testnet network, looks like so:
97+
[This example](./src/examples/EvmPriceServiceClient.ts) fetches `PriceFeed` updates using both a HTTP-request API and a streaming websocket API. You can run it with `npm run example-client`. A full command that prints BTC and ETH price feeds, in the testnet network, looks like so:
8798

8899
```bash
89-
npm run example-client -- --http https://prices-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6
100+
npm run example-client -- --endpoint https://prices-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6
90101
```
91102

92103
#### EvmRelay
@@ -100,7 +111,7 @@ npm run example-client -- --http https://prices-testnet.pyth.network --price-ids
100111
You can run this example with `npm run example-relay`. A full command that updates BTC and ETH prices on the BNB Chain testnet network looks like so:
101112

102113
```bash
103-
npm run example-relay -- --network bnb_testnet --mnemonic "my good mnemonic" --http https://prices-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6
114+
npm run example-relay -- --network bnb_testnet --mnemonic "my good mnemonic" --endpoint https://prices-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6
104115
```
105116

106117
## How Pyth Works on EVM Chains
@@ -113,7 +124,7 @@ This signed message can then be submitted to the Pyth contract on the EVM networ
113124

114125
Price updates are not submitted on the EVM networks automatically: rather, when a consumer needs to use the value of a price they should first submit the latest Wormhole update for that price to the Pyth contract on the EVM network they are working on. This will make the most recent price update available on-chain for EVM contracts to use.
115126

116-
## Price Service Endpoints
127+
## Price Service endpoints
117128

118129
Public endpoints for the Price Service are provided for both mainnet and testnet. These can be used regardless of which network you deploy your own contracts to as long as it is a Pyth supported network. For example, you can use the testnet Price Service whether you are deploying your contract to the BNB or Polygon testnet.
119130

pyth-evm-js/package-lock.json

Lines changed: 95 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyth-evm-js/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-evm-js",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Pyth Network EVM Utils in JS",
55
"homepage": "https://pyth.network",
66
"author": {
@@ -49,6 +49,6 @@
4949
"yargs": "^17.4.1"
5050
},
5151
"dependencies": {
52-
"@pythnetwork/pyth-common-js": "^0.1.0"
52+
"@pythnetwork/pyth-common-js": "^0.2.0"
5353
}
5454
}

0 commit comments

Comments
 (0)