Skip to content

Commit eb980e1

Browse files
authored
fix: move cheats, improve docs (#256)
* fix: move some cheats * fix: move cheats, bases * docs: remove suggestions
1 parent c12c6ad commit eb980e1

File tree

5 files changed

+78
-61
lines changed

5 files changed

+78
-61
lines changed

src/Common.sol renamed to src/Base.sol

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22
pragma solidity >=0.6.2 <0.9.0;
33

44
import {StdStorage} from "./StdStorage.sol";
5-
import {Vm} from "./Vm.sol";
5+
import {Vm, VmSafe} from "./Vm.sol";
66

77
abstract contract CommonBase {
88
// Cheat code address, 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D.
99
address internal constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code"))));
10-
1110
// console.sol and console2.sol work by executing a staticcall to this address.
1211
address internal constant CONSOLE = 0x000000000000000000636F6e736F6c652e6c6f67;
13-
1412
// Default address for tx.origin and msg.sender, 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38.
1513
address internal constant DEFAULT_SENDER = address(uint160(uint256(keccak256("foundry default caller"))));
16-
1714
// Address of the test contract, deployed by the DEFAULT_SENDER.
1815
address internal constant DEFAULT_TEST_CONTRACT = 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f;
1916

2017
uint256 internal constant UINT256_MAX =
2118
115792089237316195423570985008687907853269984665640564039457584007913129639935;
2219

2320
Vm internal constant vm = Vm(VM_ADDRESS);
24-
2521
StdStorage internal stdstore;
2622
}
23+
24+
abstract contract TestBase is CommonBase {}
25+
26+
abstract contract ScriptBase is CommonBase {
27+
// Used when deploying with create2, https://github.com/Arachnid/deterministic-deployment-proxy.
28+
address internal constant CREATE2_FACTORY = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
29+
30+
VmSafe internal constant vmSafe = VmSafe(VM_ADDRESS);
31+
}

src/Script.sol

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.6.2 <0.9.0;
33

4-
import {CommonBase} from "./Common.sol";
4+
// 💬 ABOUT
5+
// Standard Library's default Script.
6+
7+
// 🧩 MODULES
8+
import {ScriptBase} from "./Base.sol";
59
import {console} from "./console.sol";
610
import {console2} from "./console2.sol";
711
import {StdChains} from "./StdChains.sol";
@@ -12,13 +16,11 @@ import {StdStorage, stdStorageSafe} from "./StdStorage.sol";
1216
import {StdUtils} from "./StdUtils.sol";
1317
import {VmSafe} from "./Vm.sol";
1418

15-
abstract contract ScriptBase is CommonBase {
16-
// Create2 factory used by scripts when deploying with create2, https://github.com/Arachnid/deterministic-deployment-proxy.
17-
address internal constant CREATE2_FACTORY = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
18-
19-
VmSafe internal constant vmSafe = VmSafe(VM_ADDRESS);
20-
}
19+
// 📦 BOILERPLATE
20+
import {ScriptBase} from "./Base.sol";
2121

22+
// ⭐️ SCRIPT
2223
abstract contract Script is StdChains, StdCheatsSafe, StdUtils, ScriptBase {
24+
// Note: IS_SCRIPT() must return true.
2325
bool public IS_SCRIPT = true;
2426
}

src/StdCheats.sol

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ pragma solidity >=0.6.2 <0.9.0;
44
pragma experimental ABIEncoderV2;
55

66
import {StdStorage, stdStorage} from "./StdStorage.sol";
7-
import {Vm, VmSafe} from "./Vm.sol";
7+
import {Vm} from "./Vm.sol";
88

99
abstract contract StdCheatsSafe {
10-
VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
10+
Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
11+
12+
bool private gasMeteringOff;
1113

1214
// Data structures to parse Transaction objects from the broadcast artifact
1315
// that conform to EIP1559. The Raw structs is what is parsed from the JSON
@@ -422,6 +424,45 @@ abstract contract StdCheatsSafe {
422424
require(b.length <= 32, "StdCheats _bytesToUint(bytes): Bytes length exceeds 32.");
423425
return abi.decode(abi.encodePacked(new bytes(32 - b.length), b), (uint256));
424426
}
427+
428+
function isFork() internal virtual returns (bool status) {
429+
try vm.activeFork() {
430+
status = true;
431+
} catch (bytes memory) {}
432+
}
433+
434+
modifier skipWhenForking() {
435+
if (!isFork()) {
436+
_;
437+
}
438+
}
439+
440+
modifier skipWhenNotForking() {
441+
if (isFork()) {
442+
_;
443+
}
444+
}
445+
446+
modifier noGasMetering() {
447+
vm.pauseGasMetering();
448+
// To prevent turning gas monitoring back on with nested functions that use this modifier,
449+
// we check if gasMetering started in the off position. If it did, we don't want to turn
450+
// it back on until we exit the top level function that used the modifier
451+
//
452+
// i.e. funcA() noGasMetering { funcB() }, where funcB has noGasMetering as well.
453+
// funcA will have `gasStartedOff` as false, funcB will have it as true,
454+
// so we only turn metering back on at the end of the funcA
455+
bool gasStartedOff = gasMeteringOff;
456+
gasMeteringOff = true;
457+
458+
_;
459+
460+
// if gas metering was on when this modifier was called, turn it back on at the end
461+
if (!gasStartedOff) {
462+
gasMeteringOff = false;
463+
vm.resumeGasMetering();
464+
}
465+
}
425466
}
426467

427468
// Wrappers around cheatcodes to avoid footguns
@@ -431,8 +472,6 @@ abstract contract StdCheats is StdCheatsSafe {
431472
StdStorage private stdstore;
432473
Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
433474

434-
bool private gasMeteringOff;
435-
436475
// Skip forward or rewind time by the specified number of seconds
437476
function skip(uint256 time) internal virtual {
438477
vm.warp(block.timestamp + time);
@@ -523,43 +562,4 @@ abstract contract StdCheats is StdCheatsSafe {
523562
stdstore.target(token).sig(0x18160ddd).checked_write(totSup);
524563
}
525564
}
526-
527-
function isFork() internal virtual returns (bool status) {
528-
try vm.activeFork() {
529-
status = true;
530-
} catch (bytes memory) {}
531-
}
532-
533-
modifier noGasMetering() {
534-
vm.pauseGasMetering();
535-
// To prevent turning gas monitoring back on with nested functions that use this modifier,
536-
// we check if gasMetering started in the off position. If it did, we don't want to turn
537-
// it back on until we exit the top level function that used the modifier
538-
//
539-
// i.e. funcA() noGasMetering { funcB() }, where funcB has noGasMetering as well.
540-
// funcA will have `gasStartedOff` as false, funcB will have it as true,
541-
// so we only turn metering back on at the end of the funcA
542-
bool gasStartedOff = gasMeteringOff;
543-
gasMeteringOff = true;
544-
545-
_;
546-
547-
// if gas metering was on when this modifier was called, turn it back on at the end
548-
if (!gasStartedOff) {
549-
gasMeteringOff = false;
550-
vm.resumeGasMetering();
551-
}
552-
}
553-
554-
modifier skipWhenForking() {
555-
if (!isFork()) {
556-
_;
557-
}
558-
}
559-
560-
modifier skipWhenNotForking() {
561-
if (isFork()) {
562-
_;
563-
}
564-
}
565565
}

src/StdUtils.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
pragma solidity >=0.6.2 <0.9.0;
33

44
// TODO Remove import.
5-
import {Vm} from "./Vm.sol";
5+
import {VmSafe} from "./Vm.sol";
66

77
abstract contract StdUtils {
8-
Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
8+
VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
99
address private constant CONSOLE2_ADDRESS = 0x000000000000000000636F6e736F6c652e6c6f67;
1010

1111
uint256 private constant INT256_MIN_ABS =
@@ -109,6 +109,8 @@ abstract contract StdUtils {
109109
return address(uint160(uint256(bytesValue)));
110110
}
111111

112+
// Used to prevent the compilation of console, which shortens the compilation time when console is not used elsewhere.
113+
112114
function console2_log(string memory p0, uint256 p1) private view {
113115
(bool status,) = address(CONSOLE2_ADDRESS).staticcall(abi.encodeWithSignature("log(string,uint256)", p0, p1));
114116
status;

src/Test.sol

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.6.2 <0.9.0;
33

4-
import {CommonBase} from "./Common.sol";
5-
import {DSTest} from "ds-test/test.sol";
4+
// 💬 ABOUT
5+
// Standard Library's default Test
6+
7+
// 🧩 MODULES
68
import {console} from "./console.sol";
79
import {console2} from "./console2.sol";
810
import {StdAssertions} from "./StdAssertions.sol";
@@ -15,6 +17,12 @@ import {StdStorage, stdStorage} from "./StdStorage.sol";
1517
import {StdUtils} from "./StdUtils.sol";
1618
import {Vm} from "./Vm.sol";
1719

18-
abstract contract TestBase is CommonBase {}
20+
// 📦 BOILERPLATE
21+
import {TestBase} from "./Base.sol";
22+
import {DSTest} from "ds-test/test.sol";
1923

20-
abstract contract Test is DSTest, StdAssertions, StdChains, StdCheats, StdUtils, TestBase {}
24+
// ⭐️ TEST
25+
abstract contract Test is DSTest, StdAssertions, StdChains, StdCheats, StdUtils, TestBase {
26+
// Note: IS_TEST() must return true.
27+
// Note: Must have failure system, https://github.com/dapphub/ds-test/blob/cd98eff28324bfac652e63a239a60632a761790b/src/test.sol#L39-L76.
28+
}

0 commit comments

Comments
 (0)