PriceServiceClient.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import { PriceServiceConnection } from "../index";
  4. function sleep(ms: number) {
  5. return new Promise((resolve) => setTimeout(resolve, ms));
  6. }
  7. const argv = yargs(hideBin(process.argv))
  8. .option("endpoint", {
  9. description:
  10. "Endpoint URL for the price service. e.g: https://endpoint/example",
  11. type: "string",
  12. required: true,
  13. })
  14. .option("price-ids", {
  15. description:
  16. "Space separated price feed ids (in hex without leading 0x) to fetch." +
  17. " e.g: f9c0172ba10dfa4d19088d...",
  18. type: "array",
  19. required: true,
  20. })
  21. .help()
  22. .alias("help", "h")
  23. .parserConfiguration({
  24. "parse-numbers": false,
  25. })
  26. .parseSync();
  27. async function run() {
  28. const connection = new PriceServiceConnection(argv.endpoint, {
  29. logger: console, // Providing logger will allow the connection to log it's events.
  30. priceFeedRequestConfig: {
  31. binary: true,
  32. },
  33. });
  34. const priceIds = argv.priceIds as string[];
  35. const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
  36. console.log(priceFeeds);
  37. console.log(priceFeeds?.at(0)?.getPriceNoOlderThan(60));
  38. console.log("Subscribing to price feed updates.");
  39. await connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
  40. console.log(
  41. `Current price for ${priceFeed.id}: ${JSON.stringify(
  42. priceFeed.getPriceNoOlderThan(60)
  43. )}.`
  44. );
  45. console.log(priceFeed.getVAA());
  46. });
  47. await sleep(600000);
  48. // To close the websocket you should either unsubscribe from all
  49. // price feeds or call `connection.stopWebSocket()` directly.
  50. console.log("Unsubscribing from price feed updates.");
  51. await connection.unsubscribePriceFeedUpdates(priceIds);
  52. // connection.closeWebSocket();
  53. }
  54. run();