Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Solidity Zombies

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Solidity Zombies". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Solidity Zombies.

ZombieFactory.sol

Pragma solidity ^ 0.4.19 × import ". / ownable.sol"; contract ZombieFactory is Ownable {event NewZombie (uint zombieId, string name, uint dna); uint dnaDigits = 16; uint dnaModulus = 10 * * dnaDigits; uint cooldownTime = 1 days; struct Zombie {string name; uint dna; uint32 level; uint32 readyTime;} 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, uint32 (now + cooldownTime))-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);}}

Ownable is an Ownable contract from the OpenZeppelin Solidity library. Events are a mechanism for communication between contracts and blockchain. Your front-end application "listens" to certain events and reacts. Such as:

Var abi = / / abi is generated by the compiler var ZombieFactoryContract = web3.eth.contract (abi) var contractAddress = / contract address var ZombieFactory = ZombieFactoryContract.at (contractAddress) / / ZombieFactory generated on Ethernet Square after release can access public functions and events / / listen for NewZombie events, and update UI var event = ZombieFactory.NewZombie (function (error, result) {if (error) return generateZombie (result.zombieId, result.name, result.dna)})

ZombieFeeding.sol

Pragma solidity ^ 0.4.19 X 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 {KittyInterface kittyContract; modifier ownerOf (uint _ zombieId) {require (msg.sender = zombieToOwner [_ zombieId]) _;} function setKittyContractAddress (address _ address) external onlyOwner {kittyContract = KittyInterface (_ address);} function _ triggerCooldown (Zombie storage _ zombie) internal {_ zombie.readyTime = uint32 (now + cooldownTime);} function _ isReady (Zombie storage _ zombie) internal view returns (bool) {return (_ zombie.readyTime = _ level); _;} function withdraw () external onlyOwner {owner.transfer (this.balance) } function setLevelUpFee (uint _ fee) external onlyOwner {levelUpFee = _ fee;} function levelUp (uint _ zombieId) external payable {require (msg.value = = levelUpFee); zombies [_ zombieId] .level + +;} function changeName (uint _ zombieId, string _ newName) external aboveLevel (2, _ zombieId) ownerOf (_ zombieId) {zombies [_ zombieId]. Name = _ newName } function changeDna (uint _ zombieId, uint _ newDna) external aboveLevel (20, _ zombieId) ownerOf (_ zombieId) {zombies [_ zombieId]. Dna = _ newDna;} function getZombiesByOwner (address _ owner) external view returns (uint []) {uint [] memory result = new uint [] (ownerZombieCount [_ owner]); uint counter = 0; for (uint I = 0; I < zombies.length) If +) {if (zombieToOwner [I] = = _ owner) {result [counter] = I; counter++;}} return result;}}

Msg.value is a way to see how many ethers are sent to the contract, and ether is a built-in unit.

What happens here is that some people will call this function from web3.js (from the front end of DApp), like this:

/ / suppose `OnlineStore` points to your contract on Ethernet Square: OnlineStore.buySomething () .send (from: web3.eth.defaultAccount, value: web3.utils.toWei (0.001))

ZombieBattle.sol

Pragma solidity ^ 0.4.19 × import ". / zombiehelper.sol"; contract ZombieBattle is ZombieHelper {uint randNonce = 0; uint attackVictoryProbability = 70; function randMod (uint _ modulus) internal returns (uint) {randNonce++; return uint (keccak256 (now, msg.sender, randNonce))% _ modulus;} function attack (uint _ zombieId, uint _ targetId) external ownerOf (_ zombieId) {Zombie storage myZombie = zombies [_ zombieId]; Zombie storage enemyZombie = zombies [_ targetId]; uint rand = randMod If (rand

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report