Skip to content

Commit

Permalink
add prev slot, price and conf to PythPriceAccount (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel authored Jul 25, 2022
1 parent 30303a1 commit 9716905
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pythclient/pythaccounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ def __init__(self, key: SolanaPublicKey, solana: SolanaClient, *, product: Optio
self.derivations: Dict[EmaType, int] = {}
self.timestamp: int = 0 # unix timestamp in seconds
self.min_publishers: Optional[int] = None
self.prev_slot: int = 0
self.prev_price: float = field(init=False)
self.prev_conf: float = field(init=False)
self.prev_timestamp: int = 0 # unix timestamp in seconds

@property
Expand All @@ -512,7 +515,7 @@ def aggregate_price_confidence_interval(self) -> Optional[float]:
return self.aggregate_price_info.confidence_interval
else:
return None

@property
def aggregate_price_status(self) -> Optional[PythPriceStatus]:
"""The aggregate price status."""
Expand Down Expand Up @@ -562,9 +565,11 @@ def update_from(self, buffer: bytes, *, version: int, offset: int = 0) -> None:
timestamp, min_publishers = struct.unpack_from("<qB", buffer, offset)
offset += 16 # struct.calcsize("qBbhi") ("bhi" is drv_2, drv_3, drv_4)
product_account_key_bytes, next_price_account_key_bytes = struct.unpack_from("32s32s", buffer, offset)
offset += 88 # struct.calcsize("32s32sQqQ") ("QqQ" is prev_slot, prev_price, prev_conf)
prev_timestamp = struct.unpack_from("<q", buffer, offset)[0]
offset += 8 # struct.calcsize("q")
offset += 64 # struct.calcsize("32s32s")
prev_slot, prev_price, prev_conf, prev_timestamp = struct.unpack_from("<QqQq", buffer, offset)
offset += 32 # struct.calcsize("QqQq")
prev_price *= (10 ** exponent)
prev_conf *= (10 ** exponent)
elif version == _VERSION_1:
price_type, exponent, num_components, _, last_slot, valid_slot, product_account_key_bytes, next_price_account_key_bytes, aggregator_key_bytes = struct.unpack_from(
"<IiIIQQ32s32s32s", buffer, offset)
Expand Down Expand Up @@ -601,6 +606,9 @@ def update_from(self, buffer: bytes, *, version: int, offset: int = 0) -> None:
self.price_components = price_components
self.timestamp = timestamp
self.min_publishers = min_publishers
self.prev_slot = prev_slot
self.prev_price = prev_price
self.prev_conf = prev_conf
self.prev_timestamp = prev_timestamp

def __str__(self) -> str:
Expand Down

0 comments on commit 9716905

Please sign in to comment.