Skip to content

Commit 4b5adc2

Browse files
authored
feat(pyth-sui-js): Migrate from price-service-client to hermes-client
2 parents 1dd2aaa + 4c68fb6 commit 4b5adc2

File tree

7 files changed

+50
-37
lines changed

7 files changed

+50
-37
lines changed

pnpm-lock.yaml

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

target_chains/sui/sdk/js/README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const priceUpdateData = await connection.getPriceFeedsUpdateData(priceIds); // s
6161
// It is either injected from browser or instantiated in backend via some private key
6262
const wallet: SignerWithProvider = getWallet();
6363
// Get the state ids of the Pyth and Wormhole contracts from
64-
// https://docs.pyth.network/documentation/pythnet-price-feeds/sui
64+
// https://docs.pyth.network/price-feeds/contract-addresses/sui
6565
const wormholeStateId = " 0xFILL_ME";
6666
const pythStateId = "0xFILL_ME";
6767

@@ -115,36 +115,36 @@ pnpm turbo run example-relay --filter @pythnetwork/pyth-sui-js -- \
115115
## Off-chain prices
116116

117117
Many applications additionally need to display Pyth prices off-chain, for example, in their frontend application.
118-
The `SuiPriceServiceConnection` provides two different ways to fetch the current Pyth price.
118+
The `SuiPriceServiceConnection` is an extension of the [Hermes client](https://github.com/pyth-network/pyth-crosschain/tree/main/apps/hermes/client/js)
119+
and provides two different ways to fetch the current Pyth price.
119120
The code blocks below assume that the `connection` and `priceIds` objects have been initialized as shown above.
120121
The first method is a single-shot query:
121122

122123
```typescript
123-
// `getLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has
124-
// utility functions to get the current and exponentially-weighted moving average price, and other functionality.
125-
const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
126-
// Get the price if it is not older than 60 seconds from the current time.
127-
console.log(priceFeeds[0].getPriceNoOlderThan(60)); // Price { conf: '1234', expo: -8, price: '12345678' }
128-
// Get the exponentially-weighted moving average price if it is not older than 60 seconds from the current time.
129-
console.log(priceFeeds[1].getEmaPriceNoOlderThan(60));
124+
// `getLatestPriceFeeds` returns a `PriceUpdate`; see the [hermes-client](https://github.com/pyth-network/pyth-crosschain/tree/main/apps/hermes/client/js) documentation for details.
125+
const priceUpdate: PriceUpdate = await connection.getLatestPriceUpdates(priceIds, { parsed: true });
126+
if (priceUpdate.parsed) {
127+
console.log("ParsedPriceUpdate:", priceUpdate.parsed);
128+
}
130129
```
131130

132-
The object also supports a streaming websocket connection that allows you to subscribe to every new price update for a given feed.
131+
The object also supports a streaming Server-Sent Events (SSE) connection that allows you to subscribe to every new price update for a given feed.
133132
This method is useful if you want to show continuously updating real-time prices in your frontend:
134133

135134
```typescript
136-
// Subscribe to the price feeds given by `priceId`. The callback will be invoked every time the requested feed
137-
// gets a price update.
138-
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
139-
console.log(
140-
`Received update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(60)}`,
141-
);
142-
});
143-
144-
// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully.
145-
setTimeout(() => {
146-
connection.closeWebSocket();
147-
}, 60000);
135+
// Streaming price updates
136+
const eventSource = await connection.getPriceUpdatesStream(priceIds, { parsed: true });
137+
eventSource.onmessage = (event) => {
138+
console.log("Received price update:", event.data);
139+
};
140+
eventSource.onerror = (error) => {
141+
console.error("Error receiving updates:", error);
142+
eventSource.close();
143+
};
144+
await sleep(5000);
145+
// To stop listening to the updates, you can call eventSource.close();
146+
console.log("Closing event source.");
147+
eventSource.close();
148148
```
149149

150150
## Hermes endpoints

target_chains/sui/sdk/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-sui-js",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "Pyth Network Sui Utilities",
55
"homepage": "https://pyth.network",
66
"author": {
@@ -55,7 +55,7 @@
5555
},
5656
"dependencies": {
5757
"@mysten/sui": "^1.3.0",
58-
"@pythnetwork/price-service-client": "workspace:*",
58+
"@pythnetwork/hermes-client": "workspace:*",
5959
"buffer": "^6.0.3"
6060
}
6161
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
2-
PriceServiceConnection,
2+
HermesClient,
33
HexString,
4-
} from "@pythnetwork/price-service-client";
4+
PriceUpdate,
5+
} from "@pythnetwork/hermes-client";
56
import { Buffer } from "buffer";
67

7-
export class SuiPriceServiceConnection extends PriceServiceConnection {
8+
export class SuiPriceServiceConnection extends HermesClient {
89
/**
910
* Gets price update data (either batch price attestation VAAs or accumulator messages, depending on the chosen endpoint), which then
1011
* can be submitted to the Pyth contract to update the prices. This will throw an axios error if there is a network problem or
@@ -15,7 +16,12 @@ export class SuiPriceServiceConnection extends PriceServiceConnection {
1516
*/
1617
async getPriceFeedsUpdateData(priceIds: HexString[]): Promise<Buffer[]> {
1718
// Fetch the latest price feed update VAAs from the price service
18-
const latestVaas = await this.getLatestVaas(priceIds);
19-
return latestVaas.map((vaa) => Buffer.from(vaa, "base64"));
19+
const updateData: PriceUpdate = await this.getLatestPriceUpdates(priceIds, {
20+
encoding: "base64",
21+
parsed: false,
22+
});
23+
return updateData.binary.data.map((update) =>
24+
Buffer.from(update, "base64"),
25+
);
2026
}
2127
}

target_chains/sui/sdk/js/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SuiClient } from "@mysten/sui/client";
22
import { SUI_CLOCK_OBJECT_ID } from "@mysten/sui/utils";
33
import { Transaction } from "@mysten/sui/transactions";
44
import { bcs } from "@mysten/sui/bcs";
5-
import { HexString } from "@pythnetwork/price-service-client";
5+
import { HexString } from "@pythnetwork/hermes-client";
66
import { Buffer } from "buffer";
77

88
const MAX_ARGUMENT_SIZE = 16 * 1024;

target_chains/sui/sdk/js/src/examples/SuiRelay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async function run() {
8989
const wallet = Ed25519Keypair.fromSecretKey(
9090
Buffer.from(process.env.SUI_KEY, "hex"),
9191
);
92-
92+
tx.setGasBudget(1000000);
9393
const result = await provider.signAndExecuteTransaction({
9494
signer: wallet,
9595
transaction: tx,

target_chains/sui/sdk/js/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ export { SuiPriceServiceConnection } from "./SuiPriceServiceConnection";
22
export { SuiPythClient } from "./client";
33

44
export {
5+
AssetType,
6+
BinaryPriceUpdate,
57
DurationInMs,
8+
DurationInSeconds,
9+
EncodingType,
10+
HermesClientConfig,
611
HexString,
7-
Price,
8-
PriceFeed,
9-
PriceServiceConnectionConfig,
12+
PriceFeedMetadata,
13+
PriceIdInput,
14+
PriceUpdate,
15+
PublisherCaps,
16+
TwapsResponse,
1017
UnixTimestamp,
11-
} from "@pythnetwork/price-service-client";
18+
} from "@pythnetwork/hermes-client";

0 commit comments

Comments
 (0)