TOKEN-代币

什么是TOKEN(代币)

  • 通常是指私人发行的,类似硬币形状的物体,具备少量价值;如游戏币
  • 以太坊上的代币是指基于区块链的一种抽象的资产,可以被持有并且用来代表资产、现金等

代币有哪些使用方式

  • 货币
  • 资产
  • 访问权限
  • 权益(股东权益)
  • 投票权
  • 收藏品
  • 身份
  • 。。。

代币标准

ERC20

ERC20是一个token标准,体现到合约上是一个接口规范。接口如下(不同编译版本的solidity的接口可能语法不一样,但是描述的是一样的):

我的0.8.0版本接口定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// SPDX-License-Identifier: GPL-3.0

pragma solidity=0.8.0;

interface ERC20Interface {

function balanceOf(address _owner) external view returns (uint256 balance);

function transfer(address _to, uint256 _value) external returns (bool success);

function transferFrom(address _from, address _to, uint256 _value) external returns (bool success);

function approve(address _spender, uint256 _value) external returns (bool success);

function allowance(address _owner, address _spender) external view returns (uint256 remaining);

event Transfer(address indexed _from, address indexed _to, uint256 _value);

event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

ERC20-[ConsenSys定义]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Abstract contract for the full ERC 20 Token standard
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
pragma solidity ^0.4.21;


contract EIP20Interface {
/* This is a slight change to the ERC20 base standard.
function totalSupply() constant returns (uint256 supply);
is replaced with:
uint256 public totalSupply;
This automatically creates a getter function for the totalSupply.
This is moved to the base contract since public getter functions are not
currently recognised as an implementation of the matching abstract
function by the compiler.
*/
/// total amount of tokens
uint256 public totalSupply;

/// @param _owner The address from which the balance will be retrieved
/// @return The balance
function balanceOf(address _owner) public view returns (uint256 balance);

/// @notice send `_value` token to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transfer(address _to, uint256 _value) public returns (bool success);

/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);

/// @notice `msg.sender` approves `_spender` to spend `_value` tokens
/// @param _spender The address of the account able to transfer the tokens
/// @param _value The amount of tokens to be approved for transfer
/// @return Whether the approval was successful or not
function approve(address _spender, uint256 _value) public returns (bool success);

/// @param _owner The address of the account owning tokens
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens allowed to spent
function allowance(address _owner, address _spender) public view returns (uint256 remaining);

// solhint-disable-next-line no-simple-event-func-name
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

ERC721

ERC721是NFT(Non-Fungible Token)的标准。直接看官方接口定义ERC721openzeppelin的实现

ERC777

一种兼容ERC20的代币标准,这个标准在转账的时候会检查接收方支不支持接收当前代币(即有没有实现某个接口);如果没有实现转账会失败,这可以防止代币转入一个不支持的合约中