-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathindex.ts
77 lines (69 loc) · 2.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-empty-function */
import { PythLazerClient } from "../src/index.js";
// Ignore debug messages
console.debug = () => { };
const client = await PythLazerClient.create(
["wss://pyth-lazer.dourolabs.app/v1/stream"],
"access_token",
3, // Optionally specify number of parallel redundant connections to reduce the chance of dropped messages. The connections will round-robin across the provided URLs. Default is 3.
console, // Optionally log socket operations (to the console in this case.)
);
// Read and process messages from the Lazer stream
client.addMessageListener((message) => {
console.info("got message:", message);
switch (message.type) {
case "json": {
if (message.value.type == "streamUpdated") {
console.info(
"stream updated for subscription",
message.value.subscriptionId,
":",
message.value.parsed?.priceFeeds,
);
}
break;
}
case "binary": {
if ("solana" in message.value) {
console.info("solana message:", message.value.solana?.toString("hex"));
}
if ("evm" in message.value) {
console.info("evm message:", message.value.evm?.toString("hex"));
}
break;
}
}
});
// Monitor for all connections in the pool being down simultaneously (e.g. if the internet goes down)
// The connections may still try to reconnect in the background. To shut down the client completely, call shutdown().
client.addAllConnectionsDownListener(() => {
console.error("All connections are down!");
});
// Create and remove one or more subscriptions on the fly
await client.subscribe({
type: "subscribe",
subscriptionId: 1,
priceFeedIds: [1, 2],
properties: ["price"],
formats: ["solana"],
deliveryFormat: "binary",
channel: "fixed_rate@200ms",
parsed: false,
jsonBinaryEncoding: "base64",
});
await client.subscribe({
type: "subscribe",
subscriptionId: 2,
priceFeedIds: [1, 2, 3, 4, 5],
properties: ["price", "exponent", "publisherCount", "confidence"],
formats: ["evm"],
deliveryFormat: "json",
channel: "fixed_rate@200ms",
parsed: true,
jsonBinaryEncoding: "hex",
});
await new Promise((resolve) => setTimeout(resolve, 10_000));
await client.unsubscribe(1);
await client.unsubscribe(2);
client.shutdown();