Etherscan API

Access Etherscan API with Mamba

Etherscan website has API for developers to use so they can get information about tokens, accounts, etc, on the Ethereum platform. You must register first to get the API key. As usual, initialize your Mamba project.


$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip install black-mamba
(.venv) $ mkdir etherscan_project
(.venv) $ cd etherscan_project
(.venv) $ mamba init

Edit settings.py file to put the API key.


etherscan = {
    "development": {
        "api_key": "ETHERSCAN_API_KEY"
    }
}

Replace ETHERSCAN_API_KEY with your API key. Then open the Python console in this environment. Import tokens and accounts module from etherscan package.


(.venv) $ python
>>> from black_mamba.etherscan import accounts, tokens

accounts module is mapped to Etherscan Accounts API page. tokens module is mapped to Etherscan Tokens API page. Right now only those two parts of Etherscan API are supported. Pro API is not supported. Work is in progress. You can see the list of methods from the module with dir method.


>>> dir(tokens)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'api_key', 'erc20_account_balance', 'erc20_total_supply', 'etherscan', 'requests', 'server', 'settings', 'settings_directory']
>>> dir(accounts)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'api_key', 'blocks_mined', 'blocks_mined_paginated', 'erc20_token_transfer_events', 'erc20_token_transfer_events_contract_address', 'erc20_token_transfer_events_paginated', 'erc721_token_transfer_events', 'erc721_token_transfer_events_contract_address', 'erc721_token_transfer_events_paginated', 'ether_balance_multi', 'ether_balance_single', 'etherscan', 'internal_transactions', 'internal_transactions_block_range', 'internal_transactions_paginated', 'internal_transactions_transaction_hash', 'normal_transactions', 'normal_transactions_paginated', 'requests', 'server', 'settings', 'settings_directory']

You can get the help of the method which interests you.


>>> help(accounts.ether_balance_single)
Help on function ether_balance_single in module black_mamba.etherscan.accounts:

ether_balance_single(account_address)
    Getting the balance of ETH of an account

    Parameters
    ----------
    account_address : str
      The address of the account
(END)

Let's try some methods.


>>> accounts.erc721_token_transfer_events_paginated("0x6975be450864c02b4613023c2152ee0743572325", page=5, offset=2, sort="desc")
[{'blockNumber': '10182273', 'timeStamp': '1591045556', 'hash': '0x9c6994d57a249e0ed2c4b181b295e21325d2e02c0449e43b3370eb0c84808047', 'nonce': '365', 'blockHash': '0xff13bb31cc9f5534cbb37f57055fa42ebd1fb21bd535ef88ecd6b22b7f07a3f7', 'from': '0x6975be450864c02b4613023c2152ee0743572325', 'contractAddress': '0x0e3a2a1f2146d86a604adc220b4967a898d7fe07', 'to': '0x4ef40d1bf0983899892946830abf99eca2dbc5ce', 'tokenID': '41321815', 'tokenName': 'Gods Unchained Cards', 'tokenSymbol': 'CARD', 'tokenDecimal': '0', 'transactionIndex': '20', 'gas': '5324193', 'gasPrice': '21800000000', 'gasUsed': '3506002', 'cumulativeGasUsed': '4452645', 'input': 'deprecated', 'confirmations': '1602088'}, {'blockNumber': '10162398', 'timeStamp': '1590778520', 'hash': '0x29c38433c6a34c94207bfbb92a681b452a278383e70e5e1143ef8563fe4a5d55', 'nonce': '2046', 'blockHash': '0x2b0caed26528e9b432b0c9eebfd18416507f705fdfc50fd6a4039dae490b957a', 'from': '0x4ef40d1bf0983899892946830abf99eca2dbc5ce', 'contractAddress': '0x0e3a2a1f2146d86a604adc220b4967a898d7fe07', 'to': '0x6975be450864c02b4613023c2152ee0743572325', 'tokenID': '18773842', 'tokenName': 'Gods Unchained Cards', 'tokenSymbol': 'CARD', 'tokenDecimal': '0', 'transactionIndex': '44', 'gas': '446868', 'gasPrice': '12100000000', 'gasUsed': '304056', 'cumulativeGasUsed': '7183730', 'input': 'deprecated', 'confirmations': '1621963'}]
>>> tokens.erc20_total_supply("0x57d90b64a1a57749b0f932f1a3395792e12e7055")
'21265524714464'