1
ERC721 tokens
在这个游戏里面,我们使用ERC721 tokens标准,通常的情况下,使用的是ERC20 tokens。
有兴趣的童学可以研究一下两个标准的不同。
ERC721 tokens有两种方式交易"金币"的方式。虽然在这里我用的"金币"一词,但是可以是如何东西,你的加密猫,你的无敌英雄角色。
下面的 transfer 和 approve + takeOwnership 是ERC721 tokens标准里面的两个交易接口。完成的功能相同。
function transfer(address _to, uint256 _tokenId) public;
function approve(address _to, uint256 _tokenId) public;
function takeOwnership(uint256 _tokenId) public;
2
Event
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);
}
3
mapping
定义一个mapping
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
4
require
require(newOwner != address(0));
5
SafeMath
OpenZeppelin提供了一个库:SafeMath,可以解决overflow问题。 overflow也叫做溢出。
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);
6
自定义 library
在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
- 招募50名开发者(识别下图二维码或点击“阅读原文”即可了解详情并报名)
北京blockathon回顾:
Blockathon(北京):48小时极客开发,区块松11个现场交付项目创意公开
成都blockathon回顾:
Blockathon2018(成都站)比赛落幕,留给我们这些区块链应用思考
来源:oschina
链接:https://my.oschina.net/u/3782027/blog/2223235