Solidity
帶有字元串參數的事件不起作用(未編入索引)
我正在嘗試收聽我在本地執行的 rinkeby 測試中的一個事件。當我的參數是 int 時,每件事都可以正常工作,例如
event EventTester(int values);
當我將事件參數更改為字元串(它沒有被索引)時,它不起作用並且我看不到日誌。event EventTester(string values);
. 知道為什麼嗎?當我檢查https://rinkeby.etherscan.io上的交易時,我沒有看到“事件日誌”選項卡。謝謝
var fs = require('fs'); var Web3 = require('web3'); var util = require('ethereumjs-util'); var tx = require('ethereumjs-tx'); var lightwallet = require('eth-lightwallet'); var txutils = lightwallet.txutils; var web3 = new Web3( new Web3.providers.HttpProvider('http://localhost:8545') ); var address = '0x.....'; var key = ''; var bytecode = '6060...90029'; var interface = [{"constant":false,"inputs":[{"name":"input","type":"string"}],"name":"getOut","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"values","type":"string"}],"name":"EventTester","type":"event"}] function sendRaw(rawTx) { var privateKey = new Buffer(key, 'hex'); var transaction = new tx(rawTx); transaction.sign(privateKey); var serializedTx = transaction.serialize().toString('hex'); web3.eth.sendRawTransaction( '0x' + serializedTx, function(err, result) { if(err) { console.log("ERROR HAPPENED:" +err); } }); } var contractAddress = '0x...'; strInterface='[{"constant":false,"inputs":[{"name":"input","type":"string"}],"name":"getOut","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"values","type":"string"}],"name":"EventTester","type":"event"}]' abiDefinition = JSON.parse(strInterface) myContract = web3.eth.contract(abiDefinition) var contractInstance = myContract.at(contractAddress) var txOptions = { nonce: web3.toHex(web3.eth.getTransactionCount(address)), gasLimit: web3.toHex(700000), gasPrice: web3.toHex(10000000000), to: contractAddress }; var rawTx = txutils.functionTx(interface, 'getOut', ["hi"], txOptions); sendRaw(rawTx);
我剛剛在 Ropsten 上試了一下,似乎正在工作。這是交易:https ://gist.github.com/pabloruiz55/d73fabb23677f146c58d8c4d3950c0fa
您可以在日誌中看到我發送了一個字元串“hola”。
我使用的程式碼和你寫的完全一樣,在 Remix 上測試過。
(無法在 Rinkeby 上測試它,因為我剛用完 Ether,但如果你給我發一些,我很樂意試一試:0x0fC20E774EA6674087494a36F8F8f09a4Ee3a131)
更新:在 Rinkeby 上也能正常工作。 https://rinkeby.etherscan.io/tx/0x5cf1cfd9c401b9b2d2c0d7a7d967f50e33aaa9bf9cfc97afad9f90b247a93ee9#eventlog
如果這不是使用 web3 的限制,我建議您使用它來呼叫該方法,這將避免您出現許多常見錯誤(已在包裝器中解決)。
使用 web3@^0.20.6:
contractInstance.getOut.sendTransaction('hi', (err, result) => { if (err) { console.error(err); } console.log(result); });
與 web3@^1.0.0
contractInstance.methods.getOut('hi').send({ from: address }).then((result) => { console.log(result); }) .catch((err) => { console.error(err); });