from web3 import Web3
import json

url = 'https://kovan.infura.io/v3/NODE'
web3 = Web3(Web3.HTTPProvider(url))

abi = json.loads('[{"ABI"}]')
address = web3.toChecksumAddress(Raw_ADDRESS) 
contract = web3.eth.contract(address=address,abi=ABI)
default_GasPrice = '5'

def firstMethod(Address1,publicKey,privateKey,GasPrice = default_GasPrice ) :

    Address1 = web3.toChecksumAddress(Address1) 
    publicKey = web3.toChecksumAddress(publicKey) 

    if web3.isConnected()  :    
        nonce = web3.eth.get_transaction_count(publicKey) 
        transaction = contract.functions.firstMethod(Address1).buildTransaction({
            'from': publicKey , 
            'gasPrice': web3.toWei(GasPrice, 'gwei'),
            'nonce': nonce ,

when i define address1 and address2 in web3s.py , codes work well but when i call firstMethod() in another file (from Django-Rest View) after import and pass same arguments , it returns :

File "venv/lib/python3.8/site-packages/eth_utils/conversions.py", line 159, in hexstr_if_str
    raise ValueError(
ValueError: when sending a str, it must be a hex string. Got: "'0x6e...24'"

i use infura not etherscan

what should i do ?

1 Answer 1


I can't tell if it solves your problem, But may give you an idea at least.

  1. The value of "'0x6e...24'" is double stringified. Which means there are multiple quotations around your hex string. One of them is excessive, you can remove it or you can avoid it to happen in the first place. It's probably a problem with your view/ui components.

  2. Try to convert your string to a hex string data type. Keep in mind to pass your address without 0x. Then use converted value in the rest of the steps.

    hex_string = bytes.fromhex('2d693f8e73d3a395ff7bb70ee278b5fed9b86564')

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.