[Ethereum] “Migrations” could not deploy due to insufficient funds

contract-deploymentmetamaskrinkebysoliditytruffle

I'm getting the error:

"Migrations" could not deploy due to insufficient funds.
Sender doesn't have enough funds to send tx.
The upfront cost is: 45000000000000000 and the sender's account only has: 0

as I'm migrating into Rinkeby test network. I have enough ether in Account #2 from faucet that I'm trying to use, but Truffle is persistently trying to use Account #1 that has 0 balance in it. At which point does Truffle try to connect to MetaMask? Is it Node or Truffle that's not working properly? And how do I change the account that Truffle recognizes so that I can connect to Account #2, instead of Account #1?

So far, I've tried:

  1. Uninstalling and re-installing Truffle
  2. Logging out and re-logging into MetaMask and toggling between the accounts
  3. Changing the gas/gasPrice to a higher value or commenting them out
  4. Changing the node version

I'm using "@truffle/hdwallet-provider": "^1.0.35"

    rinkeby: {
      provider: () =>
        new HDWalletProvider(
          mnemonic,
          `https://rinkeby.infura.io/v3/${infuraKey}`
        ),
      network_id: 4, // rinkeby's id
      gas: 4500000, // rinkeby has a lower block limit than mainnet
      gasPrice: 10000000000,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: false,
    },

Best Answer

To use accounts other than the first one you have to indicate so to HDWalletProvider. Third parameter is account index, default is 0. For example to use the fifth one:

provider: () => {
    return new HDWalletProvider(mnemonic, URL, 4);
}

If required you can indicate to load more than one account. To load the the first 5 accounts

provider: () => {
    return new HDWalletProvider(mnemonic, URL, 0, 5);
}

Truffle will use the first account returned from provider but you can change default account using from in truffle-config.js

networks: 
  develop: {
    provider: () => {...},
    from: "0x01230123...0123",
    gas: "4500000",
    gasPrice: "10000000000",
  }
}

You can also override default configuration passing new parameters when deploying

module.exports = function(deployer) {
  deployer.deploy(Migrations, {
    from: "0x444433332222....0000",
    gas: "1000000",
    gasPrice: "9000000000",
  });
};
Related Topic