Solidity

事件觀察者給出objectObject這bj和C噸這bj和C噸object Object在 Geth 控制台中

  • September 18, 2020

我在下面編譯了一個契約源:

pragma solidity ^0.4.0;
////////////////////////////////////////////////////////////
// This is an example contract hacked together at a meetup.
// It is by far not complete and only used to show some
// features of Solidity.
////////////////////////////////////////////////////////////
contract queue
{
   Queue requests;

   event ElementPopped(uint256 _element); 
   event ElementPushed(uint256 _element, uint256 _index); 

   function queue() {
       requests.data.length = 200;
   }

   struct Queue {
       uint256[] data;
       uint256 front;
       uint256 back;
   }
   /// @dev the number of elements stored in the queue.
   function length(Queue storage q) constant internal returns (uint256) {
       return q.back - q.front;
   }
   /// @dev the number of elements this queue can hold
   function capacity(Queue storage q) constant internal returns (uint256) {
       return q.data.length - 1;
   }

   function isOverlapped(Queue storage q) internal returns (bool) {
       return (q.back + 1) % q.data.length == q.front;
   }

   /// @dev push a new element to the back of the queue
   function push(Queue storage q, uint256 data) internal {
       if (isOverlapped(q)) throw;
       q.data[q.back] = data;

       ElementPushed(data, q.back);

       q.back = (q.back + 1) % q.data.length;
   }


   /// @dev remove and return the element at the front of the queue
   function pop(Queue storage q) internal returns (uint256 r)
   {
       if (q.back == q.front)
           return; // throw;
       r = q.data[q.front];
       delete q.data[q.front];
       q.front = (q.front + 1) % q.data.length;
       return r;
   }


   function addRequest(uint256 d) {
       push(requests, d);
   }

   function popRequest() {
       ElementPopped(pop(requests));
   }

   function queueLength() constant returns (uint256) {
       return length(requests);
   }
}

有一個事件是:event ElementPopped(uint256 _element); 我在https://ethereum.github.io/browser-solidity編譯原始碼,然後將 Web3 deploy 的內容粘貼到 geth 控制台。

var queue_sol_queueContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"d","type":"uint256"}],"name":"addRequest","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"queueLength","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"popRequest","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_element","type":"uint256"}],"name":"ElementPopped","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_element","type":"uint256"},{"indexed":false,"name":"_index","type":"uint256"}],"name":"ElementPushed","type":"event"}]);
var queue_sol_queue = queue_sol_queueContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000c57fe5b5b60c860006000018161001f9190610026565b505b610077565b81548183558181151161004d5781836000526020600020918201910161004c9190610052565b5b505050565b61007491905b80821115610070576000816000905550600101610058565b5090565b90565b6102b0806100866000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634ca1fad814610051578063ab91c7b014610071578063e4690a0b14610097575bfe5b341561005957fe5b61006f60048080359060200190919050506100a9565b005b341561007957fe5b6100816100b8565b6040518082815260200191505060405180910390f35b341561009f57fe5b6100a76100ca565b005b6100b460008261010d565b5b50565b60006100c460006101b4565b90505b90565b7fe32433683389a476fe90f93c07386d118310387cc5442554f0f6c2cc839c59426100f560006101c9565b6040518082815260200191505060405180910390a15b565b6101168261025a565b156101215760006000fd5b8082600001836002015481548110151561013757fe5b906000526020600020900160005b50819055507fef848106789acd461bcac88ec81e355ecd41e4be7d7491a567cca53a6cbcec96818360020154604051808381526020018281526020019250505060405180910390a1816000018054905060018360020154018115156101a657fe5b0682600201819055505b5050565b6000816001015482600201540390505b919050565b60008160010154826002015414156101e057610255565b8160000182600101548154811015156101f557fe5b906000526020600020900160005b5054905081600001826001015481548110151561021c57fe5b906000526020600020900160005b50600090558160000180549050600183600101540181151561024857fe5b0682600101819055508090505b919050565b600081600101548260000180549050600184600201540181151561027a57fe5b061490505b9190505600a165627a7a7230582025c3f2cf14912bcd818ae8b84356a11a18211d3aca9b44e6ade995ff784c95800029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

然後我在 geth 控制台中輸入以下內容:

> var event = queue_sol_queue.ElementPopped();
undefined
>event.watch(function(error, result){
   // result will contain various information
   // including the argumets given to the Deposit
   // call.
   if (!error)
       console.log(result);
});
{
 callbacks: [function(error, result)],
 filterId: "0x41099921185e90179ba1ac1ff38a91bf",
 getLogsCallbacks: [],
 implementation: {
   getLogs: function(),
   newFilter: function(),
   poll: function(),
   uninstallFilter: function()
 },
 options: {
   address: "0x8bed67280f46cc1ffd401cdb3dd5909f254c8f34",
   from: undefined,
   fromBlock: undefined,
   to: undefined,
   toBlock: undefined,
   topics: ["0xe32433683389a476fe90f93c07386d118310387cc5442554f0f6c2cc839c5942                                                                                        "]
 },
 pollFilters: [],
 requestManager: {
   polls: {
     0x41099921185e90179ba1ac1ff38a91bf: {
       data: {...},
       id: "0x41099921185e90179ba1ac1ff38a91bf",
       callback: function(error, messages),
       uninstall: function()
     }
   },
   provider: {
     newAccount: function(),
     send: function github.com/ethereum/go-ethereum/console.(*bridge).Send-fm()                                                                                        ,
     sendAsync: function github.com/ethereum/go-ethereum/console.(*bridge).Send                                                                                        -fm(),
     sign: function(),
     unlockAccount: function()
   },
   timeout: {},
   poll: function(),
   reset: function(keepIsSyncing),
   send: function(data),
   sendAsync: function(data, callback),
   sendBatch: function(data, callback),
   setProvider: function(p),
   startPolling: function(data, pollId, callback, uninstall),
   stopPolling: function(pollId)
 },
 formatter: function(),
 get: function(callback),
 stopWatching: function(callback),
 watch: function(callback)
}

然後:

> queue_sol_queue.addRequest(3,   { from: web3.eth.accounts[0] })

"0xe95758f98cda307dc602ff4c6e923a1c61df37a39256436129768bb0d4911d7a"
> queue_sol_queue.addRequest(3,   { from: web3.eth.accounts[0] })
"0x21f70350015d0a3d8200d6fcc11534e66028e5861740d113c54e279266ee3bee"
> queue_sol_queue.addRequest(3,   { from: web3.eth.accounts[0] })
"0x9f2a01aaf63d25dcd9f06b3d2edf65c14a5d44f1bc6d658777b7b68bb6697248"
> queue_sol_queue.addRequest(3,   { from: web3.eth.accounts[0] })
"0x1403a55b839759cc6036790d56d8989d0966013d855bb382db03efff1e5eb5c0"
> queue_sol_queue.popRequest(   { from: web3.eth.accounts[0] })

"0x78225091797345db0810e587425a9f79cb364c4cdb0af60ff2df44eed77a137d"
> [object Object]
> queue_sol_queue.popRequest(   { from: web3.eth.accounts[0] })
"0xc812830b2ed2c6aedbaeb7811cef51dd662eaed1c6ad28855e57571f782648e3"
> [object Object]

為什麼會這樣

$$ object Object $$? 以及如何顯示正確的結果?非常感謝你 。

result變數 inconsole.log(result)是一個包含這些欄位的對象(請參閱文件

Object - 一個事件對象,如下所示:

地址:字元串,32 字節 - 此日誌的來源地址。

args: Object - 來自事件的參數。

blockHash: String, 32 Bytes - 此日誌所在塊的雜湊值。當它掛起時為空。

blockNumber: Number - 此日誌所在的塊號。當它掛起時為空。

logIndex: Number - 塊中日誌索引位置的整數。

event: String - 事件名稱。

remove: bool - 指示創建此事件的交易是否已從區塊鏈中刪除(由於孤立塊)或永遠不會訪問它(由於拒絕交易)。

transactionIndex: Number - 創建事務索引位置日誌的整數。

transactionHash:字元串,32 字節 - 創建此日誌的事務的雜湊。

這些可以通過result.address,result.args等訪問。

您可以將對像變成一個漂亮的字元串以進行列印console.log(JSON.stringify(result))

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