2021-08-04 07:33:14 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from pyth_utils import *
|
|
|
|
|
2021-08-04 04:46:07 -07:00
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
|
|
|
|
|
|
|
import json
|
2021-08-04 07:33:14 -07:00
|
|
|
import random
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
2021-08-04 04:46:07 -07:00
|
|
|
class P2WAccEndpoint(BaseHTTPRequestHandler):
|
|
|
|
"""
|
|
|
|
A dumb endpoint to respond with a JSON containing Pyth account addresses
|
|
|
|
"""
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
print(f"Got path {self.path}")
|
|
|
|
sys.stdout.flush()
|
|
|
|
data = json.dumps(ACCOUNTS).encode("utf-8")
|
|
|
|
print(f"Sending:\n{data}")
|
|
|
|
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-Type", "application/json")
|
|
|
|
self.send_header("Content-Length", str(len(data)))
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(data)
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
|
|
|
|
ACCOUNTS = dict()
|
|
|
|
|
|
|
|
|
2021-08-04 07:33:14 -07:00
|
|
|
def publisher_random_update(price_pubkey):
|
2021-08-04 04:46:07 -07:00
|
|
|
"""
|
|
|
|
Update the specified price with random values
|
|
|
|
"""
|
2021-08-04 07:33:14 -07:00
|
|
|
value = random.randrange(1024)
|
|
|
|
confidence = 1
|
2021-08-04 04:46:07 -07:00
|
|
|
pyth_run_or_die("upd_price_val", args=[
|
|
|
|
price_pubkey, str(value), str(confidence), "trading"
|
|
|
|
])
|
|
|
|
print(f"Price {price_pubkey} value updated to {str(value)}!")
|
|
|
|
|
|
|
|
|
|
|
|
def accounts_endpoint():
|
|
|
|
"""
|
|
|
|
Run a barebones HTTP server to share the dynamic Pyth
|
|
|
|
mapping/product/price account addresses
|
|
|
|
"""
|
|
|
|
server_address = ('', 4242)
|
|
|
|
httpd = HTTPServer(server_address, P2WAccEndpoint)
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
2021-08-04 07:33:14 -07:00
|
|
|
|
|
|
|
# Fund the publisher
|
2021-08-04 04:46:07 -07:00
|
|
|
sol_run_or_die("airdrop", [
|
|
|
|
str(SOL_AIRDROP_AMT),
|
|
|
|
"--keypair", PYTH_PUBLISHER_KEYPAIR,
|
|
|
|
"--commitment", "finalized",
|
|
|
|
])
|
2021-08-04 07:33:14 -07:00
|
|
|
|
|
|
|
# Create a mapping
|
|
|
|
pyth_run_or_die("init_mapping")
|
|
|
|
|
|
|
|
# Add a product
|
2021-08-04 04:46:07 -07:00
|
|
|
prod_pubkey = pyth_run_or_die(
|
|
|
|
"add_product", capture_output=True).stdout.strip()
|
2021-08-04 07:33:14 -07:00
|
|
|
print(f"Added product {prod_pubkey}")
|
|
|
|
|
|
|
|
# Add a price
|
|
|
|
price_pubkey = pyth_run_or_die(
|
|
|
|
"add_price",
|
|
|
|
args=[prod_pubkey, "price"],
|
|
|
|
confirm=False,
|
|
|
|
capture_output=True
|
|
|
|
).stdout.strip()
|
|
|
|
|
|
|
|
print(f"Added price {price_pubkey}")
|
|
|
|
|
2021-08-04 04:46:07 -07:00
|
|
|
publisher_pubkey = sol_run_or_die("address", args=[
|
|
|
|
"--keypair", PYTH_PUBLISHER_KEYPAIR
|
|
|
|
], capture_output=True).stdout.strip()
|
2021-08-04 07:33:14 -07:00
|
|
|
|
|
|
|
# Become a publisher
|
2021-08-04 04:46:07 -07:00
|
|
|
pyth_run_or_die(
|
|
|
|
"add_publisher", args=[publisher_pubkey, price_pubkey],
|
|
|
|
confirm=False,
|
|
|
|
debug=True,
|
|
|
|
capture_output=True)
|
2021-08-04 07:33:14 -07:00
|
|
|
print(f"Added publisher {publisher_pubkey}")
|
|
|
|
|
|
|
|
# Update the price as the newly added publisher
|
|
|
|
publisher_random_update(price_pubkey)
|
|
|
|
|
2021-08-04 04:46:07 -07:00
|
|
|
print(
|
|
|
|
f"Mock updates ready to roll. Updating every {str(PYTH_PUBLISHER_INTERVAL)} seconds")
|
2021-08-04 07:33:14 -07:00
|
|
|
|
|
|
|
# Spin off the readiness probe endpoint into a separate thread
|
2021-08-04 04:46:07 -07:00
|
|
|
readiness_thread = threading.Thread(target=readiness, daemon=True)
|
|
|
|
|
|
|
|
# Start an HTTP endpoint for looking up product/price address
|
|
|
|
http_service = threading.Thread(target=accounts_endpoint, daemon=True)
|
|
|
|
|
|
|
|
ACCOUNTS["product"] = prod_pubkey
|
|
|
|
ACCOUNTS["price"] = price_pubkey
|
2021-08-04 07:33:14 -07:00
|
|
|
|
|
|
|
readiness_thread.start()
|
2021-08-04 04:46:07 -07:00
|
|
|
http_service.start()
|
2021-08-04 07:33:14 -07:00
|
|
|
|
|
|
|
while True:
|
|
|
|
publisher_random_update(price_pubkey)
|
|
|
|
time.sleep(PYTH_PUBLISHER_INTERVAL)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
readiness_thread.join()
|
2021-08-04 04:46:07 -07:00
|
|
|
http_service.join()
|