以太坊发币智能合约代码简单介绍:
发币代码如下(https://ethereum.org/token#the-code网站中获得):
pragma solidity ^0.4.16; interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; } contract TokenERC20 { // Public variables of the token string public name; string public symbol; uint8 public decimals = 18; // 18 decimals is the strongly suggested default, avoid changing it uint256 public totalSupply; // This creates an array with all balances mapping (address => uint256) public balanceOf; mapping (address => mapping (address => uint256)) public allowance; // This generates a public event on the blockchain that will notify clients event Transfer(address indexed from, address indexed to, uint256 value); // This notifies clients about the amount burnt event Burn(address indexed from, uint256 value); /** * Constructor function * * Initializes contract with initial supply tokens to the creator of the contract */ function TokenERC20( uint256 initialSupply, string tokenName, string tokenSymbol ) public { totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens name = tokenName; // Set the name for display purposes symbol = tokenSymbol; // Set the symbol for display purposes } /** * Internal transfer, only can be called by this contract */ function _transfer(address _from, address _to, uint _value) internal { // Prevent transfer to 0x0 address. Use burn() instead require(_to != 0x0); // Check if the sender has enough require(balanceOf[_from] >= _value); // Check for overflows require(balanceOf[_to] + _value > balanceOf[_to]); // Save this for an assertion in the future uint previousBalances = balanceOf[_from] + balanceOf[_to]; // Subtract from the sender balanceOf[_from] -= _value; // Add the same to the recipient balanceOf[_to] += _value; Transfer(_from, _to, _value); // Asserts are used to use static analysis to find bugs in your code. They should never fail assert(balanceOf[_from] + balanceOf[_to] == previousBalances); } /** * Transfer tokens * * Send `_value` tokens to `_to` from your account * * @param _to The address of the recipient * @param _value the amount to send */ function transfer(address _to, uint256 _value) public { _transfer(msg.sender, _to, _value); } /** * Transfer tokens from other address * * Send `_value` tokens to `_to` on behalf of `_from` * * @param _from The address of the sender * @param _to The address of the recipient * @param _value the amount to send */ function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require(_value <= allowance[_from][msg.sender]); // Check allowance allowance[_from][msg.sender] -= _value; _transfer(_from, _to, _value); return true; } /** * Set allowance for other address * * Allows `_spender` to spend no more than `_value` tokens on your behalf * * @param _spender The address authorized to spend * @param _value the max amount they can spend */ function approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; return true; } /** * Set allowance for other address and notify * * Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it * * @param _spender The address authorized to spend * @param _value the max amount they can spend * @param _extraData some extra information to send to the approved contract */ function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) { tokenRecipient spender = tokenRecipient(_spender); if (approve(_spender, _value)) { spender.receiveApproval(msg.sender, _value, this, _extraData); return true; } } /** * Destroy tokens * * Remove `_value` tokens from the system irreversibly * * @param _value the amount of money to burn */ function burn(uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); // Check if the sender has enough balanceOf[msg.sender] -= _value; // Subtract from the sender totalSupply -= _value; // Updates totalSupply Burn(msg.sender, _value); return true; } /** * Destroy tokens from other account * * Remove `_value` tokens from the system irreversibly on behalf of `_from`. * * @param _from the address of the sender * @param _value the amount of money to burn */ function burnFrom(address _from, uint256 _value) public returns (bool success) { require(balanceOf[_from] >= _value); // Check if the targeted balance is enough require(_value <= allowance[_from][msg.sender]); // Check allowance balanceOf[_from] -= _value; // Subtract from the targeted balance allowance[_from][msg.sender] -= _value; // Subtract from the sender‘s allowance totalSupply -= _value; // Update totalSupply Burn(_from, _value); return true; } }
使用的是以太坊生态链提供的语言solidity
我们来分析每段代码的用处。
contract MyToken { /* This creates an array with all balances */ mapping (address => uint256) public balanceOf; }
这段代码建立了 地址 => 余额 关联数组,并定义为public keyword,意思是在以太坊生态链中任何客户端都可以访问它,查询每个地址的余额。
这部分代码创建了一个合约,你可以立马就公布到以太坊生态链中,合同会立马生效,但没啥用:它虽然是一个合同,可以查询每个地址的余额--但是你并没有在此合同中创造币,所以查询每一个地址都会返回0(关于这个合同)。那么我们接下来就要在此合同内补充以下代码起到发币的作用:
function MyToken() { balanceOf[msg.sender] = 21000000; }
请注意,函数MyToken与contract MyToken具有相同的名称。这是非常重要的,如果您重命名一个,您也必须重命名另一个:这是一个特殊的启动函数,它只运行一次,并且只有当契约第一次上传到网络时才运行 (初始化函数)。此函数第一次运行会设置发布此合约地址的余额为2100万。
2100万这个值可以定义为参数,定义成参数后用户就可以在发布合约时自己根据实际应用设置币的总额。代码如下:
function MyToken(uint256 initialSupply) public { balanceOf[msg.sender] = initialSupply; }
至此你就可以通过上面的代码实现发布自己的erc20币,高兴不。但并没有卵用,因为这个币只有你自己这个地址拥有,没办法发给其他的地址,意味着不能流通。接下来我们就需要让它可以转发。在合约代码加入下面这个函数:
/* Send coins */ function transfer(address _to, uint256 _value) { /* Add and subtract new balances */ balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; }
这是一个非常简单的函数:它需要两个参数:一个接收地址i,一个金额值,当有人调用它时,它将从发送方余额中减去_value并将其添加到_to balance。现在有一个明显的问题:如果这个人想要发送比它拥有的更多的东西会发生什么?由于我们不想在这个特殊的合同中处理债务,我们只是简单地做一个快速检查,如果发件人没有足够的资金,合同的执行就会停止。它还可以检查溢出,避免有一个如此大的数字,使它再次变为零。
要中止合约中转币的执行,可以返回 或 复原。对于发送方sender来讲,前者消耗的gas更少,但在以太坊生态链中会记录合同中任何的改变(交易)哪怕是失败的。所以采用复原操作,取消合约的执行,恢复交易可能产生的任何变化(其实就是把币还给sender),比较遗憾的是会消耗掉sender为了执行合约所提供的gas。好在 以太坊钱包 有检测某笔交易会不会失败,会根据检测结果作出警告,从而防止发出这种失败的交易损失以太坊。