[Ethereum] Deployed contract address in Truffle does not match Ganache

ganachesoliditytruffle

I have truffle set up with Ganache. I am running tests on my solidity contract that involve sending ether to the contract and withdrawing it from the contract. Funny thing is my tests are PASSING, but when I look, none of the transactions are going to or coming from the actual address of my deployed contract as shown in Ganache.

The ACCOUNTS in Ganache are having their balances deducted or added to as one would expect, but when looking at the transactions, the contract the tests are interacting with is an address that doesn’t seem to exist. For example, on one deployment the contract address in Ganache was: 0x683EB77121Ab6A40cAE6BD78001E5eD95dbC8872 but the mystery contract address was: 0xe0107F0a6210A7200636F3D15Ee0a3741Fe815F6

Any idea how to resolve this?

Here is the code of my test file:

const CoinFlip = artifacts.require("CoinFlip");
const truffleAssert = require('truffle-assertions');

contract("CoinFlip", async function(accounts) {

  it("should make sure the bet is not less than 0.1 ether", async function(){
    let instance = await CoinFlip.deployed();
    await truffleAssert.fails(instance.bet(0, {from: accounts[1], value: web3.utils.toWei("1000000000000000", "wei")}),
    truffleAssert.ErrorType.REVERT);
  });

  it("should make sure the bet is not more than 10 ether", async function(){
    let instance = await CoinFlip.deployed();
    await truffleAssert.fails(instance.bet(0, {from: accounts[1], value: web3.utils.toWei("11", "ether")}),
    truffleAssert.ErrorType.REVERT);
  });

  it("should reject a bet if not enough in contract to cover it", async function(){
    let instance = await CoinFlip.deployed();
    await truffleAssert.fails(instance.bet(0, {from: accounts[1], value: web3.utils.toWei("2", "ether")}),
    truffleAssert.ErrorType.REVERT);
  });

  it("should accept a bet if there is enough in contract to cover it", async function(){
    let instance = await CoinFlip.deployed();
    console.log(instance.address);
    console.log(CoinFlip.address);
    await web3.eth.sendTransaction({from: accounts[0], to: instance.address, value: web3.utils.toWei("15", "ether")});
    await truffleAssert.passes(instance.bet(0, {from: accounts[1], value: web3.utils.toWei("2", "ether")}),
    truffleAssert.ErrorType.REVERT);
  });


});

Best Answer

Official Documentation on the same:

How Contract function during test run behaves

So here is the thing....

In order to test, you dont even have compile/migrate it to the local ganache server.

Let say you have a fresh workspace in Ganache... and you run

truffle test

It will compile the contracts and put it into a temp directory "C:\Users\pc\AppData\Local\Temp\test--19324-4OOYkxXEiDUd" in windows system. So here is your compiled artifact i.e. contract.json file is, no build/contract/*.json is created till now.

Then it will run those transactions which you are testing in your test, and those transactions will be recorded in ganche... as below:

Transaction List

Now if you go to Contracts tab, which is populated based on truffle-config.js... its empty i.e. no contract has been deployed yet. and that is evident from build/contracts folder too.

Contract List

So I my best guess is....

Separate Contracts are deployed at the beginning of truffle test command.

Related Topic