Getting eth_sendTransaction does not exist error while trying to get a token balance

Hi,
I’m trying to check a token balance from a wallet but I keep getting eth_sendTransaction does not exist error. I don’t want to send something just want to check the balance.

My code:

ABI = [
        {
            "constant": True,
            "inputs": [
            {
                "name": "_owner",
                "type": "address",
            },
            ],
            "name": "balanceOf",
            "outputs": [
            {
                "name": "balance",
                "type": "uint256",
            },
            ],
            "payable": False,
            "stateMutability": "view",
            "type": "function",
        },
    ]

def get_wallet(wallet_id: str):
    try:
        connection = Web3(HTTPProvider(f'{ChainConstants.ETHEREUM}{ChainConfig.API_KEY}'))
        token_contract = "<token-contract>"
        if connection.is_connected():
            print("Network connected!")
        contract = connection.eth.contract(abi=ABI, address=token_contract)
        balance = contract.functions.balanceOf(wallet_id).transact()
        print(balance)
        responseDto = ResponseDTO()
        
        return responseDto.dict(), 200

    except Exception as e:
        print(e)
        return {"error": e}, 500
2 Likes

Hello @ege_ardic and welcome to the community.

The reason you are getting the error is due to the fact that you are calling the transact() method on your contract which is using send_transcation() under the hood.

Please see this explained in the web3py docs here: Sending Transactions — web3.py 6.11.3 documentation

Infura does not support the method eth_sendTrasaction eth_sendTransaction | INFURA, hence the error you are receiving even though you are not explicitly calling the method.

An alternative would be to use the call() method instead of transact(). The call method just queries the state of the blockchain without executing a public transaction. Please see the below doc for reference:

https://web3py.readthedocs.io/en/stable/web3.contract.html#web3.contract.ContractFunction.transact

When querying the balanceOf function use the call method instead of transact.
balance = contract.functions.balanceOf(wallet_id).call()

Hope this helps.

Radu | Infura Support

3 Likes

Thank you for your answer but I already tried the call() method and it gives me a different error. I searched the web and changed it to transact() because of it.

The error message:
Could not transact with/call contract function, is contract deployed correctly and chain synced?

External Image

I couldn’t find any solution to this either

2 Likes

You are welcome.
That’s strange, because I got your code working using the call() method. I am testing on this Link token contract deployed on Goerli.
You can see that this test address holds 24 Link token and by running your code with the call() method, 24 is displayed. Please see below, just that the result needs formatting:

External Image

What’s the web3.py version you are running? I am using 5.29.2, although the latest one seems to be version 6.11.3.

2 Likes

Or maybe there’s indeed something wrong with the contract you are querying. On which network is it deployed, can you share a link to it? Does the ABI in your snippet belong to the same contract?

2 Likes

Sorry for the late reply. I think there was nothing wrong with my code just like you said. I kept sending the wrong token or chain address. I tried and get the balance at some point but I’m having another error right now. Anyways I think this solves my problem.
Thanks again

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.