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

Update to json #1

Merged
merged 3 commits into from
Mar 17, 2022
Merged
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
25 changes: 11 additions & 14 deletions pythclient/pythaccounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@ class PythPriceStatus(Enum):
class PythPriceType(Enum):
UNKNOWN = 0
PRICE = 1
# TWAP/VOL removed after V2
TWAP = 2
VOLATILITY = 3


# Join time-weighted exponential moving average for TWAP and TWAC
class TwEmaType(Enum):
# Join exponential moving average for EMA price and EMA confidence
class EmaType(Enum):
UNKNOWN = 0
TWAPVALUE = 1
TWAPNUMERATOR = 2
TWAPDENOMINATOR = 3
TWACVALUE = 4
TWACNUMERATOR = 5
TWACDENOMINATOR = 6
EMA_PRICE_VALUE = 1
EMA_PRICE_NUMERATOR = 2
EMA_PRICE_DENOMINATOR = 3
EMA_CONFIDENCE_VALUE = 4
EMA_CONFIDENCE_NUMERATOR = 5
EMA_CONFIDENCE_DENOMINATOR = 6


def _check_base64(format: str):
Expand Down Expand Up @@ -524,7 +521,7 @@ def __init__(self, key: SolanaPublicKey, solana: SolanaClient, *, product: Optio
self.next_price_account_key: Optional[SolanaPublicKey] = None
self.aggregate_price_info: Optional[PythPriceInfo] = None
self.price_components: List[PythPriceComponent] = []
self.derivations: Dict[TwEmaType, int] = {}
self.derivations: Dict[EmaType, int] = {}
self.min_publishers: Optional[int] = None

@property
Expand Down Expand Up @@ -592,7 +589,7 @@ def update_from(self, buffer: bytes, *, version: int, offset: int = 0) -> None:
last_slot, valid_slot = struct.unpack_from("<QQ", buffer, offset)
offset += 16 # QQ
derivations = list(struct.unpack_from("<6q", buffer, offset))
self.derivations = dict((type_, derivations[type_.value - 1]) for type_ in [TwEmaType.TWACVALUE, TwEmaType.TWAPVALUE])
self.derivations = dict((type_, derivations[type_.value - 1]) for type_ in [EmaType.EMA_CONFIDENCE_VALUE, EmaType.EMA_PRICE_VALUE])
offset += 48 # 6q
# All drv*_ fields sans min_publishers are currently unused
_, min_publishers = struct.unpack_from("<qQ", buffer, offset)
Expand Down Expand Up @@ -654,7 +651,7 @@ def to_json(self):
"next_price_account_key": str(self.next_price_account_key),
"aggregate_price_info": self.aggregate_price_info.to_json(),
"price_components": [x.to_json() for x in self.price_components],
"derivations": {TwEmaType(x).name: self.derivations.get(x) for x in list(self.derivations.keys())},
"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
Expand Down