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

发币代码如下(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. Go语言之高级篇beego框架之cookie与session

    1.cookie的用法 this.Ctx.SetCookie("name", name, maxage, "/") this.Ctx.SetCookie(&qu ...

  2. 读懂isi get的结果

    你想知道的一切,在这里: Isi Get & Set https://community.emc.com/community/products/isilon/blog/2018/02/21/i ...

  3. Spark2.2(三十三):Spark Streaming和Spark Structured Streaming更新broadcast总结(一)

    背景: 需要在spark2.2.0更新broadcast中的内容,网上也搜索了不少文章,都在讲解spark streaming中如何更新,但没有spark structured streaming更新 ...

  4. alpha to coverage

    alpha to coverage 在游戏中,经常使用带有半透明信息纹理的多边形模型来模拟复杂的物体,例如,草.树叶.铁丝网等.如果使用真正的模型,一颗边缘参差不齐的小草可能就要消耗掉几百个多边形:然 ...

  5. cocos2d-x 开发用到的工具

    1.VertexHelper 可用于多边形顶点的计算,可视化定点编辑器,用它创建的顶点信息可以直接导出为Box2D可使用的代码 https://github.com/jfahrenkrug/Verte ...

  6. 微软BI 之SSIS 系列 - 通过 ROW_NUMBER 或 Script Component 为数据流输出添加行号的方法

    开篇介绍 上午在天善回答看到这个问题 - SSIS 导出数据文件,能否在第一列增加一个行号,很快就帮助解决了,方法就是在 SQL 查询的时候加一个 ROW_NUMBER() 就可以了. 后来想起在两年 ...

  7. EOF多行写入文件防止变量替换

    问题描述 对多个变量及多行输出到文件,存在变量自动替换,当使用cat<<EOF不想对内容进行变量替换.命令替换.参数展开等 问题解决 转义特殊字符如 $ `等 一.对 $·\ 进行转义 c ...

  8. Effective Java 第三版——45. 明智审慎地使用Stream

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  10. android4.3 截屏功能的尝试与失败分析

    1.背景 上一篇讲了在源码中捕获到了android手机的截屏函数(同时按下电源键与音量减,详情http://blog.csdn.net/buptgshengod/article/details/199 ...