0

I am creating a ERC20 token solidity contract and when writing tests I encountered a problem.

Here is a function I use to test the token cap with.

function testTokenCap() public {
    uint256 mintValueCap = mintValue + token.cap();

    vm.expectRevert();
    vm.prank(OWNER);
    token.mint(USER, mintValueCap);
}

This function works fine but the weird thing is that if write the function like this:

function testTokenCap() public {
    vm.expectRevert();
    vm.prank(OWNER);
    token.mint(USER, mintValue + token.cap());
}

Then vm.prank(OWNER) sends the test contract's address and not the owner's one which causes my function to revert because I have an onlyOwner modifier:

modifier onlyOwner {
    require(msg.sender == i_owner, "Only owner can use this function");
    _;
}

I am guessing that vm.prank(OWNER) executes mintValue + token.cap() as the msg.sender to do the calculation and the test contract is left to execute token.mint() contract so it passes it's address. I am just starting with solidity and blockchain technology and I would like to know if I am thinking correctly.

1 Answer 1

0

You answered your question correctly. vm.prank(address) will set mgs.sender only for the next call, which is token.cap(). If you want to fix your test you can do one of two things:

  1. use vm.startPrank(address) in combination with vm.stopPrank()
  2. call token.cap() first, store it in a variable and then call token.mint(address, uint256) with the vm.prank(address)

Your Answer

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

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