Skip to content

Commit 2a7cd91

Browse files
committed
feat: Add Constants library
1 parent 3b20d60 commit 2a7cd91

5 files changed

Lines changed: 72 additions & 1 deletion

File tree

src/Base.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ abstract contract CommonBase {
1313
address internal constant CREATE2_FACTORY = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
1414
// Default address for tx.origin and msg.sender, 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38.
1515
address internal constant DEFAULT_SENDER = address(uint160(uint256(keccak256("foundry default caller"))));
16-
// Address of the test contract, deployed by the DEFAULT_SENDER.
16+
// The address of the first contract `CREATE`d by a running test contract.
17+
// When running tests, each test contract is `CREATE`d by `DEFAULT_SENDER` with nonce 1.
1718
address internal constant DEFAULT_TEST_CONTRACT = 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f;
1819
// Deterministic deployment address of the Multicall3 contract.
1920
address internal constant MULTICALL3_ADDRESS = 0xcA11bde05977b3631167028862bE2a173976CA11;

src/Script.sol

Lines changed: 1 addition & 0 deletions
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

Lines changed: 30 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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/StdConstants.t.sol

Lines changed: 38 additions & 0 deletions
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)