index.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* eslint-disable no-console */
  2. /* eslint-disable @typescript-eslint/no-empty-function */
  3. import { PythLazerClient } from "../src/index.js";
  4. // Ignore debug messages
  5. console.debug = () => {};
  6. const client = new PythLazerClient(
  7. ["wss://pyth-lazer.dourolabs.app/v1/stream"],
  8. "access_token",
  9. 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.
  10. console // Optionally log socket operations (to the console in this case.)
  11. );
  12. client.addMessageListener((message) => {
  13. console.info("got message:", message);
  14. switch (message.type) {
  15. case "json": {
  16. if (message.value.type == "streamUpdated") {
  17. console.info(
  18. "stream updated for subscription",
  19. message.value.subscriptionId,
  20. ":",
  21. message.value.parsed?.priceFeeds
  22. );
  23. }
  24. break;
  25. }
  26. case "binary": {
  27. if ("solana" in message.value) {
  28. console.info("solana message:", message.value.solana?.toString("hex"));
  29. }
  30. if ("evm" in message.value) {
  31. console.info("evm message:", message.value.evm?.toString("hex"));
  32. }
  33. break;
  34. }
  35. }
  36. });
  37. // Create and remove one or more subscriptions on the fly
  38. await client.subscribe({
  39. type: "subscribe",
  40. subscriptionId: 1,
  41. priceFeedIds: [1, 2],
  42. properties: ["price"],
  43. chains: ["solana"],
  44. deliveryFormat: "binary",
  45. channel: "fixed_rate@200ms",
  46. parsed: false,
  47. jsonBinaryEncoding: "base64",
  48. });
  49. await client.subscribe({
  50. type: "subscribe",
  51. subscriptionId: 2,
  52. priceFeedIds: [1, 2, 3, 4, 5],
  53. properties: ["price"],
  54. chains: ["evm"],
  55. deliveryFormat: "json",
  56. channel: "fixed_rate@200ms",
  57. parsed: true,
  58. jsonBinaryEncoding: "hex",
  59. });
  60. await new Promise((resolve) => setTimeout(resolve, 10_000));
  61. await client.unsubscribe(1);
  62. await client.unsubscribe(2);
  63. client.shutdown();