Skip to content

Commit 87989d2

Browse files
authoredFeb 28, 2025··
feat: Add StdConstants library (#654)
Fixes #267. Gathers up all constants used throughout forge-std in a single library so they are easier to access. ~~The refactoring around the constants is minimal, it's mostly replacements of arbitrary values with references to the new library. No changes are visible from the outside, the 3 constants removed from `StdUtil` were private. Many more refactorings, like dropping other private constants that are used more than once or are variations about `type(uintXXX).max`, may be done in separate PRs.~~
1 parent 77a1919 commit 87989d2

File tree

6 files changed

+130
-9
lines changed

6 files changed

+130
-9
lines changed
 

‎src/Base.sol

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@ import {StdStorage} from "./StdStorage.sol";
55
import {Vm, VmSafe} from "./Vm.sol";
66

77
abstract contract CommonBase {
8-
// Cheat code address, 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D.
9-
address internal constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code"))));
10-
// console.sol and console2.sol work by executing a staticcall to this address.
8+
/// @dev Cheat code address.
9+
/// Calculated as `address(uint160(uint256(keccak256("hevm cheat code"))))`.
10+
address internal constant VM_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D;
11+
/// @dev console.sol and console2.sol work by executing a staticcall to this address.
12+
/// Calculated as `address(uint160(uint88(bytes11("console.log"))))`.
1113
address internal constant CONSOLE = 0x000000000000000000636F6e736F6c652e6c6f67;
12-
// Used when deploying with create2, https://github.com/Arachnid/deterministic-deployment-proxy.
14+
/// @dev Used when deploying with create2.
15+
/// Taken from https://github.com/Arachnid/deterministic-deployment-proxy.
1316
address internal constant CREATE2_FACTORY = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
14-
// Default address for tx.origin and msg.sender, 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38.
15-
address internal constant DEFAULT_SENDER = address(uint160(uint256(keccak256("foundry default caller"))));
16-
// Address of the test contract, deployed by the DEFAULT_SENDER.
17+
/// @dev The default address for tx.origin and msg.sender.
18+
/// Calculated as `address(uint160(uint256(keccak256("foundry default caller"))))`.
19+
address internal constant DEFAULT_SENDER = 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38;
20+
/// @dev The address of the first contract `CREATE`d by a running test contract.
21+
/// When running tests, each test contract is `CREATE`d by `DEFAULT_SENDER` with nonce 1.
22+
/// Calculated as `VM.computeCreateAddress(VM.computeCreateAddress(DEFAULT_SENDER, 1), 1)`.
1723
address internal constant DEFAULT_TEST_CONTRACT = 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f;
18-
// Deterministic deployment address of the Multicall3 contract.
24+
/// @dev Deterministic deployment address of the Multicall3 contract.
25+
/// Taken from https://www.multicall3.com.
1926
address internal constant MULTICALL3_ADDRESS = 0xcA11bde05977b3631167028862bE2a173976CA11;
20-
// The order of the secp256k1 curve.
27+
/// @dev The order of the secp256k1 curve.
2128
uint256 internal constant SECP256K1_ORDER =
2229
115792089237316195423570985008687907852837564279074904382605163141518161494337;
2330

‎src/Script.sol

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {console2} from "./console2.sol";
1010
import {safeconsole} from "./safeconsole.sol";
1111
import {StdChains} from "./StdChains.sol";
1212
import {StdCheatsSafe} from "./StdCheats.sol";
13+
import {StdConstants} from "./StdConstants.sol";
1314
import {stdJson} from "./StdJson.sol";
1415
import {stdMath} from "./StdMath.sol";
1516
import {StdStorage, stdStorageSafe} from "./StdStorage.sol";

‎src/StdConstants.sol

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.6.2 <0.9.0;
3+
4+
import {IMulticall3} from "./interfaces/IMulticall3.sol";
5+
import {Vm} from "./Vm.sol";
6+
7+
library StdConstants {
8+
/// @dev Cheat code address.
9+
/// Calculated as `address(uint160(uint256(keccak256("hevm cheat code"))))`.
10+
Vm internal constant VM = Vm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
11+
/// @dev console.sol and console2.sol work by executing a staticcall to this address.
12+
/// Calculated as `address(uint160(uint88(bytes11("console.log"))))`.
13+
address internal constant CONSOLE = 0x000000000000000000636F6e736F6c652e6c6f67;
14+
/// @dev Used when deploying with create2.
15+
/// Taken from https://github.com/Arachnid/deterministic-deployment-proxy.
16+
address internal constant CREATE2_FACTORY = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
17+
/// @dev The default address for tx.origin and msg.sender.
18+
/// Calculated as `address(uint160(uint256(keccak256("foundry default caller"))))`.
19+
address internal constant DEFAULT_SENDER = 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38;
20+
/// @dev The address of the first contract `CREATE`d by a running test contract.
21+
/// When running tests, each test contract is `CREATE`d by `DEFAULT_SENDER` with nonce 1.
22+
/// Calculated as `VM.computeCreateAddress(VM.computeCreateAddress(DEFAULT_SENDER, 1), 1)`.
23+
address internal constant DEFAULT_TEST_CONTRACT = 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f;
24+
/// @dev Deterministic deployment address of the Multicall3 contract.
25+
/// Taken from https://www.multicall3.com.
26+
IMulticall3 internal constant MULTICALL3_ADDRESS = IMulticall3(0xcA11bde05977b3631167028862bE2a173976CA11);
27+
/// @dev The order of the secp256k1 curve.
28+
uint256 internal constant SECP256K1_ORDER =
29+
115792089237316195423570985008687907852837564279074904382605163141518161494337;
30+
}

‎src/Test.sol

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {safeconsole} from "./safeconsole.sol";
1313
import {StdAssertions} from "./StdAssertions.sol";
1414
import {StdChains} from "./StdChains.sol";
1515
import {StdCheats} from "./StdCheats.sol";
16+
import {StdConstants} from "./StdConstants.sol";
1617
import {stdError} from "./StdError.sol";
1718
import {StdInvariant} from "./StdInvariant.sol";
1819
import {stdJson} from "./StdJson.sol";

‎test/CommonBase.t.sol

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
import {CommonBase} from "../src/Base.sol";
5+
import {StdConstants} from "../src/StdConstants.sol";
6+
import {Test} from "../src/Test.sol";
7+
8+
contract CommonBaseTest is Test {
9+
function testVmAddressValue() public pure {
10+
assertEq(VM_ADDRESS, address(StdConstants.VM));
11+
}
12+
13+
function testConsoleValue() public pure {
14+
assertEq(CONSOLE, StdConstants.CONSOLE);
15+
}
16+
17+
function testCreate2FactoryValue() public pure {
18+
assertEq(CREATE2_FACTORY, StdConstants.CREATE2_FACTORY);
19+
}
20+
21+
function testDefaultSenderValue() public pure {
22+
assertEq(DEFAULT_SENDER, StdConstants.DEFAULT_SENDER);
23+
}
24+
25+
function testDefaultTestContractValue() public pure {
26+
assertEq(DEFAULT_TEST_CONTRACT, StdConstants.DEFAULT_TEST_CONTRACT);
27+
}
28+
29+
function testMulticall3AddressValue() public pure {
30+
assertEq(MULTICALL3_ADDRESS, address(StdConstants.MULTICALL3_ADDRESS));
31+
}
32+
33+
function testSecp256k1OrderValue() public pure {
34+
assertEq(SECP256K1_ORDER, StdConstants.SECP256K1_ORDER);
35+
}
36+
37+
function testUint256MaxValue() public pure {
38+
assertEq(UINT256_MAX, type(uint256).max);
39+
}
40+
41+
function testVmValue() public pure {
42+
assertEq(address(vm), address(StdConstants.VM));
43+
}
44+
}

‎test/StdConstants.t.sol

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
import {StdConstants} from "../src/StdConstants.sol";
5+
import {Test} from "../src/Test.sol";
6+
7+
contract StdConstantsTest is Test {
8+
function testVm() public view {
9+
assertEq(StdConstants.VM.getBlockNumber(), 1);
10+
}
11+
12+
function testVmDerivation() public pure {
13+
assertEq(address(StdConstants.VM), address(uint160(uint256(keccak256("hevm cheat code")))));
14+
}
15+
16+
function testConsoleDerivation() public pure {
17+
assertEq(StdConstants.CONSOLE, address(uint160(uint88(bytes11("console.log")))));
18+
}
19+
20+
function testDefaultSender() public view {
21+
assertEq(StdConstants.DEFAULT_SENDER, msg.sender);
22+
}
23+
24+
function testDefaultSenderDerivation() public pure {
25+
assertEq(StdConstants.DEFAULT_SENDER, address(uint160(uint256(keccak256("foundry default caller")))));
26+
}
27+
28+
function testDefaultTestContract() public {
29+
assertEq(StdConstants.DEFAULT_TEST_CONTRACT, address(new Dummy()));
30+
}
31+
32+
function testDefaultTestContractDerivation() public view {
33+
assertEq(address(this), StdConstants.VM.computeCreateAddress(StdConstants.DEFAULT_SENDER, 1));
34+
assertEq(StdConstants.DEFAULT_TEST_CONTRACT, StdConstants.VM.computeCreateAddress(address(this), 1));
35+
}
36+
}
37+
38+
contract Dummy {}

0 commit comments

Comments
 (0)