Skip to content

Commit

Permalink
oct-1355: add blocks_reward for staking
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarbacinski committed Feb 20, 2024
1 parent e9f46f9 commit d345205
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions backend/app/infrastructure/external_api/etherscan/blocks_reward.py
Original file line number Diff line number Diff line change
@@ -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}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class AccountAction(StrEnum):


class BlockAction(StrEnum):
BLOCK = "getblocknobytime"
BLOCK_TS = "getblocknobytime"
BLOCK_REWARD = "getblockreward"


class ClosestValue(StrEnum):
Expand Down
4 changes: 2 additions & 2 deletions backend/app/modules/staking/proceeds/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion backend/app/modules/staking/proceeds/service/aggregated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,13 +27,18 @@ 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,
)

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
Expand All @@ -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)

0 comments on commit d345205

Please sign in to comment.