Skip to content

Instantly share code, notes, and snippets.

@dendisuhubdy
Forked from banteg/get_tokens.py
Created August 13, 2021 04:07
Show Gist options
  • Save dendisuhubdy/2110383d33e31bdc2ead101cda830742 to your computer and use it in GitHub Desktop.
Save dendisuhubdy/2110383d33e31bdc2ead101cda830742 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