這篇博客是 通過小游戲?qū)W習(xí)Ethereum DApps編程 系列的第6篇。
- 通過小游戲?qū)W習(xí)Ethereum DApps編程(6)← 你在這里
- 通過小游戲?qū)W習(xí)Ethereum DApps編程(5)
- 通過小游戲?qū)W習(xí)Ethereum DApps編程(4)
- 通過小游戲?qū)W習(xí)Ethereum DApps編程(3)
- 通過小游戲?qū)W習(xí)Ethereum DApps編程(2)
- 通過小游戲?qū)W習(xí)Ethereum DApps編程(1)

在第四章完結(jié)的時候,我們制造出了這個獨一無二可愛至極的角色:

這里我們繼續(xù)總結(jié)一些關(guān)于solidity語言的知識點。并且開始了解一些比較高級的內(nèi)容。
ERC20 tokens以及ERC721標(biāo)準(zhǔn),和crypto-collectible。這些知識可以讓我們可以和其他玩家交易自己的創(chuàng)造的角色。
ERC721 tokens
在這個游戲里面,我們使用ERC721 tokens標(biāo)準(zhǔn),通常的情況下,使用的是ERC20 tokens。
有興趣的童學(xué)可以研究一下兩個標(biāo)準(zhǔn)的不同。
ERC721 tokens有兩種方式交易"金幣"的方式。雖然在這里我用的"金幣"一詞,但是可以是如何東西,你的加密貓,你的無敵英雄角色。
下面的 transfer 和 approve + takeOwnership 是ERC721 tokens標(biāo)準(zhǔn)里面的兩個交易接口。完成的功能相同。
function transfer(address _to, uint256 _tokenId) public;
function approve(address _to, uint256 _tokenId) public;
function takeOwnership(uint256 _tokenId) public;
Event
Event的調(diào)用方法:
定義一個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;
}
調(diào)用一個Event
function _transfer(address _from, address _to, uint256 _tokenId) private {
ownerZombieCount[_to]++;
ownerZombieCount[_from]--;
zombieToOwner[_tokenId] = _to;
Transfer(_from, _to, _tokenId);
}
mapping
定義一個mapping
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
require
require(newOwner != address(0));
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自動成為函數(shù)的第一個參數(shù)
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;
}
}
可以這樣代替已有的算數(shù)符號
Ex. Instead of doing:
myUint++;
We would do:
myUint = myUint.add(1);
自定義 library
在Solidity里面,可以將幾個library定義到同一文件里面。
可以這樣調(diào)用:
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;
}
}
哈哈,我們終于完成了這一章節(jié)的學(xué)習(xí)。下一章節(jié)我們要學(xué)習(xí)如何發(fā)布到ETH網(wǎng)絡(luò)上。


圖片來源
圖片來自原作者官方網(wǎng)站