|
| 1 | +import pytest |
| 2 | + |
| 3 | +from _pytest.logging import LogCaptureFixture |
| 4 | + |
| 5 | +from pythclient.solana import SolanaClient, SolanaPublicKey |
| 6 | +from pythclient.pythaccounts import PythAccount, PythAccountType, PythProductAccount |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def solana_pubkey() -> SolanaPublicKey: |
| 11 | + return SolanaPublicKey("AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J") |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def product_account_pubkey() -> SolanaPublicKey: |
| 16 | + return SolanaPublicKey("89GseEmvNkzAMMEXcW9oTYzqRPXTsJ3BmNerXmgA1osV") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def price_account_pubkey() -> SolanaPublicKey: |
| 21 | + return SolanaPublicKey("4EQrNZYk5KR1RnjyzbaaRbHsv8VqZWzSUtvx58wLsZbj") |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def product_account_b64() -> str: |
| 26 | + return ('1MOyoQIAAAACAAAAlwAAADAClHlZh5cpDjY4oXEsKb3iNn0OynlPd4sltaRy8ZLeBnN5bWJvbAdCQ0gv' |
| 27 | + 'VVNECmFzc2V0X3R5cGUGQ3J5cHRvDnF1b3RlX2N1cnJlbmN5A1VTRAtkZXNjcmlwdGlvbgdCQ0gvVVNE' |
| 28 | + 'DmdlbmVyaWNfc3ltYm9sBkJDSFVTRARiYXNlA0JDSA==') |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def pyth_account(solana_pubkey: SolanaPublicKey, solana_client: SolanaClient) -> PythAccount: |
| 33 | + return PythAccount( |
| 34 | + key=solana_pubkey, |
| 35 | + solana=solana_client, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@ pytest.fixture |
| 40 | +def product_account(solana_client: SolanaClient, |
| 41 | + product_account_pubkey: SolanaPublicKey, |
| 42 | + price_account_pubkey: SolanaPublicKey) -> PythProductAccount: |
| 43 | + product_account = PythProductAccount( |
| 44 | + key=product_account_pubkey, |
| 45 | + solana=solana_client, |
| 46 | + ) |
| 47 | + product_account.slot = 96866599 |
| 48 | + product_account.attrs = { |
| 49 | + 'asset_type': 'Crypto', |
| 50 | + 'symbol': 'BCH/USD', |
| 51 | + 'quote_currency': 'USD', |
| 52 | + 'description': 'BCH/USD', |
| 53 | + 'generic_symbol': 'BCHUSD', |
| 54 | + 'base': 'BCH' |
| 55 | + } |
| 56 | + product_account.first_price_account_key = price_account_pubkey |
| 57 | + return product_account |
| 58 | + |
| 59 | + |
| 60 | +def test_product_account_update_with_rpc_response_with_data( |
| 61 | + solana_client: SolanaClient, |
| 62 | + product_account: PythProductAccount, |
| 63 | + product_account_b64: str |
| 64 | +) -> None: |
| 65 | + actual = PythProductAccount( |
| 66 | + key=product_account.key, |
| 67 | + solana=solana_client, |
| 68 | + ) |
| 69 | + assert actual.attrs == {} |
| 70 | + |
| 71 | + slot = 96866600 |
| 72 | + value = { |
| 73 | + 'lamports': 1000000000, |
| 74 | + 'data': [ |
| 75 | + product_account_b64, |
| 76 | + 'base64' |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + actual.update_with_rpc_response(slot=slot, value=value) |
| 81 | + assert actual.slot == slot |
| 82 | + assert actual.lamports == value['lamports'] |
| 83 | + assert actual.attrs['symbol'] == product_account.attrs['symbol'] |
| 84 | + assert actual.attrs['description'] == product_account.attrs['description'] |
| 85 | + assert actual.attrs['generic_symbol'] == product_account.attrs['generic_symbol'] |
| 86 | + assert actual.attrs['base'] == product_account.attrs['base'] |
| 87 | + |
| 88 | + |
| 89 | +def test_pyth_account_update_with_rpc_response_wrong_type( |
| 90 | + pyth_account: PythAccount, |
| 91 | + caplog: LogCaptureFixture, |
| 92 | + product_account_b64: str |
| 93 | +) -> None: |
| 94 | + slot = 96866600 |
| 95 | + value = { |
| 96 | + 'lamports': 1000000000, |
| 97 | + 'data': [ |
| 98 | + product_account_b64, |
| 99 | + 'base64' |
| 100 | + ] |
| 101 | + } |
| 102 | + |
| 103 | + exc_message = f"wrong Pyth account type {PythAccountType.PRODUCT} for {type(pyth_account)}" |
| 104 | + with pytest.raises(ValueError, match=exc_message): |
| 105 | + pyth_account.update_with_rpc_response(slot=slot, value=value) |
| 106 | + |
| 107 | + |
| 108 | +def test_pyth_account_update_with_rpc_response_no_data( |
| 109 | + pyth_account: PythAccount, |
| 110 | + caplog: LogCaptureFixture |
| 111 | +) -> None: |
| 112 | + slot = 106498726 |
| 113 | + value = { |
| 114 | + "lamports": 1000000000 |
| 115 | + } |
| 116 | + |
| 117 | + exc_message = f'invalid account data response from Solana for key {pyth_account.key}: {value}' |
| 118 | + with pytest.raises(ValueError, match=exc_message): |
| 119 | + pyth_account.update_with_rpc_response(slot=slot, value=value) |
| 120 | + assert exc_message in caplog.text |
0 commit comments