-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
workaround for broken logs, allow fetching balance from node
- Loading branch information
Showing
3 changed files
with
142 additions
and
17 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
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,81 @@ | ||
import asyncio | ||
import aiohttp | ||
from gsrest.util.evm import strip_0x, eth_address_to_hex | ||
from async_lru import alru_cache | ||
|
||
|
||
def create_token_request(contract_address: str, | ||
account: str, | ||
block: str = "latest"): | ||
return { | ||
"jsonrpc": | ||
"2.0", | ||
"method": | ||
"eth_call", | ||
"params": [{ | ||
"to": | ||
contract_address, | ||
"data": | ||
f"0x70a08231000000000000000000000000{strip_0x(account)}" | ||
}, "latest"], | ||
"id": | ||
1 | ||
} | ||
|
||
|
||
def create_base_request(account: str, block: str = "latest"): | ||
return { | ||
"method": "eth_getBalance", | ||
"params": [account, block], | ||
"id": 1, | ||
"jsonrpc": "2.0" | ||
} | ||
|
||
|
||
async def send_node_request(node_url: str, payload): | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post(node_url, json=payload) as resp: | ||
return await resp.json() | ||
|
||
|
||
@alru_cache(maxsize=1000) | ||
async def get_token_balance(node_url: str, | ||
contract_address: str, | ||
account: str, | ||
block: str = "latest"): | ||
resp = await send_node_request(node_url, | ||
payload=create_token_request( | ||
contract_address, account, block)) | ||
|
||
return int(resp["result"], 16) | ||
|
||
|
||
@alru_cache(maxsize=1000) | ||
async def get_base_balance(node_url: str, account: str, block: str = "latest"): | ||
resp = await send_node_request(node_url, | ||
payload=create_base_request(account, block)) | ||
|
||
return int(resp["result"], 16) | ||
|
||
|
||
async def get_balances(node_url: str, | ||
network: str, | ||
account: str, | ||
tokenConfigs, | ||
block: str = "latest"): | ||
bb = get_base_balance(node_url, eth_address_to_hex(account), block) | ||
|
||
tbs = [ | ||
get_token_balance(node_url, eth_address_to_hex(tk["token_address"]), | ||
eth_address_to_hex(account), block) | ||
for k, tk in tokenConfigs.items() | ||
] | ||
|
||
balances = await asyncio.gather(bb, *tbs) | ||
|
||
return { | ||
k.upper(): { | ||
"balance": v | ||
} | ||
for k, v in zip([network, *tokenConfigs.keys()], balances) | ||
} |