十一課堂|經過小遊戲學習Ethereum DApps編程(4)

image

在上篇完結的時候,咱們製造出了這個獨一無二可愛至極的角色:微信

image

這裏咱們繼續總結一些關於solidity語言的知識點。而且開始瞭解一些比較高級的內容。app

ERC20 tokens以及ERC721標準,和crypto-collectible。這些知識可讓咱們能夠和其餘玩家交易本身的創造的角色。區塊鏈

1

Token

對於token的理解,衆說紛紜。爲了讓你清醒的記憶token在這裏的定義,我就不舉例其餘對token的解釋了。ui

在這裏,token就是一個Dapp,一個智能合約的意思。this

重要的事情說三遍: token就是一個Dapp,一個智能合約的意思。 token就是一個Dapp,一個智能合約的意思。code

這個智能合約能夠追溯誰擁有多少"金幣",而後有一些功能可讓"金幣"擁有者進行交易。繼承

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.token

由於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|48小時極客競賽,區塊鏈馬拉松等你挑戰(上海)

時間:2018年10月19-21日

地點:(上海黃浦)露香園路1號(近淮海東路)P2

  • 招募50名開發者(識別下圖二維碼或點擊「閱讀原文」便可瞭解詳情並報名)

image

北京blockathon回顧:

Blockathon(北京):48小時極客開發,區塊鬆11個現場交付項目創意公開

成都blockathon回顧:

Blockathon2018(成都站)比賽落幕,留給咱們這些區塊鏈應用思考

相關文章
相關標籤/搜索