pyth_publisher.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. from pyth_utils import *
  3. import random
  4. import sys
  5. import threading
  6. import time
  7. # Accept connections from readiness probe
  8. def publisher_readiness():
  9. run_or_die(["nc", "-k", "-l", "-p", READINESS_PORT])
  10. # Update the specified price with random values
  11. def publisher_random_update(price_pubkey):
  12. value = random.randrange(1024)
  13. confidence = 1
  14. pyth_run_or_die("upd_price_val", args=[price_pubkey, str(value), str(confidence), "trading"])
  15. print("Price updated!")
  16. # Fund the publisher
  17. sol_run_or_die("airdrop", [str(SOL_AIRDROP_AMT),
  18. "--keypair", PYTH_PUBLISHER_KEYPAIR,
  19. "--commitment", "finalized",
  20. ])
  21. # Create a mapping
  22. pyth_run_or_die("init_mapping")
  23. # Add a product
  24. prod_pubkey = pyth_run_or_die("add_product", capture_output=True).stdout.strip()
  25. print(f"Added product {prod_pubkey}")
  26. # Add a price
  27. price_pubkey = pyth_run_or_die(
  28. "add_price",
  29. args=[prod_pubkey, "price"],
  30. confirm=False,
  31. capture_output=True
  32. ).stdout.strip()
  33. print(f"Added price {price_pubkey}")
  34. publisher_pubkey = sol_run_or_die("address", args=["--keypair", PYTH_PUBLISHER_KEYPAIR], capture_output=True).stdout.strip()
  35. # Become a publisher
  36. pyth_run_or_die("add_publisher", args=[publisher_pubkey, price_pubkey], confirm=False, debug=True, capture_output=True)
  37. print(f"Added publisher {publisher_pubkey}")
  38. # Update the price as the newly added publisher
  39. publisher_random_update(price_pubkey)
  40. print(f"Updated price {price_pubkey}. Mock updates ready to roll. Updating every {str(PYTH_PUBLISHER_INTERVAL)} seconds")
  41. # Spin off the readiness probe endpoint into a separate thread
  42. readiness_thread = threading.Thread(target=publisher_readiness)
  43. readiness_thread.start()
  44. while True:
  45. print(f"Updating price {price_pubkey}")
  46. publisher_random_update(price_pubkey)
  47. time.sleep(PYTH_PUBLISHER_INTERVAL)
  48. sys.stdout.flush()
  49. readiness_thread.join()