ERC20 自创代币

ERC20自创代币实现

区块链实验课作业


一、ERC20标准接口

contract ERC20 {
      function name() constant returns (string name)
      function symbol() constant returns (string symbol)
      function decimals() constant returns (uint8 decimals)
      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);
    }

二、准备工作

1、选择新建一个文件夹并进入
mkdir homework
cd homework
2、初始化
truffle init
npm init

此时会出现这样的目录结构
在这里插入图片描述

3、下载标准包openzeppelin-contracts

这里的库会提供标准接口,自己创建合约时候直接import即可

npm install zeppelin-solidity

4、之后开始创建自己的代币mycoin

先在contracts文件目录下创建一个文件 mycoin.sol

pragma solidity ^0.4.24;
import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

contract mycoin is StandardToken {
  string public name = "HamburgerCoin"; 
  string public symbol = "HBC";
  uint public decimals = 2;
  uint public INITIAL_SUPPLY = 10000 * (10 ** decimals);

  function mycoin() public {
    totalSupply_ = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
  }
}

上述合约内容中指定了合约的名称、符号和供应量。在 ERC20 当中,通证的供应量其实是整数,上述合约中通证的实际供应量是 10000 * 100 个,出于显示 2 位小数的需求,你在合约浏览器、钱包软件中看到和操作的 1 个通证,实际上在交易中是以 100 个进行的

5、compile

来编译我们的合约

truffle compile

在这里插入图片描述
Compiled successfully
表示编译成功

6、deploy

在migrations文件下创建deploy_1.js文件

var mycoin = artifacts.require("mycoin");



module.exports = function(deployer) {

deployer.deploy(mycoin);

}


7、migrate
truffle migrate

在这里插入图片描述

这里可以看到地址,gas等详细信息

8、挂载上ganache测试链

将homework项目的truffle-config.js文件加入到ganache的sever中并且保存

在这里插入图片描述

在这里插入图片描述

已经挂载成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是zp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值