|
| 1 | +import json |
| 2 | +import pytest |
| 3 | +import websockets |
| 4 | +import time |
| 5 | +import itertools |
| 6 | +import random |
| 7 | + |
| 8 | +from pyth.tests.conftest import PRODUCTS |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_batch_update_price(solana_test_validator, pythd, pyth_init_product, pyth_init_price): |
| 12 | + |
| 13 | + messageIds = itertools.count() |
| 14 | + |
| 15 | + # Use a single websocket connection for the entire test, as batching is done per-user |
| 16 | + async with websockets.connect('ws://localhost:8910/') as ws: |
| 17 | + |
| 18 | + async def update_price(account, price, conf, status): |
| 19 | + msg = jrpc_req( |
| 20 | + method='update_price', |
| 21 | + params={ |
| 22 | + 'account': account, |
| 23 | + 'price': price, |
| 24 | + 'conf': conf, |
| 25 | + 'status': status, |
| 26 | + }) |
| 27 | + |
| 28 | + await send(msg) |
| 29 | + resp = await recv() |
| 30 | + assert resp['result'] == 0 |
| 31 | + |
| 32 | + async def get_product(account): |
| 33 | + msg = jrpc_req(method='get_product', params={ |
| 34 | + 'account': account |
| 35 | + }) |
| 36 | + |
| 37 | + await send(msg) |
| 38 | + result = await recv() |
| 39 | + |
| 40 | + return result |
| 41 | + |
| 42 | + def jrpc_req(method=None, params=None): |
| 43 | + return { |
| 44 | + 'jsonrpc': '2.0', |
| 45 | + 'method': method, |
| 46 | + 'params': params, |
| 47 | + 'id': next(messageIds) |
| 48 | + } |
| 49 | + |
| 50 | + async def send(msg): |
| 51 | + print("--- sending message ---") |
| 52 | + print(msg) |
| 53 | + await ws.send(json.dumps(msg)) |
| 54 | + |
| 55 | + async def recv(): |
| 56 | + data = await ws.recv() |
| 57 | + msg = json.loads(data) |
| 58 | + print("----- received message -----") |
| 59 | + print(msg) |
| 60 | + return msg |
| 61 | + |
| 62 | + def get_publisher_acc(product_acc): |
| 63 | + assert len(product_acc['result']['price_accounts']) == 1 |
| 64 | + price_acc = product_acc['result']['price_accounts'][0] |
| 65 | + |
| 66 | + assert len(price_acc['publisher_accounts']) == 1 |
| 67 | + return price_acc['publisher_accounts'][0] |
| 68 | + |
| 69 | + # Generate new values for this test |
| 70 | + new_values = { |
| 71 | + product: { |
| 72 | + 'price':random.randint(1, 150), |
| 73 | + 'conf': random.randint(1, 20), |
| 74 | + 'status': 'trading', |
| 75 | + } for product in PRODUCTS.keys() |
| 76 | + } |
| 77 | + |
| 78 | + # Update the values of the products |
| 79 | + for product in PRODUCTS.keys(): |
| 80 | + await update_price( |
| 81 | + pyth_init_price[product], |
| 82 | + new_values[product]['price'], |
| 83 | + new_values[product]['conf'], |
| 84 | + new_values[product]['status']) |
| 85 | + |
| 86 | + time.sleep(80) |
| 87 | + |
| 88 | + # Crank the products |
| 89 | + for product in PRODUCTS.keys(): |
| 90 | + await update_price( |
| 91 | + pyth_init_price[product], |
| 92 | + 1, |
| 93 | + 1, |
| 94 | + 'trading') |
| 95 | + |
| 96 | + time.sleep(80) |
| 97 | + |
| 98 | + # Check that the price has been updated |
| 99 | + for product in PRODUCTS.keys(): |
| 100 | + product_acc = await get_product(pyth_init_product[product]) |
| 101 | + publisher_acc = get_publisher_acc(product_acc) |
| 102 | + |
| 103 | + assert publisher_acc['price'] == new_values[product]['price'] |
| 104 | + assert publisher_acc['conf'] == new_values[product]['conf'] |
| 105 | + assert publisher_acc['status'] == new_values[product]['status'] |
0 commit comments