|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Prometheus Exporter for Etherscan""" |
| 3 | + |
| 4 | +import logging |
| 5 | +import time |
| 6 | +import os |
| 7 | +import json |
| 8 | +import requests |
| 9 | + |
| 10 | +log = logging.getLogger(__package__) |
| 11 | + |
| 12 | + |
| 13 | +class Etherscan: |
| 14 | + """ The EtherscanCollector class """ |
| 15 | + |
| 16 | + accounts = {} |
| 17 | + tokens = {} |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + self.settings = { |
| 21 | + 'api_key': os.environ.get("API_KEY"), |
| 22 | + 'url': os.environ.get("URL", 'https://api.etherscan.io/api'), |
| 23 | + 'addresses': os.environ.get("ADDRESSES"), |
| 24 | + 'tokens': [], |
| 25 | + } |
| 26 | + if not self.settings.get('api_key'): |
| 27 | + raise ValueError("Missing API_KEY environment variable.") |
| 28 | + |
| 29 | + if os.environ.get('TOKENS'): |
| 30 | + self.settings['tokens'] = (json.loads(os.environ.get("TOKENS"))) |
| 31 | + |
| 32 | + def get_tokens(self): |
| 33 | + """ Gets the tokens from an account """ |
| 34 | + log.info('Retrieving the tokens') |
| 35 | + for account in self.accounts: |
| 36 | + for token in self.settings['tokens']: |
| 37 | + log.debug(f"Retrieving the balance for {token['short']} on the account {account}") |
| 38 | + request_data = { |
| 39 | + 'module': 'account', |
| 40 | + 'action': 'tokenbalance', |
| 41 | + 'contractaddress': token['contract'], |
| 42 | + 'address': account, |
| 43 | + 'tag': 'latest', |
| 44 | + 'apikey': self.settings['api_key'], |
| 45 | + } |
| 46 | + decimals = 18 |
| 47 | + if token.get('decimals', -1) >= 0: |
| 48 | + decimals = int(token['decimals']) |
| 49 | + try: |
| 50 | + req = requests.get(self.settings['url'], params=request_data).json() |
| 51 | + except ( |
| 52 | + requests.exceptions.ConnectionError, |
| 53 | + requests.exceptions.ReadTimeout, |
| 54 | + ) as error: |
| 55 | + log.exception(f'Exception caught: {error}') |
| 56 | + req = {} |
| 57 | + if req.get('result') and int(req['result']) > 0: |
| 58 | + self.tokens.update({ |
| 59 | + f"{account}-{token['short']}": { |
| 60 | + 'account': account, |
| 61 | + 'name': token['name'], |
| 62 | + 'name_short': token['short'], |
| 63 | + 'contract_address': token['contract'], |
| 64 | + 'value': int(req['result']) / (10**decimals) if decimals > 0 else int(req['result']) |
| 65 | + } |
| 66 | + }) |
| 67 | + time.sleep(1) # Ensure that we don't get rate limited |
| 68 | + log.debug(f'Tokens: {self.tokens}') |
| 69 | + return self.tokens |
| 70 | + |
| 71 | + def get_balances(self): |
| 72 | + """ Gets the current balance for an account """ |
| 73 | + log.info('Retrieving the account balances') |
| 74 | + request_data = { |
| 75 | + 'module': 'account', |
| 76 | + 'action': 'balancemulti', |
| 77 | + 'address': self.settings['addresses'], |
| 78 | + 'tag': 'latest', |
| 79 | + 'apikey': self.settings['api_key'], |
| 80 | + } |
| 81 | + try: |
| 82 | + req = requests.get(self.settings['url'], params=request_data).json() |
| 83 | + except ( |
| 84 | + requests.exceptions.ConnectionError, |
| 85 | + requests.exceptions.ReadTimeout, |
| 86 | + ) as error: |
| 87 | + log.exception(f'Exception caught: {error}') |
| 88 | + req = {} |
| 89 | + if req.get('message') == 'OK' and req.get('result'): |
| 90 | + for result in req.get('result'): |
| 91 | + self.accounts.update({ |
| 92 | + result['account']: float(result['balance'])/(1000000000000000000) |
| 93 | + }) |
| 94 | + log.debug(f'Accounts: {self.accounts}') |
| 95 | + return self.accounts |
0 commit comments