Solidity
使用包含數組的事件重載和事件日誌是否存在問題?
我的事件觸發了,但從未記錄最後一個參數。它似乎甚至不存在。這是我的可靠程式碼:
pragma solidity >=0.5.6; contract EventTest { event marketCreated(address[] virtualShareAddresses, address market, uint256 indexed endTime, string description, string extraInfo);//categorical event marketCreated(address[2] virtualShareAddresses, address market, uint256 indexed endTime, string description, string extraInfo);//binary event marketCreated(address[2] virtualShareAddresses, address market, uint256 indexed endTime, string description, string extraInfo, int256 _minPrice, int256 _maxPrice, uint256 _numTicks);//scalar function createYesNoMarket( uint256 _endTime, string memory _description, string memory _extraInfo ) public payable returns (bool) { address[2] memory virtualShareAddresses; address newMarket = address(0); virtualShareAddresses[0] = address(1); virtualShareAddresses[1] = address(2); emit marketCreated(virtualShareAddresses, address(newMarket), _endTime, _description, _extraInfo); return true; } }
這是我使用 web3.js 呼叫事件的方式:
contract.methods.createYesNoMarket(1575158400, "Will it rain on Friday in Sacramento California?", "foo").send({from: '0x5680CA2e3b4F8bC043aCeB31837c89DbA8bFBa75', gas: 6000000}, (error, hash) => { console.log(hash) console.log(error) })
這是我的 event.returnValues 記錄到控制台:
{ '0': [ '0x0000000000000000000000000000000000000001', '0x0000000000000000000000000000000000000002' ], '1': '0x0000000000000000000000000000000000000000', '2': BigNumber { _hex: '0x5de30280' }, '3': 'Will it rain on Friday in Sacramento California?', endTime: BigNumber { _hex: '0x5de30280' }, virtualShareAddresses: [ '0x0000000000000000000000000000000000000001', '0x0000000000000000000000000000000000000002' ], market: '0x0000000000000000000000000000000000000000', description: 'Will it rain on Friday in Sacramento California?' }
如您所見,從不記錄 extraInfo 欄位。有誰知道這是為什麼?
您正在事件日誌中使用事件日誌重載和數組。
通過表面測試,我發現如果您放棄其中任何一個功能,它就會按預期工作。
如果您註釋掉此測試中實際未使用的日誌,那麼情況會有所改善。
pragma solidity >=0.5.6; contract EventTest { //event marketCreated(address[] virtualShareAddresses, address market, uint256 indexed endTime, string description, string extraInfo);//categorical event marketCreated(address[2] virtualShareAddresses, address market, uint256 indexed endTime, string description, string extraInfo);//binary //event marketCreated(address market, uint256 indexed endTime, string description, string extraInfo, int256 _minPrice, int256 _maxPrice, uint256 _numTicks);//scalar function createYesNoMarket( uint256 _endTime, string memory _description, string memory _extraInfo ) public payable returns (bool) { address[2] memory virtualShareAddresses; address newMarket = address(0); virtualShareAddresses[0] = address(1); virtualShareAddresses[1] = address(2); emit marketCreated(virtualShareAddresses, address(newMarket), _endTime, _description, _extraInfo); return true; } }
在 Remix 中,日誌完全符合預期:
[ { "from": "0xba8dc1a692093d8abd34e12aa05a4fe691121bb6", "topic": "0x723cfaf3d6cd591c1439c0b9b08b74eaa46598df92bd434a707f33f48fa8bbd8", "event": "marketCreated", "args": { "0": [ "0x0000000000000000000000000000000000000001", "0x0000000000000000000000000000000000000002" ], "1": "0x0000000000000000000000000000000000000000", "2": "1575158400", "3": "Will it rain on Friday in Sacramento California?", "4": "foo", "virtualShareAddresses": [ "0x0000000000000000000000000000000000000001", "0x0000000000000000000000000000000000000002" ], "market": "0x0000000000000000000000000000000000000000", "endTime": "1575158400", "description": "Will it rain on Friday in Sacramento California?", "extraInfo": "foo", "length": 5 } } ]
使用事件重載,似乎很難解釋日誌:
[ { "from": "0x9876e235a87f520c827317a8987c9e1fde804485", "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000003057696c6c206974207261696e206f6e2046726964617920696e2053616372616d656e746f2043616c69666f726e69613f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000", "topics": [ "0x723cfaf3d6cd591c1439c0b9b08b74eaa46598df92bd434a707f33f48fa8bbd8", "0x000000000000000000000000000000000000000000000000000000005de30280" ] } ]
事件重載應該可以工作。如果刪除了數組元素,則可以顯示它可以工作:
contract EventTest { // event marketCreated(address market, uint256 indexed endTime, string description, string extraInfo);//categorical event marketCreated(address market, uint256 indexed endTime, string description, string extraInfo);//binary event marketCreated(address market, uint256 indexed endTime, string description, string extraInfo, int256 _minPrice, int256 _maxPrice, uint256 _numTicks);//scalar function createYesNoMarket( ... emit marketCreated(address(newMarket), _endTime, _description, _extraInfo);
這也可以按預期工作:
[ { "from": "0xde6a66562c299052b1cfd24abc1dc639d429e1d6", "topic": "0x0d1dda2dfb9ffa94623e4da9d1b79c3d2f94286cf597898c7cb8551e2971b988", "event": "marketCreated", "args": { "0": "0x0000000000000000000000000000000000000000", "1": "1575158400", "2": "Will it rain on Friday in Sacramento California?", "3": "foo", "market": "0x0000000000000000000000000000000000000000", "endTime": "1575158400", "description": "Will it rain on Friday in Sacramento California?", "extraInfo": "foo", "length": 4 } } ]
因此,即使數組沒問題,即使事件重載也沒問題,但將這些功能一起使用似乎確實存在問題。這可能是問題報告的候選者,或者可能有人會提出這些功能相互排斥的已知原因。
希望能幫助到你。