Skip to content

Commit

Permalink
Add function for checking whether contracts ready
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudmi committed Jun 18, 2021
1 parent 021bd07 commit 314bf12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 10 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import time

from contracts import get_reward_eth_contract
from src.merkle_distributor.distributor import Distributor
from src.settings import (
WEB3_WS_ENDPOINT,
Expand All @@ -25,7 +26,7 @@
ETHERSCAN_ADDRESS_BASE_URL,
)
from src.staking_rewards.rewards import Rewards
from src.staking_rewards.utils import wait_prysm_ready
from src.staking_rewards.utils import wait_prysm_ready, wait_contracts_ready
from src.utils import (
get_web3_client,
configure_default_account,
Expand Down Expand Up @@ -73,6 +74,14 @@ def main() -> None:
disable_web_page_preview=True,
)

# wait for contracts to be upgraded to the oracles supported version
reward_eth = get_reward_eth_contract(web3_client)
wait_contracts_ready(
test_query=reward_eth.functions.lastUpdateBlockNumber(),
interrupt_handler=interrupt_handler,
process_interval=PROCESS_INTERVAL,
)

# wait that node is synced before trying to do anything
wait_prysm_ready(
interrupt_handler=interrupt_handler,
Expand Down
23 changes: 21 additions & 2 deletions src/staking_rewards/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from grpc import insecure_channel, RpcError, StatusCode
from tenacity import retry, Retrying
from tenacity.before_sleep import before_sleep_log
from typing import Set, Dict, Tuple
from typing import Set, Dict, Tuple, Callable
from web3 import Web3
from web3.contract import Contract
from web3.contract import Contract, ContractFunction
from web3.exceptions import ContractLogicError
from web3.types import Wei, BlockNumber, Timestamp, BlockIdentifier

Expand Down Expand Up @@ -242,6 +242,25 @@ def get_validators_total_balance(
return total_balance


def wait_contracts_ready(
test_query: ContractFunction,
interrupt_handler: InterruptHandler,
process_interval: int,
) -> None:
"""
Wait that smart contracts are ready to for interactions.
"""
while not interrupt_handler.exit:
try:
# This will bomb with ContractLogicError if contract are not ready
test_query.call()
break
except ContractLogicError:
logger.warning("Waiting for contracts to be upgraded...")

time.sleep(process_interval)


def wait_prysm_ready(
interrupt_handler: InterruptHandler,
endpoint: str,
Expand Down

0 comments on commit 314bf12

Please sign in to comment.