Skip to content

Commit

Permalink
Adding option for explicit block for get checkpoint (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui authored May 30, 2024
1 parent 638d481 commit e2616c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/agent0/ethpy/hyperdrive/interface/read_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ def get_hyperdrive_state(self, block: BlockData | None = None) -> PoolState:
block_number = self.get_block_number(block)
pool_info = get_hyperdrive_pool_info(self.hyperdrive_contract, block_number)
checkpoint_time = self.calc_checkpoint_id(self.pool_config.checkpoint_duration, self.get_block_timestamp(block))
checkpoint = get_hyperdrive_checkpoint(self.hyperdrive_contract, checkpoint_time)
exposure = get_hyperdrive_checkpoint_exposure(self.hyperdrive_contract, checkpoint_time)
checkpoint = get_hyperdrive_checkpoint(self.hyperdrive_contract, checkpoint_time, block_number)
exposure = get_hyperdrive_checkpoint_exposure(self.hyperdrive_contract, checkpoint_time, block_number)

try:
variable_rate = self.get_variable_rate(block_number)
Expand Down Expand Up @@ -339,20 +339,25 @@ def get_hyperdrive_state(self, block: BlockData | None = None) -> PoolState:
gov_fees_accrued=gov_fees_accrued,
)

def get_checkpoint(self, checkpoint_time: Timestamp) -> CheckpointFP:
def get_checkpoint(self, checkpoint_time: Timestamp, block_number: BlockNumber | None = None) -> CheckpointFP:
"""Use an RPC to get the checkpoint info for the Hyperdrive contract for a given checkpoint_time index.
Arguments
---------
checkpoint_time: Timestamp
The block timestamp that indexes the checkpoint to get.
block_number: BlockNumber, optional
The number for any minted block.
If not given, the latest block number is used.
Returns
-------
CheckpointFP
The dataclass containing the checkpoint info in fixed point
"""
return get_hyperdrive_checkpoint(self.hyperdrive_contract, checkpoint_time)
if block_number is None:
block_number = self.get_block_number(self.get_current_block())
return get_hyperdrive_checkpoint(self.hyperdrive_contract, checkpoint_time, block_number)

def get_total_supply_withdrawal_shares(self, block_number: BlockNumber | None) -> FixedPoint:
"""Use an RPC to get the total supply of withdrawal shares in the pool at the given block.
Expand Down Expand Up @@ -449,7 +454,8 @@ def get_standardized_variable_rate(self, time_range: int = 604800) -> FixedPoint
The standardized variable rate.
"""
# Get the vault share price of the checkpoint in the past `time_range`
current_block_time = self.get_block_timestamp(self.current_pool_state.block)
current_block = self.current_pool_state.block
current_block_time = self.get_block_timestamp(current_block)
start_checkpoint_id = self.calc_checkpoint_id(block_timestamp=Timestamp(current_block_time - time_range))
start_vault_share_price = self.get_checkpoint(start_checkpoint_id).vault_share_price

Expand Down
14 changes: 10 additions & 4 deletions src/agent0/ethpy/hyperdrive/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def get_hyperdrive_pool_info(hyperdrive_contract: IHyperdriveContract, block_num
return pool_info_to_fixedpoint(pool_info)


def get_hyperdrive_checkpoint(hyperdrive_contract: IHyperdriveContract, checkpoint_time: Timestamp) -> CheckpointFP:
def get_hyperdrive_checkpoint(
hyperdrive_contract: IHyperdriveContract, checkpoint_time: Timestamp, block_number: BlockNumber
) -> CheckpointFP:
"""Get the checkpoint info for the Hyperdrive contract at a given block.
Arguments
Expand All @@ -67,18 +69,20 @@ def get_hyperdrive_checkpoint(hyperdrive_contract: IHyperdriveContract, checkpoi
The contract to query the pool info from.
checkpoint_time: Timestamp
The block timestamp that indexes the checkpoint to get.
block_number: BlockNumber
The block number to query from the chain.
Returns
-------
CheckpointFP
The dataclass containing the checkpoint info in fixed point
"""
checkpoint = hyperdrive_contract.functions.getCheckpoint(checkpoint_time).call()
checkpoint = hyperdrive_contract.functions.getCheckpoint(checkpoint_time).call(None, block_number)
return checkpoint_to_fixedpoint(checkpoint)


def get_hyperdrive_checkpoint_exposure(
hyperdrive_contract: IHyperdriveContract, checkpoint_time: Timestamp
hyperdrive_contract: IHyperdriveContract, checkpoint_time: Timestamp, block_number: BlockNumber
) -> FixedPoint:
"""Get the checkpoint exposure for the Hyperdrive contract at a given block.
Expand All @@ -89,13 +93,15 @@ def get_hyperdrive_checkpoint_exposure(
checkpoint_time: Timestamp
The block timestamp that indexes the checkpoint to get.
This must be an exact checkpoint time for the deployed pool.
block_number: BlockNumber
The block number to query from the chain.
Returns
-------
CheckpointFP
The dataclass containing the checkpoint info in fixed point.
"""
exposure = hyperdrive_contract.functions.getCheckpointExposure(checkpoint_time).call()
exposure = hyperdrive_contract.functions.getCheckpointExposure(checkpoint_time).call(None, block_number)
return FixedPoint(scaled_value=exposure)


Expand Down

0 comments on commit e2616c5

Please sign in to comment.