0

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


});
12
  • Please explain how exactly you are deducing all of this information (which is most likely where the problem is). – goodvibration Nov 4 '20 at 16:57
  • I thought I already answered this above. But let me just take the current deployment for example: - Result of Truffle migrate shows the address for my CoinFlip contract is: 0x0fF211B1A628412afF0299B672620642e9A5dD8B - In Ganache, it also shows the address for that contract to be 0x0fF211B1A628412afF0299B672620642e9A5dD8B - when I click on the Transactions tab in ganache, and view the transactions executed by my tests, each of the transactions are showing a 'to contract address' as: 0x16f9f503005f23C96cf1676f0A9302f9b8453484 There is no contract in ganache showing with this address – CryptoEatsTheWorld Nov 4 '20 at 17:27
  • Just print all the relevant addresses in your Truffle test. – goodvibration Nov 4 '20 at 17:49
  • Not sure what you are asking me to do exactly. But in the console when I type 'truffle networks' it outputs: Network: ganache (id: 5777) CoinFlip: 0x0fF211B1A628412afF0299B672620642e9A5dD8B Migrations: 0xC0a6067FD293B443d47BCD6181de537d50Fc26a1 There are no specific contract addresses hardcoded in my tests. I am using lines such as: 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")}), – CryptoEatsTheWorld Nov 4 '20 at 18:04
  • 1
    According to the Truffle documentation: "Before each contract() function is run, your contracts are redeployed to the running Ethereum client so the tests within it run with a clean contract state.". More info: trufflesuite.com/docs/truffle/testing/… – alberto Nov 4 '20 at 19:38
1

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.

10
  • No this is not correct, at least in my case. I DO have contract deployed and showing in Ganache before the tests are run, because I went ahead and linked truffle-config.js to Ganache. And that is the point of my question - why does Ganache show my contract deployed with an address that is different than the one truffle has and is working with. – CryptoEatsTheWorld Nov 4 '20 at 19:29
  • Thats the whole point of my analysis... I wanted to check even if we dont deploy, are we able to run our test? And conlusion is yes, we can run our test without deploying the contract. Which tells that while running test command separate deployment of contracts happen, Which is different than on already deployed in local network. Because of that everytime you run test, you see different contract addresses... – Ashutosh Singh Nov 4 '20 at 19:34
  • OK, but how does this help resolve the ganache address problem? – CryptoEatsTheWorld Nov 4 '20 at 19:37
  • What I feel its not a problem but a behavior/feature of truffle. Dealing with contract deployment while running test. – Ashutosh Singh Nov 4 '20 at 19:38
  • It IS a problem. The whole point of Ganache is to provide a block explorer-type UI for interacting with the blockchain during development. As such, one SHOULD be able to click on a contract and see the transactions. Also, one should not see transactions to non-deployed contracts in the block explorer. It is a disconnect that should not be. – CryptoEatsTheWorld Nov 4 '20 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.