文章内容主要来自于 https://www.ethereum.org/token。 笔者没有过英语4级,翻译的不好请见谅 _

代币

我们将创建一个数字令牌(digital token)。以太坊生态系统中的token可以代表任何可替换可交易的物品:硬币,金币,游戏内物品等。任何token都以标准方式实现一些基本功能。这意味着您的token可以兼容以太坊钱包和其他任何使用形同标准的contract。
我们所说的代币,绝大多数都是token。

一个基于ERC20发行的token

先看一下代币的样子:
进入网站:https://etherscan.io/。随便找一个代币,点进去查看:

接下来看一下ERC20标准。以下是ERC20标准的接口声明:

  • ERC20标准英文文档
  • ERC20标准中文文档
// https://github.com/ethereum/EIPs/issues/20
contract ERC20 {string public constant name = "Token Name";//token的名字、代币名称string public constant symbol = "SYM"; // token的简写/代币符号uint8 public constant decimals = 18;  // 代币小数点位数,代币的最小单位, 18表示我们可以拥有 .0000000000000000001单位个代币。 官方推荐18。不要轻易改变function totalSupply() constant returns (uint totalSupply);//发行代币总量。 function balanceOf(address _owner) constant returns (uint balance);//查看对应账号的代币余额。 function transfer(address _to, uint _value) returns (bool success);//实现代币交易,用于给用户发送代币(从我们的账户里)。 function transferFrom(address _from, address _to, uint _value) returns (bool success);//实现代币用户之间的交易。 function approve(address _spender, uint _value) returns (bool success);//允许用户可花费的代币数。function allowance(address _owner, address _spender) constant returns (uint remaining);//控制代币的交易,如可交易账号及资产。 event Transfer(address indexed _from, address indexed _to, uint _value);event Approval(address indexed _owner, address indexed _spender, uint _value);
}

直接上代码:

pragma solidity ^0.4.16;interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }contract LBL {// 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 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 LBL (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 {_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 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;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;}
}

将上述代码贴到Remix 中(https://ethereum.github.io/browser-solidity):


上图中create按钮填入你想要的发行量,名称及代号,就可以创建合约了。
这时MetaMask会弹出一个交易确认框,点SUBMIT。待合约部署交易确认之后,复制合约地址。

如果你没有余额,需要先充值,才能发布成功。复制上述合约地址,牢记,否则合约就白部署了。你的以太币也浪费了。

代币的交易

上面部署好了以后,不方便查看和交易,下面我们将通过MetaMask和myetherwallet两个钱包来实现我们代笔的交易。

以太坊轻钱包MetaMask详细图文教程

参见 以太坊轻钱包MetaMask详细图文教程。 读者请自行安装部署

安装好了以后,打开Metamask界面,切换到TOKENS,点添加合约,出现如下对话框:

填入刚刚复制的地址,点ADD,这时你就可以看到你创建的代币了,如图:

到此为止,我们已经完成了代币的发放部署。可以在Etherscan上查询到我们刚刚部署的代币了

可以在以太坊浏览器上查看到我的代币 https://etherscan.io/address/0xFFCABB58bE7de3579bE5c423451896eb89bF18Ff:

使用MyEtherWallet交易

由于MetaMask插件没有提供代币交易功能,以太坊钱包又受限于网速等原因特别不好用。所以我们选择myetherwallet。

  1. 进入myetherwallet网页钱包地址, 第一次进入有一些安全提示需要用户确认。
  2. 进入之后,按照下图进行设置:
  3. 链接上以后,添加代币地址:

  1. 进行代币转账交易

    在接下来的交易确认也,点击确认即可。
  2. 交易完成后,可以看到MetaMask中代币余额减少了,如图:

基于以太坊实现代币|基于以太坊实现你自己的加密货币相关推荐

  1. erc20钱包下载_以太坊ERC20代币数据集【1000+】

    Erc20Tokens数据集包含超过1000种主流的以太坊ERC20代币的描述数据清单和图标,可用于钱包等区块链应用的开发,支持使用Java.Python.Php.NodeJs.C#等各种开发语言查询 ...

  2. java 创建以太坊代币_以太坊 (五)编写智能合约-建立简易加密代币

    本篇文章,我们将写一个简易的加密代币的智能合约来给大家诠释加密代币的原理 启动 ganache-cli 打开终端,启动ganache-cli,相关环境在区块链学习日记(四)这篇文章里面已经有具体说明. ...

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

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

  4. 以太坊ERC20代币合约案例

    一.ERC20代币合约与web3调用 ERC20代币合约在小白看来觉得很高大上,但其实就是一个代币的定义标准,方便其他dapp统一调用各种代币的方法.如图: 二.ERC20合约标准 [官方链接] co ...

  5. 基于BCH的SLP代币超过1000种,探秘SLP的内部生态

    在市值排行前六的加密货币中,有三种加密货币是主打支付功能的,有两种加密货币则是主打公链功能,而BCH则是其中一个既具有支付功能,又具有Token功能的加密货币.这得益于BCH社区推出的一系列Token ...

  6. [转]NEO与以太坊:为什么NEO可能是2018年最强的加密货币

    本文转自:https://baijiahao.baidu.com/s?id=1591291802129464257&wfr=spider&for=pc NEO,它可以与以太坊竞争吗?N ...

  7. 纳斯达克支持的欧盟交易所DX推出证券型代币交易

    点击上方 "蓝色字" 可关注我们! 暴走时评: 总部位于爱沙尼亚的纳斯达克数字交易平台DX.Exchange凭借其推出的证券型代币交易和STO,成为行业首创. DX.Exchang ...

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

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

  9. 以太坊构建DApps系列教程(二):构建TNS代币

    在本系列关于使用以太坊构建DApps教程的第1部分中,我们引导大家做了两个版本的本地区块链进行开发:一个Ganache版本和一个完整的私有PoA版本. 在这一部分中,我们将深入研究并构建我们的TNS代 ...

最新文章

  1. R语言tidyr包pivot_longer函数、pivot_wider函数数据表变换实战(长表到宽表、宽表到长表)
  2. 初次树莓派遇到的一些小问题
  3. Python 使用@property对属性进行数据规范性校验
  4. boost::basic_thread_pool相关的测试程序
  5. Scala编译器安装,开发工具安装,通过IDEA创建scala的工程代码,Scala SDK的设置
  6. 旧访客设计模式的新生活
  7. Java集合之TreeMap源码解析上篇
  8. 智慧城市这份试卷 国外城市怎么答题?
  9. 创建索引的方法有两种
  10. CvMat与LIplmage之间的相互转换__cvConvert()
  11. linux root 设置中文,ubuntu 8.04 root用户下的中文环境配置-Linux频道-中国IT实验室
  12. 2013年思科万物互联IoE十大见解
  13. java自学之路-day19
  14. WCDMA中的基本概念
  15. AutoIt:工具栏中没有其它工具,eg,Koda
  16. 软件版本的GA 代表什么意思?
  17. python from用法_python学习笔记1_import与from方法总结
  18. buctoj 2407 B 竖式 题解
  19. 微信公众号开发整理(五)--自定义菜单
  20. 使用c#完成数据库的crud操作

热门文章

  1. python csv空格分隔符_CSV导入到Python中的空格分隔符
  2. 光路科技工业PoE交换机助力智利5G网络建设
  3. linux文件系统和日志分析!
  4. Canvas绘制动画
  5. sql server小型案例-自动生成销售单号的触发器
  6. java程序员—工作中开发经验总结
  7. 遥控助手-支持蓝牙、红外、WIFI、投屏
  8. ​深度相机(TOF)的工作原理​
  9. c语言编译时打印宏的值
  10. 分享一个基于 ABP(.NET 5.0) + vue-element-admin 管理后台