0

I follow the code of MetaCoin project and wonder how different between two types of test:

pragma solidity >=0.4.25 <0.6.0;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/MetaCoin.sol";

contract TestMetaCoin {

    function testInitialBalanceUsingDeployedContract() public {
        MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
        uint expected = 10000;
        Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially");
    }

    function testInitialBalanceWithNewMetaCoin() public {
        MetaCoin meta = new MetaCoin();
        uint expected = 10000;
        Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially");
    }
}

The first one uses DeployedAddress.MetaCoin() to get the address of deployed contract, it's ok. But in case 2, MetaCoin meta = new MetaCoin(), as I understand that will create new instance of smart contract, so it's not the contract deployed before? so why this test have been passed?

Any helps, thanks

  • I think the owner of the new MetaCoin contract is not the (tx.origin), it is the first smart contract, try to replace (tx.origin) in the second method with (address(this)) – Majd TL Nov 8 at 13:40

Your Answer

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

Browse other questions tagged or ask your own question.