Skip to content

Commit

Permalink
fix erc20 balance for bera and evm wallet (#8)
Browse files Browse the repository at this point in the history
* fix erc20 balance for bera and evm wallet
* fix lint
  • Loading branch information
kerto07 authored Feb 11, 2025
1 parent 94c3005 commit a6b7eca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 48 deletions.
6 changes: 0 additions & 6 deletions bera.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,3 @@ def get_bera_queued_boost(bgt_address, wallet, api):
bgt = create_contract(bgt_address, "./abi/BGT.json", api)
result = bgt.functions.queuedBoost(wallet).call()
return result


def get_bera_balance(bgt_address, wallet, api):
bgt = create_contract(bgt_address, "./abi/BGT.json", api)
result = bgt.functions.balanceOf(wallet).call()
return result
43 changes: 29 additions & 14 deletions ethereum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from web3 import Web3
from metrics_enum import MetricsUrlStatus, TokenType
from utils import http_json_call

from metrics_enum import MetricsUrlStatus
from web3 import Web3


def get_evm_chains_data(rpc_call_status_counter):
Expand All @@ -25,7 +24,25 @@ def get_chain_symbol(chain_id, chain_data):

def get_ethereum_balance(apiprovider, wallet, rpc_call_status_counter, chains_evm):
try:
balances = []
addr = wallet["address"]

web3 = Web3(Web3.HTTPProvider(apiprovider))
balance = web3.eth.get_balance(addr)
balance_ether = web3.from_wei(balance, "ether")
chain_id = web3.eth.chain_id
symbol = get_chain_symbol(chain_id=chain_id, chain_data=chains_evm)
rpc_call_status_counter.labels(
url=apiprovider, status=MetricsUrlStatus.SUCCESS.value
).inc()
balances.append(
{
"balance": balance_ether,
"symbol": symbol,
"token_type": TokenType.NATIVE.value,
}
)

# if it is erc20
if "contract_address" in wallet:
contract_address = wallet["contract_address"]
Expand All @@ -38,17 +55,15 @@ def get_ethereum_balance(apiprovider, wallet, rpc_call_status_counter, chains_ev
rpc_call_status_counter.labels(
url=apiprovider, status=MetricsUrlStatus.SUCCESS.value
).inc()
return {"balance": erc20_data["balance"], "symbol": erc20_data["symbol"]}
else:
web3 = Web3(Web3.HTTPProvider(apiprovider))
balance = web3.eth.get_balance(addr)
balance_ether = web3.from_wei(balance, "ether")
chain_id = web3.eth.chain_id
symbol = get_chain_symbol(chain_id=chain_id, chain_data=chains_evm)
rpc_call_status_counter.labels(
url=apiprovider, status=MetricsUrlStatus.SUCCESS.value
).inc()
return {"balance": balance_ether, "symbol": symbol}
balances.append(
{
"balance": erc20_data["balance"],
"symbol": erc20_data["symbol"],
"token_type": TokenType.ERC_20.value,
}
)

return balances
except Exception as addr_balancer_err:
rpc_call_status_counter.labels(
url=apiprovider, status=MetricsUrlStatus.FAILED.value
Expand Down
77 changes: 49 additions & 28 deletions exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import time

from bera import (
get_bera_balance,
get_bera_boostees,
get_bera_boosts,
get_bera_queued_boost,
Expand All @@ -20,7 +19,7 @@
)
from dotenv import load_dotenv
from ethereum import get_ethereum_balance, get_evm_chains_data
from metrics_enum import MetricsAccountInfo, NetworkType
from metrics_enum import MetricsAccountInfo, NetworkType, TokenType
from prometheus_client import Counter, Gauge, start_http_server
from substrate import get_substrate_account_balance
from utils import configure_logging, read_config_file
Expand All @@ -43,7 +42,7 @@ def __init__(self, polling_interval_seconds=60, walletconfig=False, logging=Fals
self.account_info = Gauge(
"account_info",
"account information",
["address", "name", "network", "type"],
["address", "name", "network", "type", "token", "token_type"],
)
self.rpc_call_status_counter = Counter(
"rpc_call_status",
Expand Down Expand Up @@ -79,19 +78,36 @@ def fetch_balance(self, network, wallet, chain_registry):
self.logging.info(
f"{wallet['address']} has {balance} {chain_registry['symbol']}"
)
elif network_type == NetworkType.EVM.value:
balance_data = get_ethereum_balance(
self.account_info.labels(
network=network_name,
address=wallet["address"],
name=wallet["name"],
token=chain_registry["symbol"],
token_type=TokenType.NATIVE.value,
type=MetricsAccountInfo.BALANCE.value,
).set(balance)
elif (
network_type == NetworkType.EVM.value
or network_type == NetworkType.BERA.value
):
balances_data = get_ethereum_balance(
apiprovider=network["api"],
wallet=wallet,
rpc_call_status_counter=self.rpc_call_status_counter,
chains_evm=self.chains_evm,
)
balance = balance_data["balance"]
symbol = balance_data["symbol"]
if "contract_address" in wallet:
self.logging.info(f"{wallet['address']} has {balance} {symbol}")
else:
for balance_data in balances_data:
balance = balance_data["balance"]
symbol = balance_data["symbol"]
self.logging.info(f"{wallet['address']} has {balance} {symbol}")
self.account_info.labels(
network=network_name,
address=wallet["address"],
name=wallet["name"],
token_type=balance_data["token_type"],
token=symbol,
type=MetricsAccountInfo.BALANCE.value,
).set(balance)
elif network_type == NetworkType.SUBSTRATE.value:
substrate_info = get_substrate_account_balance(
node_url=network["api"],
Expand All @@ -103,23 +119,14 @@ def fetch_balance(self, network, wallet, chain_registry):
)
symbol = substrate_info.get("symbol")
self.logging.info(f"{wallet['address']} has {balance} {symbol}")
elif network_type == NetworkType.BERA.value:
balance = (
get_bera_balance(
bgt_address=network["bgt_address"],
wallet=wallet["address"],
api=network["api"],
)
/ 10**18
)
self.logging.info(f"{wallet['address']} has {balance} BGT")

self.account_info.labels(
network=network_name,
address=wallet["address"],
name=wallet["name"],
type=MetricsAccountInfo.BALANCE.value,
).set(balance)
self.account_info.labels(
network=network_name,
address=wallet["address"],
name=wallet["name"],
token=symbol,
token_type=TokenType.NATIVE.value,
type=MetricsAccountInfo.BALANCE.value,
).set(balance)

def fetch_delegations(self, network, wallet, chain_registry):
network_name = network["name"]
Expand All @@ -139,6 +146,8 @@ def fetch_delegations(self, network, wallet, chain_registry):
network=network_name,
address=wallet["address"],
name=wallet["name"],
token=chain_registry["symbol"],
token_type=TokenType.NATIVE.value,
type=MetricsAccountInfo.DELEGATIONS.value,
).set(delegations)

Expand All @@ -155,11 +164,13 @@ def fetch_boosts(self, network, wallet):
)
/ 10**18
)
self.logging.info(f"{wallet['address']} has {bera_boosts} bera boosts")
self.logging.info(f"{wallet['address']} has {bera_boosts} bgt boosts")
self.account_info.labels(
network=network_name,
address=wallet["address"],
name=wallet["name"],
token="BGT",
token_type=TokenType.ERC_20.value,
type=MetricsAccountInfo.BOOSTS.value,
).set(bera_boosts)

Expand All @@ -182,6 +193,8 @@ def fetch_unbounding_delegations(self, network, wallet, chain_registry):
network=network_name,
address=wallet["address"],
name=wallet["name"],
token=chain_registry["symbol"],
token_type=TokenType.NATIVE.value,
type=MetricsAccountInfo.UNBOUNDING_DELEGATIONS.value,
).set(unbounding_delegations)

Expand All @@ -203,6 +216,8 @@ def fetch_rewards(self, network, wallet, chain_registry):
network=network_name,
address=wallet["address"],
name=wallet["name"],
token=chain_registry["symbol"],
token_type=TokenType.NATIVE.value,
type=MetricsAccountInfo.REWARDS.value,
).set(rewards)

Expand All @@ -226,6 +241,8 @@ def fetch_boostees(self, network, wallet):
network=network_name,
address=wallet["address"],
name=wallet["name"],
token="BGT",
token_type=TokenType.ERC_20.value,
type=MetricsAccountInfo.VALIDATOR_BOOSTEES.value,
).set(bera_boostees)

Expand All @@ -249,6 +266,8 @@ def fetch_unboosted(self, network, wallet):
network=network_name,
address=wallet["address"],
name=wallet["name"],
token="BGT",
token_type=TokenType.ERC_20.value,
type=MetricsAccountInfo.UNBOOSTED.value,
).set(bera_unboosted)

Expand All @@ -272,6 +291,8 @@ def fetch_queued_boost(self, network, wallet):
network=network_name,
address=wallet["address"],
name=wallet["name"],
token="BGT",
token_type=TokenType.ERC_20.value,
type=MetricsAccountInfo.QUEUED_BOOST.value,
).set(bera_queued_boost)

Expand Down
5 changes: 5 additions & 0 deletions metrics_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ class NetworkType(Enum):
EVM = "evm"
SUBSTRATE = "substrate"
BERA = "bera"


class TokenType(Enum):
ERC_20 = "erc20"
NATIVE = "native"

0 comments on commit a6b7eca

Please sign in to comment.