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);
网友评论