Skip to content

Commit

Permalink
Merge pull request #118 from nomoixyz/support-new-forge-features
Browse files Browse the repository at this point in the history
feat: add missing forge-std functions
  • Loading branch information
gnkz authored Aug 18, 2023
2 parents 01ccb33 + bf61bf5 commit 631df88
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
7 changes: 6 additions & 1 deletion docs/src/reference/modules/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ Sets the specified `slot` in the storage of the given `self` address to the prov

#### **`setNonce(address self, uint64 n) → (address)`**

Sets the nonce of the given `self` address to the provided value `n`.
Sets the nonce of the given `self` address to the provided value `n`. It will revert if the new
nonce is lower than the current address nonce.

#### **`setNonceUnsafe(address self, uint64 n) → (address)`**

Sets the nonce of the given `self` address to the provided arbitrary value `n`.

#### **`impersonateOnce(address self) → (address)`**

Expand Down
48 changes: 48 additions & 0 deletions docs/src/reference/modules/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ sets the `block.basefee` to `baseFee`

sets the `block.basefee` to `baseFee`

#### **`setBlockPrevrandao(Context self, bytes32 newPrevrandao) → (Context)`**

sets the `block.prevrandao` to `newPrevrandao`

#### **`setBlockPrevrandao(bytes32 newPrevrandao) → (Context)`**

sets the `block.prevrandao` to `newPrevrandao`

#### **`setChainId(Context self, uint64 chainId) → (Context)`**

sets the `block.chainid` to `chainId`
Expand All @@ -73,6 +81,14 @@ Sets the block coinbase to `who`.

Sets the block coinbase to `who`.

#### **`setGasPrice(Context self, address newGasPrice) → (Context)`**

Sets the gas price to `newGasPrice`.

#### **`setGasPrice(address newGasPrice) → (Context)`**

Sets the gas price to `newGasPrice`.

#### **`expectRevert(bytes revertData)`**

Function used to check whether the next call reverts or not.
Expand Down Expand Up @@ -113,6 +129,22 @@ Used to check if a call to `callee` with `data` was made.

Used to check if a call to `callee` with `data` and `msgValue` was made.

#### **`expectCallMinGas(address callee, uint256 msgValue, uint64 minGas, bytes calldata data)`**

Expect a call from `callee` with the specified `msgValue` and `data`, and a minimum amount of gas `minGas`.

#### **`expectCallMinGas(address callee, uint256 msgValue, uint64 minGas, bytes calldata data, uint64 count)`**

Expect a number of calls `count` from `callee` with the specified `msgValue` and `data`, and a minimum amount of gas `minGas`.

#### **`expectSafeMemory(uint64 min, uint64 max)`**

Allows to write on memory only between [0x00, 0x60) and [`min`, `max`) in the current subcontext

#### **`expectsafememorycall(uint64 min, uint64 max)`**

Allows to write on memory only between [0x00, 0x60) and [`min`, `max`) in the next subcontext

#### **`snapshot(Context) → (uint256)`**

Takes a snapshot of the current state of the vm and returns an identifier.
Expand All @@ -129,3 +161,19 @@ Reverts the state of the vm to the snapshot with id `snapshotId`.

Reverts the state of the vm to the snapshot with id `snapshotId`.

#### **`addBreakpoint(Context self, string memory name)`**

Creates a breakpoint to jump to in the debugger with `name`.

#### **`addBreakpoint(string memory name)`**

Creates a breakpoint to jump to in the debugger with `name`.

#### **`addConditionalBreakpoint(Context self, string memory name, bool condition)`**

Creates a conditional breakpoint to jump to in the debugger with name `name` and condition `condition`.

#### **`addConditionalBreakpoint(string memory name, bool condition)`**

Creates a conditional breakpoint to jump to in the debugger with name `name` and condition `condition`.

12 changes: 11 additions & 1 deletion src/_modules/Accounts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ library accounts {
return self;
}

/// @dev Sets the nonce of the given `self` address to the provided value `n`.
/// @dev Sets the nonce of the given `self` address to the provided value `n`. It will revert if
// the new nonce is lower than the current address nonce.
/// @param self The address to set the nonce for.
/// @param n The value to set the nonce to.
/// @return The updated address with the modified nonce.
Expand All @@ -186,6 +187,15 @@ library accounts {
return self;
}

/// @dev Sets the nonce of the given `self` address to the arbitrary provided value `n`.
/// @param self The address to set the nonce for.
/// @param n The value to set the nonce to.
/// @return The updated address with the modified nonce.
function setNonceUnsafe(address self, uint64 n) internal returns (address) {
vulcan.hevm.setNonceUnsafe(self, n);
return self;
}

/// @dev Sets the `msg.sender` of the next call to `self`.
/// @param self The address to impersonate.
/// @return The address that was impersonated.
Expand Down
91 changes: 91 additions & 0 deletions src/_modules/Context.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ library ctx {
return setBlockBaseFee(Context.wrap(0), baseFee);
}

/// @dev Sets block.prevrandao.
/// @param newPrevrandao The new `block.prevrandao`.
function setBlockPrevrandao(Context self, bytes32 newPrevrandao) internal returns (Context) {
vulcan.hevm.prevrandao(newPrevrandao);
return self;
}

/// @dev Sets block.prevrandao.
/// @param newPrevrandao The new `block.prevrandao`.
function setBlockPrevrandao(bytes32 newPrevrandao) internal returns (Context) {
return setBlockPrevrandao(Context.wrap(0), newPrevrandao);
}

/// @dev sets the `block.chainid` to `chainId`
/// @param chainId the new block chain id
function setChainId(Context self, uint64 chainId) internal returns (Context) {
Expand Down Expand Up @@ -194,6 +207,19 @@ library ctx {
return setBlockCoinbase(Context.wrap(0), who);
}

/// @dev Sets the transaction gas price.
/// @param newGasPrice The new transaction gas price.
function setGasPrice(Context self, uint256 newGasPrice) internal returns (Context) {
vulcan.hevm.txGasPrice(newGasPrice);
return self;
}

/// @dev Sets the transaction gas price.
/// @param newGasPrice The new transaction gas price.
function setGasPrice(uint256 newGasPrice) internal returns (Context) {
return setGasPrice(Context.wrap(0), newGasPrice);
}

/// @dev Function used to check whether the next call reverts or not.
/// @param revertData The function call data that that is expected to fail.
function expectRevert(bytes memory revertData) internal {
Expand Down Expand Up @@ -269,6 +295,43 @@ library ctx {
vulcan.hevm.expectCall(callee, msgValue, data);
}

/// @dev Expect a call to an address with the specified msg.value and calldata, and a minimum amount of gas.
/// @param callee The address that is expected to be called.
/// @param msgValue The `msg.value` that is expected to be sent.
/// @param minGas The expected minimum amount of gas for the call.
/// @param data The call data that is expected to be used.
function expectCallMinGas(address callee, uint256 msgValue, uint64 minGas, bytes calldata data) internal {
vulcan.hevm.expectCallMinGas(callee, msgValue, minGas, data);
}

/// @dev Expect a number call to an address with the specified msg.value and calldata, and a minimum amount of gas.
/// @param callee The address that is expected to be called.
/// @param msgValue The `msg.value` that is expected to be sent.
/// @param minGas The expected minimum amount of gas for the call.
/// @param data The call data that is expected to be used.
/// @param count The number of calls that are expected.
function expectCallMinGas(address callee, uint256 msgValue, uint64 minGas, bytes calldata data, uint64 count)
external
{
vulcan.hevm.expectCallMinGas(callee, msgValue, minGas, data, count);
}

/// @dev Allows to write on memory only between [0x00, 0x60) and [min, max) in the current.
/// subcontext.
/// @param min The lower limit of the allowed memory slot.
/// @param max The upper limit of the allowed memory slot.
function expectSafeMemory(uint64 min, uint64 max) external {
vulcan.hevm.expectSafeMemory(min, max);
}

/// @dev Allows to write on memory only between [0x00, 0x60) and [min, max) in the next
// subcontext.
/// @param min The lower limit of the allowed memory slot.
/// @param max The upper limit of the allowed memory slot.
function expectsafememorycall(uint64 min, uint64 max) external {
vulcan.hevm.expectSafeMemoryCall(min, max);
}

/// @dev Takes a snapshot of the current state of the vm and returns an identifier.
/// @return The snapshot identifier.
function snapshot(Context) internal returns (uint256) {
Expand All @@ -294,6 +357,34 @@ library ctx {
function revertToSnapshot(uint256 snapshotId) internal returns (bool) {
return revertToSnapshot(Context.wrap(0), snapshotId);
}

/// @dev Creates a breakpoint to jump to in the debugger.
/// @param name The name of the breakpoint.
function addBreakpoint(Context self, string memory name) internal returns (Context) {
vulcan.hevm.breakpoint(name);
return self;
}

/// @dev Creates a breakpoint to jump to in the debugger.
/// @param name The name of the breakpoint.
function addBreakpoint(string memory name) internal returns (Context) {
return addBreakpoint(Context.wrap(0), name);
}

/// @dev Creates a breakpoint to jump to in the debugger.
/// @param name The name of the breakpoint.
/// @param condition The condition that needs to be fulfilled in order to add the breakpoint.
function addConditionalBreakpoint(Context self, string memory name, bool condition) internal returns (Context) {
vulcan.hevm.breakpoint(name, condition);
return self;
}

/// @dev Creates a breakpoint to jump to in the debugger.
/// @param name The name of the breakpoint.
/// @param condition The condition that needs to be fulfilled in order to add the breakpoint.
function addConditionalBreakpoint(string memory name, bool condition) internal returns (Context) {
return addConditionalBreakpoint(Context.wrap(0), name, condition);
}
}

using ctx for Context global;

0 comments on commit 631df88

Please sign in to comment.