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

api hotfix for updating cache #934

Merged
merged 2 commits into from
Sep 30, 2023
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
58 changes: 35 additions & 23 deletions lib/ethpy/ethpy/hyperdrive/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(override=True)
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
Expand Down Expand Up @@ -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()))
Expand All @@ -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"]),
Expand Down Expand Up @@ -211,22 +212,31 @@ 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, 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(
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
Expand Down Expand Up @@ -576,6 +586,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"],
Expand Down Expand Up @@ -631,6 +642,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"],
Expand Down
Loading