5,Dapp的部署與交互
5.1 使用traffle 部署合約
進(jìn)入一個(gè)空目錄,執(zhí)行
$truffle init
在contract目錄下,新增.sol文件用來(lái)寫(xiě)合約代碼
在migration目錄下新增2_deploy_contracts.js文件,內(nèi)容如下:
var YourContract = artifacts.require(“./你的合約文件名.sol");
module.exports = function(deployer) {
deployer.deploy(YourContract);
};
本目錄下執(zhí)行
$truffle compile
在truffle項(xiàng)目下獲取package.json
$npm init -f
安裝web3
$npm install —save web3@0.20
編輯 truffle.js 加入步驟1的node11節(jié)點(diǎn)信息
module.exports = {
networks: {
nodeth: { // “nodeth” 是我給網(wǎng)絡(luò)起的名字
network_id: 999, // network id associated with your node
host:'127.0.0.1',
port:8811, // same with node11 supported
gas: 400000000,
from: “0x6875483cd851990ddfcd5fd49f6732d71cbedb46”. // coinbase for node11
}
}
};
Truffle 目錄下執(zhí)行命令
$truffle deploy — network nodeth // “nodeth” 對(duì)應(yīng)truffle.js 配置里的網(wǎng)絡(luò)名
第二次部署可以用· truffle migrate —reset —network {you Network}
在geth控制臺(tái)終端執(zhí)行挖礦,將會(huì)看到新合約被部署了,合約地址被返回