Skip to content

Instantly share code, notes, and snippets.

@i-asimkhan
Created August 1, 2022 19:25
Show Gist options
  • Save i-asimkhan/b588fb53d98376a67d276eee802f0226 to your computer and use it in GitHub Desktop.
Save i-asimkhan/b588fb53d98376a67d276eee802f0226 to your computer and use it in GitHub Desktop.
Solidity Inheritance
pragma solidity >=0.5.0 <0.6.0;
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 memory _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
emit NewZombie(id, _name, _dna);
}
function _generateRandomDna(string memory _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
function createRandomZombie(string memory _name) public {
require(ownerZombieCount[msg.sender] == 0);
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
contract ZombieFeeding is ZombieFactory {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment