At the end of the last chapter, we created this unique and incredibly lovable character:
Here we continue to sum up some knowledge about solidity languages. And get to know something more advanced.
ERC20 Tokens and ERC721 standards, and Crypto-Collectible. This knowledge allows us to trade our characters with other players.
1
Token
Opinions differ on the understanding of token. In order to refresh your memory of the definition of token here, I will not cite other explanations of token.
In this case, token means a Dapp, a smart contract.
Important things to say three times: Token is a Dapp, a smart contract. Token is a Dapp, a smart contract.
The smart contract tracks who owns how much gold and then has features that allow the owner to trade.
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 is an already implemented Dapp. That means you can use ERC20 Tokens directly in your Dapp without having to define your own “coins”. In ERC20 Tokens Dapp, one “coin” is exactly equal to another “coin”. If you don’t have change for “gold coins”, you can give the other person a large “gold coin” and the other person can get change.
But in our game, the value of the invincible character you create and train is completely different from the value of the other newly created characters, and when trading, it’s impossible to find 0.5 characters.
ERC721 Tokens applies to this game.
ERC721 tokens are not interchangeable since each one is assumed to be unique, and are not divisible.
2
inheritance
In Solidity we can create a smart contract like this and form another
Pragma solidity ^ 0.4.19; import"./ZombieAttack.sol";
contract ZombieOwnership is ZombieAttack {
}
Copy the code
ERC721 Standard Multiple inheritance
This is the definition of ERC721 Standard: we just need to implement these interfaces.
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
ERC721 Standard is still a draft, not an official release. In this game we will use the implementation directly from the OpenZeppelin library.
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
Implementing an interface
Import, as with inheritance, and place the interface to be implemented after the is defined by the contract.
import "./zombieattack.sol";
import "./erc721.sol";
contract ZombieOwnership is ZombieAttack, erc721 {
}
Copy the code
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