使用zeppelin-solidity建立标准代币

1 构建工程

1.1 使用truffle创建工程
$ truffle init

$ truffle init
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

1.2 生成package.json文件,运行命令:
$ npm init

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (deepcoin) dpc
version: (1.0.0) 
description: 
entry point: (truffle-config.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/fc/work/truffledemo/deepcoin/package.json:

{
  "name": "dpc",
  "version": "1.0.0",
  "description": "",
  "main": "truffle-config.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

2 安装OpenZeppelin

OpenZeppelin是一个加密合约函数库,提供了兼容ERC20的智能合约,可以使用它来简化钱包的开发过程。
使用npm进行安装

sudo npm install zeppelin-solidity

3 编写代码

3.1 在contracts目录下,创建DeepCoin.sol文件,内容如下:

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

contract DeepCoin is StandardToken {
  string public name = "DeepCoin";
  string public symbol = "DPC";
  uint8 public decimals = 4;
  uint256 public INITIAL_SUPPLY = 888888;
  constructor() public{
    totalSupply();
    balances[msg.sender] = INITIAL_SUPPLY;
  }
}

3.2 在migrations下面创建2_deploy_deepcoin.js文件,内容如下:

var DeepCoin = artifacts.require("./DeepCoin.sol");

module.exports = function(deployer) {
  deployer.deploy(DeepCoin);
};

4 编译、部署、运行

进入控制台:$ truffle develop
编译:compile
部署: migrate
第二次部署:migrate --reset
创建实例:let contract = DeepCoin.deployed().then(instance => contract = instance);

Accounts:
(0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
(1) 0xf17f52151ebef6c7334fad080c5704d77216b732

contract.name();
contract.symbol();
contract.decimals();
contract.INITIAL_SUPPLY();
contract.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57");
contract.balanceOf("0xf17f52151ebef6c7334fad080c5704d77216b732");
contract.transfer("0xf17f52151ebef6c7334fad080c5704d77216b732",8888);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值