Based on
Cryptozombies.ioZombiefactory
pragma solidity ^0.4.19;contract zombiefactory {///events, Web3.js can monitor it event Newzombie (UINT Zombieid, string name, UI NT DNA); UINT dnadigits = 16; UINT Dnamodulus = ten * * dnadigits; exponentiation//defines struct struct Zombie {string name; UINT DNA; }//define array zombie[] public zombies; Define the mapping structure, which can be understood as Python dict mapping (UINT = address) public zombietoowner; Mapping (address = uint) Ownerzombiecount; function _createzombie (string _name, uint _dna) Internal {UINT ID = Zombies.push (Zombie (_name, _dna))-1;//Get just The ID of the element placed into the array//Msg.sender is the address of the caller. Zombietoowner[id] = Msg.sender; ownerzombiecount[msg.sender]++; Trigger event 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 {//requires everyAn account can only have one zombie, otherwise exit require (ownerzombiecount[msg.sender] = = 0); UINT Randdna = _generaterandomdna (_name); Randdna = randdna-randdna% 100; _createzombie (_name, Randdna); }}
I learned that.
- Definition of a function
- Use of arrays
- Use of mapping
- Use of require
- Use of events
Zombiefeeding
pragma solidity ^0.4.19;import "./zombiefactory.sol";//define a contract interface in such a way that the public methods of other contracts can be called 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 ge Neration, uint256 genes);} Inherit contract zombiefeeding is zombiefactory {kittyinterface kittycontract; The function modifier is used to ensure that only the contract account itself can call the method function setkittycontractaddress (address _address) external Onlyowner {//instantiate interface by contract address , the method kittycontract = Kittyinterface (_address) that can be used to invoke the contract later through this interface; }//transport struct pointer function _triggercooldown (Zombie storage _zombie) Internal {_zombie.readytime = UInt32 (now + cooldownti ME); }//returns BOOL type function _isready (Zombie storage _zombie) Internal view Returns (BOOL) {return (_zombie.readytime <= now); } function feedandmultiply (UINT _zombieid, uint _targetdna, string species) internAl {require (Msg.sender = = Zombietoowner[_zombieid]); Zombie storage Myzombie = Zombies[_zombieid]; Require (_isready (Myzombie)); _targetdna = _targetdna% Dnamodulus; UINT Newdna = (Myzombie.dna + _targetdna)/2; if (keccak256 (species) = = keccak256 ("kitty")) {///if statement using Newdna = Newdna-newdna% 100 + 99; } _createzombie ("NoName", Newdna); _triggercooldown (Myzombie); } function Feedonkitty (UINT _zombieid, uint _kittyid) public {uint Kittydna; method to invoke other contracts (,,,,,,,,, Kittydna) = Kittycontract.getkitty (_kittyid); Multiple return values obtained by feedandmultiply (_zombieid, Kittydna, "Kitty"); }}
I learned that.
- Methods to invoke other contracts
- Structural Mass Transfer value
- Methods to receive multiple return values
- Use of function modifiers
Zombiehelper
pragma solidity ^0.4.19;import "./zombiefeeding.sol"; contract Zombiehelper is zombiefeeding {uint Levelupfee = 0.001 eth Er Defines a modifier function that calls modifier abovelevel (UINT _level, uint _zombieid) before the modified function call {//If the specified zombie level is less than _level, it exits, otherwise the modified function continues to execute Require (Zombies[_zombieid].level >= _level); _; }//used to present the ETH function withdraw () external Onlyowner {Owner.transfer (this.balance) inside the Ethereum; }//function Setlevelupfee (UINT _fee) external Onlyowner {levelupfee = _fee; }//payable modifier indicates that you can send the ETH function levelUp (UINT _zombieid) external payable {require (Msg.value = = Levelupfee) to this method ; Determine the value of the ETH zombies[_zombieid].level++; }//modifier is used when the level is greater than 2 o'clock to modify the name function ChangeName (UINT _zombieid, string _newname) external Abovelevel (2, _zombieid) { require (Msg.sender = = Zombietoowner[_zombieid]); Zombies[_zombieid].name = _newname; } function Changedna (UINT _zombieid, uint _newdna) external abovelevel (_zombieid) {require (Msg.sender = Zombieto Owner[_zombieid]); Zombies[_zombieid].dna = _newdna; }//Returns a list function Getzombiesbyowner (address _owner) external view Returns (Uint[]) {//define memory array, save gas u int[] Memory result = new uint[] (Ownerzombiecount[_owner]); UINT counter = 0; for (UINT i = 0; i < zombies.length; i++) {if (zombietoowner[i] = = _owner) {Result[counter] = i; counter++; }} return result; }}
I learned that.
- Defining modifier functions and passing parameters to the modifier functions
- Receive, extract
eth
- Return
uint[]
memory
variables, for
use of loops
Followed SafeMath
by the use of
pragma solidity ^0.4.19;import "./zombieattack.sol"; import "./erc721.sol"; import "./safemath.sol"; contract Zombieownership is Zombieattack, ERC721 {using Safemath for uint256; Mapping (UINT = address) zombieapprovals; function balanceof (address _owner) public view Returns (Uint256 _balance) {return ownerzombiecount[_owner]; } function Ownerof (uint256 _tokenid) public view Returns (address _owner) {return Zombietoowner[_tokenid]; } function _transfer (address _from, address _to, uint256 _tokenid) Private {ownerzombiecount[_to] = ownerzombiecount[ _to].add (1); Ownerzombiecount[msg.sender] = ownerzombiecount[msg.sender].sub (1); Zombietoowner[_tokenid] = _to; Transfer (_from, _to, _tokenid); } function Transfer (address _to, uint256 _tokenid) public onlyownerof (_tokenid) {_transfer (Msg.sender, _to, _tokenid) ; } function approve (address _to, uint256 _tokenid) public onlyownerof (_tokenid) {Zombieapprovals[_tokenid] = _to; Approval (Msg.sender, _to,_tokenid); } function TakeOwnership (uint256 _tokenid) public {require (zombieapprovals[_tokenid] = = Msg.sender); Address owner = ownerof (_tokenid); _transfer (owner, Msg.sender, _tokenid); }}
Solidity Grammar Learning