Skip to content

Commit ae5b8a9

Browse files
authored
Merge pull request #38 from metacontract/dev/devkit
[Refactor] MCGlobal Methods & MCBase Contracts
2 parents 94114f5 + f6df00c commit ae5b8a9

27 files changed

+296
-227
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ jobs:
6161
mkdir /tmp/install-with-template-test
6262
cd /tmp/install-with-template-test
6363
forge init mc-example-project -t metacontract/template
64-
forge test
6564
- name: Run forge test
6665
run: |
6766
cd ./mc-example-project
68-
forge init mc-example-project -t metacontract/template
6967
forge test
7068
7169
# slither:

devkit/MCBase.sol

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,32 @@ import {Script as ForgeScript} from "forge-std/Script.sol";
77
import {Test as ForgeTest} from "forge-std/Test.sol";
88

99
import {MCDevKit} from "devkit/MCDevKit.sol";
10-
import {ForgeHelper} from "devkit/utils/ForgeHelper.sol";
10+
import {System} from "devkit/system/System.sol";
1111

1212

1313
abstract contract MCBase is CommonBase {
1414
MCDevKit internal mc;
1515
uint256 internal deployerKey;
1616
address internal deployer;
17+
18+
constructor() {
19+
System.Config().load();
20+
}
1721
}
1822

1923
abstract contract MCScriptBase is MCBase, ForgeScript {
2024
modifier startBroadcastWith(string memory envKey) {
21-
_startBroadcastWith(envKey);
22-
_;
23-
}
24-
25-
modifier startBroadcastWithDeployerPrivKey() {
26-
_startBroadcastWith("DEPLOYER_PRIV_KEY");
27-
_;
28-
}
29-
30-
function _startBroadcastWith(string memory envKey) internal {
31-
deployerKey = ForgeHelper.getPrivateKey(envKey);
25+
deployerKey = mc.loadPrivateKey(envKey);
3226
deployer = vm.addr(deployerKey);
3327
vm.startBroadcast(deployerKey);
28+
_;
3429
}
3530
}
3631

3732
abstract contract MCTestBase is MCBase, ForgeTest {
3833
modifier startPrankWith(string memory envKey) {
39-
_startPrankWith(envKey);
40-
_;
41-
}
42-
modifier startPrankWithDeployer() {
43-
_startPrankWith("DEPLOYER");
44-
_;
45-
}
46-
function _startPrankWith(string memory envKey) internal {
4734
deployer = vm.envOr(envKey, makeAddr(envKey));
4835
vm.startPrank(deployer);
49-
}
50-
51-
modifier assumeAddressIsNotReserved(address addr) {
52-
ForgeHelper.assumeAddressIsNotReserved(addr);
5336
_;
5437
}
5538
}

devkit/MCDevKit.sol

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import {DictionaryRegistry} from "devkit/registry/DictionaryRegistry.sol";
99
import {ProxyRegistry} from "devkit/registry/ProxyRegistry.sol";
1010

1111
// Global Methods
12-
import {MCSetupLib} from "devkit/utils/global/MCSetupLib.sol";
13-
import {MCBundleLib} from "devkit/utils/global/MCBundleLib.sol";
12+
import {MCInitLib} from "devkit/utils/global/MCInitLib.sol";
1413
import {MCDeployLib} from "devkit/utils/global/MCDeployLib.sol";
1514
import {MCFinderLib} from "devkit/utils/global/MCFinderLib.sol";
16-
import {MCContextLib} from "devkit/utils/global/MCContextLib.sol";
17-
import {MCTestLib} from "devkit/utils/global/MCTestLib.sol";
15+
import {MCMockLib} from "devkit/utils/global/MCMockLib.sol";
16+
import {MCHelpers} from "devkit/utils/global/MCHelpers.sol";
1817

19-
// System
18+
// System Methods
2019
import {Tracer} from "devkit/system/Tracer.sol";
2120

2221

@@ -30,10 +29,9 @@ struct MCDevKit {
3029
DictionaryRegistry dictionary;
3130
ProxyRegistry proxy;
3231
}
33-
using MCSetupLib for MCDevKit global;
34-
using MCBundleLib for MCDevKit global;
32+
using MCInitLib for MCDevKit global;
3533
using MCDeployLib for MCDevKit global;
3634
using MCFinderLib for MCDevKit global;
37-
using MCContextLib for MCDevKit global;
38-
using MCTestLib for MCDevKit global;
35+
using MCMockLib for MCDevKit global;
36+
using MCHelpers for MCDevKit global;
3937
using Tracer for MCDevKit global;

devkit/MCScript.sol

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ import {MCScriptBase} from "./MCBase.sol";
1212
// ⭐️ MC SCRIPT
1313
abstract contract MCScript is MCScriptBase {
1414
constructor() {
15-
System.Config().load();
1615
if (System.Config().SETUP.STD_FUNCS) mc.setupStdFunctions();
1716
}
1817
}
19-
20-
// ⭐️ MC SCRIPT without Setup
21-
abstract contract MCScriptWithoutSetup is MCScriptBase {
22-
constructor() {
23-
System.Config().load();
24-
}
25-
}

devkit/MCTest.sol

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {MCTestBase} from "./MCBase.sol";
1818
// ⭐️ MC TEST
1919
abstract contract MCTest is MCTestBase {
2020
constructor() {
21-
System.Config().load();
2221
if (System.Config().SETUP.STD_FUNCS) mc.setupStdFunctions();
2322
}
2423
}
@@ -36,7 +35,7 @@ abstract contract MCStateFuzzingTest is MCTestBase, OZProxy { // solhint-disable
3635
address dictionary;
3736

3837
constructor() {
39-
System.Config().load();
38+
// System.Config().load();
4039
implementations[bytes4(0)] = address(new Receive());
4140
}
4241

@@ -55,10 +54,3 @@ abstract contract MCStateFuzzingTest is MCTestBase, OZProxy { // solhint-disable
5554
}
5655

5756
}
58-
59-
// 🌟 MC TEST for DevKit
60-
abstract contract MCDevKitTest is MCTestBase {
61-
constructor() {
62-
System.Config().load();
63-
}
64-
}

devkit/system/Validator.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ library Validator {
4747

4848
// Validate without broadcast
4949
modifier noBroadcast() {
50-
ForgeHelper.pauseBroadcast();
50+
(bool isBroadcasting, address currentSender) = ForgeHelper.pauseBroadcast();
5151
_;
52-
ForgeHelper.resumeBroadcast();
52+
ForgeHelper.resumeBroadcast(isBroadcasting, currentSender);
5353
}
5454

5555

devkit/utils/ForgeHelper.sol

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,36 @@ library ForgeHelper {
2727
/**-------------------
2828
🔧 Env File
2929
---------------------*/
30-
function getPrivateKey(string memory envKey) internal view returns(uint256) {
30+
function loadPrivateKey(string memory envKey) internal view returns(uint256) {
3131
return uint256(vm.envBytes32(envKey));
3232
}
3333

3434
function loadAddressFromEnv(string memory envKey) internal view returns(address) {
3535
return vm.envOr(envKey, address(0));
3636
}
3737

38-
// TODO: check version
39-
function canGetDeployedContract(string memory envKey) internal view returns(bool) {
40-
if (vm.envOr(envKey, address(0)).code.length != 0) return true;
41-
return false;
42-
}
43-
4438

4539
/**------------------
4640
📍 Address
4741
--------------------*/
48-
function loadAddress(address target, bytes32 slot) internal view returns(address) {
42+
function getAddress(address target, bytes32 slot) internal view returns(address) {
4943
return address(uint160(uint256(vm.load(target, slot))));
5044
}
5145

5246
function getDictionaryAddress(address proxy) internal view returns(address) {
53-
return loadAddress(proxy, ProxyUtils.DICTIONARY_SLOT);
47+
return getAddress(proxy, ProxyUtils.DICTIONARY_SLOT);
48+
}
49+
50+
function injectCode(address target, bytes memory runtimeBytecode) internal {
51+
vm.etch(target, runtimeBytecode);
52+
}
53+
54+
function injectAddressToStorage(address target, bytes32 slot, address addr) internal {
55+
vm.store(target, slot, bytes32(uint256(uint160(addr))));
56+
}
57+
58+
function injectDictionary(address proxy, address dictionary) internal {
59+
injectAddressToStorage(proxy, ProxyUtils.DICTIONARY_SLOT, dictionary);
5460
}
5561

5662
function assumeAddressIsNotReserved(address addr) internal pure {
@@ -112,13 +118,14 @@ library ForgeHelper {
112118
/**------------------
113119
📡 Broadcast
114120
--------------------*/
115-
function pauseBroadcast() internal {
116-
(VmSafe.CallerMode mode,,) = vm.readCallers();
117-
if (mode == VmSafe.CallerMode.RecurrentBroadcast) vm.stopBroadcast();
121+
function pauseBroadcast() internal returns(bool isBroadcasting, address) {
122+
(,address currentSender,) = vm.readCallers();
123+
isBroadcasting = vm.isContext(VmSafe.ForgeContext.ScriptBroadcast);
124+
if (isBroadcasting) vm.stopBroadcast();
125+
return (isBroadcasting, currentSender);
118126
}
119-
function resumeBroadcast() internal {
120-
(VmSafe.CallerMode mode,,) = vm.readCallers();
121-
if (mode == VmSafe.CallerMode.RecurrentBroadcast) vm.startBroadcast(getPrivateKey("DEPLOYER_PRIV_KEY")); // Without CALL TODO
127+
function resumeBroadcast(bool isBroadcasting, address currentSender) internal {
128+
if (isBroadcasting) vm.startBroadcast(currentSender);
122129
}
123130

124131
}

devkit/utils/global/MCContextLib.sol

Lines changed: 0 additions & 25 deletions
This file was deleted.

devkit/utils/global/MCDeployLib.sol

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import {NameGenerator} from "devkit/utils/mapping/NameGenerator.sol";
2323
using NameGenerator for mapping(string => Proxy);
2424

2525

26-
/***************************************
27-
🚀 Deployment
28-
🌞 Deploy Meta Contract
29-
🏠 Deploy Proxy
30-
📚 Deploy Dictionary
31-
🔂 Duplicate Dictionary
32-
💽 Load Dictionary
33-
****************************************/
26+
/************************************
27+
* 🚀 Deployment
28+
* 🌞 Deploy Meta Contract
29+
* 🏠 Deploy Proxy
30+
* 📚 Deploy Dictionary
31+
* 🔂 Duplicate Dictionary
32+
* 💽 Load Dictionary
33+
*************************************/
3434
library MCDeployLib {
3535

3636
/**-----------------------------

devkit/utils/global/MCFinderLib.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ import {Proxy} from "devkit/core/Proxy.sol";
1212
import {Dictionary} from "devkit/core/Dictionary.sol";
1313

1414

15-
/**********************************
16-
🔍 Finder
17-
🏠 Find Proxy
18-
📚 Find Dictionary
19-
***********************************/
15+
/********************************************
16+
* 🔍 Finder
17+
* 🏠 Find Current Proxy Address
18+
* 📚 Find Current Dictionary Address
19+
*********************************************/
2020
library MCFinderLib {
2121

22-
/**-------------------
23-
🏠 Find Proxy
24-
---------------------*/
22+
/**----------------------------------
23+
🏠 Find Current Proxy Address
24+
------------------------------------*/
2525
function toProxyAddress(MCDevKit storage mc) internal returns(address) {
2626
return mc.proxy.findCurrent().addr;
2727
}
2828

29-
/**------------------------
30-
📚 Find Dictionary
31-
--------------------------*/
29+
/**----------------------------------------
30+
📚 Find Current Dictionary Address
31+
------------------------------------------*/
3232
function toDictionaryAddress(MCDevKit storage mc) internal returns(address) {
3333
return mc.dictionary.findCurrent().addr;
3434
}

0 commit comments

Comments
 (0)