From d345205613fc2649597e4ffb25ad633c63aa767d Mon Sep 17 00:00:00 2001 From: "kacper.golem" Date: Tue, 20 Feb 2024 10:47:02 +0100 Subject: [PATCH] oct-1355: add blocks_reward for staking --- .../external_api/etherscan/blocks.py | 2 +- .../external_api/etherscan/blocks_reward.py | 44 +++++++++++++++++++ .../external_api/etherscan/req_params.py | 3 +- backend/app/modules/staking/proceeds/core.py | 4 +- .../staking/proceeds/service/aggregated.py | 8 +++- 5 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 backend/app/infrastructure/external_api/etherscan/blocks_reward.py diff --git a/backend/app/infrastructure/external_api/etherscan/blocks.py b/backend/app/infrastructure/external_api/etherscan/blocks.py index b770917192..252cba7af7 100644 --- a/backend/app/infrastructure/external_api/etherscan/blocks.py +++ b/backend/app/infrastructure/external_api/etherscan/blocks.py @@ -12,7 +12,7 @@ def get_block_num_from_ts(timestamp: int) -> int: app.logger.debug(f"Getting block number from timestamp: {timestamp}") - api_url = _get_api_url(timestamp, BlockAction.BLOCK) + api_url = _get_api_url(timestamp, BlockAction.BLOCK_TS) try: response = requests.get(api_url) raise_for_status(response) diff --git a/backend/app/infrastructure/external_api/etherscan/blocks_reward.py b/backend/app/infrastructure/external_api/etherscan/blocks_reward.py new file mode 100644 index 0000000000..49eebeedf3 --- /dev/null +++ b/backend/app/infrastructure/external_api/etherscan/blocks_reward.py @@ -0,0 +1,44 @@ +import app as app_module +import requests +from app.constants import ETHERSCAN_API +from app.exceptions import ExternalApiException +from app.infrastructure.external_api.etherscan.helpers import raise_for_status +from app.infrastructure.external_api.etherscan.req_params import BlockAction +from flask import current_app as app + + +def get_blocks_reward(address: str, start_block: int, end_block: int) -> int: + app.logger.debug( + f"Getting blocks reward from {start_block} and {end_block} for {address} address" + ) + + block_reward = 0 + for i in range(start_block, end_block + 1): + api_url = _get_api_url(i, BlockAction.BLOCK_REWARD) + + try: + response = requests.get(api_url) + raise_for_status(response) + json_response = response.json() + except requests.exceptions.RequestException as e: + app_module.ExceptionHandler.print_stacktrace(e) + raise ExternalApiException(api_url, e, 500) + + result = json_response["result"] + if result["blockMiner"] == address: + block_reward += float(result["blockReward"]) + + return block_reward + + +def _get_api_url( + block_nr: int, + block_action: BlockAction, +) -> str: + api_key = app.config["ETHERSCAN_API_KEY"] + return ( + f"{ETHERSCAN_API}?module=block" + f"&action={block_action.value}" + f"&blockno={block_nr}" + f"&apikey={api_key}" + ) diff --git a/backend/app/infrastructure/external_api/etherscan/req_params.py b/backend/app/infrastructure/external_api/etherscan/req_params.py index 3034b8fa3e..4073012fbd 100644 --- a/backend/app/infrastructure/external_api/etherscan/req_params.py +++ b/backend/app/infrastructure/external_api/etherscan/req_params.py @@ -8,7 +8,8 @@ class AccountAction(StrEnum): class BlockAction(StrEnum): - BLOCK = "getblocknobytime" + BLOCK_TS = "getblocknobytime" + BLOCK_REWARD = "getblockreward" class ClosestValue(StrEnum): diff --git a/backend/app/modules/staking/proceeds/core.py b/backend/app/modules/staking/proceeds/core.py index 1fd4085c74..ab141406d6 100644 --- a/backend/app/modules/staking/proceeds/core.py +++ b/backend/app/modules/staking/proceeds/core.py @@ -54,8 +54,8 @@ def sum_withdrawals(withdrawals_txs: list[dict]) -> int: return w3.to_wei(int(total_gwei), "gwei") -def aggregate_proceeds(mev: int, withdrawals: int) -> int: - return mev + withdrawals +def aggregate_proceeds(mev: int, withdrawals: int, blocks_reward: int) -> int: + return mev + withdrawals + blocks_reward def _filter_deposit_withdrawals(amount: mpz) -> mpz: diff --git a/backend/app/modules/staking/proceeds/service/aggregated.py b/backend/app/modules/staking/proceeds/service/aggregated.py index 7b827386a2..03940e3cf7 100644 --- a/backend/app/modules/staking/proceeds/service/aggregated.py +++ b/backend/app/modules/staking/proceeds/service/aggregated.py @@ -5,6 +5,7 @@ get_transactions, AccountAction, ) +from app.infrastructure.external_api.etherscan.blocks_reward import get_blocks_reward from app.modules.staking.proceeds.core import ( sum_mev, sum_withdrawals, @@ -26,6 +27,8 @@ def get_staking_proceeds(self, context: Context) -> int: - int: Aggregated value for MEV and withdrawals. """ withdrawals_target = app.config["WITHDRAWALS_TARGET_CONTRACT_ADDRESS"].lower() + blocks_reward = 0 + start_block, end_block = ( context.epoch_details.start_block, context.epoch_details.end_block, @@ -33,6 +36,9 @@ def get_staking_proceeds(self, context: Context) -> int: if end_block is not None: end_block -= 1 + blocks_reward = get_blocks_reward( + withdrawals_target, start_block, end_block + ) normal = get_transactions( withdrawals_target, start_block, end_block, tx_type=AccountAction.NORMAL @@ -50,4 +56,4 @@ def get_staking_proceeds(self, context: Context) -> int: mev_value = sum_mev(withdrawals_target, normal, internal) withdrawals_value = sum_withdrawals(withdrawals) - return aggregate_proceeds(mev_value, withdrawals_value) + return aggregate_proceeds(mev_value, withdrawals_value, blocks_reward)