0

Using python 3.11.4, web3 6.9.0

I am using a reduced ERC20 token abi to be able to get decimal & symbol information. Abi is:

ERC20_ABI = [
    {
        "inputs": [],
        "name": "decimals",
        "outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}],
        "stateMutability": "view",
        "type": "function",
        "constant": True
    },
    {
        "inputs": [],
        "name": "symbol",
        "outputs": [{"internalType": "string", "name": "", "type": "string"}],
        "stateMutability": "view",
        "type": "function",
        "constant": True
    }
]

W/ this, code works as expected. However, if I wrap the ABI definition in triple quotes:

ERC20_ABI = """[
    {
        "inputs": [],
        "name": "decimals",
        "outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}],
        "stateMutability": "view",
        "type": "function",
        "constant": True
    },
    {
        "inputs": [],
        "name": "symbol",
        "outputs": [{"internalType": "string", "name": "", "type": "string"}],
        "stateMutability": "view",
        "type": "function",
        "constant": True
    }
]"""

Then I get the following error:

apply_formatters_to_dict
    raise ValueError(new_error_message) from exc
ValueError: Could not format invalid value '[\n    {\n\t\t"inputs": [],\n        "name": "decimals",\n        "outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}],\n        "stateMutability": "view",\n        "type": "function",\n        "constant": True\n    },\n    {\n        "inputs": [],\n        "name": "symbol",\n        "outputs": [{"internalType": "string", "name": "", "type": "string"}],\n        "stateMutability": "view",\n        "type": "function",\n        "constant": True\n    }\n]' as field 'abi'

Does anyone know why this ABI cannot be in JSON format? Not vital that it must be but I am curious why web3 is having trouble decoding it...

2 Answers 2

1

Triple quotes turn your ABI into a string instead of a dictionary. If you want to go the string route, you need to turn the string back into a data structure with something like json.loads (that will parse your JSON)/.

For example:

import json

ERC20_ABI = """[
    {
        "inputs": [],
        "name": "decimals",
        "outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}],
        "stateMutability": "view",
        "type": "function",
        "constant": True
    },
    {
        "inputs": [],
        "name": "symbol",
        "outputs": [{"internalType": "string", "name": "", "type": "string"}],
        "stateMutability": "view",
        "type": "function",
        "constant": True
    }
]"""

ERC20_ABI = json.loads(ERC20_ABI)
1
  • web3 internally does this. I have other ABIs in string format that work fine but specifically this one generates an error. I am curious if there is anything specific to this ABI that causes an error in web3's parser.
    – AdamMuzz
    Commented Sep 30, 2023 at 19:28
0

Turns out it was capitalizing the Booleans! If using json strings need to change "constant": True to "constant": true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.