十一课堂|通过小游戏学习Ethereum DApps编程(4)

image

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

image

这里我们继续总结一些关于solidity语言的知识点。并且开始了解一些比较高级的内容。

ERC20 tokens以及ERC721标准,和crypto-collectible。这些知识可以让我们可以和其他玩家交易自己的创造的角色。

1

Token

对于token的理解,众说纷纭。为了让你清醒的记忆token在这里的定义,我就不举例其他对token的解释了。

在这里,token就是一个Dapp,一个智能合约的意思。

重要的事情说三遍:
token就是一个Dapp,一个智能合约的意思。
token就是一个Dapp,一个智能合约的意思。

这个智能合约可以追溯谁拥有多少"金币",然后有一些功能可以让"金币"拥有者进行交易。

So basically a token is just a contract that keeps track of who owns how much of that token, and some functions so those users can transfer their tokens to other addresses.

因为ERC20 tokens是一个已经被实现了的Dapp,就意味着,你可以直接在你的Dapp里面使用ERC20 tokens,不需要自己去定义自己的"金币"。
在ERC20 tokens这个Dapp里面,一个"金币",完全等于另外一个"金币"。如果你没有零钱"金币",你可以付给对方一个大面值的"金币",对方可以找零。

但在我们这个游戏里面,你创造和训练的无敌角色,和其他刚刚创造的角色的价值是完全不对等的,而且,在交易的时候,也不可能说是找0.5个角色。

与之对应的另外一个token标准:ERC721 tokens可是适用于我们这个游戏。

ERC721 tokens are not interchangeable since each one is assumed to be unique, and are not divisible.

2

继承

在Solidity里面,我们可以这样创建一个智能合约,并且结成另外一个

pragma solidity ^0.4.19;

import "./ZombieAttack.sol";
contract ZombieOwnership is ZombieAttack {
}

ERC721 Standard 多重继承

这是ERC721 Standard的定义:我们只需要实现这些接口。

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;

}

ERC721 standard还只是一个草稿,并非正式版。在这个游戏里面我们就直接使用OpenZeppelin库里面的实现版本。

Note: The ERC721 standard is currently a draft, and there is no officially agreed-upon implementation yet. For this tutorial we’re using the current version from OpenZeppelin’s library

3

实现接口

和继承一样,import,并且把要实现的接口放到合约定义的 is 后面。

import "./zombieattack.sol";

import "./erc721.sol";

contract ZombieOwnership is ZombieAttack, erc721 {
}

本系列文章作者:HiBlock区块链技术布道群-Amywu

原文发布于简书

加微信baobaotalk_com,加入技术布道群

北京blockathon回顾:

Blockathon(北京):48小时极客开发,区块松11个现场交付项目创意公开

成都blockathon回顾:

Blockathon2018(成都站)比赛落幕,留给我们这些区块链应用思考

以下是我们的社区介绍,欢迎各种合作、交流、学习:)

image

猜你喜欢

转载自blog.csdn.net/HiBlock/article/details/82947655
今日推荐