美文网首页ETH
通过小游戏学习Ethereum DApps编程(6)

通过小游戏学习Ethereum DApps编程(6)

作者: 假装在去天使之城的路上 | 来源:发表于2018-09-17 14:04 被阅读18次

    这篇博客是 通过小游戏学习Ethereum DApps编程 系列的第6篇。

    第四章完结

    在第四章完结的时候,我们制造出了这个独一无二可爱至极的角色:

    这里我们继续总结一些关于solidity语言的知识点。并且开始了解一些比较高级的内容。
    ERC20 tokens以及ERC721标准,和crypto-collectible。这些知识可以让我们可以和其他玩家交易自己的创造的角色。

    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;
    

    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);
      }
    

    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自动成为函数的第一个参数
    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);
    

    自定义 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

    2018-09-09_191317.jpg 2018-09-09_191531.jpg

    图片来源

    图片来自原作者官方网站

    相关链接

    HiBlock区块链技术布道 GitHub

    相关文章

      网友评论

        本文标题:通过小游戏学习Ethereum DApps编程(6)

        本文链接:https://www.haomeiwen.com/subject/pzzrgftx.html