Search code examples
web3py

Can we get token name from web3.py by providing the contract address?


For example, can we get 'USDC' returned by providing its contract address 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48?


Solution

  • You can use Web3.py module:

    from web3 import Web3
    import json
    
    web3 = Web3(Web3.HTTPProvider('https://rpc.ankr.com/eth'))
    
    abi = [{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]
    address = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
       
    contract = web3.eth.contract(address , abi = abi)
    
    token_name = contract.functions.name().call() 
    token_symbol = contract.functions.symbol().call() 
    
    print('Name:', token_name)
    print('Symbol:', token_symbol)
    

    For more examples, you can refer this docs.