参考:https://www.ethereum.org/token

pragma solidity ^0.4.16;interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }contract TokenERC20 {// Public variables of the tokenstring public name;string public symbol;uint8 public decimals = 18;// 18 decimals is the strongly suggested default, avoid changing ituint256 public totalSupply;// This creates an array with all balancesmapping (address => uint256) public balanceOf;mapping (address => mapping (address => uint256)) public allowance;// This generates a public event on the blockchain that will notify clientsevent Transfer(address indexed from, address indexed to, uint256 value);// This generates a public event on the blockchain that will notify clientsevent Approval(address indexed _owner, address indexed _spender, uint256 _value);// This notifies clients about the amount burntevent 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 amountbalanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokensname = tokenName;                                   // Set the name for display purposessymbol = 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() insteadrequire(_to != 0x0);// Check if the sender has enoughrequire(balanceOf[_from] >= _value);// Check for overflowsrequire(balanceOf[_to] + _value >= balanceOf[_to]);// Save this for an assertion in the futureuint previousBalances = balanceOf[_from] + balanceOf[_to];// Subtract from the senderbalanceOf[_from] -= _value;// Add the same to the recipientbalanceOf[_to] += _value;emit Transfer(_from, _to, _value);// Asserts are used to use static analysis to find bugs in your code. They should never failassert(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 returns (bool success) {_transfer(msg.sender, _to, _value);return true;}/*** 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 allowanceallowance[_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) publicreturns (bool success) {allowance[msg.sender][_spender] = _value;emit Approval(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)publicreturns (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 enoughbalanceOf[msg.sender] -= _value;            // Subtract from the sendertotalSupply -= _value;                      // Updates totalSupplyemit 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 enoughrequire(_value <= allowance[_from][msg.sender]);    // Check allowancebalanceOf[_from] -= _value;                         // Subtract from the targeted balanceallowance[_from][msg.sender] -= _value;             // Subtract from the sender's allowancetotalSupply -= _value;                              // Update totalSupplyemit Burn(_from, _value);return true;}
}

approveallowance的用法

账户A有1000个ETH,想允许B账户随意调用100个ETH。A账户按照以下形式调用approve函数approve(B,100)。当B账户想用这100个ETH中的10个ETH给C账户时,则调用transferFrom(A, C, 10)。这时调用allowance(A, B)可以查看B账户还能够调用A账户多少个token。

—未完待续—

【以太坊开发】发币指南--进阶篇相关推荐

  1. 以太坊开发入门,完整入门篇(小白可以看看,高手看看自己有没有遗漏的

    2019独角兽企业重金招聘Python工程师标准>>> 翻译自:https://medium.com/@mattcondon/getting-up-to-speed-on-ether ...

  2. 【以太坊开发】发币指南--基础篇

    官宣:https://www.ethereum.org/token 核心逻辑演进按照官网的示例,个人的工作就是从我自己的视角讲清楚这个逻辑.图文仅作为参考. 首先我们需要知道,标准的发币合约是比较复杂 ...

  3. 以太坊开发入门,完整入门篇

    一个适合区块链新手的以太坊DApp开发教程: http://xc.hubwiz.com/course/5a952991adb3847553d205d1 一个用区块链.星际文件系统(IPFS).Node ...

  4. 以太坊是什么 - 以太坊开发入门指南

    本文首发于深入浅出区块链社区 原文链接:以太坊是什么 - 以太坊开发入门指南 很多同学已经跃跃欲试投入到区块链开发队伍当中来,可是又感觉无从下手,本文将基于以太坊平台,以通俗的方式介绍以太坊开发中涉及 ...

  5. 在以太坊开发自己的ERC-20代币及如何ICO

    今天我将向你展示如何在以太坊区块链上开发你自己的加密货币并将其出售!我将向你展示如何使用以太坊智能合约逐步创建自己的ERC-20代币和众筹销售,如何测试智能合约,如何将智能合约部署到以太坊区块链,以及 ...

  6. 用OpenZeppelin在RSK上进行以太坊ERC20代币开发

    在本文中,我们将讨论通过RSK网络部署和交互Smart-Contracts智能合约.我们的合约将是一个基于OpenZeppelin库的ERC20代币,我们将把它直接部署到Mainnet中. 创建合约 ...

  7. 以太坊java开发指南_java以太坊开发库ethereumj

    EthereumJ是以太坊协议的纯Java实现.有关以太坊及其目标的高级信息,请访问ethereum.org,其 白皮书 提供了一个完整的概念的概述,和 黄皮书 一起提供了协议的正式定义. 我们尽可能 ...

  8. 币图网以太坊开发实例_去中心化概念模型与架构设计

    IM 去中心化概念模型与架构设计 今天打算写写关于 IM 去中心化涉及的架构模型变化和设计思路,去中心化的概念就是说用户的访问不是集中在一个数据中心,这里的去中心是针对数据中心而言的. 站在这个角度而 ...

  9. Web程序员如何入门以太坊开发

    我经常构建使用以太坊的Web应用程序,我理所当然地认为每天都使用的是神奇的工具集.我们的生态系统正在迅速发展,我认为很多新人都感到不知所措.以太坊是一项了不起的技术,但它也是新生的,而且根本没有足够的 ...

最新文章

  1. apache与tomcat连接
  2. docker-compose:使用docker-compose部署nginx+supervisor+uwsgi+flask程序(mongodb)
  3. ●BZOJ 4596 [Shoi2016]黑暗前的幻想乡
  4. 成本中心的费用计划/KP06
  5. 【Java7】练习:选角色,挑苹果,员工类,换心脏,斗地主,发工资,客户信息管理软件,开发团队调度系统
  6. 高级 Java 必须突破的 10 个知识点
  7. Linux kubuntu x64系统下解决QT5.12编辑菜单和工具栏不显示图标问题
  8. 7-81 单词长度 (15 分)
  9. c#ovalshape_How to: Draw Shapes with the OvalShape and RectangleShape Controls (Visual Studio)
  10. JvavScript中的函数与对象
  11. 电脑打开任务管理器的方法有哪几种
  12. win10软件拒绝访问删不掉_谷歌工程师正在解决Windows 10防病毒软件导致谷歌浏览器不稳定问题...
  13. 深度学习模型高并发方案
  14. Linux的PDF工具,Linux 系统中的pdf阅读器以及工具
  15. Uni-app 小程序 App 的广告变现之路:激励视频广告
  16. 【从蛋壳到满天飞】JS 数据结构解析和算法实现-链表
  17. PIXI 宝物猎人(7)
  18. 计算机网络的雏形为,计算机网络的发展雏形是什么(图文)
  19. 网站服务器迁移域名怎么配置文件,记录网站服务器搬家如何迁移 Let’s Encrypt 证书 | 老左笔记...
  20. 如何使用谷歌浏览器远程调试安卓/ios真机H5应用?

热门文章

  1. (01)OpenGL es中只在指定区域渲染view
  2. 基于catia活塞的有限元分析_渐开线插齿刀自动化设计系统及有限元分析
  3. 三菱a系列motion软体_通化三菱Q00JCPU
  4. 土豆 android 缓存路径,#土豆记事#教你开发Android App之 —— Hello Android
  5. android开发 解析 b5,张绍文android开发高手课读书笔记4-启动优化篇
  6. python怎么索引txt数据中第四行_python-在熊猫数据框中按行计数编制索引
  7. 静态配置_配置静态LSP示例
  8. python与贝叶斯_python-与PyMC3的贝叶斯相关
  9. apan在PHP什么意思,Apanteles是什么意思
  10. Hadoop体系结构– YARN,HDFS和MapReduce