在这个游戏里面,咱们使用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回顾: