0

I have a geth client running (private) and I have a simple test contract to test events.

> con4.changedAge().watch()
{
  callbacks: [undefined],
  filterId: "0x3cf5f07c90dd95c5d97276659bf78d0f",
  getLogsCallbacks: [],
  implementation: {
    getLogs: function(),
    newFilter: function(),
    poll: function(),
    uninstallFilter: function()
  },
  options: {
    address: "0xa6b4017e8437ace3301f9751735e84836cfc0b21",
    from: undefined,
    fromBlock: undefined,
    to: undefined,
    toBlock: undefined,
    topics:     
["0x25d30a71a6b93533225c97277375c3d49d3451741fad4852f106d04a5f083b5a"]
  },
  pollFilters: [],
  requestManager: {
    polls: {
      0x3cf5f07c90dd95c5d97276659bf78d0f: {
        data: {...},
        id: "0x3cf5f07c90dd95c5d97276659bf78d0f",
        callback: function(error, messages),
        uninstall: function()
      },
      0x49268254732d941a071b3c422d35104e: {
        data: {...},
        id: "0x49268254732d941a071b3c422d35104e",
        callback: function(error, messages),
        uninstall: function()
      }
    },
    provider: {
      newAccount: function(),
      openWallet: 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)

}

Here is the contract -

pragma solidity ^0.4.18;

contract CollegeAdmin {
uint public studentAge=18;
event changedAge(uint age);
function setAge(uint age) public {
studentAge = age;
changedAge(age);
}

} 

I just want to test when the event is triggered. I thought something like con4.setAge(19) could then trigger the watch. Has anyone any ideas?

0

You can call this:

var changedAgeEvent = contract.changedAge();

then:

changedAgeEvent.watch(function(error, result){
            if (!error)
                {
                    console.log(result);
                } else {
                    console.log(error);
                }
        });

it will trigger and log the event variables to the console each time changedAge is triggered.

0

watch is a async method. Checkout below code for more details.

$> ageCallback = con4.changedAge()

Will save ageCallback method ref.Which will have a watch() method. Watch method will take a function as a callback.

$> ageCallback.watch(function(error, result){

     if(error){
       console.log("Error", error);
       return;
     }
    console.log("Result", result);
});

So final code without 2nd variable:

$> con4.changedAge().watch(function(error, result){
     if(error){
       console.log("Error", error);
       return;
     }
    console.log("Result", result);
});
3
0

This worked for me -

var myEvent = con.changedAge({}, {fromBlock: 0, toBlock: 'latest'});
r=1
myEvent.watch(function(error, result){
 console.log(result)
r=result.args.age
console.log(r)
});

docs here - https://github.com/ethereum/wiki/wiki/JavaScript-API#callback-return

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.