简体   繁体   中英

interaction with rinkeby smart contract using web3.py python

Here is my piece of web3.py code. I have implemented the smart contract on rinkeby testnet using remix. I am able to call other functions, but when I am calling the transact function I am getting following error.

CODE:

web3 = Web3(Web3.HTTPProvider(url))


web3.middleware_onion.inject(geth_poa_middleware, layer=0)
print(web3.isConnected())

class SendCoin:
    def send_coin_on_reps(self, reps):
        print(web3.isConnected())
    
    # web3.eth.defaultAccount = web3.eth.accounts[-1]
    # INTRACTING WITH REMIX CONTRACT
    abi = json.load()
    deployed_contract_address = web3.toChecksumAddress('0x40eab3d93EFE536560Ad5802B15EAb56203c3A48')
    
    contract = web3.eth.contract(address = deployed_contract_address, abi = abi)
    
    print(contract)
    reward = contract.functions.getReward().call()
    print("reward = ", reward)

    tx_hash = contract.functions.setReward(reps).transact()

ERROR:

  File "/home/sohail/Blockchain/local_ganache_network_web3_app.py", line 48, in send_coin_on_reps
    tx_hash = contract.functions.setReward(reps).transact()

  File "/home/sohail/anaconda3/lib/python3.9/site-packages/web3/contract.py", line 997, in transact
    return transact_with_contract_function(

  File "/home/sohail/anaconda3/lib/python3.9/site-packages/web3/contract.py", line 1590, in transact_with_contract_function
    txn_hash = web3.eth.send_transaction(transact_transaction)

  File "/home/sohail/anaconda3/lib/python3.9/site-packages/web3/eth.py", line 815, in send_transaction
    return self._send_transaction(transaction)

  File "/home/sohail/anaconda3/lib/python3.9/site-packages/web3/module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,

  File "/home/sohail/anaconda3/lib/python3.9/site-packages/web3/manager.py", line 198, in request_blocking
    return self.formatted_response(response,

  File "/home/sohail/anaconda3/lib/python3.9/site-packages/web3/manager.py", line 171, in formatted_response
    raise ValueError(response["error"])

ValueError: {'code': -32601, 'message': 'The method eth_sendTransaction does not exist/is not available'}

It looks to me like you're trying to use a hosted node as if it were a local node. You can read more about the difference in the web3.py docs .

In short: there is no eth_sendTransaction on a hosted node (like Infura, Alchemy, etc), because hosted nodes don't have access to your private keys.

In order to transact() , you need an account funded with ether. Where is the private key for that account? If you have it in python, then you'll want to use the API for signing a contract transaction with local keys .

Otherwise, if the private key is in a local node, like geth, then you'll need to connect to that correctly, probably using an IPC connection. Then a simple transact() invocation should run fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM