使用Web3.Py Python与Rinkeby智能合约的互动
这是我的Web3.py代码。我已经使用混音在Rinkeby TestNet上实现了智能合约。我可以调用其他功能,但是当我调用Transact功能时,我会遇到以下错误。
代码:
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'}
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'}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您就像您试图使用托管节点一样,就好像它是本地节点一样。您可以阅读有关 web3.py文档。
简而言之:托管节点上没有
eth_sendtransaction
(例如infura,Alchemy等),因为托管节点无法访问您的私钥。为了
Transact()
,您需要一个由Ether资助的帐户。该帐户的私钥在哪里?如果您在Python中有它,则需要将API用于用本地密钥签署合同事务。否则,如果私钥在本地节点(例如Geth)中,则需要正确连接到该节点,可能会使用IPC连接。然后,简单的
transact()
调用应该可以运行良好。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.