|
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() |