簡體   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