简体   繁体   中英

How to change account in truffle(ganache)?

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?

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.

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"
    }
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM