通過小游戲?qū)W習(xí)Ethereum DApps編程(6)

這篇博客是 通過小游戲?qū)W習(xí)Ethereum DApps編程 系列的第6篇。

第四章完結(jié)

在第四章完結(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ò)上。

cryptozombies

2018-09-09_191317.jpg

2018-09-09_191531.jpg

圖片來源

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

相關(guān)鏈接

HiBlock區(qū)塊鏈技術(shù)布道 GitHub

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容