Skip to content

Instantly share code, notes, and snippets.

@roberto-butti
Last active May 28, 2018 13:32
Show Gist options
  • Save roberto-butti/6ac9a190fbce8d57be26d3465c85049e to your computer and use it in GitHub Desktop.
Save roberto-butti/6ac9a190fbce8d57be26d3465c85049e to your computer and use it in GitHub Desktop.
Sample Solidity file
pragma solidity ^0.4.19;
// 1. Import here
import "./ownable.sol";
// 2. Inherit here:
contract ZombieFactory is Ownable {
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment