繁体   English   中英

使用 web3.py python 与 rinkeby 智能合约交互

[英]interaction with rinkeby smart contract using web3.py python

这是我的一段 web3.py 代码。 我已经使用 remix 在 rinkeby 测试网上实现了智能合约。 我可以调用其他函数,但是当我调用事务函数时,出现以下错误。

代码:

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()

错误:

  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'}

在我看来,您正在尝试使用托管节点,就好像它是本地节点一样。 您可以在web3.py 文档中阅读更多关于差异的信息。

简而言之:托管节点(如 Infura、Alchemy 等)上没有eth_sendTransaction ,因为托管节点无权访问您的私钥。

为了进行transact() ,您需要一个由以太币资助的帐户。 该帐户的私钥在哪里? 如果您在 python 中使用它,那么您将需要使用 API 来使用本地密钥签署合同交易

否则,如果私钥在本地节点中,例如 geth,那么您需要正确连接到该节点,可能使用 IPC 连接。 然后一个简单的transact()调用应该可以正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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