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. Now, my tests are PASSING, but when I look, none of the transactions are being done against the actual address of my deployed contract as shown in Ganache and Truffle.
In other words, when I run migrate in Truffle, a contract address is shown as the deployed address. Let’s call that . When I look in the Ganache UI, under contracts, it shows the migrations contract and the contract - same as in Truffle. OK, so far so good.
Now, when I run my tests, they seems to run fine (no errors). And in Ganache the balances of the affected accounts are changing their amounts as expected, and the transactions are showing in Ganache transactions tab. BUT, the transactions are being sent to an entirely different contract address!!
I have no idea where that address is coming from or why my tests think it is the contract address when Truffle and Ganace seem to think otherwise.
Here is my test file, in case you’re curious about it:
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();
console.log(instance.address);
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();
console.log(instance.address);
await truffleAssert.fails(instance.bet(0, {from: accounts[1], value: web3.utils.toWei("9", "ether")}),
truffleAssert.ErrorType.REVERT);
});
it("should reject a bet if not enough in contract to cover it", async function(){
let instance = await CoinFlip.deployed();
console.log(instance.address);
await truffleAssert.fails(instance.bet(0, {from: accounts[2], 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);
});
});