In the documentation and suggested links and this question, it is mentioned that events are bound to methods and fire when a method is called (via a transaction). There is an example of a contract:
contract ClientReceipt {
event Deposit(
address indexed _from,
bytes32 indexed _id,
uint _value
);
function deposit(bytes32 _id) {
// Any call to this function (even deeply nested) can
// be detected from the JavaScript API by filtering
// for `Deposit` to be called.
Deposit(msg.sender, _id, msg.value);
}
}
in which an event is fired every time method deposit() is called. I've tried to change the argument types and written the following, simplified, contract:
contract ClientReceipt {
event Deposit(
uint256 a
);
function deposit(uint256 a) {
// Any call to this function (even deeply nested) can
// be detected from the JavaScript API by filtering
// for `Deposit` to be called.
Deposit(a);
}
}
but I get the error:
TypeError: Cannot read property 'event' of undefined
when deploying and calling the method (in browser-solidity).
- Why doesn't the event fire properly?
- Can a listener be used as a server from Javascript, in that, every time a method is called from the script?
- When an event fires in a function that changes the state of the function, does it fire after the state has been changed (the transaction corresponindg to it was mined)?
Edit:
The Javascript event listener:
var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at(0x123 /* address */);
var event = clientReceipt.Deposit(function(error, result) {
if (!error)
console.log(result);
else {
// Based on the result call another method of the contract
clientReceipt.callMethod({gas: 4700000});
}
});
event
variable is strange. However, when I tried your code, with a minor change to work within Truffle, it just worked as expected.