truffle部署合約到以太坊主網(wǎng)上

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目錄):


image.png

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 developGanache提供的本地以太坊網(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)交易簽名。

參考資料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容