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

发币代码如下(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. linux sar详解

    sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...

  2. __Linux__操作系统发展史

    常见操作系统win7.Mac.Android.iOS . 操作系统的发展史 1.Unix 1965年之前的时候,电脑并不像现在一样普遍,它可不是一般人能碰的起的,除非是军事或者学院的研究机构,而且当时 ...

  3. 使用 IntraWeb (32) - Url 映射与 THandlers

    最简单的 Url 映射是使用 TIWAppForm 的 class 方法: SetURL; THandlers 是 IntraWeb XIV 新增的内容处理器, 它能完成的不仅仅是 Url 映射(转发 ...

  4. 阿里最新热修复Sophix与QQ超级补丁和Tinker的实现与总结

    2015年以来,Android开发领域里对热修复技术的讨论和分享越来越多,同时也出现了一些不同的解决方案,如QQ空间补丁方案.阿里AndFix以及微信Tinker(Bugly sdk也集成Tikner ...

  5. Kafka的安装和设置

    Kafka是一种分布式发布订阅消息系统. Kafka有三种模式: (1)单节点单Broker,在一台机器上运行一个Kafka实例: (2)单节点多Broker,在一台机器上运行多个Kafka实例: ( ...

  6. 【微信上传素材接口--永久性】微信永久性上传、获取返回的medie_id 和url

    上传图片到微信服务器获得media_id和url (永久性) 其他接口类:https://www.cnblogs.com/gjw-hsf/p/7375261.html 转载地址:https://blo ...

  7. 通过jarjar.jar来替换jar包名的详细介绍

    有时候我们根据一些场景 需要替换第三方jar包的包名,比如Android广告平台sdk,更换他们jar包包名的话,可以防止市场检测到有广告插件,所以,今天就介绍一下如何使用jarjar.jar工具来替 ...

  8. aaronyang的百度地图API之LBS云与.NET开发 Javascript API 2.0【把数据存到LBS云2/2】

    中国的IT 需要无私分享和贡献的人,一起努力 本篇博客来自地址:http://www.cnblogs.com/AaronYang/p/3672898.html,请支持原创,未经允许不许转载 1.新建一 ...

  9. [k8s]nginx-ingress配置4/7层测试

    基本原理 default-backend提供了2个功能: 1. 404报错页面 2. healthz页面 # Any image is permissable as long as: # 1. It ...

  10. Linux嵌入式时区修改