在這個遊戲裏面,咱們使用ERC721 tokens標準,一般的狀況下,使用的是ERC20 tokens。編程
有興趣的童學能夠研究一下兩個標準的不一樣。微信
ERC721 tokens有兩種方式交易"金幣"的方式。雖然在這裏我用的"金幣"一詞,可是能夠是如何東西,你的加密貓,你的無敵英雄角色。網絡
下面的 transfer 和 approve + takeOwnership 是ERC721 tokens標準裏面的兩個交易接口。完成的功能相同。app
function transfer(address _to, uint256 _tokenId) public; function approve(address _to, uint256 _tokenId) public; function takeOwnership(uint256 _tokenId) public;
Event的調用方法:函數
定義一個Event學習
contract ERC721 { event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId); function balanceOf(address _owner) public view returns (uint256 _balance); function ownerOf(uint256 _tokenId) public view returns (address _owner); function transfer(address _to, uint256 _tokenId) public; function approve(address _to, uint256 _tokenId) public; function takeOwnership(uint256 _tokenId) public; }
調用一個Event區塊鏈
function _transfer(address _from, address _to, uint256 _tokenId) private { ownerZombieCount[_to]++; ownerZombieCount[_from]--; zombieToOwner[_tokenId] = _to; Transfer(_from, _to, _tokenId); }
定義一個mappingui
mapping (uint => address) public zombieToOwner; mapping (address => uint) ownerZombieCount;
require(newOwner != address(0));
OpenZeppelin提供了一個庫:SafeMath,能夠解決overflow問題。 overflow也叫作溢出。this
Let's say we have a uint8, which can only have 8 bits. That means the largest number we can store is binary 11111111 (or in decimal, 2^8 - 1 = 255).加密
能夠這樣使用:
using SafeMath for uint256; uint256 a = 5; uint256 b = a.add(3); // 5 + 3 = 8 a自動成爲函數的第一個參數 uint256 c = a.mul(2); // 5 * 2 = 10
這裏是原代碼:
library SafeMath { function mul(uint256 a, uint256 b) internal pure returns (uint256) { if (a == 0) { return 0; } uint256 c = a * b; assert(c / a == b); return c; } function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } function sub(uint256 a, uint256 b) internal pure returns (uint256) { assert(b <= a); return a - b; } function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; assert(c >= a); return c; } }
能夠這樣代替已有的算數符號
Ex. Instead of doing: myUint++; We would do: myUint = myUint.add(1);
在Solidity裏面,能夠將幾個library定義到同一文件裏面。 能夠這樣調用:
using SafeMath16 for uint16; /** * @title SafeMath16 * @dev SafeMath library implemented for uint16 */ library SafeMath16 { function mul(uint16 a, uint16 b) internal pure returns (uint16) { if (a == 0) { return 0; } uint16 c = a * b; assert(c / a == b); return c; } function div(uint16 a, uint16 b) internal pure returns (uint16) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint16 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } function sub(uint16 a, uint16 b) internal pure returns (uint16) { assert(b <= a); return a - b; } function add(uint16 a, uint16 b) internal pure returns (uint16) { uint16 c = a + b; assert(c >= a); return c; } }
咱們終於完成了這一章節的學習。下一章節咱們要學習如何發佈到ETH網絡上。
cryptozombies
拓展閱讀:
十一課堂|經過小遊戲學習Ethereum DApps編程(1)
十一課堂|經過小遊戲學習Ethereum DApps編程(2)
十一課堂|經過小遊戲學習Ethereum DApps編程(3)
十一課堂|經過小遊戲學習Ethereum DApps編程(4)
本系列文章做者:HiBlock區塊鏈技術佈道羣-Amywu
原文發佈於簡書
加微信baobaotalk_com,加入技術佈道羣
Blockathon|48小時極客競賽,區塊鏈馬拉松等你挑戰(上海)
時間:2018年10月19-21日
地點:(上海黃浦)露香園路1號(近淮海東路)P2
北京blockathon回顧:
Blockathon(北京):48小時極客開發,區塊鬆11個現場交付項目創意公開
成都blockathon回顧: