Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upstream 58bf161 #48

Merged
merged 14 commits into from
Nov 11, 2024
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Braqzen"><img src="https://avatars.githubusercontent.com/u/103777923?v=4?s=100" width="100px;" alt="Braqzen"/><br /><sub><b>Braqzen</b></sub></a><br /><a href="https://github.com/foundry-rs/book/commits?author=Braqzen" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/rolandolopez36"><img src="https://avatars.githubusercontent.com/u/140282898?v=4?s=100" width="100px;" alt="rolandolopez36"/><br /><sub><b>rolandolopez36</b></sub></a><br /><a href="https://github.com/foundry-rs/book/commits?author=rolandolopez36" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="http://juanmgutierrez.com"><img src="https://avatars.githubusercontent.com/u/7672379?v=4?s=100" width="100px;" alt="Juan Gutierrez"/><br /><sub><b>Juan Gutierrez</b></sub></a><br /><a href="https://github.com/foundry-rs/book/commits?author=juannyG" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/TomasCImach"><img src="https://avatars.githubusercontent.com/u/43709975?v=4?s=100" width="100px;" alt="Tomas C Imach"/><br /><sub><b>Tomas C Imach</b></sub></a><br /><a href="https://github.com/foundry-rs/book/commits?author=TomasCImach" title="Documentation">📖</a></td>
</tr>
</tbody>
</table>

Expand Down
8 changes: 8 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
- [`forge cache ls`](./reference/cli/forge/cache/ls.md)
- [`forge clean`](./reference/cli/forge/clean.md)
- [`forge clone`](./reference/cli/forge/clone.md)
- [`forge compiler`](./reference/cli/forge/compiler.md)
- [`forge compiler resolve`](./reference/cli/forge/compiler/resolve.md)
- [`forge completions`](./reference/cli/forge/completions.md)
- [`forge config`](./reference/cli/forge/config.md)
- [`forge coverage`](./reference/cli/forge/coverage.md)
Expand Down Expand Up @@ -188,7 +190,9 @@
- [`cast completions`](./reference/cli/cast/completions.md)
- [`cast compute-address`](./reference/cli/cast/compute-address.md)
- [`cast concat-hex`](./reference/cli/cast/concat-hex.md)
- [`cast constructor-args`](./reference/cli/cast/constructor-args.md)
- [`cast create2`](./reference/cli/cast/create2.md)
- [`cast creation-code`](./reference/cli/cast/creation-code.md)
- [`cast decode-eof`](./reference/cli/cast/decode-eof.md)
- [`cast decode-transaction`](./reference/cli/cast/decode-transaction.md)
- [`cast disassemble`](./reference/cli/cast/disassemble.md)
Expand All @@ -197,6 +201,7 @@
- [`cast etherscan-source`](./reference/cli/cast/etherscan-source.md)
- [`cast find-block`](./reference/cli/cast/find-block.md)
- [`cast format-bytes32-string`](./reference/cli/cast/format-bytes32-string.md)
- [`cast format-units`](./reference/cli/cast/format-units.md)
- [`cast from-bin`](./reference/cli/cast/from-bin.md)
- [`cast from-fixed-point`](./reference/cli/cast/from-fixed-point.md)
- [`cast from-rlp`](./reference/cli/cast/from-rlp.md)
Expand All @@ -222,6 +227,7 @@
- [`cast nonce`](./reference/cli/cast/nonce.md)
- [`cast parse-bytes32-address`](./reference/cli/cast/parse-bytes32-address.md)
- [`cast parse-bytes32-string`](./reference/cli/cast/parse-bytes32-string.md)
- [`cast parse-units`](./reference/cli/cast/parse-units.md)
- [`cast pretty-calldata`](./reference/cli/cast/pretty-calldata.md)
- [`cast proof`](./reference/cli/cast/proof.md)
- [`cast publish`](./reference/cli/cast/publish.md)
Expand All @@ -238,6 +244,7 @@
- [`cast sig-event`](./reference/cli/cast/sig-event.md)
- [`cast storage`](./reference/cli/cast/storage.md)
- [`cast storage-root`](./reference/cli/cast/storage-root.md)
- [`cast string-decode`](./reference/cli/cast/string-decode.md)
- [`cast to-ascii`](./reference/cli/cast/to-ascii.md)
- [`cast to-base`](./reference/cli/cast/to-base.md)
- [`cast to-bytes32`](./reference/cli/cast/to-bytes32.md)
Expand Down Expand Up @@ -439,6 +446,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;
Jrigada marked this conversation as resolved.
Show resolved Hide resolved

/// 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
6 changes: 3 additions & 3 deletions src/cheatcodes/create-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Creates a new Wallet struct when given a parameter to derive the private key fro
#### `uint256`

```solidity
Wallet memory wallet = vm.createWallet(uint256(keccak256(bytes("1"))));
Vm.Wallet memory wallet = vm.createWallet(uint256(keccak256(bytes("1"))));

emit log_uint(wallet.privateKey); // uint256(keccak256(bytes("1")))

Expand All @@ -58,7 +58,7 @@ emit log_string(vm.getLabel(wallet.addr)); // ""
#### `string`

```solidity
Wallet memory wallet = vm.createWallet("bob's wallet");
Vm.Wallet memory wallet = vm.createWallet("bob's wallet");

emit log_uint(wallet.privateKey); // uint256(keccak256(bytes("bob's wallet")))

Expand All @@ -80,7 +80,7 @@ emit log_string(vm.getLabel(wallet.addr)); // "bob's wallet"
#### `uint256` and `string`

```solidity
Wallet memory wallet = vm.createWallet(uint256(keccak256(bytes("1"))), "bob's wallet");
Vm.Wallet memory wallet = vm.createWallet(uint256(keccak256(bytes("1"))), "bob's wallet");

emit log_uint(wallet.privateKey); // uint256(keccak256(bytes("1")))

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

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);
}
```
29 changes: 29 additions & 0 deletions src/config/vyper.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ Otherwise, you can set the path to `vyper` in your `foundry.toml` by adding the
path = "/path/to/vyper"
```

#### Vyper libraries via `forge install`

If you want an import like the following to work in your Vyper contract:

```vyper
from snekmate.utils import eip712_domain_separator
```

You can install Vyper the desired library via `forge install` e.g. `forge install pcaversaccio/snekmate`.

You then need to adjust your `foundry.toml` as follows (replacing "snekmate" with the name of your
desired package):

```toml
skip = ["**/lib/snekmate/**"]
libs = ["lib", "lib/snekmate/src"]
```

#### Vyper libraries via `pip`

Alternatively if you want to install the package via `pip` into your system's python configuration
or a virtual environment you can point foundry to it by modifying your `foundry.toml` as follows:

```toml
# Assuming you have a virtual environment in `.venv` and are using Python 3.12
libs = ["lib", ".venv/lib/python3.12/site-packages/"]
```

Note that compatible alternative python package managers like `uv` will work too.

### 2. Solidity tests

Expand Down
10 changes: 8 additions & 2 deletions src/forge/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ Forge uses the following keywords in tests:
{{#include ../../projects/writing_tests/test/Basic.t.sol:testFailSubtract43}}
```

A good practice is to use the pattern `test_Revert[If|When]_Condition` in combination with the [`expectRevert`](../cheatcodes/expect-revert.md) cheatcode (cheatcodes are explained in greater detail in the following [section](./cheatcodes.md)). Also, other testing practices can be found in the [Tutorials section](../tutorials/best-practices.md).
A good practice is to use the pattern `test_Revert[If|When]_Condition` in combination with the [`expectRevert`](../cheatcodes/expect-revert.md) cheatcode (cheatcodes are explained in greater detail in the following [section](./cheatcodes.md)). Also, other testing practices can be found in the [Tutorials section](../tutorials/best-practices.md).

> **Note**: To use `stdError` constants (like `arithmeticError` in the example below), make sure to import `StdError.sol`:
> ```solidity
> import {stdError} from "forge-std/StdError.sol";
> ```

Now, instead of using `testFail`, you know exactly what reverted and with which error:

```solidity
Expand Down Expand Up @@ -63,7 +69,7 @@ It is possible to simulate multiple transactions in a single test, with a depend
```solidity
function beforeTestSetup(
bytes4 testSelector
public returns (bytes[] memory beforeTestCalldata)
) public returns (bytes[] memory beforeTestCalldata)
```

where
Expand Down
8 changes: 0 additions & 8 deletions src/output/cheatcodes/forge-test-cheatcodes-expectrevert

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions src/output/fuzz_testing/forge-test-success-fuzz

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/output/hello_foundry/forge-test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/output/hello_foundry/tree-with-files

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/output/test_filters/forge-test-match-contract-and-test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/output/test_filters/forge-test-match-path

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/reference/cli/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- [`forge cache ls`](./forge/cache/ls.md)
- [`forge clean`](./forge/clean.md)
- [`forge clone`](./forge/clone.md)
- [`forge compiler`](./forge/compiler.md)
- [`forge compiler resolve`](./forge/compiler/resolve.md)
- [`forge completions`](./forge/completions.md)
- [`forge config`](./forge/config.md)
- [`forge coverage`](./forge/coverage.md)
Expand Down Expand Up @@ -70,7 +72,9 @@
- [`cast completions`](./cast/completions.md)
- [`cast compute-address`](./cast/compute-address.md)
- [`cast concat-hex`](./cast/concat-hex.md)
- [`cast constructor-args`](./cast/constructor-args.md)
- [`cast create2`](./cast/create2.md)
- [`cast creation-code`](./cast/creation-code.md)
- [`cast decode-eof`](./cast/decode-eof.md)
- [`cast decode-transaction`](./cast/decode-transaction.md)
- [`cast disassemble`](./cast/disassemble.md)
Expand All @@ -79,6 +83,7 @@
- [`cast etherscan-source`](./cast/etherscan-source.md)
- [`cast find-block`](./cast/find-block.md)
- [`cast format-bytes32-string`](./cast/format-bytes32-string.md)
- [`cast format-units`](./cast/format-units.md)
- [`cast from-bin`](./cast/from-bin.md)
- [`cast from-fixed-point`](./cast/from-fixed-point.md)
- [`cast from-rlp`](./cast/from-rlp.md)
Expand All @@ -103,6 +108,7 @@
- [`cast nonce`](./cast/nonce.md)
- [`cast parse-bytes32-address`](./cast/parse-bytes32-address.md)
- [`cast parse-bytes32-string`](./cast/parse-bytes32-string.md)
- [`cast parse-units`](./cast/parse-units.md)
- [`cast pretty-calldata`](./cast/pretty-calldata.md)
- [`cast proof`](./cast/proof.md)
- [`cast publish`](./cast/publish.md)
Expand All @@ -118,6 +124,8 @@
- [`cast sig`](./cast/sig.md)
- [`cast sig-event`](./cast/sig-event.md)
- [`cast storage`](./cast/storage.md)
- [`cast storage-root`](./cast/storage-root.md)
- [`cast string-decode`](./cast/string-decode.md)
- [`cast to-ascii`](./cast/to-ascii.md)
- [`cast to-base`](./cast/to-base.md)
- [`cast to-bytes32`](./cast/to-bytes32.md)
Expand Down
Loading
Loading