Skip to content

Instantly share code, notes, and snippets.

@banteg
Last active August 13, 2021 04:07
Show Gist options
  • Save banteg/adde9491ce5751e227e3e39ad1cb2c18 to your computer and use it in GitHub Desktop.
Save banteg/adde9491ce5751e227e3e39ad1cb2c18 to your computer and use it in GitHub Desktop.
get all tokens a user has interacted with
from web3 import Web3, HTTPProvider
from eth_utils import encode_hex, event_signature_to_log_topic
from eth_abi import encode_single
w3 = Web3(HTTPProvider('http://127.0.0.1:8545', {'timeout': 120}))
transfer_topic = encode_hex(event_signature_to_log_topic('Transfer(address,address,uint256)'))
def get_tokens(address):
address_topic = encode_hex(encode_single('address', address))
filter_common = {'fromBlock': 'earliest', 'toBlock': 'latest', 'address': []}
transfers_from = w3.eth.getLogs({'topics': [transfer_topic, address_topic], **filter_common})
transfers_to = w3.eth.getLogs({'topics': [transfer_topic, None, address_topic], **filter_common})
return {log['address'] for log in transfers_from + transfers_to}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment