Skip to content

[Refactor] MCGlobal Methods & MCBase Contracts #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Continuous Integration

Check warning on line 1 in .github/workflows/ci.yml

View workflow job for this annotation

GitHub Actions / review-workflow-changes

1:1 [document-start] missing document start "---"

on:

Check warning on line 3 in .github/workflows/ci.yml

View workflow job for this annotation

GitHub Actions / review-workflow-changes

3:1 [truthy] truthy value should be one of [false, true]
pull_request:
types:
- opened
Expand Down Expand Up @@ -61,11 +61,9 @@
mkdir /tmp/install-with-template-test
cd /tmp/install-with-template-test
forge init mc-example-project -t metacontract/template
forge test
- name: Run forge test
run: |
cd ./mc-example-project
forge init mc-example-project -t metacontract/template
forge test

# slither:
Expand Down
31 changes: 7 additions & 24 deletions devkit/MCBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,32 @@ import {Script as ForgeScript} from "forge-std/Script.sol";
import {Test as ForgeTest} from "forge-std/Test.sol";

import {MCDevKit} from "devkit/MCDevKit.sol";
import {ForgeHelper} from "devkit/utils/ForgeHelper.sol";
import {System} from "devkit/system/System.sol";


abstract contract MCBase is CommonBase {
MCDevKit internal mc;
uint256 internal deployerKey;
address internal deployer;

constructor() {
System.Config().load();
}
}

abstract contract MCScriptBase is MCBase, ForgeScript {
modifier startBroadcastWith(string memory envKey) {
_startBroadcastWith(envKey);
_;
}

modifier startBroadcastWithDeployerPrivKey() {
_startBroadcastWith("DEPLOYER_PRIV_KEY");
_;
}

function _startBroadcastWith(string memory envKey) internal {
deployerKey = ForgeHelper.getPrivateKey(envKey);
deployerKey = mc.loadPrivateKey(envKey);
deployer = vm.addr(deployerKey);
vm.startBroadcast(deployerKey);
_;
}
}

abstract contract MCTestBase is MCBase, ForgeTest {
modifier startPrankWith(string memory envKey) {
_startPrankWith(envKey);
_;
}
modifier startPrankWithDeployer() {
_startPrankWith("DEPLOYER");
_;
}
function _startPrankWith(string memory envKey) internal {
deployer = vm.envOr(envKey, makeAddr(envKey));
vm.startPrank(deployer);
}

modifier assumeAddressIsNotReserved(address addr) {
ForgeHelper.assumeAddressIsNotReserved(addr);
_;
}
}
16 changes: 7 additions & 9 deletions devkit/MCDevKit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {DictionaryRegistry} from "devkit/registry/DictionaryRegistry.sol";
import {ProxyRegistry} from "devkit/registry/ProxyRegistry.sol";

// Global Methods
import {MCSetupLib} from "devkit/utils/global/MCSetupLib.sol";
import {MCBundleLib} from "devkit/utils/global/MCBundleLib.sol";
import {MCInitLib} from "devkit/utils/global/MCInitLib.sol";
import {MCDeployLib} from "devkit/utils/global/MCDeployLib.sol";
import {MCFinderLib} from "devkit/utils/global/MCFinderLib.sol";
import {MCContextLib} from "devkit/utils/global/MCContextLib.sol";
import {MCTestLib} from "devkit/utils/global/MCTestLib.sol";
import {MCMockLib} from "devkit/utils/global/MCMockLib.sol";
import {MCHelpers} from "devkit/utils/global/MCHelpers.sol";

// System
// System Methods
import {Tracer} from "devkit/system/Tracer.sol";


Expand All @@ -30,10 +29,9 @@ struct MCDevKit {
DictionaryRegistry dictionary;
ProxyRegistry proxy;
}
using MCSetupLib for MCDevKit global;
using MCBundleLib for MCDevKit global;
using MCInitLib for MCDevKit global;
using MCDeployLib for MCDevKit global;
using MCFinderLib for MCDevKit global;
using MCContextLib for MCDevKit global;
using MCTestLib for MCDevKit global;
using MCMockLib for MCDevKit global;
using MCHelpers for MCDevKit global;
using Tracer for MCDevKit global;
8 changes: 0 additions & 8 deletions devkit/MCScript.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ import {MCScriptBase} from "./MCBase.sol";
// ⭐️ MC SCRIPT
abstract contract MCScript is MCScriptBase {
constructor() {
System.Config().load();
if (System.Config().SETUP.STD_FUNCS) mc.setupStdFunctions();
}
}

// ⭐️ MC SCRIPT without Setup
abstract contract MCScriptWithoutSetup is MCScriptBase {
constructor() {
System.Config().load();
}
}
10 changes: 1 addition & 9 deletions devkit/MCTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {MCTestBase} from "./MCBase.sol";
// ⭐️ MC TEST
abstract contract MCTest is MCTestBase {
constructor() {
System.Config().load();
if (System.Config().SETUP.STD_FUNCS) mc.setupStdFunctions();
}
}
Expand All @@ -36,7 +35,7 @@ abstract contract MCStateFuzzingTest is MCTestBase, OZProxy { // solhint-disable
address dictionary;

constructor() {
System.Config().load();
// System.Config().load();
implementations[bytes4(0)] = address(new Receive());
}

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

}

// 🌟 MC TEST for DevKit
abstract contract MCDevKitTest is MCTestBase {
constructor() {
System.Config().load();
}
}
4 changes: 2 additions & 2 deletions devkit/system/Validator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ library Validator {

// Validate without broadcast
modifier noBroadcast() {
ForgeHelper.pauseBroadcast();
(bool isBroadcasting, address currentSender) = ForgeHelper.pauseBroadcast();
_;
ForgeHelper.resumeBroadcast();
ForgeHelper.resumeBroadcast(isBroadcasting, currentSender);
}


Expand Down
37 changes: 22 additions & 15 deletions devkit/utils/ForgeHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,36 @@ library ForgeHelper {
/**-------------------
🔧 Env File
---------------------*/
function getPrivateKey(string memory envKey) internal view returns(uint256) {
function loadPrivateKey(string memory envKey) internal view returns(uint256) {
return uint256(vm.envBytes32(envKey));
}

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

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


/**------------------
📍 Address
--------------------*/
function loadAddress(address target, bytes32 slot) internal view returns(address) {
function getAddress(address target, bytes32 slot) internal view returns(address) {
return address(uint160(uint256(vm.load(target, slot))));
}

function getDictionaryAddress(address proxy) internal view returns(address) {
return loadAddress(proxy, ProxyUtils.DICTIONARY_SLOT);
return getAddress(proxy, ProxyUtils.DICTIONARY_SLOT);
}

function injectCode(address target, bytes memory runtimeBytecode) internal {
vm.etch(target, runtimeBytecode);
}

function injectAddressToStorage(address target, bytes32 slot, address addr) internal {
vm.store(target, slot, bytes32(uint256(uint160(addr))));
}

function injectDictionary(address proxy, address dictionary) internal {
injectAddressToStorage(proxy, ProxyUtils.DICTIONARY_SLOT, dictionary);
}

function assumeAddressIsNotReserved(address addr) internal pure {
Expand Down Expand Up @@ -112,13 +118,14 @@ library ForgeHelper {
/**------------------
📡 Broadcast
--------------------*/
function pauseBroadcast() internal {
(VmSafe.CallerMode mode,,) = vm.readCallers();
if (mode == VmSafe.CallerMode.RecurrentBroadcast) vm.stopBroadcast();
function pauseBroadcast() internal returns(bool isBroadcasting, address) {
(,address currentSender,) = vm.readCallers();
isBroadcasting = vm.isContext(VmSafe.ForgeContext.ScriptBroadcast);
if (isBroadcasting) vm.stopBroadcast();
return (isBroadcasting, currentSender);
}
function resumeBroadcast() internal {
(VmSafe.CallerMode mode,,) = vm.readCallers();
if (mode == VmSafe.CallerMode.RecurrentBroadcast) vm.startBroadcast(getPrivateKey("DEPLOYER_PRIV_KEY")); // Without CALL TODO
function resumeBroadcast(bool isBroadcasting, address currentSender) internal {
if (isBroadcasting) vm.startBroadcast(currentSender);
}

}
25 changes: 0 additions & 25 deletions devkit/utils/global/MCContextLib.sol

This file was deleted.

16 changes: 8 additions & 8 deletions devkit/utils/global/MCDeployLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import {NameGenerator} from "devkit/utils/mapping/NameGenerator.sol";
using NameGenerator for mapping(string => Proxy);


/***************************************
🚀 Deployment
🌞 Deploy Meta Contract
🏠 Deploy Proxy
📚 Deploy Dictionary
🔂 Duplicate Dictionary
💽 Load Dictionary
****************************************/
/************************************
* 🚀 Deployment
* 🌞 Deploy Meta Contract
* 🏠 Deploy Proxy
* 📚 Deploy Dictionary
* 🔂 Duplicate Dictionary
* 💽 Load Dictionary
*************************************/
library MCDeployLib {

/**-----------------------------
Expand Down
22 changes: 11 additions & 11 deletions devkit/utils/global/MCFinderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import {Proxy} from "devkit/core/Proxy.sol";
import {Dictionary} from "devkit/core/Dictionary.sol";


/**********************************
🔍 Finder
🏠 Find Proxy
📚 Find Dictionary
***********************************/
/********************************************
* 🔍 Finder
* 🏠 Find Current Proxy Address
* 📚 Find Current Dictionary Address
*********************************************/
library MCFinderLib {

/**-------------------
🏠 Find Proxy
---------------------*/
/**----------------------------------
🏠 Find Current Proxy Address
------------------------------------*/
function toProxyAddress(MCDevKit storage mc) internal returns(address) {
return mc.proxy.findCurrent().addr;
}

/**------------------------
📚 Find Dictionary
--------------------------*/
/**----------------------------------------
📚 Find Current Dictionary Address
------------------------------------------*/
function toDictionaryAddress(MCDevKit storage mc) internal returns(address) {
return mc.dictionary.findCurrent().addr;
}
Expand Down
Loading
Loading