美文网首页
使用MetaMask+Truffle部署合约

使用MetaMask+Truffle部署合约

作者: arthur25 | 来源:发表于2019-01-16 10:41 被阅读26次

Steps to deploy a contract using MetaMask and Truffle

Deploy smart contract on Ropsten network with MetaMask & Truffle. Two solutions to start with, one is to run your own geth node which consumes lot of cpu from your machine. In my MacBook Pro, running a geth node for 15 minutes will drain the full battery and the other solution is to connect with a public node like infura

Solution 1: With you own node

You can easily deploy on the ropsten network if you own a full node running on your machine.

  • Run geth
$ geth --fast --cache=1048 --testnet --unlock "0xmyaddress" --rpc --rpcapi "eth,net,web3" --rpccorsdomain '*' --rpcaddr localhost --rpcport 8545
  • In truffle.js, add the following configure for the ropsten network
module.exports = {
  networks: {
    localhost: {
      host: "localhost", 
      port: 8545,
      network_id: "*" 
    },  
    ropsten: {
      host: "localhost",
      port: 8545,
      gas: 4700000,
      gasPrice: 1000000000,
      network_id: "3"
    }
  }
};
  • Deploy on the ropsten network
truffle migrate --network ropsten
Solution 2: With a public node like Infura
  • Install the needed libraries

    Navigate into the project folder and run the following command:

    npm install truffle-hdwallet-provider --save
    
  • In truffle.js, Add the following code to unlock your Metamask account and configure the Infura Ropsten node as entry point by providing the mnemonic phrase (Metamask / Settings / Reveal Seed Words)

var HDWalletProvider = require("truffle-hdwallet-provider");
var infura_apikey = "XXXXXX";
var mnemonic = "twelve words you can find in metamask/settings/reveal seed words";
module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    ropsten: {
      provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/"+infura_apikey),
      network_id: 3
    }
  }
};
  • Deploy on the ropsten network
$ truffle migrate --network ropsten

相关文章

网友评论

      本文标题:使用MetaMask+Truffle部署合约

      本文链接:https://www.haomeiwen.com/subject/ryipdqtx.html