Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
- [`setNonce`](./cheatcodes/set-nonce.md)
- [`getNonce`](./cheatcodes/get-nonce.md)
- [`mockCall`](./cheatcodes/mock-call.md)
- [`mockCalls`](./cheatcodes/mock-calls.md)
- [`mockCallRevert`](./cheatcodes/mock-call-revert.md)
- [`mockFunction`](./cheatcodes/mock-function.md)
- [`clearMockedCalls`](./cheatcodes/clear-mocked-calls.md)
Expand Down
14 changes: 14 additions & 0 deletions src/cheatcodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ interface CheatCodes {
// function will be mocked.
function mockCall(address, bytes calldata, bytes calldata) external;

/// Mocks a call to an address with a specific `msg.value`, returning specified data.
/// Calldata match takes precedence over `msg.value` in case of ambiguity.
function mockCall(address, uint256, bytes calldata, bytes calldata) external;

// Mocks multiple call to an address, returning specified data for each call.
//
// Calldata can either be strict or a partial match, e.g. if you only
// pass a Solidity selector to the expected calldata, then the entire Solidity
// function will be mocked.
function mockCalls(address, bytes calldata, bytes[] calldata) external;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the version with uint256 msgValue should also be added, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure, since the mockCall with msgValue is not included. By I can include it if you think it should be documented. In that case I should probably add it for mockCall as well?

Copy link
Contributor Author

@Tudmotu Tudmotu Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both mockCall and mockCalls with msg.value, lmk if is ok.


/// Mocks multiple calls to an address with a specific `msg.value`, returning specified data for each call.
function mockCalls(address, uint256, bytes calldata, bytes[] calldata) external;

// Reverts a call to an address, returning the specified error
//
// Calldata can either be strict or a partial match, e.g. if you only
Expand Down
1 change: 1 addition & 0 deletions src/cheatcodes/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [`setNonce`](./set-nonce.md)
- [`getNonce`](./get-nonce.md)
- [`mockCall`](./mock-call.md)
- [`mockCalls`](./mock-calls.md)
- [`mockCallRevert`](./mock-call-revert.md)
- [`mockFunction`](./mock-function.md)
- [`clearMockedCalls`](./clear-mocked-calls.md)
Expand Down
75 changes: 75 additions & 0 deletions src/cheatcodes/mock-calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
## `mockCalls`

### Signature

```solidity
function mockCalls(address where, bytes calldata data, bytes[] calldata retdata) external;
```

```solidity
function mockCalls(
address where,
uint256 value,
bytes calldata data,
bytes[] calldata retdata
) external;
```

### Description

Mocks all calls to an address `where` if the call data either strictly or loosely
matches `data` and returns different data for each call based on the `retdata`
array values.

See [`mockCall`](./mock-call.md) for more information on mocking calls and
matching precedence.

> ℹ️ **Note**
>
> Any invocation of the mocked call beyond the number of elements in `retdata`
> will receive the last `retdata` element in response. `clearMockedCalls` can be
> called to clear the mock

### Examples
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have an example that uses the version with uint256 msgValue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add and update you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an example with msg.value


Mocking multiple `balanceOf(address)` calls:

```solidity
function testMockCall() public {
bytes[] memory mocks = new bytes[](2);
mocks[0] = abi.encode(2 ether);
mocks[1] = abi.encode(1 ether);

vm.mockCalls(
address(0),
abi.encodeWithSelector(IERC20.balanceOf.selector, address(1)),
mocks
);

assertEq(IERC20(address(0)).balanceOf(address(1)), 2 ether);
assertEq(IERC20(address(0)).balanceOf(address(1)), 1 ether);
}
```

Mocking multiple calls with `msg.value`:

```solidity
function testMockCallsWithMsgValue() public {
bytes[] memory mocks = new bytes[](2);
mocks[0] = abi.encode(2 ether);
mocks[1] = abi.encode(1 ether);

vm.mockCalls(
address(0),
1 ether,
abi.encodeWithSelector(DexPool.swapETHForToken.selector),
mocks
);

uint tokenAmount1 = DexPool(address(0)).swapETHForToken{value: 1 ether}();
uint tokenAmount2 = DexPool(address(0)).swapETHForToken{value: 1 ether}();

assertEq(tokenAmount1, 2 ether);
assertEq(tokenAmount2, 1 ether);
}
```