truffle集以太坊智能合約編譯、測(cè)試、部署為一體的開(kāi)源框架,方法簡(jiǎn)單,功能強(qiáng)大。
1、安裝truffle
安裝比較簡(jiǎn)單
$ npm install -g truffle
2、創(chuàng)建項(xiàng)目
有兩種創(chuàng)建方式,第一種是初始化一個(gè)空白項(xiàng)目,第二種是克隆一個(gè)truffle倉(cāng)庫(kù)unbox中的一個(gè)項(xiàng)目??梢愿鶕?jù)自己需求選擇。
我這里直接使用官方提供的一個(gè)發(fā)幣合約metacoin合約。
$ mkdir MetaCoin
$ cd MetaCoin
$ truffle unbox metacoin //下載metacoin合約,如果是初始化一個(gè)空項(xiàng)目,使用truffle init
下載完成后如下圖所示(編譯完成后還會(huì)有一個(gè)build目錄):

3、編譯
$ truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/linjingjing/Documents/project/MetaCoin/build/contracts
> Compiled successfully using:
- solc: 0.5.0+commit.1d4f565a.Emscripten.clang
編譯完成后合約的ABI和Bytecode在build/contracts目錄下。
4、發(fā)布
網(wǎng)上很多寫(xiě)truffle的資料,都是將合約發(fā)布到truffle develop或Ganache提供的本地以太坊網(wǎng)絡(luò)中。本地網(wǎng)絡(luò)已提供了解鎖的以太坊賬戶供測(cè)試,而如果要發(fā)布到主網(wǎng)時(shí),就需要使用真實(shí)以太坊賬號(hào)私鑰進(jìn)行簽名部署。
這里需要對(duì)truffle-config.js文件進(jìn)行配置。
安裝truffle-hdwallet-provider模塊
npm install truffle-hdwallet-provider
truffle-config.js內(nèi)容如下:
const HDWalletProvider = require('truffle-hdwallet-provider');
module.exports = {
networks: {
//開(kāi)發(fā)測(cè)試網(wǎng)絡(luò)
development: {
host: "127.0.0.1",
port: 9545,
network_id: "*"
},
// ropsten測(cè)試網(wǎng)絡(luò),mnemonic為賬戶的助記詞或者私鑰
ropsten: {
provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
network_id: 3, // Ropsten's id
gas: 5500000, // Ropsten has a lower block limit than mainnet
confirmations: 2, // # of confs to wait between deployments. (default: 0)
timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
},
//主網(wǎng)
advanced: {
provider: () => new HDWalletProvider(mnemonic, `https://mainnet.infura.io`),
network_id: 1, // Custom network
gas: 8500000, // Gas sent with each transaction (default: ~6700000)
gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
from: <address>, // Account to send txs from (default: accounts[0])
websockets: true // Enable EventEmitter interface for web3 (default: false)
}
},
// Set default mocha options here, use special reporters etc.
mocha: {
// timeout: 100000
},
// Configure your compilers
compilers: {
solc: {
version: "0.4.25", // Fetch exact version from solc-bin (default: truffle's version)
docker: false, // Use "0.5.1" you've installed locally with docker (default: false)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: false,
runs: 200
},
evmVersion: "byzantium"
}
}
}
}
這里需要說(shuō)明的是,mnemonic參數(shù)可以是賬戶的助記詞也可以是私鑰(private),keystore轉(zhuǎn)換為privatekey可參考文章:
http://www.itdecent.cn/p/1e5c55529eff
4、發(fā)布
$ truffle migrate --network advanced
--network參數(shù)可選擇你要發(fā)布到哪個(gè)網(wǎng)絡(luò)上。
5、查看
$ truffle console --network ropsten
truffle(ropsten)> let instance = await MetaCoin.deployed()
undefined
truffle(ropsten)> let accounts = await web3.eth.getAccounts()
undefined
truffle(ropsten)> let balance = await instance.getBalance(accounts[0])
undefined
truffle(ropsten)> balance.toNumber() //發(fā)行的metacoin幣總額
10000
總結(jié)
使用truffle發(fā)不到非本地的以太坊主網(wǎng)或者測(cè)試網(wǎng)時(shí),需要提供錢包的助記詞或私鑰,使用truffle-hdwallet-provider模塊來(lái)實(shí)現(xiàn)交易簽名。
參考資料
- 官網(wǎng):https://truffleframework.com/docs/truffle/overview
- github:https://github.com/trufflesuite/truffle
- 博客:http://truffle.tryblockchain.org
- truffle-hdwallet-provider:https://www.npmjs.com/package/truffle-hdwallet-provider
- 以太坊各種網(wǎng)絡(luò)鏈接地址: https://infura.io
- 獲取ropsten測(cè)試幣:https://faucet.metamask.ioimage.png
