Bitcoin Forum
June 28, 2024, 07:54:55 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Solidity Event  (Read 126 times)
DashingAgent (OP)
Member
**
Offline Offline

Activity: 538
Merit: 17

So many books, so little time


View Profile
September 23, 2019, 07:39:49 AM
Last edit: September 23, 2019, 08:14:50 AM by DashingAgent
 #1

If any body passed the chapter 13 on cryptozombie, can he please elaborate regarding how we will write our final contract in cryptozombies chapter 13?
        https://cryptozombies.io/en/lesson/1/chapter/13
It says that
We want an event to let our front-end know every time a new zombie was created, so the app can display it.

Declare an event called NewZombie. It should pass zombieId (a uint), name (a string), and dna (a uint).

Modify the _createZombie function to fire the NewZombie event after adding the new Zombie to our zombies array.

You're going to need the zombie's id. array.push() returns a uint of the new length of the array - and since the first item in an array has index 0, array.push() - 1 will be the index of the zombie we just added. Store the result of zombies.push() - 1 in a uint called id, so you can use this in the NewZombie event in the next line.


Contract.sol
pragma solidity ^0.4.25;

contract ZombieFactory {

    // declare our event here

    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;

    struct Zombie {
        string name;
        uint dna;
    }

    Zombie[] public zombies;

    function _createZombie(string _name, uint _dna) private {
        zombies.push(Zombie(_name, _dna));
        // and fire it here
    }

    function _generateRandomDna(string _str) private view returns (uint) {
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    function createRandomZombie(string _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

I am unable to pass this chapter as I write on line 5 (// declare our event here

event NewZombie(_zombieId, _dna, _name);
function add(_zombieId, _dna) public {
uint _name = _zombieId+_dna;


and on line 15, I modify the array like this

Zombie[] public Newzombie;

I then edit and modify function on line 17-18

 function _createZombie(string _name, uint _dna) private {
        Newzombie.push(Zombie(_name, _dna));

and on line 19 (// and fire it here

emit  _zombieId.Newzombie.push(_name, _dna) -1;
return _name;

but getting the error, can anyone please correct all my mistakes and provide the complete source?

It'll be appreciated if anyone may help

Regards
 

Consider Donations: BTC 17ox7CoDkfwM9bpTXvnhH1YJAy1Cproa7D - Has any whale the power to donate a penny by considering 1000BTC=$1? (Consider "penny")
ETH:: 0xa0ad5e0e8fc86a8440992ad57b201fadefbaf595 (Not for whales, but for crypto lovers to donate me eth or tokens)
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!