# Learning from crypto zombies lesson 2

By [KevinWang](https://paragraph.com/@kevin-wang) · 2025-08-30

---

Hi all, this is my second crypto zombies lesson homework!

[https://share.cryptozombies.io/zh/lesson/2/share/kevin?id=Y3p8NjIzMTk2](https://share.cryptozombies.io/zh/lesson/2/share/kevin?id=Y3p8NjIzMTk2)

In this section, I will show my new contract and the knowledge behind the code.

    pragma solidity ^0.4.19;
    
    contract ZombieFactory {
    
        event NewZombie(uint zombieId, string name, uint dna);
    
        uint dnaDigits = 16;
        uint dnaModulus = 10 ** dnaDigits;
    
        struct Zombie {
            string name;
            uint dna;
        }
    
        Zombie[] public zombies;
    
        mapping (uint => address) public zombieToOwner;
        mapping (address => uint) ownerZombieCount;
    
        function _createZombie(string _name, uint _dna) internal {
            uint id = zombies.push(Zombie(_name, _dna)) - 1;
            zombieToOwner[id] = msg.sender;
            ownerZombieCount[msg.sender]++;
            NewZombie(id, _name, _dna);
        }
    
        function _generateRandomDna(string _str) private view returns (uint) {
            uint rand = uint(keccak256(_str));
            return rand % dnaModulus;
        }
    
        function createRandomZombie(string _name) public {
            require(ownerZombieCount[msg.sender] == 0);
            uint randDna = _generateRandomDna(_name);
            randDna = randDna - randDna % 100;
            _createZombie(_name, randDna);
        }
    
    }
    

    pragma solidity ^0.4.19;
    import "./zombiefactory.sol";
    contract KittyInterface {
      function getKitty(uint256 _id) external view returns (
        bool isGestating,
        bool isReady,
        uint256 cooldownIndex,
        uint256 nextActionAt,
        uint256 siringWithId,
        uint256 birthTime,
        uint256 matronId,
        uint256 sireId,
        uint256 generation,
        uint256 genes
      );
    }
    contract ZombieFeeding is ZombieFactory {
    
      address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
      KittyInterface kittyContract = KittyInterface(ckAddress);
    
      function feedAndMultiply(uint _zombieId, uint _targetDna, string _species) public {
        require(msg.sender == zombieToOwner[_zombieId]);
        Zombie storage myZombie = zombies[_zombieId];
        _targetDna = _targetDna % dnaModulus;
        uint newDna = (myZombie.dna + _targetDna) / 2;
        if (keccak256(_species) == keccak256("kitty")) {
          newDna = newDna - newDna % 100 + 99;
        }
        _createZombie("NoName", newDna);
      }
    
      function feedOnKitty(uint _zombieId, uint _kittyId) public {
        uint kittyDna;
        (,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
        feedAndMultiply(_zombieId, kittyDna, "kitty");
      }
    
    }
    

Inheritance
===========

    contract ZombieFeeding is ZombieFactory {}
    

When we create a contract with “is”, it is called inheritance.

And in son contract, we can call function or member variables in father contract.

Address
=======

Hexadecimal literals that pass the address checksum test is address.

The msg.sender also return an address.

We can also get a contract on chain by address.

Interface
=========

    contract KittyInterface {
      function getKitty(uint256 _id) external view returns (
        bool isGestating,
        bool isReady,
        uint256 cooldownIndex,
        uint256 nextActionAt,
        uint256 siringWithId,
        uint256 birthTime,
        uint256 matronId,
        uint256 sireId,
        uint256 generation,
        uint256 genes
      );
    }
    

Interface looks like a common contract, but the function in interface don’t have body. The function in interface only has the declaration.

And we can call the contract by interface like this.

    KittyInterface kittyContract = KittyInterface(ckAddress);
     (,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
    

keccak256
=========

In solidity, we can only compare if the strings are equal by keccak256.

keccak256 is a hash algorithm.

---

*Originally published on [KevinWang](https://paragraph.com/@kevin-wang/learning-from-crypto-zombies-lesson-2)*
