1

I have a contract where some functions can be called only by the owner of the contract and I want to test it with a solidity test in truffle. The contract looks like this:

contract owned {
    address public owner;

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;
    }
}

contract MyContract is owned{

    function doSomething() onlyOwner public {
       // do something ..
    }
}

I have setup truffle to connect to a Ganache blockchain that has only one address A. I have also verified that the address saved in owner matches the only address A. The problem is that when I try to test any of the functions that can be run only by an owner my test fails and I get the error Error: Returned error: VM Exception while processing transaction: revert. In my test I call the functions like this myContract.doSomething(params..);. Why does the transaction fail even if I have only one address in my blockchain? How can I solve this?

4
  • 1
    It's impossible for the blockchain to "only have one address". All addresses are available in the blockchain. So I guess you're accidentally using a different address. Apr 2, 2020 at 19:28
  • @LauriPeltonen I meant that I have configured truffle and Ganache to have only one blockchain account and so I would expect them to use only that one
    – David
    Apr 2, 2020 at 19:37
  • 1. Maybe it reverts because of something else in your function. You haven't shared the full code, so we can't tell for sure. 2. You haven't shared your test and how you call your functions, so we can't really explain that either (although, if you've indeed configured Ganache with a single account then it should rule out this factor as a possible cause... but then again, you haven't shared your Ganache configuration, so we can't really rule out this option either). Apr 2, 2020 at 20:00
  • 2
    Can you create a small example that cause the error? My guess is that you are not testing correctly.
    – Ismael
    Apr 4, 2020 at 3:46

1 Answer 1

0

Sorry, i can't leave a comment because i don't have enough reputation points.

  1. Have you checked if the revert is caused by an out of gas ?
  2. Your contract is executed directly by you (as a msg.owner) ? because if you execute the function from another contract you are the tx.origin, so msg.owner is the in the middle.
  3. Make a function that return msg.owner and the owner, and compare them.
  4. Try to use case sensitive address (camelcase) on 3rd party softwares, that was one of my problems when dealing with python.
  5. Please share the code with us to see it.
3
  • I think point 2 may be the issue. I am telling my test contract to execute the function in the main contract and so it is possible that msg.owner becomes the address of the test contract. Is there a way to work around this?
    – David
    Apr 3, 2020 at 16:51
  • Yes. If you want the user to be the owner you set the function with : constructor() public { owner = tx.origin; } modifier onlyOwner { require(tx.origin == owner); _; }
    – sniper
    Apr 5, 2020 at 2:35
  • Have you solved the problem ? @David
    – sniper
    Apr 9, 2020 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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