Skip to content

Instantly share code, notes, and snippets.

@avirajkhare00
Created July 19, 2021 03:20
Show Gist options
  • Save avirajkhare00/587e18dc92a1950a17c4896c0c97df8d to your computer and use it in GitHub Desktop.
Save avirajkhare00/587e18dc92a1950a17c4896c0c97df8d to your computer and use it in GitHub Desktop.
ZombieFactory smart contract
pragma solidity >=0.5.0 <0.6.0;
contract ZombieFactory {
// declare our event here
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
function _createZombie(string memory _name, uint _dna) private {
// and fire it here
uint id = zombies.push(Zombie(_name, _dna)) - 1;
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 {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment