Assignment - Event

  1. Create a listener for the birth event.
  2. Create a popup of a newborn cat whenever you get an event of type birth.

Read about Event listeners in Web3.js: https://web3js.readthedocs.io/en/v1.2.9/web3-eth-contract.html#contract-events

Make sure to take a look at their example code for hints about implementation.

Hint from Filip:
Make sure that the instance variable is defined and initialized when you define your event listener.

1 Like

Event Assignment Code

index.js
var web3 = new Web3(Web3.givenProvider);



var instance;
var user;
var contractAddress = "0x6Ab01DdFF59dc9E6a53CbEE4937d35670468cFD1";

$(document).ready(function(){
    window.ethereum.enable().then(function(accounts) {
        instance = new web3.eth.Contract(abi, contractAddress, {from: accounts[0]});
        user = accounts[0];

        console.log(instance);

        
    })
})

async function createNewBear(){
    var dnaString = getDna();
    console.log(dnaString);
    await instance.methods.createBearGen0(dnaString).send({}, function(error, txHash){
        if (error){
            console.log(error);
        }
        else{
            console.log(txHash);
            alert("Transaction has been sent succesfully!");
            instance.events.Birth({}, function(error, event){
                if(error){
                    alert("Birthproblems!")
                }
                else{
                    console.log(event);
                    alert("A new bear was born!");
                }
                
            });
        }
    })


}

FineTune:

ReturnValues
var web3 = new Web3(Web3.givenProvider);



var instance;
var user;
var contractAddress = "0x9455BfdE053c0E02340b6e0De750ae094Da2981d";

$(document).ready(function(){
    window.ethereum.enable().then(function(accounts) {
        instance = new web3.eth.Contract(abi, contractAddress, {from: accounts[0]});
        user = accounts[0];

        console.log(instance);

        
    })
})

async function createNewBear(){
    var dnaString = getDna();
    console.log(dnaString);
    await instance.methods.createBearGen0(dnaString).send({}, function(error, txHash){
        if (error){
            console.log(error);
        }
        else{
            console.log(txHash);
            instance.events.Birth().on('data', function(event){
                    console.log(event);
                    let owner = event.returnValues.owner;
                    console.log(owner);
                    let bearId = event.returnValues.bearId;
                    console.log(bearId);
                    let mumId = event.returnValues.mumId;
                    console.log(mumId);
                    let dadId = event.returnValues.dadId;
                    console.log(dadId);
                    let genes = event.returnValues.genes;
                    console.log(genes);

                    $("#bearCreated").css("display", "block");
                    $("#bearCreated").text("Bear Id: " + bearId +
                                           " Owner: " + owner + 
                                           " MumId: " + mumId + 
                                           " DadId: " + dadId +
                                           " Genes: " + genes );
            }).on('error', console.error);
        }
    })


}

1 Like