Reading assignment - Solidity Events

  1. How are events declared?
  2. The syntax used to declare events is as followed

    event EventName (type1 name1, typei namei);
    

    for example the following event is declared when to have the following parameters. Notice we are only declaring the event inside the contract but outside any function. We are not initializing the event or emmiting anythings yet.

    event addedDog(address owner, string name, uint dogId, string breed);
    

  3. How can we emit events?
  4. Events can be emited by calling it within a function using the following syntax

    EventName (value1, valuei);
    

    for example combining both concepts into a dog Deposit Receipt;

    contract DogReceipt {
       event DogDeposit(
           address indexed _from,
           address indexed _to,
           bytes32 indexed _id,
           string _name,
           string _breed,
           uint _value
       );
    
       function dogDeposit(address _to, bytes32 _id, string _name, string _breed){
           DogDeposit(msg.sender, _to, _id, _name, _breed, msg.value);
       }
    }
    
  5. How and where do we listen for events?
  6. We listen for events using JavaScript API.

    for example:

    var abi = /* abi as generated by the compiler */;
    var ContractName = web3.eth.contract(abi);
    var contractName = ContractName.at(0x123 /* address */);
    
    var event = ContractName.EventName();
    
    
    // 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 = contractName.EventName(function(error, result) {
       if (!error)
           console.log(result);
    });
    
    

    Here is the API for our DogReceipt. I just coppied and pasted the API

    var abi = [
    	{
    		"constant": false,
    		"inputs": [
    			{
    				"name": "_to",
    				"type": "address"
    			},
    			{
    				"name": "_id",
    				"type": "bytes32"
    			},
    			{
    				"name": "_name",
    				"type": "string"
    			},
    			{
    				"name": "_breed",
    				"type": "string"
    			}
    		],
    		"name": "dogDeposit",
    		"outputs": [],
    		"payable": false,
    		"stateMutability": "nonpayable",
    		"type": "function"
    	},
    	{
    		"anonymous": false,
    		"inputs": [
    			{
    				"indexed": true,
    				"name": "_from",
    				"type": "address"
    			},
    			{
    				"indexed": true,
    				"name": "_to",
    				"type": "address"
    			},
    			{
    				"indexed": true,
    				"name": "_id",
    				"type": "bytes32"
    			},
    			{
    				"indexed": false,
    				"name": "_name",
    				"type": "string"
    			},
    			{
    				"indexed": false,
    				"name": "_breed",
    				"type": "string"
    			},
    			{
    				"indexed": false,
    				"name": "_value",
    				"type": "uint256"
    			}
    		],
    		"name": "DogDeposit",
    		"type": "event"
    	}
    ];
    var DogReceipt = web3.eth.contract(abi);
    var dogReceipt = DogReceipt.at(0x779831119fe1e7bb6c6dc6f36d2daad38d6e7fe8);
    
    var event = DogReceipt.DogDeposit();
    
    
    // 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 = dogReceipt.DogDeposit(function(error, result) {
       if (!error)
           console.log(result);
    });
    
    

    Contract is live at

1 Like

Just watched the Event video by Philip. In the example above I added the events as a new contract inherited from Kennel. Phili places it inside the contract and function addDog itself. I have revised the code on the gist to include this.

  1. Events are declared with the “event” keyword and then the name of the event afterwards. In parenthesis, you put the arguments inside.
  2. Events are emitted by typing the “eventName (arguments)”.
  3. The dapp is what is used to listen for events.

1- First we write the keyword “event” then write the event name.for example:
event Deposit(arguments);

2- we can emit events by calling them : emit EventName(parameters);

3- Events allow the convenient usage of the EVM logging facilities, which in turn can be used to “call” JavaScript callbacks in the user interface of a dapp, which listen for these events.

2 Likes
  1. How are events declared?

Inside a contract an event is declared as: event EventName(type1 _arg1, type2 _arg2, …);

  1. How can we emit events?

Calling EventName() with appropriate arguments will create an event.

  1. How and where do we listen for events?

We can then listen for an event using the JavaScript API.

1 Like

How are events declared?

Event are declared as a struct, but with the keyword event
event test(
address indexed _from;
bytes32 indexed _id;
uint _value;
);

How can we emit events?

you just need to instantiate an event inside your contract function
Deposit(msg.sender, _id, msg.value);

How and where do we listen for events?

event are called by js callbacks in the user inteface of a dapp
they get called inside a contract function

1 Like
  1. event myEvent (arguments);

  2. myEvent (arguments);

  3. We can then listen for an event using the JavaScript API.

1 Like
  • How are events declared? With the keyboard Event
  • How can we emit events? EventName();
  • How and where do we listen for events? EVM logging facilities can be used to “call” JavaScript callbacks in the user interface of a dapp
1 Like

1. How are events declared?
Events are declared using ‘event’ key word in the smart contract e.g.

event Person(
   string name,
   uint age
);

2. How can we emit events?
We call event in a function by event name and passing any arguments if required. e.g.

Person(_fName, _age);

3. How and where do we listen for events?
Events should be listened in the front end with a callback to perform some operation. Events can be listened using watch() e.g.

var personEvent = {SmartContractName}.Person();
personEvent.watch(function(error, result) {
  //do something here
}
1 Like
  1. Events are declared with the “event” keyword.
  2. Events are emitted by calling the event name.
  3. We listen for events in the user interface of a dapp with the JavaScript callbacks.
1 Like
  1. We use event keyword
  2. By calling an event
  3. Events are part of a transaction and are visible on the blockchain. In JS we can have callbacks that listen for this events in the UI of a dapp.
1 Like
  1. Events are declared using the event keyword:

    event eventName {
    // event parameters
    };
    

    Note the semi-colon at the end of the event declaration

  2. Events are emitted using the emit keyword:

    emit eventName(param1, param2, ...);
    
  3. Events are listened for in the Javascript API using the watch keyword, as follows:

    event.watch(function(error, result){
        if(!error){
            //do something with the results
        }
    })
    
1 Like

Filip…
After doing the reading, watching your video, adding a small bit that I know and guessing (LOL)… I went out on a limb and answered the questions specifically based on our Dog contract… How close did I get?

How are events declared?

in the Dog contract:
event onAddedDog(address owner, string name, uint dogID);

in user interface of a dapp (javascript):
var event = DogContract.onAddedDog();

How can we emit events?

calling the following in the addDog() function of the Dog contract:
emit onAddedDog(owner, _name, id);

or

onAddedDog(owner, _name, id);

in user interface of a dapp:
maybe when user clicks “Add Dog” button in the html page?

How and where do we listen for events?

in user interface of a dapp (javascript):
var event = DogContract.onAddedDog();
event.watch(function(error, result){
…stuff
};

or

var event = DogContract.onAddedDog(function(error, result) {
    if (!error)
        console.log(result);
});
1 Like
  1. event EventName(
    <parameters>
    );

  2. Simply by using emit keyword and passing args to the event call. The syntax is similar to a function call. e.g:
    emit EventName(<args>);

  3. In the JavaScript API of UI of our Dapp.

var event = ourContract.EventName();
event.watch(<callback function>);
1 Like
  1. Events are declared in smart contracts with event keyword and event name, following with parameters for what sort of information to be passed into event, for example: event myEvent(address owner, uint transactionID)

  2. Event is emitted when we call/fire event with name and parameters in code within function, for example:
    code line 1
    code line 2
    myEvent(address, transactionID)
    code line 3
    code line 4

  3. Website JavaScript front-end where back-end is smart contract with event, can react/listen to events using JavaScript callbacks or with using event.watch(function(error, result) {}

1 Like

1. How are events declared?
With the keyword event, followed by the name and its parameters.
2. How can we emit events?
Calling the event by its name and its parameters inside a contract function.
3. How and where do we listen for events?
Events are listened in the front end with a callback to perform some operation.

1 Like

1.How are events declared?
Using the ‘event’ keyword then arguments

2. How can we emit events?
By calling eventName(arguments)

3. How and where do we listen for events?
In a dapp Via JavaScript.

1 Like

How are events declared?
Using the ‘event’ keyword followed by a function-like declaration.

How can we emit events?
By calling the event like a function.

How and where do we listen for events?
In the front-end JavaScript API

1 Like

How are events declared?

 event EventName(args...);

For example

event Payed(address _from, address _to, uint256 _amount);

How can we emit events?

Just calling it like a method Payed(from, to, 10);

How and where do we listen for events?

their output can be seen on the logging using an EVM, or can be used with a callback in javascript.

1 Like

Looks good! Great job.

1 Like