整個過程主要演示chrome擴展 METAMASK, OpenZepplin庫和truffle框架的使用。
主要參考之前的以太坊-私有鏈搭建初步實踐, 這裏只用單節點的網絡。
還是先準備賬戶:
mkdir node0
# 會在node0/keystore目錄裏生成一個keyfile json文件
geth --datadir node0 account new
#利用puppeth生成genesis.json的過程不表,參考上邊的鏈接
geth --datadir node0 init genesis.json
# 把剛纔的賬號的密碼寫入node0/password文件
# 啓動私鏈,順便開啓console
echo node0 > node0/password
geth --datadir node0 --port 30000 --nodiscover --unlock '0' --password ./node0/password --mine --rpc --rpccorsdomain "*" --rpcapi "eth,net,web3,admin,personal" console
我們把這個賬號的json文件導入到chorme插件metamask裏,便於後面調試和演示
ubuntu系統上的chrome插件會有窗口消失的bug,在URL欄裏打開chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/popup.html
需要下載truffle命令號
npm install -g truffle
mkdir token && cd token
# 利用trulle下載token代筆示例
truffle unbox tutorialtoken
npm intall zeppelin-solidity
如上必要的依賴框架和庫已經下載到了本地, 接下來就創建自己的代幣合約
在contract目錄創建TutorialToken.sol文件,內容如下:
pragma solidity ^0.4.11;
import 'zeppelin-solidity/contracts/token/StandardToken.sol';
/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `StandardToken` functions.
*/
contract TutorialToken is StandardToken {
string public name = "TutorialToken";
string public symbol = "SIM";
uint256 public decimals = 18;
uint256 public INITIAL_SUPPLY = 10000;
/**
* @dev Contructor that gives msg.sender all of existing tokens.
*/
function TutorialToken() {
totalSupply = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
}
在以太坊裏幾乎所有操作都是當做交易來看的,部署合約就是一種交易,交易就要花錢(gas消耗),所以truffle做的是增量部署(少消耗gas),現在在migrations
目錄添加新的部署文件2_deploy_contracts.js
。
var TutorialToken = artifacts.require("./TutorialToken.sol");
module.exports = function(deployer) {
deployer.deploy(TutorialToken);
};
一切準備就緒,編譯,部署開始:
# 編譯 ➜ truffle compile Compiling ./contracts/Migrations.sol... Compiling ./contracts/TutorialToken.sol... Compiling zeppelin-solidity/contracts/math/SafeMath.sol... Compiling zeppelin-solidity/contracts/token/BasicToken.sol... Compiling zeppelin-solidity/contracts/token/ERC20.sol... Compiling zeppelin-solidity/contracts/token/ERC20Basic.sol... Compiling zeppelin-solidity/contracts/token/StandardToken.sol... Writing artifacts to ./build/contracts # 根據truffle.js的配置進行部署 ➜ truffle migrate Using network 'development'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0x65ccd2d6a4f4248466dd7887da7a2ac35d18c7ab0ec826cb25580bc785a2c3b8 Migrations: 0xc64569558f90302f4b3884929ac5540c645674dc Saving successful migration to network... ... 0xf9043ca886d352f05a05642047f63eed11d9b328fb815becc68baffc4d953d60 Saving artifacts... Running migration: 2_deploy_contracts.js Deploying TutorialToken... ... 0x19350625474c36316046b103e671eaad45834a60c17a5b9c64cf96316754560f TutorialToken: 0x7f469dc1ec17c3b7c52a3ad74611cb4b7e6807e1 Saving successful migration to network... ... 0xe57ba56dd5f1b18d410577def8bc7089f7de56e8d8718c3098430995d4b81353 Saving artifacts...
轉自:https://github.com/xiaoping378
順便分享兩個教程給大家,可以瞜一眼:
一個適合區塊鏈新手的以太坊DApp開發:
http://xc.hubwiz.com/course/5a952991adb3847553d205d1
一個用區塊鏈、星際文件系統(IPFS)、Nodejs和MongoDB構建電商平臺:
http://xc.hubwiz.com/course/5abbb7acc02e6b6a59171dd6