在疯狂的ICO众筹的推动下,加密货币在几年时间内增长到了一千多种。这种爆发式得到增长不得不引起注意,这些众筹企业筹集了巨额的资金。但这些数字货币的价值到底有多少?它们来自哪里?众筹资金必须要这些数字货币吗?可能并不会有多少人关注。读完这篇文章你将会了解ERC20代币的本质,以及它背后的技术原理。
原文来自medium.com
鉴于许多ICO项目的启动,企业可以在销售、提供数字货币的同时筹集数百万美元的现金,但重要的是我们要认识到基本上所有的数字货币都要基于一项技术:ERC20。
Ethereum Request for Comments 20或ERC20是由Fabian Vogelsteller在2015年底推出的以太坊改进提案。这是许多以太坊流行的智能合约所遵循的标准。它非常好地使智能合约与传统的加密货币(如比特币或以太坊本身)表现起来非常相似。在这种情况下托管在以太坊区块链上的代币可以发送、接收以及查看其总供应量,并查看在单个地址上可用的货币数量。这类似于从钱包中发送和接收以太币或比特币,能够获悉流通中的硬币总量,并能得到特定代币钱包的代币余额。遵循此标准的智能合约称为ERC20代币。
通过定义一组函数允许智能合约模拟数字代币的所有先前描述的所有功能。但是这有什么用呢?
ERC20定义了函数balanceOf,totalSupply,transfer,transferFrom,approve和allowance。它还有一些可选字段,如代币名称、符号和将会被测量的小数位数。
注意:这是ERC20合约示例的简明声明。
// Grabbed from: https://github.com/ethereum/EIPs/issues/20
contract ERC20 {
function totalSupply() constant returns (uint theTotalSupply);
function balanceOf(address _owner) constant returns (uint balance);
function transfer(address _to, uint _value) returns (bool success);
function transferFrom(address _from, address _to, uint _value) returns (bool success);
function approve(address _spender, uint _value) returns (bool success);
function allowance(address _owner, address _spender) constant returns (uint remaining);
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
合约中每个字段的概述和示例如下。
totalSupply()
虽然供应可以很容易地被固定,就像比特币一样,但是这个功能允许合约计算并返回正在流通中代币的总量。
contract MyERCToken {
// In this case, the total supply
// of MyERCToken is fixed, but
// it can very much be changed
uint256 _totalSupply = 1000000;
function totalSupply() constant returns (uint256 theTotalSupply) {
// Because our function signature
// states that the returning variable
// is "theTotalSupply", we'll just set that variable
// to the value of the instance variable "_totalSupply"
// and return it
theTotalSupply = _totalSupply;
return theTotalSupply;
}
}
balanceOf()
此功能允许智能合约存储和返回所输入地址的余额。该函数接受一个地址作为参数,因此我们可以知道任何地址的余额是公开的。
contract MyERCToken {
// Create a table so that we can map addresses
// to the balances associated with them
mapping(address => uint256) balances;
// Owner of this contract
address public owner;
function balanceOf(address _owner) constant returns (uint256 balance) {
// Return the balance for the specific address
return balances[_owner];
}
}
Approve()
在调用此函数时,合约所有者授权或批准给定地址,以从所有者的地址中取出代币。
在以后的片段中,你可能会看到变量msg。这是由外部应用程序(如钱包)提供的隐式字段,以便它们可以更好地与合约交互。以太坊虚拟机(EVM)允许我们使用此字段来存储和处理外部应用程序提供的数据。
在这个示例中,msg.sender是合约所有者的地址。
contract MyERCToken {
// Create a table so that we can map
// the addresses of contract owners to
// those who are allowed to utilize the owner's contract
mapping(address => mapping (address => uint256)) allowed;
function approve(address _spender, uint256 _amount) returns (bool success) {
allowed[msg.sender][_spender] = _amount;
// Fire the event "Approval" to execute any logic
// that was listening to it
Approval(msg.sender, _spender, _amount);
return true;
}
}
Transfer()
此函数允许合约所有者将给定数量的代币发送到另一个地址,就像传统的加密货币交易一样。
contract MyERCToken {
mapping(address => uint256) balances;
// Note: This function returns a boolean value
// indicating whether the transfer was successful
function transfer(address _to, uint256 _amount) returns (bool success) {
// If the sender has sufficient funds to send
// and the amount is not zero, then send to
// the given address
if (balances[msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[msg.sender] -= _amount;
balances[_to] += _amount;
// Fire a transfer event for any
// logic that's listening
Transfer(msg.sender, _to, _amount);
return true;
} else {
return false;
}
}
}
transferFrom()
此功能允许智能合约自动执行转账过程并代表所有者发送给定数量的代币。
看到这一点有些人可能会有一些疑问。有人可能会质疑为什么我们需要transfer()和transferFrom()函数。
考虑转账以支付账单。通过花时间填写支票并邮寄以支付账单来手动汇款是非常普遍的。这就像使用transfer():你自己在做汇款的整个过程,没有另一方的帮助。
在另一种情况下,你可以与银行签订自动付款的协议。这就像使用transferFrom():银行的机器会自动汇款以代你支付账单。使用这个功能,合约可以代表你将一定数量的代币发送到另一个地址,而不需要你的干预。
contract MyERCToken {
mapping(address => uint256) balances;
function transferFrom(address _from, address _to, uint256 _amount) returns (bool success) {
if (balances[_from] >= _amount
&& allowed[_from][msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[_from] -= _amount;
balances[_to] += _amount;
Transfer(_from, _to, _amount);
return true;
} else {
return false;
}
}
}
代币名称
这是一个可选字段,但许多流行的代币都包含它,因此像Mist和MyEtherWallet这样的流行钱包能够识别它们。
contract MyERCToken {
string public constant name = "My Custom ERC20 Token";
}
代币符号
另一个用于标识代币的可选字段,这是代币的三个或四个字母缩写,就像BTC,ETH,AUG或SJCX一样。
contract MyERCToken {
string public constant symbol = "MET";
}
小数位数
一个可选字段,用于确定计算代币数量的小数位。最常见小数位数是18位。
contract MyERCToken {
uint8 public constant decimals = 18;
}
我们刚刚创建的ERC20代币的总源代码可以在这里找到。
最初的ERC20提案相当不受重视。它开辟了一系列智能合约的新途径,这些合约可以像比特币或以太坊一样以相同的方式创建和分发。事实证明,这对于初创公司来说非常诱人,因为整个ERC20生态系统都托管在以太坊的区块链上,这是一个已有的大型计算机网络。这意味着开发商和初创公司不必为了维护他们的代币而吸引矿工,这可以节省很多钱。而且,这些代币可以像其他资产一样托管在交易所进行交易,因此投资者可以像其他受欢迎的货币一样轻松买卖这些代币。
最新回复