Skip to content
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

feat(rework): implement script #138

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import "hardhat-deploy";
import {
HardhatUserConfig,
NetworkUserConfig,
} from "hardhat/types";
import * as dotenv from 'dotenv';

dotenv.config();

const { TESTNET_URL, MAINNET_URL } = process.env;

const testnet: NetworkUserConfig = {
chainId: 2021,
url: TESTNET_URL || "https://saigon-testnet.roninchain.com/rpc",
};

const mainnet: NetworkUserConfig = {
chainId: 2020,
url: MAINNET_URL || "https://api.roninchain.com/rpc",
};

const config: HardhatUserConfig = {
paths: {
sources: "./src",
},

networks: {
"ronin-testnet": testnet,
"ronin-mainnet": mainnet,
},
};

export default config;
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foundry-deployment-kit",
"version": "1.0.0",
"version": "3.0.0",
"description": "Deployment kit for Foundry",
"author": "Axie Infinity Engineering <[email protected]>",
"license": "MIT",
Expand All @@ -9,8 +9,13 @@
"src/**/*.sol"
],
"devDependencies": {
"dotenv": "^16.3.1",
"hardhat": "^2.12.7",
"hardhat-deploy": "0.11.29",
"husky": "^8.0.3",
"lint-staged": "^14.0.1"
"lint-staged": "^14.0.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"lint-staged": {
"{src,script,test}/**/*.sol": [
Expand Down
66 changes: 58 additions & 8 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
verify_arg=""
extra_argument=""

index=0
op_command=""
network_name=""
is_broadcast=false
should_verify=false

for arg in "$@"; do
case $arg in
--trezor)
Expand All @@ -10,29 +16,73 @@ for arg in "$@"; do
set -- "${@/#--no-postcheck/}"
extra_argument+=no-postcheck@
;;
--generate-artifacts)
set -- "${@/#--generate-artifacts/}"
extra_argument+=generate-artifact@
--verify)
should_verify=true
set -- "${@/#--verify/}"
;;
-f | --fork-url)
network_name=${@:index+2:1}
extra_argument+="network.${network_name}@"

set -- "${@/#-f/}"
set -- "${@/#--fork-url/}"
set -- "${@/#$network_name/}"
;;
--fork-block-number)
fork_block_number=${@:index+2:1}
extra_argument+="fork-block-number.${fork_block_number}@"

set -- "${@/#--fork-block-number/}"
set -- "${@/#$fork_block_number/}"
;;
-atf)
set -- "${@/#-atf/}"
extra_argument+=generate-artifact@
--broadcast)
is_broadcast=true
;;
*) ;;
esac
index=$((index + 1))
done

should_verify=$([[ $should_verify == true && $is_broadcast == true ]] && echo true || echo false)

if [[ $should_verify == true ]]; then
extra_argument+=generate-artifact@
fi

if [[ $should_verify == true ]] && [[ ! $network_name == "ronin-mainnet" ]] && [[ ! $network_name == "ronin-testnet" ]]; then
verify_arg="--verify --retries 5"
fi

echo "Should Verify Contract: $should_verify"

# Remove the @ character from the end of extra_argument
extra_argument="${extra_argument%%@}"

op_command=""
## Check if the private key is stored in the .env file
if [[ ! $extra_argument == *"sender"* ]] && [[ ! $extra_argument == *"trezor"* ]]; then
source .env

if [[ $MAINNET_PK == op* ]] || [[ $TESTNET_PK == op* ]] || [[ $LOCAL_PK == op* ]]; then
op_command="op run --env-file="./.env" --"
fi
fi

calldata=$(cast calldata 'run()')
${op_command} forge script ${verify_arg} ${@} -g 200 --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
start_time=$(date +%s)

echo ${op_command} forge script ${verify_arg} ${@} -g 200 --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
${op_command} forge script ${verify_arg} ${@} -g 200 --sig 'run(bytes,string)' ${calldata} "${extra_argument}"

# Check if the command was successful
if [ $? -eq 0 ]; then
if [[ $should_verify == true ]]; then
if [[ $network_name == "ronin-mainnet" ]] || [[ $network_name == "ronin-testnet" ]]; then
echo "Verifying contract..."
${op_command} yarn hardhat sourcify --endpoint https://sourcify.roninchain.com/server --network ${network_name}
fi
fi
fi

end_time=$(date +%s)

echo "Execution time: $((end_time - start_time))s"
13 changes: 11 additions & 2 deletions script/BaseGeneralConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DefaultContract } from "./utils/DefaultContract.sol";
import { LibSharedAddress } from "./libraries/LibSharedAddress.sol";

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

fallback() external {
Expand Down Expand Up @@ -122,7 +122,16 @@ contract BaseGeneralConfig is RuntimeConfig, WalletConfig, ContractConfig, Netwo
return getAllAddressesByRawData(_networkDataMap[network].chainId);
}

function _handleRuntimeConfig() internal virtual override {
function logSenderInfo() public view {
console.log(
"GeneralConfig:".cyan(),
"Sender:",
vm.getLabel(getSender()),
string.concat("| Balance: ", vm.toString(getSender().balance / 1 ether), " ETHER\n").magenta()
);
}

function buildRuntimeConfig() public virtual override {
if (_option.trezor) {
_loadTrezorAccount();
label(block.chainid, _trezorSender, "TrezorSender");
Expand Down
16 changes: 14 additions & 2 deletions script/BaseMigration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { OnchainExecutor } from "./OnchainExecutor.s.sol"; // cheat to load arti
import { IMigrationScript } from "./interfaces/IMigrationScript.sol";
import { LibProxy } from "./libraries/LibProxy.sol";
import { DefaultContract } from "./utils/DefaultContract.sol";
import { TContract } from "./types/Types.sol";
import { TContract, TNetwork } from "./types/Types.sol";
import { LibErrorHandler } from "../lib/contract-libs/src/LibErrorHandler.sol";

abstract contract BaseMigration is ScriptExtended {
Expand All @@ -39,8 +39,20 @@ abstract contract BaseMigration is ScriptExtended {
deploySharedAddress(address(ARTIFACT_FACTORY), type(ArtifactFactory).creationCode, "ArtifactFactory");
}

function switchTo(TNetwork networkType, uint256 forkBlockNumber)
public
virtual
override
returns (TNetwork currNetwork, uint256 currForkId)
{
(currNetwork, currForkId) = super.switchTo(networkType, forkBlockNumber);
// Should rebuild the shared arguments since different chain may have different shared arguments
_storeRawSharedArguments();
// Should rebuild runtime config
CONFIG.buildRuntimeConfig();
}

function _storeRawSharedArguments() internal virtual {
if (CONFIG.areSharedArgumentsStored()) return;
CONFIG.setRawSharedArguments(_sharedArguments());
}

Expand Down
5 changes: 0 additions & 5 deletions script/configs/MigrationConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ abstract contract MigrationConfig is IMigrationConfig {
bytes internal _migrationConfig;

function setRawSharedArguments(bytes memory config) public virtual {
if (areSharedArgumentsStored()) return;
_migrationConfig = config;
}

function areSharedArgumentsStored() public view virtual returns (bool) {
return _migrationConfig.length != 0;
}

function getRawSharedArguments() public view virtual returns (bytes memory) {
return _migrationConfig;
}
Expand Down
Loading
Loading