3

Following is a very simple contract which implements deposit and withdraw functions. I have defined two events :

  1. deposited(address from, string msg)
  2. withDrawn(address from, string msg)

The contract is as follows:

pragma solidity ^0.4.4;
contract Bank{
uint balance;
address owner;

event deposited(address from, string msg);
event withDrawn(address from, string msg);

function deposit(uint amount){
    balance = balance + amount;
    deposited(msg.sender, "Amount has been deposited successfully");
}

function withDraw(uint amount){
    if(amount >= balance){
        throw;
    }
    else{
        balance = balance - amount;
        withDrawn(msg.sender, "Amount has been withdrawn successfully");
    }
}

function getBalance()constant returns (uint){
    return balance;
}

}

Now, when i run this contract from command prompt using its compiled code, the events are not shown. How should i output them in nodeJS.

1
  • It is a good practice that accepting the answer as correct if it helped you resolved the issue. That is the whole point of SE none of the answers for your questions are marked as correct. FYI, if youre not aware of it.
    – niksmac
    Dec 23, 2016 at 2:53

1 Answer 1

2

You can use events like filters and they have the same methods, but you pass different objects to create the event filter.

Parameters

Object - Indexed return values you want to filter the logs by, e.g. {'valueA': 1, 'valueB': [myFirstAddress, mySecondAddress]}.
Object - Additional filter options, see filters parameter 1 for more.

var event = myContractInstance.MyEvent({valueA: 23} [, additionalFilterObject])

// watch for changes
event.watch(function(error, result){
  if (!error)
    console.log(result);
});

// Or pass a callback to start watching immediately
var event = myContractInstance.MyEvent([{valueA: 23}] [, additionalFilterObject] , function(error, result){
  if (!error)
    console.log(result);
});

Read Contract Events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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