Solidity
如何通過 Python web3.py、w3.eth.sendRawTransaction() 呼叫 write 函式
您好我正在執行 Python web3.py(不是 web3.js)來與合約函式互動:
w3 = Web3(HTTPProvider('http://room1.abc.com:8545/')) txn = ctrtInstance.functions.setzString(zString).buildTransaction() txn['nonce'] = 3643 # I have to add those into txn because Python complains about missing arguments txn['chainId'] = 3 # for Ropsten network signed = w3.eth.account.signTransaction(txn, privateKey) txn_hash = w3.eth.sendRawTransaction(signed.rawTransaction)
我的變數具有以下值:
>>> txn { 'value': 0, 'gas': 33504, 'gasPrice': 1000000000, 'chainId': 3, 'to': '0x5227Dxyzxyzxyz', 'data': '0xb32e420700000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007476f204d616e2100000000000000000000000000000000000000000000000000', 'nonce': 3643 } >>> signed AttrDict({ 'rawTransaction': HexBytes('0xf8ca820e3f8408f0d1808282e0945227d720d8efdcb259c6c79c74f3cfe04dc4d4fa80b864b32e420700000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007476f204d616e21000000000000000000000000000000000000000000000000002aa0d6caf903ed36cbd971612805e0506c8800d8d7d2bf8e96efcbd17ef87ca103dda02875ef8165c191658e91d81c3f1a52e32f0ecb57956a943496290f9bd400080e'), 'hash': HexBytes('0xcd96d8b646eabcaf40955a44b3bfd0bdd8eb8c8baab1d5eb1788802905fedd0d'), 'r': 97153571344605986608608195363439103576254965532977107907893318995927525032925, 's': 18300888055835900205586458475722080051361212052928774052376466183455388993550, 'v': 42 }) >>> txn_hash b'\xcd\x96\xd8\xb6F\xea\xbc\xaf@\x95ZD\xb3\xbf\xd0\xbd\xd8\xeb\x8c\x8b\xaa\xb1\xd5\xeb\x17\x88\x80)\x05\xfe\xdd\r' >>> txn_hash.hex() 'cd96d8b646eabcaf40955a44b3bfd0bdd8eb8c8baab1d5eb1788802905fedd0d'
Python 在廣播時沒有給我一個錯誤,但是 Ropsten EtherScan 沒有顯示我的交易。我已經等了幾分鐘,所以挖礦延遲不是原因。而且顯然這個交易沒有通過,因為在檢查了合約變數值之後,它仍然是舊值。
那麼出了什麼問題呢?請幫忙。謝謝你。
參考:http ://web3py.readthedocs.io/en/latest/web3.eth.html#web3.eth.Eth.sendRawTransaction
您的問題可能是您使用了錯誤的隨機數,而不是手動設置它,您可以在 web3py 中使用:
nonce = w3.eth.getTransactionCount('your account address')
希望這可以幫助