From 1668645528dc2b9c5345ded9fbe60e63aa0fd19b Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 30 Sep 2023 15:41:43 -0400 Subject: [PATCH 1/2] api hotfix for updating cache --- lib/ethpy/ethpy/hyperdrive/api.py | 51 +++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/ethpy/ethpy/hyperdrive/api.py b/lib/ethpy/ethpy/hyperdrive/api.py index dfb93e4b8..025e64fa8 100644 --- a/lib/ethpy/ethpy/hyperdrive/api.py +++ b/lib/ethpy/ethpy/hyperdrive/api.py @@ -74,31 +74,30 @@ def __init__( self.config, addresses ) self.last_state_block_number = copy.copy(self.current_block_number) + # static self._contract_pool_config = get_hyperdrive_pool_config(self.hyperdrive_contract) self.pool_config = process_hyperdrive_pool_config( copy.deepcopy(self._contract_pool_config), self.hyperdrive_contract.address ) + # will update with trades self._contract_pool_info: dict[str, Any] = {} self._pool_info: dict[str, Any] = {} self._contract_latest_checkpoint: dict[str, int] = {} self._latest_checkpoint: dict[str, Any] = {} - self.update_pool_info_and_checkpoint() # fill these in initially + # fill in initial cache + self._ensure_current_state() super().__init__(eth_config, addresses) @property def pool_info(self) -> dict[str, Any]: """Returns the current pool state info.""" - if self.current_block_number > self.last_state_block_number: - self.last_state_block_number = copy.copy(self.current_block_number) - self.update_pool_info_and_checkpoint() + self._ensure_current_state() return self._pool_info @property def latest_checkpoint(self) -> dict[str, Any]: """Returns the latest checkpoint info.""" - if self.current_block_number > self.last_state_block_number: - self.last_state_block_number = copy.copy(self.current_block_number) - self.update_pool_info_and_checkpoint() + self._ensure_current_state() return self._latest_checkpoint @property @@ -163,6 +162,7 @@ def seconds_since_latest_checkpoint(self) -> int: """Return the amount of seconds that have passed since the latest checkpoint. The time is rounded to the nearest second. """ + self._ensure_current_state() latest_checkpoint_datetime: datetime = self.latest_checkpoint["timestamp"] current_block_datetime = datetime.fromtimestamp(int(self.current_block_time)) return int(round((current_block_datetime - latest_checkpoint_datetime).total_seconds())) @@ -176,6 +176,7 @@ def spot_price(self) -> FixedPoint: FixedPoint The current spot price. """ + self._ensure_current_state() pool_config_str = PoolConfig( base_token=self._contract_pool_config["baseToken"], initial_share_price=str(self._contract_pool_config["initialSharePrice"]), @@ -211,22 +212,24 @@ def spot_price(self) -> FixedPoint: spot_price = pyperdrive.get_spot_price(pool_config_str, pool_info_str) # pylint: disable=no-member return FixedPoint(scaled_value=int(spot_price)) - def update_pool_info_and_checkpoint(self) -> None: - """Update the cached pool info and latest checkpoint.""" - self._contract_pool_info = get_hyperdrive_pool_info(self.hyperdrive_contract, self.current_block_number) - self._pool_info = process_hyperdrive_pool_info( - copy.deepcopy(self._contract_pool_info), - self.web3, - self.hyperdrive_contract, - self.pool_config["positionDuration"], - self.current_block_number, - ) - self._contract_latest_checkpoint = get_hyperdrive_checkpoint( - self.hyperdrive_contract, self.current_block_number - ) - self._latest_checkpoint = process_hyperdrive_checkpoint( - copy.deepcopy(self._contract_latest_checkpoint), self.web3, self.current_block_number - ) + def _ensure_current_state(self) -> None: + """Update the cached pool info and latest checkpoint if needed.""" + if self.current_block_number > self.last_state_block_number: + self.last_state_block_number = copy.copy(self.current_block_number) + self._contract_pool_info = get_hyperdrive_pool_info(self.hyperdrive_contract, self.current_block_number) + self._pool_info = process_hyperdrive_pool_info( + copy.deepcopy(self._contract_pool_info), + self.web3, + self.hyperdrive_contract, + self.pool_config["positionDuration"], + self.current_block_number, + ) + self._contract_latest_checkpoint = get_hyperdrive_checkpoint( + self.hyperdrive_contract, self.current_block_number + ) + self._latest_checkpoint = process_hyperdrive_checkpoint( + copy.deepcopy(self._contract_latest_checkpoint), self.web3, self.current_block_number + ) def bonds_given_shares_and_rate(self, target_rate: FixedPoint) -> FixedPoint: r"""Returns the bond reserves for the market share reserves @@ -576,6 +579,7 @@ def get_max_long(self, budget: FixedPoint) -> FixedPoint: FixedPoint The maximum long as a FixedPoint representation of a Solidity uint256 value. """ + self._ensure_current_state() # pylint: disable=no-member pool_config_str = PoolConfig( base_token=self._contract_pool_config["baseToken"], @@ -631,6 +635,7 @@ def get_max_short(self, budget: FixedPoint) -> FixedPoint: FixedPoint The maximum long as a FixedPoint representation of a Solidity uint256 value. """ + self._ensure_current_state() # pylint: disable=no-member pool_config_str = PoolConfig( base_token=self._contract_pool_config["baseToken"], From d459d27b666ffe4e3a91d646c24d3143b02bd716 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 30 Sep 2023 16:18:12 -0400 Subject: [PATCH 2/2] bugfix --- lib/ethpy/ethpy/hyperdrive/api.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/ethpy/ethpy/hyperdrive/api.py b/lib/ethpy/ethpy/hyperdrive/api.py index 025e64fa8..a1b492c1d 100644 --- a/lib/ethpy/ethpy/hyperdrive/api.py +++ b/lib/ethpy/ethpy/hyperdrive/api.py @@ -85,7 +85,7 @@ def __init__( self._contract_latest_checkpoint: dict[str, int] = {} self._latest_checkpoint: dict[str, Any] = {} # fill in initial cache - self._ensure_current_state() + self._ensure_current_state(override=True) super().__init__(eth_config, addresses) @property @@ -212,9 +212,16 @@ def spot_price(self) -> FixedPoint: spot_price = pyperdrive.get_spot_price(pool_config_str, pool_info_str) # pylint: disable=no-member return FixedPoint(scaled_value=int(spot_price)) - def _ensure_current_state(self) -> None: - """Update the cached pool info and latest checkpoint if needed.""" - if self.current_block_number > self.last_state_block_number: + def _ensure_current_state(self, override: bool = False) -> None: + """Update the cached pool info and latest checkpoint if needed. + + Attributes + ---------- + override : bool + If True, then reset the variables even if it is not needed. + + """ + if self.current_block_number > self.last_state_block_number or override: self.last_state_block_number = copy.copy(self.current_block_number) self._contract_pool_info = get_hyperdrive_pool_info(self.hyperdrive_contract, self.current_block_number) self._pool_info = process_hyperdrive_pool_info(