-
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.
improve api communication with bitquery
- Loading branch information
1 parent
d345205
commit 07f7dd9
Showing
11 changed files
with
173 additions
and
57 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
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
Empty file.
43 changes: 43 additions & 0 deletions
43
backend/app/infrastructure/external_api/bitquery/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,43 @@ | ||
import requests | ||
|
||
import app as app_module | ||
from app.constants import BITQUERY_API | ||
from app.exceptions import ExternalApiException | ||
from app.extensions import w3 | ||
from app.infrastructure.external_api.bitquery.req_producer import ( | ||
produce_payload, | ||
BitQueryActions, | ||
get_bitquery_header, | ||
) | ||
|
||
|
||
def accumulate_blocks_reward(blocks: list) -> int: | ||
blocks_reward_gwei = 0.0 | ||
for block in blocks: | ||
blocks_reward_gwei += float(block["reward"]) | ||
|
||
return int(w3.to_wei(blocks_reward_gwei, "gwei")) | ||
|
||
|
||
def get_blocks_reward(address: str, start_time: str, end_time: str) -> int: | ||
payload = produce_payload( | ||
action_type=BitQueryActions.GET_BLOCK_REWARDS, | ||
address=address, | ||
start_time=start_time, | ||
end_time=end_time, | ||
) | ||
headers = get_bitquery_header() | ||
|
||
api_url = BITQUERY_API | ||
|
||
try: | ||
response = requests.request("POST", api_url, headers=headers, data=payload) | ||
response.raise_for_status() | ||
json_response = response.json() | ||
except requests.exceptions.RequestException as e: | ||
app_module.ExceptionHandler.print_stacktrace(e) | ||
raise ExternalApiException(api_url, e, 500) | ||
|
||
blocks = json_response.json()["data"]["ethereum"]["blocks"] | ||
blocks_reward = accumulate_blocks_reward(blocks) | ||
return blocks_reward |
56 changes: 56 additions & 0 deletions
56
backend/app/infrastructure/external_api/bitquery/req_producer.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,56 @@ | ||
import json | ||
from enum import IntEnum | ||
|
||
from flask import current_app as app | ||
|
||
|
||
class BitQueryActions(IntEnum): | ||
GET_BLOCK_REWARDS = 0 | ||
|
||
|
||
def get_bitquery_header(): | ||
headers = { | ||
"Content-Type": "application/json", | ||
"X-API-KEY": app.config["BITQUERY_API_KEY"], | ||
"Authorization": "Bearer ory_at_yeIAwzWgtkssnWzCCpDKnrDv74AEkhaRbO1VHsrCSz0.nVnX2zUQiYr2AKSu823GYOuyIsiyVyp5WLDBFcawQT8", | ||
} | ||
|
||
return headers | ||
|
||
|
||
def produce_payload(action_type: BitQueryActions, **query_values) -> str: | ||
payloads_variations = {BitQueryActions.GET_BLOCK_REWARDS: _block_rewards_payload} | ||
|
||
return payloads_variations[action_type](**query_values) | ||
|
||
|
||
def _block_rewards_payload( | ||
start_time: str, end_time: str, address: str, **kwargs | ||
) -> str: | ||
payload = json.dumps( | ||
{ | ||
"query": f"""query ($network: EthereumNetwork!, $from: ISO8601DateTime, $till: ISO8601DateTime) {{ | ||
ethereum(network: $network) {{ | ||
blocks(time: {{since: $from, till: $till}}) {{ | ||
timestamp {{ | ||
unixtime | ||
}} | ||
reward | ||
address: miner(miner: {{is: "{address}"}}) {{ | ||
address | ||
}} | ||
}} | ||
}} | ||
}}""", | ||
"variables": json.dumps( | ||
{ | ||
"network": "ethereum", | ||
"from": start_time, | ||
"till": end_time, | ||
"dateFormat": "%Y-%m-%d", | ||
} | ||
), | ||
} | ||
) | ||
|
||
return payload |
44 changes: 0 additions & 44 deletions
44
backend/app/infrastructure/external_api/etherscan/blocks_reward.py
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
from datetime import datetime | ||
|
||
|
||
def timestamp_to_isoformat(timestamp_sec: int) -> str: | ||
return datetime.fromtimestamp(timestamp_sec).isoformat() |
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
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