Skip to content

Commit

Permalink
feat: allow script to load env or trezor address by tag
Browse files Browse the repository at this point in the history
  • Loading branch information
TuDo1403 committed Dec 28, 2023
1 parent 08eff70 commit e3cfe45
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
8 changes: 1 addition & 7 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
verify_arg=""
extra_argument=""
op_command="op run --env-file="./.env" --"

for arg in "$@"; do
case $arg in
--trezor)
op_command=""
extra_argument+=trezor@
;;
--broadcast)
op_command="op run --env-file="./.env" --"
# verify_arg="--verify --verifier sourcify --verifier-url https://sourcify.roninchain.com/server/"
;;
--log)
set -- "${@/#--log/}"
extra_argument+=log@
Expand All @@ -24,4 +18,4 @@ done
extra_argument="${extra_argument%%@}"

calldata=$(cast calldata 'run()')
${op_command} forge script ${verify_arg} --legacy ${@} --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
forge script ${verify_arg} ${@} -g 200 --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
25 changes: 9 additions & 16 deletions script/BaseGeneralConfig.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { Vm } from "../lib/forge-std/src/Vm.sol";
import { Vm, VmSafe } from "../lib/forge-std/src/Vm.sol";
import { StdStyle } from "../lib/forge-std/src/StdStyle.sol";
import { console2 as console } from "../lib/forge-std/src/console2.sol";
import { LibString } from "../lib/solady/src/utils/LibString.sol";
import { WalletConfig } from "./configs/WalletConfig.sol";
import { RuntimeConfig } from "./configs/RuntimeConfig.sol";
import { MigrationConfig } from "./configs/MigrationConfig.sol";
Expand All @@ -17,11 +16,8 @@ import { LibSharedAddress } from "./libraries/LibSharedAddress.sol";

contract BaseGeneralConfig is RuntimeConfig, WalletConfig, ContractConfig, NetworkConfig, MigrationConfig {
using StdStyle for string;
using LibString for string;
using EnumerableSet for EnumerableSet.AddressSet;

Vm internal constant vm = Vm(LibSharedAddress.VM);

fallback() external {
if (msg.sig == ISharedParameter.sharedArguments.selector) {
bytes memory returnData = getRawSharedArguments();
Expand Down Expand Up @@ -91,19 +87,12 @@ contract BaseGeneralConfig is RuntimeConfig, WalletConfig, ContractConfig, Netwo
}

function _setUpDefaultSender() private {
// by default we will read private key from .env
_envPk = vm.envUint(getPrivateKeyEnvLabel(getCurrentNetwork()));
_envSender = vm.rememberKey(_envPk);

label(block.chainid, _envSender, "ENVSender");
console.log("GeneralConfig:", vm.getLabel(_envSender));

_setUpSender();
}

function getSender() public view virtual override returns (address payable sender) {
sender = _option.trezor ? payable(_trezorSender) : payable(_envSender);
require(sender != address(0), "GeneralConfig: Sender is address(0x0)");
require(sender != address(0x0), "GeneralConfig: Sender is address(0x0)");
}

function setAddress(TNetwork network, TContract contractType, address contractAddr) public virtual {
Expand All @@ -127,10 +116,14 @@ contract BaseGeneralConfig is RuntimeConfig, WalletConfig, ContractConfig, Netwo

function _handleRuntimeConfig() internal virtual override {
if (_option.trezor) {
string memory str = vm.envString(deployerEnvLabel());
_trezorSender = vm.parseAddress(str.replace(trezorPrefix(), ""));
_loadTrezorAccount();
label(block.chainid, _trezorSender, "TrezorSender");
console.log("GeneralConfig:", vm.getLabel(_trezorSender));
console.log("GeneralConfig:", vm.getLabel(_trezorSender), "Enabled!");
} else {
string memory envLabel = getPrivateKeyEnvLabel(getCurrentNetwork());
_loadENVAccount(envLabel);
label(block.chainid, _envSender, "ENVSender");
console.log("GeneralConfig:", vm.getLabel(_envSender), "Enabled!");
}
}
}
65 changes: 59 additions & 6 deletions script/configs/WalletConfig.sol
Original file line number Diff line number Diff line change
@@ -1,24 +1,77 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { CommonBase } from "../../lib/forge-std/src/Base.sol";
import { LibString } from "../../lib/solady/src/utils/LibString.sol";
import { IWalletConfig } from "../interfaces/configs/IWalletConfig.sol";

abstract contract WalletConfig is IWalletConfig {
uint256 internal _envPk;
abstract contract WalletConfig is CommonBase, IWalletConfig {
using LibString for string;

string internal _envLabel;
address internal _envSender;
address internal _trezorSender;
WalletOption internal _walletOption;

function getSender() public view virtual returns (address payable sender);

function getSenderPk() public view virtual returns (uint256) {
return _envPk;
}

function trezorPrefix() public view virtual returns (string memory) {
return "trezor://";
}

function deployerEnvLabel() public view virtual returns (string memory) {
return "DEPLOYER";
}

function _loadTrezorAccount() internal {
if (tx.origin != DEFAULT_SENDER) {
_trezorSender = tx.origin;
} else {
try vm.envString(deployerEnvLabel()) returns (string memory str) {
_trezorSender = vm.parseAddress(str.replace(trezorPrefix(), ""));
} catch {
revert(
string.concat(
"\nGeneralConfig: Error finding trezor address!\n- Please override default sender with `--sender {your_trezor_account}` tag \n- Or make `.env` file and create field `",
deployerEnvLabel(),
"=",
trezorPrefix(),
"{your_trezor_account}`"
)
);
}
}
_walletOption = WalletOption.Trezor;
}

function _loadENVAccount(string memory envLabel) internal {
_envLabel = envLabel;
_walletOption = WalletOption.Env;
_envSender = vm.rememberKey(_loadENVPrivateKey(envLabel));
}

function _loadENVPrivateKey(string memory envLabel) private returns (uint256) {
try vm.envUint(envLabel) returns (uint256 pk) {
return pk;
} catch {
string[] memory commandInput = new string[](3);

try vm.envString(envLabel) returns (string memory data) {
commandInput[2] = data;
} catch {
revert(
string.concat(
"\nGeneralConfig: Error finding env address!\n- Please make `.env` file and create field `",
envLabel,
"=",
"{op_secret_reference_or_your_private_key}`"
)
);
}
commandInput[0] = "op";
commandInput[1] = "read";

return vm.parseUint(vm.toString(vm.ffi(commandInput)));
}
}
}

0 comments on commit e3cfe45

Please sign in to comment.