Reading assignment - Solidity Events

  1. How are events declared?
    Events are declared in the contract body using the event keyword.

  2. How can we emit events?
    An event can be emitted in the same way as you would call any function,
    by using its name and passing to it the necessary parameters.

  3. How and where do we listen for events?
    The JavaScript API allows us to listen for events with a dapp’s user interface using a callback to the logs field via the event manager.

1 Like
How are events declared?

in the contract, we can use event keyword to declare events.
for example: event event_name(argument_type arg_name1, argument_type arg_name2,…);

How can we emit events?

to emit the events we need to call them using event’s name.
for example: if we add this into the function we emit this event:
event_name(arg1, arg2);

How and where do we listen for events?

we can listen events using the JavaScript api (front end), by checking the log.

1 Like

How are events declared?
event
How can we emit events?
call it like a function or var.
How and where do we listen for events?
check the EVM log using JavaScript Api

1 Like
  1. How are events declared?
    

Inside an ethereum smart contract have the following

event EventName(
    // up to 3 items can be added to an event
    // these will be logged to the blockchain
    /// and can be monitored by a js API
    dataType indexed _variable,
);
  1. How can we emit events?
    

Inside an ethereum smart contract’s functions call the event function. This will log the desired events to the blockchain.

contact Contract {

	// create an event here and define what to store in it
	event Deposit(
		address indexed _from,
		byte32 indexed _id,
		uint _value
	);

	function deposit(bytes32, _id){
		// do something
		// call the event to log and pass the data to log
		Deposit(param1, param2, param3);

	}
}
  1. How and where do we listen for events?
    

We can use a JS API to monitor the blockchain for certain events within a smart contract

1 Like
  1. Events are declared with the ‘event’ keyword.
  2. You can call an event within a function that’s in the contract.
  3. In the front-end part of the DAPP, you can listen for events which in turn, can have JS callbacks.
1 Like

1. How are events declared?

Events are declared by the syntax:

event eventName(Argument)

2. How can we emit events?

One can emit events by declaring the syntax:

emit emitName(Argument)

3. How and where do we listen for events?

“Through EVM log which call back/post/inform Javascript API UI that EventName has happened.”

1 Like

events

  1. event EventName(Arguments) ;

  2. EventName(Arguments) ;

  3. In the front-end of a dapp you can listen for events using Javascript callbacks (trough EVM logging Facilities) to interact with the contract function.

1 Like

How are events declared --> event EventName (arguments);

How can we emit events --> We can call them like our other functions EventName(arguments);

How and where do we listen for events? --> We can listen for events within JavaScript with callbacks.

1 Like
  1. How are events declared?
    Events are declared like structs by using the keyword event Nameofevent () ;
  2. How can we emit events?
    calling the name of the event by its name given
    like : CallingEvent();
  3. How and where do we listen for events?
    Events allows convenient usage of EVM logging facility which is use to call the javascript callbacks in the dapps that listen for events
1 Like
How are events declared?
  • event ReturnValue(arguments…);

    How can we emit events?

  • events are emitted within a function, by calling the event:
    function x(){
    ReturnValue(parameters…);
    }

    How and where do we listen for events?

  • EVM logging will call the Javascript callback on the front end:

// watch for changes
event.watch(function(error, result){
// result will contain various information
// including the argumets given to the Deposit
// call.
if (!error)
console.log(result);

1 Like

Why in the declaration of an event we use the indexed between address indexed _from. ?

  1. Events are declared with the event keyword followed by the name of the event and it’s parameter list of values to be logged. Three of the parameters may be given the indexed keyword and are then able to be used to filter the events.
event SomethingHappened(address indexed perpetrator, uint indexed typeID);
  1. We can emit events from a contract by making a call to the Event providing the signature’s parameters to be emitted.
function doingSomething() public {
    //Doing stuff
    SomethingHappened(msg.sender, 1);
}
  1. We listen for events in the front end of a Dapp. We use JavaScript and the web3.js library to subscribe and filter a callback function to the events of a contract we are interested in. This function has access the the object emitted from the contract log.
1 Like

How are events declared?
You declare an event with the keyword event MyEventName(address indexed _from , bytes32 indexed _id , uint value); inside our contract.

How can we emit events?
We call the event inside a function that trigger it like emit MyEventName(args);

How and where do we listen for events?
The event is triggered by the user at the front end of our app using callbacks or event.watch.

1 Like

That is just the name of the variable. You can name it pretty much whatever you want. You don’t need the underscore.

  1. They are declared almost exactly as Structs, with some minor differences in brackets and commas - collons: Event paid(address indexed _to, uint indexed _value); - Indexed is used to be able to filter the results in front-end applications
  2. We just write the constructor of the event with all the necessary input data inside, usually after running some function which changed the state of the blockchain
  3. In JavaScript front-end web applications with some kind of listening script
1 Like
  1. Events are declared using the ‘event’ keyword
  2. One can emit events using the event name and arguments:
     Deposit(msg.sender, _id, msg.value);
    
  3. Events are listened using the .watch() and are listened in the front end
1 Like
  1. How are events declared?
  • under the contract event and give a name of the event
  1. How can we emit events?
  • emit Sample();
  1. How and where do we listen for events?
  • “call” JavaScript callbacks in the user interface of a dapp,
1 Like

1.) event eventName();
2.) eventName();
3.) Listening is possible (JS call) in UX of a Dapp (e.g., event.watch())

1 Like
  1. In the Solidity source code, to define an event, you mark it thus by preceding it with the event keyword (similar in usage to the function keyword).

  2. You may fire events from any function using the emit keyword.

  3. Utilize an outside source such as Parity to do so.

1 Like
  • How are events declared?
    We can declare events as follow:
event Deposit(
        address indexed _from,
        bytes32 indexed _id,
        uint _value
    );
  • How can we emit events?
    for Deposit to be called.
        Deposit(msg.sender, _id, msg.value);
  • How and where do we listen for events?
    We listen to events on the front end applications (dapps).
    The use in the JavaScript API would be as follows:
var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at(0x123 /* address */);

var event = clientReceipt.Deposit();

// watch for changes
event.watch(function(error, result){
    // result will contain various information
    // including the argumets given to the Deposit
    // call.
    if (!error)
        console.log(result);
});

// Or pass a callback to start watching immediately
var event = clientReceipt.Deposit(function(error, result) {
    if (!error)
        console.log(result);
});

1 Like