Skip to content

Instantly share code, notes, and snippets.

@banteg
Last active January 23, 2019 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banteg/4298259dfce052d99bb8a4313b2377b4 to your computer and use it in GitHub Desktop.
Save banteg/4298259dfce052d99bb8a4313b2377b4 to your computer and use it in GitHub Desktop.

eth historical

installation

you'll need python 3.6+, then install these requirements:

pip3 install click web3

usage

first argument is your address, everything else is optional. specify one or more token contract addresses with -t option. use -b option to specify block number (default: 4832686 mined at 2018-01-01 00:00:02+00:00).

python eth_historical.py <address> -t <token_address> -t <token_address_2> -b <block>

example

python eth_historical.py 0x2a0c0dbecc7e4d658f48e01e3fa353f44050c208 -t 0xcdcfc0f66c522fd086a1b725ea3c0eeb9f9e8814
import click
from web3 import Web3, HTTPProvider
from datetime import datetime, timezone
abi = [{'constant': True,
'inputs': [],
'name': 'name',
'outputs': [{'name': '', 'type': 'string'}],
'payable': False,
'stateMutability': 'view',
'type': 'function'},
{'constant': True,
'inputs': [],
'name': 'decimals',
'outputs': [{'name': '', 'type': 'uint8'}],
'payable': False,
'stateMutability': 'view',
'type': 'function'},
{'constant': True,
'inputs': [{'name': 'who', 'type': 'address'}],
'name': 'balanceOf',
'outputs': [{'name': '', 'type': 'uint256'}],
'payable': False,
'stateMutability': 'view',
'type': 'function'},
{'constant': True,
'inputs': [],
'name': 'symbol',
'outputs': [{'name': '', 'type': 'string'}],
'payable': False,
'stateMutability': 'view',
'type': 'function'}]
@click.command()
@click.argument('address')
@click.option('-t', '--token', multiple=True)
@click.option('-b', '--block', type=click.IntRange(min=0), default=4832686)
def main(address, token, block):
w3 = Web3(HTTPProvider('https://mainnet.infura.io/metamask'))
w3.eth.defaultBlock = block
address = w3.toChecksumAddress(address)
block = w3.eth.getBlock(block)
ts = datetime.fromtimestamp(block.timestamp, tz=timezone.utc)
print(f'block {block.number} ({ts})')
print(f'account: {address}')
ether = w3.fromWei(w3.eth.getBalance(address), 'ether')
print(f'ETH: {ether}')
for t in token:
contract = w3.eth.contract(w3.toChecksumAddress(t), abi=abi)
symbol = contract.functions.symbol().call()
decimals = contract.functions.decimals().call()
balance = contract.functions.balanceOf(address).call() / 10 ** decimals
print(f'{symbol}: {balance}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment