Skip to content

Commit

Permalink
Refine base tests (#77)
Browse files Browse the repository at this point in the history
* test: New utility contracts: Defaults.sol, Types.sol Constants.sol
test: Add constructor test
test: Use constants from Defaults contract
test: Replace StdUtils with PRBMathUtils
test: Replace StdAssertions with PRBMathAssertions

test: inherit Defaults in Base contract

test: remove redundant variables

* test: merge Defaults and Constants

---------

Co-authored-by: andreivladbrg <andreivladbrg@gmail.com>
  • Loading branch information
smol-ninja and andreivladbrg authored May 22, 2024
1 parent be51f6f commit b5f70b6
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 43 deletions.
45 changes: 10 additions & 35 deletions test/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,28 @@ import { SablierV2OpenEnded } from "src/SablierV2OpenEnded.sol";
import { ERC20Mock } from "./mocks/ERC20Mock.sol";
import { ERC20MissingReturn } from "./mocks/ERC20MissingReturn.sol";
import { Assertions } from "./utils/Assertions.sol";
import { Constants } from "./utils/Constants.sol";
import { Events } from "./utils/Events.sol";
import { Modifiers } from "./utils/Modifiers.sol";
import { Users } from "./utils/Types.sol";
import { Utils } from "./utils/Utils.sol";

struct Users {
address sender;
address recipient;
address eve;
}

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

/*//////////////////////////////////////////////////////////////////////////
DEFAULTS
VARIABLES
//////////////////////////////////////////////////////////////////////////*/

bool public constant IS_TRANFERABLE = true;
uint128 public constant RATE_PER_SECOND = 0.001e18; // 86.4 daily
uint128 public constant DEPOSIT_AMOUNT = 50_000e18;
uint40 internal constant MAY_1_2024 = 1_714_518_000;
uint40 public immutable ONE_MONTH = 30 days; // "30/360" convention
uint128 public constant ONE_MONTH_STREAMED_AMOUNT = 2592e18; // 86.4 * 30
uint128 public constant ONE_MONTH_REFUNDABLE_AMOUNT = DEPOSIT_AMOUNT - ONE_MONTH_STREAMED_AMOUNT;
uint128 public constant REFUND_AMOUNT = 10_000e18;
uint40 public immutable WARP_ONE_MONTH;
uint128 public constant WITHDRAW_AMOUNT = 2500e18;
uint40 public immutable WITHDRAW_TIME;

Users internal users;

/*//////////////////////////////////////////////////////////////////////////
TEST CONTRACTS
//////////////////////////////////////////////////////////////////////////*/

ERC20Mock internal dai = new ERC20Mock("Dai stablecoin", "DAI");
ERC20MissingReturn internal usdt = new ERC20MissingReturn("USDT stablecoin", "USDT", 6);
SablierV2OpenEnded internal openEnded;

/*//////////////////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////////////////*/

constructor() {
// Warp to May 1, 2024 at 00:00 GMT to provide a more realistic testing environment.
vm.warp({ newTimestamp: MAY_1_2024 });

WARP_ONE_MONTH = uint40(block.timestamp + ONE_MONTH);
WITHDRAW_TIME = uint40(block.timestamp) + 2_500_000;
}
ERC20MissingReturn internal usdt = new ERC20MissingReturn("USDT stablecoin", "USDT", 6);

/*//////////////////////////////////////////////////////////////////////////
SET-UP FUNCTION
Expand All @@ -72,13 +44,16 @@ abstract contract Base_Test is Assertions, Events, Modifiers, Test, Utils {
openEnded = deployOptimizedOpenEnded();
}

users.sender = createUser("sender");
users.recipient = createUser("recipient");
users.eve = createUser("eve");
users.recipient = createUser("recipient");
users.sender = createUser("sender");

labelConctracts();

resetPrank(users.sender);

// Warp to May 1, 2024 at 00:00 GMT to provide a more realistic testing environment.
vm.warp({ newTimestamp: MAY_1_2024 });
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 16 additions & 0 deletions test/integration/constructor.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.22;

import { SablierV2OpenEnded } from "src/SablierV2OpenEnded.sol";
import { Integration_Test } from "./Integration.t.sol";

contract Constructor_Integration_Concrete_Test is Integration_Test {
function test_Constructor() external {
// Construct the contract.
SablierV2OpenEnded constructedOpenEnded = new SablierV2OpenEnded();

uint256 actualStreamId = constructedOpenEnded.nextStreamId();
uint256 expectedStreamId = 1;
assertEq(actualStreamId, expectedStreamId, "nextStreamId");
}
}
3 changes: 2 additions & 1 deletion test/invariant/handlers/BaseHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ pragma solidity >=0.8.22 <0.9.0;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { StdCheats } from "forge-std/src/StdCheats.sol";

import { Constants } from "../../utils/Constants.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, Utils {
abstract contract BaseHandler is Constants, StdCheats, Utils {
/*//////////////////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////////////////*/
Expand Down
5 changes: 2 additions & 3 deletions test/utils/Assertions.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.22;

import { StdAssertions } from "forge-std/src/StdAssertions.sol";

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { PRBMathAssertions } from "@prb/math/test/utils/Assertions.sol";
import { OpenEnded } from "src/types/DataTypes.sol";

abstract contract Assertions is StdAssertions {
abstract contract Assertions is PRBMathAssertions {
/*//////////////////////////////////////////////////////////////////////////
ASSERTIONS
//////////////////////////////////////////////////////////////////////////*/
Expand Down
17 changes: 17 additions & 0 deletions test/utils/Constants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.22;

abstract contract Constants {
uint128 public constant DEPOSIT_AMOUNT = 50_000e18;
uint128 public constant DEPOSIT_AMOUNT_WITH_FEE = 50_251.256281407035175879e18; // deposit + broker fee
bool public constant IS_TRANFERABLE = true;
uint40 internal constant MAY_1_2024 = 1_714_518_000;
uint40 public immutable ONE_MONTH = 30 days; // "30/360" convention
uint128 public constant ONE_MONTH_STREAMED_AMOUNT = 2592e18; // 86.4 * 30
uint128 public constant ONE_MONTH_REFUNDABLE_AMOUNT = DEPOSIT_AMOUNT - ONE_MONTH_STREAMED_AMOUNT;
uint128 public constant RATE_PER_SECOND = 0.001e18; // 86.4 daily
uint128 public constant REFUND_AMOUNT = 10_000e18;
uint40 public immutable WARP_ONE_MONTH = MAY_1_2024 + ONE_MONTH;
uint128 public constant WITHDRAW_AMOUNT = 2500e18;
uint40 public immutable WITHDRAW_TIME = MAY_1_2024 + 2_500_000;
}
4 changes: 2 additions & 2 deletions test/utils/Modifiers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ abstract contract Modifiers {
CREATE
//////////////////////////////////////////////////////////////////////////*/

modifier whenSenderNonZeroAddress() {
modifier whenAssetContract() {
_;
}

modifier whenRecipientNonZeroAddress() {
_;
}

modifier whenAssetContract() {
modifier whenSenderNonZeroAddress() {
_;
}

Expand Down
13 changes: 13 additions & 0 deletions test/utils/Types.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;

struct Users {
// Default stream broker.
address payable broker;
// Malicious user.
address payable eve;
// Default stream recipient.
address payable recipient;
// Default stream sender.
address payable sender;
}
4 changes: 2 additions & 2 deletions test/utils/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pragma solidity >=0.8.22;

import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { PRBMathUtils } from "@prb/math/test/utils/Utils.sol";
import { CommonBase } from "forge-std/src/Base.sol";
import { StdUtils } from "forge-std/src/StdUtils.sol";

abstract contract Utils is CommonBase, StdUtils {
abstract contract Utils is CommonBase, PRBMathUtils {
/// @dev Checks if the Foundry profile is "test-optimized".
function isTestOptimizedProfile() internal view returns (bool) {
string memory profile = vm.envOr({ name: "FOUNDRY_PROFILE", defaultValue: string("default") });
Expand Down

0 comments on commit b5f70b6

Please sign in to comment.