0

I want to test a function from an existing smart contract written in solidity 0.4.

I'm using foundry/forge for that and I managed to test all the functions within this contract except the withdraw.

 //Old method using deprecated stuff
 function withdraw() {
        if (isPaused) throw;
        uint amount = pendingWithdrawals[msg.sender];
        pendingWithdrawals[msg.sender] = 0;
        msg.sender.transfer(amount);
    }

Forge test



receive() external payable {}

fallback() external payable {}
function test_withdraw() public {
        //Setup test
        bool isPaused = instance.isPaused();
        assertFalse(isPaused);

        vm.prank(address(1));
        instance.mint(0);
        address getOwner = instance.ownerOfIndex(0);
        assertEq(getOwner, address(1));

        vm.prank(address(1));
        instance.offer(0,1000 wei);
        offerStatus memory offer = instance.offerStatus(0);
        assertTrue(offer.isOffered);
        assertEq(offer.price,1000 wei);

        vm.deal(address(2), 1 ether);
        vm.prank(address(2));
        instance.purchase(0, 1000 wei);
        getOwner = instance.ownerOfIndex(0);
        assertEq(getOwner, address(2));
        offer = instance.offerStatus(0);
        assertFalse(offer.isOffered);
        assertEq(offer.price,0 wei);

        //test
        vm.deal(address(1), 1 ether);
        uint pendingWithdrawals = instance.pendingWithdrawals(
            address(1)
        );
        assertEq(pendingWithdrawals, 1000);
        uint balance = address(1).balance;
        console2.log(pendingWithdrawals, balance);
        vm.prank(payable(address(1)));
        instance.withdraw();
        assertEq(address(1).balance, balance + pendingWithdrawals);
    }

Every assertion runs fine up to instance.withdraw(). Than I get the following error:

oldContract::withdraw()
    │   ├─ [0] 0x0000000000000000000000000000000000000001::fallback{value: 1000}()
[FAIL. Reason: EvmError: Revert]

So, the assertion doesn't fail, but there's an error at the EVM. My test check if it matches the precondition for executing the method Any ideas?

1 Answer 1

1

Fix the withdraw function

function withdraw() external {
    if (isPaused) throw;
    uint amount = pendingWithdrawals[msg.sender];
    pendingWithdrawals[msg.sender] = 0;
    payable(msg.sender).transfer(amount);
}
1
  • But that should not be the issue, since this contract has been online for a while now. The challenge is that I can only modify what is in my test contract. Jan 10 at 16:13

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.