How to change account in truffle(ganache)?

Viewed 4178

I use ganache-cli to build local blockchain and truffle to deploy contract. To interact with deployed contracts i use truffle console. For example i transfer tokens from my current account(that is web3.eth.personal.getAccounts()[0]) to web3.eth.personal.getAccounts()[1] after this i want to change my current account to web3.eth.personal.getAccounts()[1] address.

How to do that?

2 Answers

Changing the value of web3.eth.defaultAccount doesn't work for me. But configuring from in the truffle.js works.

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*", 
      from: "0x12345678"
    }
  }
};

You can set the default account as

web3.eth.defaultAccount = web3.eth.personal.getAccounts()[1];

or simply as an address

web3.eth.defaultAccount = '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe';

Mind that Ganache by default only has 10 predefined accounts that it knows the private keys to. So you need to pass an address that Ganache knows private key to. If you pass an unknown account address, web3 (and Ganache) will not be able to submit transactions using this (unknown) address.

Related