1

ERC721 tokens

In this game, we use the ERC721 Tokens standard, usually ERC20 Tokens.

Interested children can study the difference between the two standards.

ERC721 Tokens has two ways to trade “coins”. I’m using the word “gold coin” here, but it could be anything, your crypto cat, your invincible hero character.

The following transfer and Approve + takeOwnership are two transaction interfaces in ERC721 Tokens standard. It does the same thing.

function transfer(address _to, uint256 _tokenId) public;

function approve(address _to, uint256 _tokenId) public;

function takeOwnership(uint256 _tokenId) public;
Copy the code

2

Event

Call method of Event:

Define an 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;

}
Copy the code

Call an Event

function _transfer(address _from, address _to, uint256 _tokenId) private {

    ownerZombieCount[_to]++;

    ownerZombieCount[_from]--;

    zombieToOwner[_tokenId] = _to;

    Transfer(_from, _to, _tokenId);

  }
Copy the code

3

mapping

Define a Mapping

mapping (uint => address) public zombieToOwner;

   mapping (address => uint) ownerZombieCount;
Copy the code

4

require

require(newOwner ! = address(0));Copy the code

5

SafeMath

OpenZeppelin provides a library, SafeMath, that can solve overflow problems. Overflow is also called 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).

It can be used like this:

using SafeMath foruint256; uint256 a = 5; uint256 b = a.add(3); Uint256 c = a.mul(2); // 5 * 2 = 10Copy the code

Here is the original code:

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; }}Copy the code

You can do this instead of existing arithmetic symbols

Ex. Instead of doing:
myUint++;

We would do:
myUint = myUint.add(1);
Copy the code

6

The custom library

In Solidity you can define several libraries into the same file. It can be called like this:

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; }}Copy the code

We have finished the chapter at last. In the next chapter we will learn how to publish to the ETH network.

cryptozombies
Copy the code

Read more:

11 class | learn by little game Ethereum DApps programming (1)

11 class | learn by little game Ethereum DApps programming (2)

11 class | learn by little game Ethereum DApps programming (3)

11 class | learn by little game Ethereum DApps programming (4)

Author of this series of articles: Amywu, HiBlock Blockchain Technology Evangelist Group

Originally published in Jane’s book

Add wechat baobaotalk_com to join the technical evangelist group

Geek race, 48 hours Blockathon | block chain marathon waiting for you to challenge (Shanghai)

Time: October 19-21, 2018

Venue: P2, 1 Luxiangyuan Road (near Huaihai East Road), Huangpu, Shanghai

  • Recruit 50 developers (please check the qr code below or click “Read the original” to learn more and register)

Beijing Blockathon Review:

Blockathon (Beijing) : 48 hours geek development, block Pine 11 on-site delivery project ideas open

Chengdu Blockathon Review:

The closing of Blockathon2018 (chengdu) competition leaves us thinking about blockchain applications