TP Blockchain Ethereum - Part II

Introduction to Events

In a Smart Contract, a called function can emit an event (event). The events in addition to being registered in the blockchain, can be retrieved by a Listener in the front-end application. This allows for example to notify the user that a transaction has taken place.

For the following work we will use the ethers.js library (instead of web3.js). The installation is done with the command npm i ethers. (Be careful with the Lyon 1 proxy: npm config set proxy "http://proxy.univ-lyon1.fr:3128" ).

Asynchronous programming

In order to better understand the following exercises it is important to understand the asynchronous programming mode of JavaScript (Promise, async/await). Here is a first example that allows you to retrieve the number of the last mined block. This piece of code can be executed directly in the console of node or with the command node followed by the name of the javascript file.

const ethers = require('ethers');
const provider = ethers.getDefaultProvider(); // homstead (mainnet)

provider.getBlockNumber()
        .then(blockNumber => {
                console.log("Block number : " + blockNumber);
        });

or with the help of Infura

const ethers = require('ethers');
const provider = new ethers.providers.InfuraProvider('homestead','64da4byourprojectid.....');

provider.getBlockNumber()
        .then(blockNumber => {
                console.log("Block number : " + blockNumber);
        });

Let's observe the evolution of the blockchain

The first step consists to observe in "real time" the creation of new blocks in the homestead blockchain (mainnet). The appearance of a new block is an event in itself. Let's say the file index.js.

const ethers = require('ethers');
const provider = ethers.getDefaultProvider(); // homstead (mainnet)

provider.on('block', (blockNumber) => {
    console.log('-- New Block: ' + blockNumber);
});

Let's observe the birth of cats (CK)

CryptoKitties

There is a Smart Contract (ERC-721) at the address 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d which allows to collect CryptoKitties (electronic cat). The front-end application (https://www.cryptokitties.co/) allows users to buy, sell (speculation) or to reproduce them in order to create new generations of "cats". The source code of the Smart Contract that allows this is available at 0x06012c8cf97bead5deae237070f9587f8e7a266d.

If you look at the Solidity code of this Smart Contract (lines 226 and 415) you will notice that a event is emitted each time a "cat" is born. The following JavaScript source code source code allows to observe the birth of the cats in real time.

const ethers = require('ethers');
const provider = ethers.getDefaultProvider(); // homstead (mainnet)

let abi = [
    "event Birth(address owner, uint256 kittyId, uint256 matronId, uint256 sireId, uint256 genes)"
];
let contractAddress = "0x06012c8cf97BEaD5deAe237070F9587f8E7A266d";
let contract = new ethers.Contract(contractAddress, abi, provider);

contract.on("Birth", (owner, kittyId, matronId, sireId, genes , event) => {
    console.log("# block # : " + event.blockNumber);
    console.log("# Owner : "   + owner);
    console.log("# kittyId : " + kittyId);
});

You may have to wait a few long minutes before a "happy event" occurs. Then, with the owner's address and etherscan.io you should be able to observe the transaction on the blockchain, its cost and especially what the newborn "cat" looks like (see Inventory tab).

Work to be done:

  1. Write a piece of code that displays the number of each new mined block, and
  2. if this block contains the birth of a cat, display the Ethereum address of the address of its owner and the Id of the kitten.

It's up to you to observe! (ERC-20)

ERC-20 contracts define a Transfer(address indexed _from, address indexed _to, uint _value) called by all functions transferring from one account to another (i.e. transfer(...) and transferFrom(...)).

Choose the token of your choice (e.g. BAT, ZRX, ...) and using the previous examples, write a write a piece of Javascript code that displays the number of Token transmitted (value) between the accounts in real time.

Work to be done :

  1. Write a piece of code that displays the number of each new mined block
  2. If this new block contains a transaction of the Token you have chosen, take the opportunity to display the address of the sender and the address of the receiver as well as the value transmitted.
  3. Why did we not need to get any Ethers beforehand ?

Reflection work:

Compare the cost in units of GAS of the OP_CODE SSTORE and the OP_CODE LOG (used by the emit function of an event):

  1. What does this tell you?
  2. What is the limitation?