Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/to json (#2) #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions pythclient/pythaccounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ def update_from(self, buffer: bytes, *, version: int, offset: int = 0) -> None:
def __str__(self) -> str:
return f"PythMappingAccount ({self.key})"

def to_json(self):

return {
'entries': [str(x) for x in self.entries],
'next_account_key': str(self.next_account_key)
}


class PythProductAccount(PythAccount):
"""
Expand Down Expand Up @@ -350,6 +357,12 @@ def __iter__(self):
if not key.startswith('_'):
yield key, val

def to_json(self):

return {
'symbol': self.symbol,
}


@dataclass
class PythPriceInfo:
Expand Down Expand Up @@ -405,6 +418,19 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return str(self)

def to_json(self):

return {
"price": self.price,
"confidence_interval": self.confidence_interval,
"price_status": self.price_status.name,
"pub_slot": self.pub_slot,
"exponent": self.exponent,
"raw_confidence_interval": self.raw_confidence_interval,
"raw_price": self.raw_price
}



@dataclass
class PythPriceComponent:
Expand Down Expand Up @@ -449,6 +475,15 @@ def deserialise(buffer: bytes, offset: int = 0, *, exponent: int) -> Optional[Py
latest_price = PythPriceInfo.deserialise(buffer, offset, exponent=exponent)
return PythPriceComponent(key, last_aggregate_price, latest_price, exponent)

def to_json(self):

return {
"publisher_key": str(self.publisher_key),
"last_aggregate_price_info": self.last_aggregate_price_info.to_json(),
"latest_price_info": self.latest_price_info.to_json(),
"exponent": self.exponent
}


class PythPriceAccount(PythAccount):
"""
Expand Down Expand Up @@ -603,6 +638,25 @@ def __str__(self) -> str:
else:
return f"PythPriceAccount {self.price_type} ({self.key})"

def to_json(self):

return {
"product": self.product.to_json() if self.product else None,
"price_type": self.price_type.name,
"exponent": self.exponent,
"num_components": self.num_components,
"last_slot": self.last_slot,
"valid_slot": self.valid_slot,
"product_account_key": str(self.product_account_key),
"next_price_account_key": str(self.next_price_account_key),
"aggregate_price_info": self.aggregate_price_info.to_json() if self.aggregate_price_info else None,
"price_components": [x.to_json() for x in self.price_components],
"derivations": {EmaType(x).name: self.derivations.get(x) for x in list(self.derivations.keys())},
"min_publishers": self.min_publishers,
"aggregate_price": self.aggregate_price,
"aggregate_price_confidence_interval": self.aggregate_price_confidence_interval
}


_ACCOUNT_TYPE_TO_CLASS = {
PythAccountType.MAPPING: PythMappingAccount,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_mapping_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ def test_mapping_account_str(mapping_account, solana_client):
actual = str(mapping_account)
expected = f"PythMappingAccount ({mapping_account.key})"
assert actual == expected


def test_mapping_account_to_json(mapping_account, solana_client):

ignore_keys = {"key", "solana", "lamports", "slot"}
must_contain_keys = set()

keys_json = set(mapping_account.to_json().keys())
keys_orig = set(mapping_account.__dict__.keys())

# test for differences
assert keys_orig - ignore_keys == keys_json - ignore_keys

# test for missing keys
assert must_contain_keys.issubset(keys_json)
19 changes: 19 additions & 0 deletions tests/test_price_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,22 @@ def test_price_account_get_aggregate_price_status_got_stale(

price_status = price_account.aggregate_price_status
assert price_status == PythPriceStatus.UNKNOWN

def test_price_account_to_json(
price_account_bytes: bytes, price_account: PythPriceAccount
):
price_account.update_from(buffer=price_account_bytes, version=2, offset=0)
price_account.slot = price_account.aggregate_price_info.pub_slot

# attributes which are not part of to_json
ignore_keys = {"aggregate_price", "aggregate_price_confidence_interval", "solana", "slot", "key", "lamports"}
must_contain_keys = {"aggregate_price", "aggregate_price_confidence_interval"}

keys_json = set(price_account.to_json().keys())
keys_orig = set(price_account.__dict__.keys())

# test for differences
assert keys_orig - ignore_keys == keys_json - ignore_keys

# test for missing keys
assert must_contain_keys.issubset(keys_json)
15 changes: 15 additions & 0 deletions tests/test_price_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ def test_deserialise_null_publisher_key(price_component: PythPriceComponent, pri
bad_bytes = bytes(b'\x00' * SolanaPublicKey.LENGTH) + price_component_bytes[SolanaPublicKey.LENGTH:]
actual = PythPriceComponent.deserialise(bad_bytes, exponent=price_component.exponent)
assert actual is None


def test_price_component_to_json(price_component: PythPriceComponent, price_component_bytes: bytes):

ignore_keys = set()
must_contain_keys = set()

keys_json = set(price_component.to_json().keys())
keys_orig = set(price_component.__dict__.keys())

# test for differences
assert keys_orig - ignore_keys == keys_json - ignore_keys

# test for missing keys
assert must_contain_keys.issubset(keys_json)
14 changes: 14 additions & 0 deletions tests/test_price_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ def test_price_info_str(price_info_trading):
expected = "PythPriceInfo status PythPriceStatus.TRADING price 596.09162"
assert str(price_info_trading) == expected
assert repr(price_info_trading) == expected

def test_price_info_to_json(price_info_trading):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to add a test which checks that the result of to_json is what we expect, instead of making assertions about the keys?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember very well at this point, but I think I made it that way so that tests fail if a developer updates the class attributes without updating the to_json.


ignore_keys = set()
must_contain_keys = set()

keys_json = set(price_info_trading.to_json().keys())
keys_orig = set(price_info_trading.__dict__.keys())

# test for differences
assert keys_orig - ignore_keys == keys_json - ignore_keys

# test for missing keys
assert must_contain_keys.issubset(keys_json)
15 changes: 15 additions & 0 deletions tests/test_product_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,18 @@ def test_symbol_property_unknown(product_account: PythProductAccount, solana_cli
solana=solana_client,
)
assert actual.symbol == "Unknown"


def test_product_account_to_json(product_account: PythProductAccount):
# attributes which are not part of to_json
ignore_keys = {"_prices", "attrs", "first_price_account_key", "key", "lamports", "slot", "solana", "symbol"}
must_contain_keys = {"symbol"}

keys_json = set(product_account.to_json().keys())
keys_orig = set(product_account.__dict__.keys())

# test for differences
assert keys_orig - ignore_keys == keys_json - ignore_keys

# test for missing keys
assert must_contain_keys.issubset(keys_json)