Skip to content

vm.expectCall unexpected behavior  #4879

@garyghayrat

Description

@garyghayrat

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (e807429 2023-05-04T00:03:45.171389475Z)

What command(s) is the bug in?

forge test

Operating System

Linux

Describe the bug

vm.expectCall is giving contradictory errors when used within a helper function and a loop.

To reproduce:

  1. git clone git@github.com:ScopeLift/umbra-protocol.git
  2. git checkout expect-call-issue
  3. cd contracts-periphery/ && cp .env.example .env and fill in ETH_RPC_URL in .env
  4. forge test --mc UniswapWithdrawHookTest

Relevant lines of codes are at 71-83 and 151-153.

In this case:
vm.expectCall( address(withdrawHook), abi.encodeWithSelector(withdrawHook.tokensWithdrawn.selector) );

When you run the test command at step 4. You'll see this error:

Failing tests:
Encountered 2 failing tests in test/UniswapWithdrawHook.fork.1.t.sol:UniswapWithdrawHookTest
[FAIL. Reason: Expected at least one call to 0x5991a2df15a8f6a256d3ec51e99254cd3fb576a9 with data 0x579073de, but got **none** Counterexample: calldata=0x3d93c91a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, args=[0, 0, 0, 0]] testFuzz_HookTest_withdrawTokenAndCall(uint256,uint256,uint256,uint256) (runs: 0, μ: 0, ~: 0)
[FAIL. Reason: Expected at least one call to 0x5991a2df15a8f6a256d3ec51e99254cd3fb576a9 with data 0x579073de, but got **none** Counterexample: calldata=0xc410223f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, args=[0, 0, 0, 0]] testFuzz_HookTest_withdrawTokenAndCallOnBehalf(uint256,uint256,uint256,uint256) (runs: 0, μ: 0, ~: 0)

If you change the code to:
vm.expectCall( address(withdrawHook), abi.encodeWithSelector(withdrawHook.tokensWithdrawn.selector), **0** );
and then run the test, you'll see:

Failing tests:
Encountered 2 failing tests in test/UniswapWithdrawHook.fork.1.t.sol:UniswapWithdrawHookTest
[FAIL. Reason: Expected call to 0x5991a2df15a8f6a256d3ec51e99254cd3fb576a9 with data 0x579073de to be made 0 time(s), but was called 10 time(s) Counterexample: calldata=0x3d93c91a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, args=[0, 0, 0, 0]] testFuzz_HookTest_withdrawTokenAndCall(uint256,uint256,uint256,uint256) (runs: 0, μ: 0, ~: 0)
[FAIL. Reason: Expected call to 0x5991a2df15a8f6a256d3ec51e99254cd3fb576a9 with data 0x579073de to be made 0 time(s), but was called 10 time(s) Counterexample: calldata=0xc410223f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, args=[0, 0, 0, 0]] testFuzz_HookTest_withdrawTokenAndCallOnBehalf(uint256,uint256,uint256,uint256) (runs: 0, μ: 0, ~: 0)

If you change the code to:
vm.expectCall( address(withdrawHook), abi.encodeWithSelector(withdrawHook.tokensWithdrawn.selector), **10** );
and then run the test, you'll see:

Failing tests:
Encountered 2 failing tests in test/UniswapWithdrawHook.fork.1.t.sol:UniswapWithdrawHookTest
[FAIL. Reason: Expected call to 0x5991a2df15a8f6a256d3ec51e99254cd3fb576a9 with data 0x579073de to be made 10 time(s), but was called 0 time(s) Counterexample: calldata=0x3d93c91a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, args=[0, 0, 0, 0]] testFuzz_HookTest_withdrawTokenAndCall(uint256,uint256,uint256,uint256) (runs: 0, μ: 0, ~: 0)
[FAIL. Reason: Expected call to 0x5991a2df15a8f6a256d3ec51e99254cd3fb576a9 with data 0x579073de to be made 10 time(s), but was called 0 time(s) Counterexample: calldata=0xc410223f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, args=[0, 0, 0, 0]] testFuzz_HookTest_withdrawTokenAndCallOnBehalf(uint256,uint256,uint256,uint256) (runs: 0, μ: 0, ~: 0)

The expected number of calls should actually be 1:
vm.expectCall( address(withdrawHook), abi.encodeWithSelector(withdrawHook.tokensWithdrawn.selector), **1**);
But this also fails with the error saying it was called 10 times.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions