Last active
August 13, 2021 04:07
-
-
Save banteg/adde9491ce5751e227e3e39ad1cb2c18 to your computer and use it in GitHub Desktop.
get all tokens a user has interacted with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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