[Ethereum] How to specify which wallet address to deploy contract with

truffleweb3js

When deploying with Truffle how do you specify which wallet address to deploy with? I know if you're using web3 you can specify web3.eth.defaultAccount but how would I do this Truffle? I read somewhere to adjust your truffle.js file and add a from but it spits an error for me. How would truffle know which address I'm using to interact with it, e.g. me using MetaMask.

Best Answer

By default, truffle uses the first account but in truffle.js you can specify the account address in the from property:

module.exports = {
  networks: {
    development: {
      host: 'localhost',
      port: 8545,
      network_id: '*', 
      from: '0x0c439ff0170Ab7d7Ef55f2993554eede7321c9b7'
    }
  }
}

If you want to deploy to a network other than localhost then you need to import a wallet provider and pass it your mnemonic.

Here's an example where the mnemonic is read from an environment variable and the provider is set to rinkeby testnet:

const HDWalletProvider = require('truffle-hdwallet-provider')

const mnemonic = process.env.MNEMONIC

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" 
    },
    rinkeby: {
      provider: new HDWalletProvider(mnemonic, 'https://rinkeby.infura.io'),
      network_id: '*',
      gas: 4500000,
      gasPrice: 25000000000
    }
  }
}
Related Topic