Truffle and Ganache – Accounts and Migration Explained

ganachesoliditytruffletruffle-test

I have a few question regarding Truffle, Ganache, and testing:

  1. When I execute truffle develop and get 10 accounts and 10 private keys, do those represent my accounts in my wallet or other users? For example, are they supposed to represent what shows up in msg.sender, which is myself, or accounts that I can interact with by sending funds or processing them in my smart contract.

  2. How are the 10 accounts on Ganache different from the 10 accounts from question #1? Do these 10 accounts on Ganache represent the account in my wallet or other users? Are they the same except Ganache's accounts are on HTTP://127.0.0.1:7545 whereas the Truffle generated accounts are on http://127.0.0.1:9545/?

  3. When I perform the Mocha test, do the accounts that are being passed as an argument represent my own accounts in my wallet? For example, are they the 10 accounts generated by truffle develop?

contract("Some test", async (accounts) => {
  console.log(accounts)
});
  1. Someone said "migration contract helps keep track which migrations have been executed already. So the process is to create a new migration file with an increased number and to deploy just run truffle migrate." Is migration similar to versioning different contracts?
module.exports = function (deployer) {
  deployer.deploy(ASmartContract); // is this migrating or deploying?
};
Where are different versions stored and selected is the above process?
  1. I've seen some tests that deploy the contract:
let instance = await SomeSmartContract.deployed();

whereas other tests simply instantiate it through its constructor without deploying:

let someSmartContract = await SomeSmartContract.new();

Is there any difference between the two?

  1. I learned that the Solidity Compiler compiles the contract definition into two things: byte code ready for deployment and ABI. 1) Is it only necessary to compile when you make changes to the Solidity contract or other things like the front end code or the tests? 1) Is it necessary to compile each time before performing a test or only once before deploying/migrating? 2) Does the command "truffle migrate –reset" also compile automatically before migrating?

Best Answer

Ganache allocates by default 10 accounts for private keys 0x000...1 thru 0x000...a.

You obviously don't want to use these accounts in your actual deployment, because everybody knows these private keys, they are most likely continuously monitored, and whenever somebody transfers ether into any of them, it is transferred out of them sooner than later.

You can override this configuration by stating your desired set of private keys, for example:

ganache-cli
    --account=0x00000000000000000000000000000000000000000000000000000000deadbeef,99999999999
    --account=0x00000000000000000000000000000000000000000000000000000000cafebabe,99999999999

But for testing purpose, there is no reason not to use the default set of private keys.

That is, unless you need more than 10 accounts in any of your tests.

Related Topic