Skip to content

Commit db01db1

Browse files
feat(lazer/js/sdk): add REST endpoints and examples (#3075)
* feat(lazer-sdk): add History service support with get_symbols() function - Add LazerClientConfig type wrapping WebSocketPoolConfig with historyServiceUrl - Implement get_symbols() method to query /v1/symbols endpoint - Add TypeScript interfaces for SymbolResponse and query parameters - Update example to demonstrate BTC symbol search and subscription - Add cross-fetch dependency for Node.js compatibility - Bump version to 2.1.0 following semver for new feature Co-Authored-By: Tejas Badadare <[email protected]> * move history endpoint to constants * feat(lazer-sdk): add Router service support with latest_price and price endpoints - Add TypeScript interfaces for LatestPriceRequest, PriceRequest, and JsonUpdate - Update LazerClientConfig to include routerServiceUrl with default - Implement get_latest_price() and get_price() HTTP methods in PythLazerClient - Create router-example.ts demonstrating all Router endpoint use cases - Bump version to 2.2.0 to reflect new Router service functionality - Add router-example script to package.json for easy testing Co-Authored-By: Tejas Badadare <[email protected]> * feat(lazer-js-sdk): update names, add examples * make wsp.urls optional (use default) * make wsp.urls optional (use default) * format * lint, format * update lazer sui js sdk to use updated js sdk * display symbols in streaming example * lint * add comment --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent e204c9f commit db01db1

File tree

14 files changed

+898
-257
lines changed

14 files changed

+898
-257
lines changed

Cargo.lock

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

lazer/contracts/sui/sdk/js/examples/fetch-and-verify-update.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
11
import { SuiClient } from "@mysten/sui/client";
22
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
33
import { Transaction } from "@mysten/sui/transactions";
4-
import type { Request as SubscriptionRequest } from "@pythnetwork/pyth-lazer-sdk";
54
import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";
65
import yargs from "yargs";
76
import { hideBin } from "yargs/helpers";
87

98
import { addParseAndVerifyLeEcdsaUpdateCall } from "../src/client.js";
109

11-
async function getOneLeEcdsaUpdate(urls: string[], token: string) {
10+
async function getOneLeEcdsaUpdate(token: string) {
1211
const lazer = await PythLazerClient.create({
13-
urls,
1412
token,
15-
numConnections: 1,
1613
});
1714

18-
const subscription: SubscriptionRequest = {
19-
subscriptionId: 1,
20-
type: "subscribe",
15+
const latestPrice = await lazer.get_latest_price({
2116
priceFeedIds: [1],
2217
properties: ["price", "bestBidPrice", "bestAskPrice", "exponent"],
2318
formats: ["leEcdsa"],
2419
channel: "fixed_rate@200ms",
25-
deliveryFormat: "binary",
2620
jsonBinaryEncoding: "hex",
27-
};
28-
29-
lazer.subscribe(subscription);
30-
31-
return new Promise<Uint8Array>((resolve) => {
32-
lazer.addMessageListener((event) => {
33-
if (event.type === "binary" && event.value.leEcdsa) {
34-
const buf = event.value.leEcdsa;
35-
36-
// For the purposes of this example, we only need one update.
37-
lazer.shutdown();
38-
resolve(buf);
39-
}
40-
});
4121
});
22+
23+
return latestPrice;
4224
}
4325

4426
async function main() {
@@ -87,10 +69,7 @@ async function main() {
8769
const provider = new SuiClient({ url: args.fullnodeUrl });
8870

8971
// Fetch the price update
90-
const updateBytes = await getOneLeEcdsaUpdate(
91-
args.lazerUrls,
92-
args.lazerToken,
93-
);
72+
const update = await getOneLeEcdsaUpdate(args.lazerToken);
9473

9574
// Build the Sui transaction
9675
const tx = new Transaction();
@@ -100,7 +79,7 @@ async function main() {
10079
tx,
10180
packageId: args.packageId,
10281
stateObjectId: args.stateObjectId,
103-
updateBytes,
82+
updateBytes: Buffer.from(update.leEcdsa?.data ?? "", "hex"),
10483
});
10584

10685
// --- You can add more calls to the transaction that consume the parsed update here ---

lazer/contracts/sui/sdk/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-lazer-sui-js",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "TypeScript SDK for the Pyth Lazer Sui contract",
55
"license": "Apache-2.0",
66
"type": "module",

lazer/sdk/js/examples/history.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable no-console */
2+
3+
import { displayParsedPrices } from "./util.js";
4+
import { PythLazerClient } from "../src/index.js";
5+
6+
const client = await PythLazerClient.create({
7+
token: "your-token-here",
8+
logger: console,
9+
});
10+
11+
// Example 1: Get latest price for BTC using feed IDs
12+
console.log("\n=== Example 1: Latest BTC price (requested with feed ID) ===");
13+
const response1 = await client.get_latest_price({
14+
priceFeedIds: [1],
15+
properties: ["price", "confidence", "exponent"],
16+
formats: [],
17+
jsonBinaryEncoding: "hex",
18+
parsed: true,
19+
channel: "fixed_rate@200ms",
20+
});
21+
displayParsedPrices(response1);
22+
23+
// Example 2: Get latest price using symbols
24+
console.log("\n=== Example 2: Latest ETH price (requested with symbols) ===");
25+
const response2 = await client.get_latest_price({
26+
priceFeedIds: [2],
27+
properties: ["price", "confidence", "exponent"],
28+
formats: [],
29+
parsed: true,
30+
channel: "real_time",
31+
});
32+
displayParsedPrices(response2);
33+
34+
// Example 3: Get historical price at specific timestamp
35+
console.log("\n=== Example 3: Historical BTC price at timestamp ===");
36+
const timestamp = 1_754_348_458_565_000;
37+
console.log(
38+
`Requesting price from timestamp: ${timestamp.toString()} (${new Date(timestamp / 1000).toISOString()})`,
39+
);
40+
const response3 = await client.get_price({
41+
timestamp: timestamp,
42+
priceFeedIds: [1],
43+
properties: ["price", "confidence", "exponent"],
44+
formats: [],
45+
parsed: true,
46+
channel: "real_time",
47+
});
48+
displayParsedPrices(response3);

lazer/sdk/js/examples/index.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)