-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oct-1355: add blocks_reward for staking
- Loading branch information
1 parent
e9f46f9
commit d345205
Showing
5 changed files
with
56 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
backend/app/infrastructure/external_api/etherscan/blocks_reward.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters