以太坊发币智能合约代码简单介绍:

发币代码如下(https://ethereum.org/token#the-code网站中获得):

pragma solidity ^0.4.;

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 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 * ** 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] = ;
}

请注意,函数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。好在 以太坊钱包 有检测某笔交易会不会失败,会根据检测结果作出警告,从而防止发出这种失败的交易损失以太坊。下面就是加了检测的代码:

    function transfer(address _to, uint256 _value) {
/* Check if sender has balance and for overflows */
require(balanceOf[msg.sender] >= _value && balanceOf[_to] + _value >= balanceOf[_to]); /* Add and subtract new balances */
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
}

现在缺少的是关于合同的一些基本信息。在不久的将来,这可以由一个令牌注册表来处理,但是现在我们将直接将它们添加到合同中:

string public name;
string public symbol;
uint8 public decimals;

现在我们更新构造函数,允许在开始时设置所有这些变量:

    /* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(uint256 initialSupply, string tokenName, string tokenSymbol, uint8 decimalUnits) {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
decimals = decimalUnits; // Amount of decimals for display purposes
}

最后,我们现在需要一些所谓的事件。这些是特殊的、空的函数,可以通过调用这些函数来帮助像Ethereum钱包这样的客户跟踪契约中发生的活动。事件应该以大写字母开头。在合同开始时加上这条线来宣布事件:

    event Transfer(address indexed from, address indexed to, uint256 value);

事件定义好了,还要在transfer函数中加上以下两行事件才能正常运行:记得

        /* Notify anyone listening that this transfer took place */
Transfer(msg.sender, _to, _value);

注意 事件定义的event Transfer() 要和函数中定义的Transfe(msg.sender, _to, _value)名称一样.

现在的代码几乎把发token的必备条件准备好了。

标准的token代码已经准备好了,我们可以使用mist进行部署了,还有一些功能这里就不写了,因为我也不会,

ethereum发erc20token的更多相关文章

  1. ethereum/EIPs-1077 Executable Signed Messages

    https://github.com/alexvandesande/EIPs/blob/ee2347027e94b93708939f2e448447d030ca2d76/EIPS/eip-1077.m ...

  2. 使用remix发布部署 发币 智能合约

    Remix是一个基于浏览器的编译器和IDE,使用户能够使用Solidity语言构建以太坊合约并调试事务. 在上一篇文章已经成功的使用代码讲智能合约编译并且发布部署到了链上,可是在部署 发币的智能合约 ...

  3. (转)以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘

    什么是以太坊(Ethereum) 以太坊(Ethereum)是一个基于区块链技术,允许任何人构建和使用去中心化应用的区块链平台.像比特币一样,以太坊是开源的,并由来自全世界的支持者们共同维护.与比特币 ...

  4. ethereum/EIPs-725

    https://github.com/ethereum/EIPs/blob/master/EIPS/eip-725.md eip title author discussions-to status ...

  5. 【Ethereum】以太坊ERC20 Token标准完整说明

    什么是ERC20 token 市面上出现了大量的用ETH做的代币,他们都遵守REC20协议,那么我们需要知道什么是REC20协议. 概述 token代表数字资产,具有价值,但是并不是都符合特定的规范. ...

  6. [中文] 以太坊(Ethereum )白皮书

    以太坊(Ethereum ):下一代智能合约和去中心化应用平台 翻译|巨蟹 .少平 译者注|中文读者可以到以太坊爱好者社区(www.ethfans.org)获取最新的以太坊信息. 当中本聪在2009年 ...

  7. 以太坊智能合约[ERC20]发币记录

    以太坊被称为区块链2.0,就是因为以太坊在应用层提供了虚拟机,使得开发者可以基于它自定义逻辑,通常被称为智能合约,合约中的公共接口可以作为区块链中的普通交易执行.本文就智能合约发代币流程作一完整介绍( ...

  8. 区块链入门到实战(27)之以太坊(Ethereum) – 智能合约开发

    智能合约的优点 与传统合同相比,智能合约有一些显著优点: 不需要中间人 费用低 代码就是规则 区块链网络中有多个备份,不用担心丢失 避免人工错误 无需信任,就可履行协议 匿名履行协议 以太坊(Ethe ...

  9. # PHP - 使用PHPMailer发邮件

    PHPMailer支持多种邮件发送方式,使用起来非常简单 1.下载PHPMailer https://github.com/PHPMailer/PHPMailer,下载完成加压后, 把下边的两个文件复 ...

随机推荐

  1. nginx日志分析工具

    https://www.linuxidc.com/Linux/2016-12/138731.htm

  2. The Secret Mixed-Signal Life of PWM Peripherals

    The Secret Mixed-Signal Life of PWM Peripherals Pulse-width modulation (PWM) peripherals have enjoye ...

  3. golang语言并发与并行——goroutine和channel的详细理解

    如果不是我对真正并行的线程的追求,就不会认识到Go有多么的迷人. Go语言从语言层面上就支持了并发,这与其他语言大不一样,不像以前我们要用Thread库 来新建线程,还要用线程安全的队列库来共享数据. ...

  4. Delphi 简单命名管道在两个进程间通讯

    服务器端代码: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Control ...

  5. C#编程(八十)---------- 异常类

    异常类 在C#里,异常处理就是C#为处理错误情况提供的一种机制.它为每种错误情况提供了定制的处理方式,并且把标志错误的代码预处理错误的代码分离开来. 对.net类来说,一般的异常类System.Exc ...

  6. MX4_ADB

    一.Ubuntu环境1. 建立或修改文件 ~/.android/adb_usb.ini,在文件开头或末尾添加一行,内容是0x2a45. 2. 建立或修改文件 /etc/udev/rules.d/51- ...

  7. python3 requests获取某网站折线图上数据

    比如要抓取某网站折线图上数据,如下截图: 借助Chrome开发者工具Network.经过分析发现获取上面的热度数据,找到对应的事件url:https://pcw-api.iqiyi.com/video ...

  8. LiteIDE 在 Windows 下为 Go 语言添加智能提示代码补全

    本文以 Windows 7 64 位为环境,go1.4.2.windows-amd64 和 liteidex27.2.1.windows-qt5 为例. 成功搭建开发环境后,发现 LiteIDE 没有 ...

  9. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

  10. 【Java】分布式RPC通信框架Apache Thrift 使用总结

    简介 Apache Thrift是Facebook开源的跨语言的RPC通信框架,目前已经捐献给Apache基金会管理,由于其跨语言特性和出色的性能,在很多互联网公司得到应用,有能力的公司甚至会基于th ...