Go-Ethereum

觀看合約的所有事件(geth):‘找不到日誌未定義的事件:未定義’

  • July 26, 2017

我正在嘗試查看包含許多事件的契約的所有事件。以下程式碼應該做到這一點:

var events = myContract.allEvents();
events.watch(function(error, event){
   if (error) {
       console.log("Error: " + error);
   } else {
       console.log('some event occured');
   }
});

但是如果我在 geth 中嘗試它並觸發一個事件,這就是控制台告訴我的:

cannot find event for log
undefined: undefined

有人可以告訴我這段程式碼有什麼問題嗎?(我同時執行霧,我只是附上)

請注意,我可以毫無困難地觀看個別事件。相反,我試圖用上面的程式碼觀看它們。

如何myContract定義?有allEvents()事件類型嗎?

我將假設您已經定義了myContract變數,但您正在嘗試使用它allEvents來檢索所有事件。

有關如何檢索合約生成的特定事件的範例,請參閱如何從 The DAO 中檢索已投票的事件。

請參閱web3.eth.filter以使用該filter(...)功能檢索所有事件。


更新回應下面的評論

這是您要檢查的一些程式碼web3.eth.filter(...)

var filter = web3.eth.filter({ address: ["0xc4af56cd5254aef959d4bce2f75874007808b701"], fromBlock: 2615848, toBlock: "latest" });
var i = 0;
filter.watch(function (error, result) {
 console.log("RESULT: Filter " + i++ + ": " + JSON.stringify(result));
});
filter.stopWatching();

產生的結果包括:

RESULT: Filter 0: {"address":"0xc4af56cd5254aef959d4bce2f75874007808b701","blockHash":"0x201154027d3c69cca298fab9983848269eee04cc26a9e1e7f2d06c9e1529b8f5","blockNumber":2615921,"data":"0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f000000000000000000000000a74476443119a942de498590fe1f2454d7d4ac0d00000000000000000000000000000000000000000000000000000000000186a0","logIndex":2,"removed":false,"topics":["0x242ddf37c1721f450a35afdb783ef36e84d032a300600da464ff17d600cceed7"],"transactionHash":"0x6a7f83c93837f4ab11e1bc9dae2a34df4b4610b0d87512e451a36b5f53fb9661","transactionIndex":17}
RESULT: Filter 1: {"address":"0xc4af56cd5254aef959d4bce2f75874007808b701","blockHash":"0x201154027d3c69cca298fab9983848269eee04cc26a9e1e7f2d06c9e1529b8f5","blockNumber":2615921,"data":"0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0000000000000000000000000399156ee3339f4b29a53e307b98cce09fda3bac7","logIndex":3,"removed":false,"topics":["0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b"],"transactionHash":"0x6a7f83c93837f4ab11e1bc9dae2a34df4b4610b0d87512e451a36b5f53fb9661","transactionIndex":17}
RESULT: Filter 2: {"address":"0xc4af56cd5254aef959d4bce2f75874007808b701","blockHash":"0x58fd992519b8bb06b8a8cddac850efad4f2b3b8dd5069a0d0d8f4deb15616b81","blockNumber":2617375,"data":"0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0000000000000000000000000b362bba1c7c5c857e3ac74d740c0007c48db7362","logIndex":5,"removed":false,"topics":["0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b"],"transactionHash":"0xb7ef581cd0e6597525e254add6f820ac6ecf860d89d01656035dfd880dac2747","transactionIndex":6}
...

上述事件是 Golem Network Token (GNT) TokenTraderFactory,原始碼可從0xc4af56cd5254aef959d4bce2f75874007808b701 獲得您可以在Trustless Golem Network Token (GNT) Selling Contract找到有關此 TokenTraderFactory 的更多資訊。

請注意,上述事件中的主題包括以下十六進製字元串:

  • 0x242ddf37c1721f450a35afdb783ef36e84d032a300600da464ff17d600cceed7
  • 0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b

以下是 TokenTraderFactory 合約中聲明的事件:

  • 事件 TradeListing(bytes32 bookid, address owner, address addr);
  • 事件NewBook(bytes32 bookid,地址資產,uint256單位);

geth

> web3.sha3('NewBook(bytes32,address,uint256)')
"0x242ddf37c1721f450a35afdb783ef36e84d032a300600da464ff17d600cceed7"
> web3.sha3('TradeListing(bytes32,address,address)')
"0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b"

您必須自己解釋數據欄位。從上面的第二個清單中,數據是0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0000000000000000000000000399156ee3339f4b29a53e307b98cce09fda3bac7

這可以分為:

0x
2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f
0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0
000000000000000000000000399156ee3339f4b29a53e307b98cce09fda3bac7

在 TokenTraderFactoryevent TradeListing(bytes32 bookid, address owner, address addr)事件中,

bookid = 0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f
owner  = 0x0020dba1d308339182239056a00fcc146d2e26e0
addr   = 0x399156ee3339f4b29a53e307b98cce09fda3bac7

另請記住,如果尚未在本地 Geth 中發現“toBlock”塊編號,則回調將不會觸發任何結果,直到 geth 完成與該塊的同步!

引用自:https://ethereum.stackexchange.com/questions/10740