Solidity
發送原始交易以呼叫智能合約方法時出現問題?
let provider_url = "https://ropsten.infura.io/v3/d5bca90ec3084aee8abd468fdf876a11"; this.web3Provider = new Web3.providers.HttpProvider(provider_url); window.web3_ = new Web3(this.web3Provider); let account = "0x25bA673A96acadD7A02f4c5834Ba80C1AF6b7758"; let nonce = window.web3_.toHex(window.web3_.eth.getTransactionCount(account)); let myPrivateKey = "****"; let privateKey = new Buffer(myPrivateKey, "hex"); let functionName = "markAttendance"; let types = ["address", "uint", "uint256"]; let args = [attendeeAddress, opinion, date]; let fullName = functionName + "(" + types.join() + ")"; let signature = CryptoJS.SHA3(fullName, {outputLength: 256}).toString(CryptoJS.enc.Hex).slice(0, 8); let dataHex = signature + coder.encodeParams(types, args); let data = "0x" + dataHex; // let nonce = web3.toHex(); let gasPrice = window.web3_.toHex(20000000000); // 20 Gwei let gasLimitHex = window.web3_.toHex(4700000); let rawTx = { 'nonce': nonce, 'gasPrice': gasPrice, 'gasLimit': gasLimitHex, 'from': account, 'to': '0xf77c958bdffee94c3f53eb763cffd646cde336d9', 'data': data, 'value': window.web3_.toHex(window.web3_.toWei("0.5", "ether")), } let tx = new Tx(rawTx); tx.sign(privateKey); tx.serialize(); console.log(tx.validate()); let serializedTx = '0x' + tx.serialize().toString('hex'); console.log(serializedTx); window.web3_.eth.sendRawTransaction(serializedTx, function (err, txHash) { console.log(err, txHash) })
契約
contract MarkAttendance { struct AttendeeDetails { address attendance_giver; address attendee; uint attendance_opinion; uint256 timestamp; uint256 date_of_attendance; } //mapping of structure for storing the attendeeDetails mapping(uint => AttendeeDetails) public attendeeDetails; uint public attendeeDetailsCount; function markAttendance(address _attendee, uint _attendance_opinion, uint256 _date) public { attendeeDetailsCount ++; attendeeDetails[attendeeDetailsCount] = AttendeeDetails(msg.sender, _attendee, _attendance_opinion, now, _date); } }
我收到了 TXHASH,但每次都取消交易。我在做什麼可能是什麼問題?誰能建議我對此的任何解決方案。
如果我從松露控制台呼叫該方法,它工作正常。我得到了成功的交易收據。
您的函式選擇器計算不正確。您正在使用
"uint"
而不是"uint256"
. (前者是後者的別名,但在計算函式選擇器時應始終使用後者。)更改此行:
let types = ["address", "uint", "uint256"];
對此:
let types = ["address", "uint256", "uint256"];
編輯
您還將乙太幣附加到交易中,但您呼叫的函式不是
payable
,所以它不會接受乙太幣。要解決此問題,請刪除該value: ...
行。