truffle編譯部署以太坊智能合約

truffle編譯部署以太坊智能合約

4 人贊了文章

truffle開發環境搭建:

第一步:

安裝nodejs和npm:

wget

nodejs.org/dist/v6.10.2

tar

-xvf node-v6.10.2-linux-x64.tar.xz -C /truffle

在truffle目錄下看到node-v6.10.2-linux-x64即ok

第二步:

設置PATH環境變數,編輯~/.bash_profile,並在末尾添加node文件夾路徑到$PATH環境變數中

PATH="$HOME/bin:$HOME/.local/bin:/truffle/node-v6.10.2-linux-x64/bin:$PATH

重啟~/.bash_profile里設置的$PATH:

source

~/.bash_profile

檢查node:node

--version

檢查npm:npm

--version

第三步:

安裝truffle:npm

install -g truffle (前提是安裝了npm和node)

Truffle部署智能合約

一、 truffle編譯智能合約

pc1終端一:

1、 新建項目,執行truffle init命令:truffle init

界面如下:

2、 編寫Coin.sol合約,並放至contract目錄下

例如:Coin.sol,內容如下:

pragma

solidity ^0.4.0;

contract

Coin {

address public minter;

mapping (address => uint) public

balances;

event Sent(address from, address to, uint

amount);

function Coin() public {

minter = msg.sender;

}

function mint(address receiver, uint

amount) public {

if (msg.sender != minter) return;

balances[receiver] += amount;

}

function send(address receiver, uint

amount) public {

if (balances[msg.sender] < amount)

return;

balances[msg.sender] -= amount;

balances[receiver] += amount;

Sent(msg.sender, receiver, amount);

}

}

3、 修改配置

Migrations目錄下添加***.js配置文件,

例如:添加2_initial_coin.js文件,內容如下:

var Coin =

artifacts.require("./Coin.sol");

module.exports

= function(deployer) {

deployer.deploy(Coin);

};

修改truffle.js文件,內容如下:

module.exports

= {

networks: {

development:

{

host:

"localhost",

port:

8545,

network_id:

9000 // match any network

},

}

}

4、 編譯Coin.sol合約

命令如下:truffle compile

Pc1終端二:

1、 開啟終端二,啟動節點:

geth --identity "3333" --rpc --rpccorsdomain

"*" --datadir

"/ethereum/privatechain/privatechain_truffle/data/00/" --port 30303 --rpcapi

"db,eth,net,web3" --networkid 9000 console

(註:若truffle.js文件中指定network_id,則啟動節點中--networkid需於truffle.js文件中network_id一致)

2、 解鎖節點賬戶

personal.unlockaccount(eth.coinbase)

3、 終端一部署合約

truffle migrate --network development

4、 終端二執行挖礦,開始部署合約

miner.start(1)

結束挖礦:miner.stop()

二、 合約交互

1、 兩台pc,實現節點信息同步

1>啟動pc2終端一

pc2:geth

--datadir /ethereum/privatechain/privatechain_truffle/data/00/ --networkid 9000

--port 20000 console

2>獲取pc2的ip及節點enode

例:

Enode:

"enode://f29b4d27e5d0ae377746ded575c6fa06849bbe4d06fa9089c2162810232dea9a523d13424c9deecfa07ae859d0f3c37d84044e466595a6afd4b449180cb694e9@[::]:20000"

Ip:192.168.31.146

3>pc1終端二同步pc2節點

例:

admin.addPeer("enode://f29b4d27e5d0ae377746ded575c6fa06849bbe4d06fa9089c2162810232dea9a523d13424c9deecfa07ae859d0f3c37d84044e466595a6afd4b449180cb694e9@192.168.31.146:20000")

界面如下:

2、 部署調用Coin合約

第一步:提取Coin.json文件中abi及合約地址

第二步:pc1終端二執行以下命令

abi = [{"constant":true,"inputs":[],"name":"minter","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"send","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Sent","type":"event"}]

test = eth.contract(abi).at("0x9a6bb419f67509922259dad54e092d52e3d023ab")

同理,在pc2終端一執行上該命令;

3、 驗證合約

1>查詢賬戶餘額

pc1終端二查看當前賬戶是否有餘額:test.balances(eth.accounts[0])

2>pc1賬戶向pc2賬戶發送交易

解鎖pc1賬戶:personal.unlockAccount(eth.coinbase)

發送交易:test.mint.sendTransaction("0x07bf271313a98f6aef8253e831cbfc3322888855",130,{from:"0x75b7880e7826d93c5e13c2d93f0441d6704b8cda"})

挖礦確認交易:miner.start(1)

查詢pc2賬戶"0x07bf271313a98f6aef8253e831cbfc3322888855"餘額是否增加:

test.balances("0x07bf271313a98f6aef8253e831cbfc3322888855")

pc2賬戶向pc1賬戶發送交易:

test.send.sendTransaction("0x75b7880e7826d93c5e13c2d93f0441d6704b8cda",30,{from:"0x07bf271313a98f6aef8253e831cbfc3322888855"})

界面如下:


推薦閱讀:

區塊鏈在實際生活場景中的應用?
如果加密貨幣背後有操縱者,那會是誰?
龍爭虎鬥,幣圈兩大巨頭因為一個人幹起來了
ETC和ETH的相同和不同
以太坊是什麼?

TAG:以太坊 | 智能合約 |