Skip to content

Commit

Permalink
test: add Utils contract
Browse files Browse the repository at this point in the history
test: move resetPrank in Utils
test: remove unneeded vm in BaseHandler
test: use only resetPrank function to change caller
  • Loading branch information
andreivladbrg committed Apr 10, 2024
1 parent 191d23f commit e82caa1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
21 changes: 10 additions & 11 deletions test/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import { ERC20MissingReturn } from "./mocks/ERC20MissingReturn.sol";
import { Assertions } from "./utils/Assertions.sol";
import { Events } from "./utils/Events.sol";
import { Modifiers } from "./utils/Modifiers.sol";
import { Utils } from "./utils/Utils.sol";

struct Users {
address sender;
address recipient;
}

abstract contract Base_Test is Assertions, Events, Modifiers, Test {
abstract contract Base_Test is Assertions, Events, Modifiers, Test, Utils {
using SafeCast for uint256;

/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -64,11 +65,9 @@ abstract contract Base_Test is Assertions, Events, Modifiers, Test {
users.sender = createUser("sender");
users.recipient = createUser("recipient");

vm.label(address(openEnded), "Open Ended");
vm.label(address(dai), "DAI");
vm.label(address(usdt), "USDT");
labelConctracts();

vm.startPrank({ msgSender: users.sender });
resetPrank(users.sender);
}

/*//////////////////////////////////////////////////////////////////////////
Expand All @@ -86,6 +85,12 @@ abstract contract Base_Test is Assertions, Events, Modifiers, Test {
return user;
}

function labelConctracts() internal {
vm.label(address(openEnded), "Open Ended");
vm.label(address(dai), "DAI");
vm.label(address(usdt), "USDT");
}

function normalizeBalance(uint256 streamId) internal view returns (uint256) {
return normalizeTransferAmount(streamId, openEnded.getBalance(streamId));
}
Expand Down Expand Up @@ -138,10 +143,4 @@ abstract contract Base_Test is Assertions, Events, Modifiers, Test {
function expectCallToTransferFrom(IERC20 asset, address from, address to, uint256 amount) internal {
vm.expectCall({ callee: address(asset), data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) });
}

/// @dev Stops the active prank and sets a new one.
function resetPrank(address msgSender) internal {
vm.stopPrank();
vm.startPrank(msgSender);
}
}
14 changes: 3 additions & 11 deletions test/invariant/handlers/BaseHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
pragma solidity >=0.8.22 <0.9.0;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Vm } from "forge-std/src/Vm.sol";
import { StdCheats } from "forge-std/src/StdCheats.sol";
import { StdUtils } from "forge-std/src/StdUtils.sol";

import { Utils } from "../../utils/Utils.sol";
import { TimestampStore } from "../stores/TimestampStore.sol";

/// @notice Base contract with common logic needed by all handler contracts.
abstract contract BaseHandler is StdCheats, StdUtils {
abstract contract BaseHandler is StdCheats, Utils {
/*//////////////////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////////////////*/

/// @dev Maximum number of streams that can be created during an invariant campaign.
uint256 internal constant MAX_STREAM_COUNT = 100;

/// @dev The virtual address of the Foundry VM.
address internal constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code"))));

/*//////////////////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////////////////*/
Expand All @@ -40,9 +36,6 @@ abstract contract BaseHandler is StdCheats, StdUtils {
/// @dev Reference to the timestamp store, which is needed for simulating the passage of time.
TimestampStore public timestampStore;

/// @dev An instance of the Foundry VM, which contains cheatcodes for testing.
Vm internal constant vm = Vm(VM_ADDRESS);

/*//////////////////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -90,8 +83,7 @@ abstract contract BaseHandler is StdCheats, StdUtils {

/// @dev Makes the provided sender the caller.
modifier useNewSender(address sender) {
vm.startPrank(sender);
resetPrank(sender);
_;
vm.stopPrank();
}
}
12 changes: 5 additions & 7 deletions test/invariant/handlers/OpenEndedHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@ contract OpenEndedHandler is BaseHandler {
modifier useFuzzedStreamRecipient() {
uint256 lastStreamId = openEndedStore.lastStreamId();
currentRecipient = openEndedStore.recipients(currentStreamId);
vm.startPrank(currentRecipient);
resetPrank(currentRecipient);
_;
vm.stopPrank();
}

modifier useFuzzedStreamSender() {
uint256 lastStreamId = openEndedStore.lastStreamId();
currentSender = openEndedStore.senders(currentStreamId);
vm.startPrank(currentSender);
resetPrank(currentSender);
_;
vm.stopPrank();
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -113,7 +111,7 @@ contract OpenEndedHandler is BaseHandler {
useFuzzedStream(streamIndexSeed)
useFuzzedStreamSender
{
// Canceled streams cannot be deposited into.
// Only non canceled streams can be deposited.
if (openEnded.isCanceled(currentStreamId)) {
return;
}
Expand Down Expand Up @@ -146,8 +144,8 @@ contract OpenEndedHandler is BaseHandler {
useFuzzedStream(streamIndexSeed)
useFuzzedStreamSender
{
// Only canceled streams can be restarted.
if (!openEnded.isCanceled(currentStreamId)) {
// Only non canceled streams can be refunded.
if (openEnded.isCanceled(currentStreamId)) {
return;
}

Expand Down
13 changes: 13 additions & 0 deletions test/utils/Utils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.22;

import { CommonBase } from "forge-std/src/Base.sol";
import { StdUtils } from "forge-std/src/StdUtils.sol";

abstract contract Utils is CommonBase, StdUtils {
/// @dev Stops the active prank and sets a new one.
function resetPrank(address msgSender) internal {
vm.stopPrank();
vm.startPrank(msgSender);
}
}

0 comments on commit e82caa1

Please sign in to comment.