1. 程式人生 > >十一課堂|通過小遊戲學習Ethereum DApps程式設計(5)

十一課堂|通過小遊戲學習Ethereum DApps程式設計(5)

image

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

image

image

拓展閱讀:

本系列文章作者:HiBlock區塊鏈技術佈道群-Amywu

原文釋出於簡書

加微信baobaotalk_com,加入技術佈道群

北京blockathon回顧:

成都blockathon回顧:

以下是我們的社群介紹,歡迎各種合作、交流、學習:)

image