1

I compiled a contract source below:

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);
    }
}

there is a event which is :event ElementPopped(uint256 _element); I compile the source in https://ethereum.github.io/browser-solidity, I and then paste the contents of Web3 deploy to the geth console.

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);
    }
 })

Then I type these below in the geth console:

> 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)
}

then:

> 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]

why it is [object Object]? and how to show the right result? thank you very much .

3

The result variable in console.log(result) is an object which contains these fields (see the docs):

Object - An event object as follows:

address: String, 32 Bytes - address from which this log originated.

args: Object - The arguments coming from the event.

blockHash: String, 32 Bytes - hash of the block where this log was in. null when its pending.

blockNumber: Number - the block number where this log was in. null when its pending.

logIndex: Number - integer of the log index position in the block.

event: String - The event name.

removed: bool - indicate if the transaction this event was created from was removed from the blockchain (due to orphaned block) or never get to it (due to rejected transaction).

transactionIndex: Number - integer of the transactions index position log was created from.

transactionHash: String, 32 Bytes - hash of the transactions this log was created from.

These can be accessed by result.address, result.args, etc.

You can turn the object into a nice string for printing with console.log(JSON.stringify(result))

  • Thanks for the answer. I'm wondering how we can assign the value returned by console.log(JSON.stringify(result)) to a variable? – Ay. Sep 25 '17 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.