2

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.

2
  • Can you paste your truffle.js file, the error, and tell us which network you want to deploy to ? Nov 30 '17 at 22:50
  • I'm trying to deploy to testrpc. I'm just wondering how you can get the address that's interacting with a deployed contract Nov 30 '17 at 23:06
3

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
    }
  }
}
2
0

The 'from' field didn't work for me, instead, I went to my truffle-config.js file and inserted an additional third parameter for new HDWalletProvider. The details are elaborated on the trufflesuite sub repo

ropsten: {
  provider: () => new HDWalletProvider(mnemonic, `https://<TESTNET NAME>.infura.io/v3/<INFURA-PROJECT-ID>`,2), //2 here means that truffle will use the wallet address at index 2 when deploying. If you have 3 wallets on MetaMask and you want to use the third wallet, the index should be 2. 
  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 )
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.