Skip to content

Commit

Permalink
tasks: check params
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlee42 committed Feb 23, 2024
1 parent 6cbb989 commit 46408b8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
65 changes: 39 additions & 26 deletions ts-src/tasks/l1.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import fs from "fs";

import { parseDuration } from "../utils/params";

const lockingPoolName = "LockingPool";

task("l1:whitelist", "Whitelist an sequencer address")
.addOptionalParam("addr", "the sequencer address")
.addOptionalParam("enable", "enable or remove the sequencer", "true")
.addParam("addr", "the sequencer address", "", types.string)
.addOptionalParam(
"enable",
"enable or remove the sequencer",
"true",
types.boolean,
)
.setAction(async (args, hre) => {
if (!hre.network.tags["l1"]) {
throw new Error(`${hre.network.name} is not an l1`);
Expand Down Expand Up @@ -36,8 +43,13 @@ task("l1:whitelist", "Whitelist an sequencer address")
});

task("l1:lock", "Lock Metis to LockingPool contract")
.addOptionalParam("key", "the private key file path for the sequencer")
.addOptionalParam("amount", "lock amount in Metis")
.addParam(
"key",
"the private key file path for the sequencer",
"",
types.string,
)
.addParam("amount", "lock amount in Metis", "", types.float)
.setAction(async (args, hre) => {
if (!hre.network.tags["l1"]) {
throw new Error(`${hre.network.name} is not an l1`);
Expand All @@ -48,19 +60,7 @@ task("l1:lock", "Lock Metis to LockingPool contract")
throw new Error(`MEITS_L1_TOKEN env is not set or it's not an address`);
}

const amountInWei = (() => {
if (args["amount"]) {
throw new Error(`amount arg should be provided`);
}

try {
return hre.ethers.parseEther(args["amount"]);
} catch {
throw new Error(
`The amount arg ${args["amount"]} is not a valid number`,
);
}
})();
const amountInWei = hre.ethers.parseEther(args["amount"]);

const { address: lockingPoolAddress } =
await hre.deployments.get("LockingPool");
Expand Down Expand Up @@ -119,8 +119,8 @@ task("l1:lock", "Lock Metis to LockingPool contract")
});

task("l1:update-lock-amount", "Update locking amount condition")
.addOptionalParam("min", "Min amount in Metis")
.addOptionalParam("max", "Max amount in Metis")
.addOptionalParam("min", "Min amount in Metis", "", types.float)
.addOptionalParam("max", "Max amount in Metis", "", types.float)
.setAction(async (args, hre) => {
if (!hre.network.tags["l1"]) {
throw new Error(`${hre.network.name} is not an l1`);
Expand All @@ -134,7 +134,9 @@ task("l1:update-lock-amount", "Update locking amount condition")
lockingPoolAddress,
);

let actions = 0;
if (args["min"]) {
actions++;
const min = hre.ethers.parseEther(args["min"]);
const min2 = await contract.minLock();
if (min != min2) {
Expand All @@ -147,6 +149,7 @@ task("l1:update-lock-amount", "Update locking amount condition")
}

if (args["max"]) {
actions++;
const max = hre.ethers.parseEther(args["max"]);
const max2 = await contract.maxLock();
if (max != max2) {
Expand All @@ -157,11 +160,20 @@ task("l1:update-lock-amount", "Update locking amount condition")
console.log("Confrimed at", tx.hash);
}
}

if (!actions) {
console.log("You need to provide --min or --max argument");
}
});

task("l1:update-mpc-address", "Update MPC address for LockingPool contract")
.addOptionalParam("addr", "The new MPC address")
.addOptionalParam("fund", "Send ETH gas to the MPC address at last")
.addParam("addr", "The new MPC address", "", types.string)
.addOptionalParam(
"fund",
"Send ETH gas to the MPC address at last",
"",
types.float,
)
.setAction(async (args, hre) => {
if (!hre.network.tags["l1"]) {
throw new Error(`${hre.network.name} is not an l1`);
Expand All @@ -180,7 +192,7 @@ task("l1:update-mpc-address", "Update MPC address for LockingPool contract")
throw new Error(`addr arg is not a valid address`);
}

console.log("Updating");
console.log("Updating the MPC address to", newAddr);
const tx = await lockingPool.updateMpc(newAddr);
console.log("Confrimed at", tx.hash);

Expand All @@ -206,11 +218,12 @@ task("l1:update-mpc-address", "Update MPC address for LockingPool contract")
});

task("l1:update-exit-delay", "update exit delay time duration")
.addOptionalParam("duration", "time duration in second")
.addParam("duration", "duration string(e.g. 1d1h30m20s)", "", types.string)
.setAction(async (args, hre) => {
if (!hre.network.tags["l1"]) {
throw new Error(`${hre.network.name} is not an l1`);
}

const { address: lockingPoolAddress } =
await hre.deployments.get("LockingPool");

Expand All @@ -219,8 +232,8 @@ task("l1:update-exit-delay", "update exit delay time duration")
lockingPoolAddress,
);

const duration = parseInt(args["duration"], 0);
console.log(`update the delay to ${duration}s`);
const duration = parseDuration(args["duration"]);
console.log(`update the delay to ${args["duration"]}(=${duration}s)`);
const tx = await lockingPool.updateWithdrawDelayTimeValue(duration);
console.log("Confrimed at", tx.hash);
});
26 changes: 11 additions & 15 deletions ts-src/tasks/l2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";

const conractName = "MetisSequencerSet";

task("l2:update-mpc-address", "Update MPC address for SequencerSet contract")
.addOptionalParam("addr", "The new MPC address")
.addOptionalParam("fund", "Send the Metis gas to the MPC address at last")
.addParam("addr", "The new MPC address")
.addOptionalParam(
"fund",
"Send the Metis gas to the MPC address at last",
"",
types.float,
)
.setAction(async (args, hre) => {
if (!hre.network.tags["l2"]) {
throw new Error(`${hre.network.name} is not an l2`);
Expand All @@ -18,21 +23,12 @@ task("l2:update-mpc-address", "Update MPC address for SequencerSet contract")
throw new Error(`addr arg is not a valid address`);
}

console.log("Updating");
console.log("Updating the MPC address to", newAddr);
const tx1 = await seqset.UpdateMpcAddress(newAddr);
console.log("Confrimed at", tx1.hash);

if (args["fund"]) {
const amountInWei = (() => {
try {
return hre.ethers.parseEther(args["fund"]);
} catch {
throw new Error(
`The amount arg ${args["fund"]} is not a valid number`,
);
}
})();

const amountInWei = hre.ethers.parseEther(args["fund"]);
console.log(`Sending ${args["fund"]} Metis to the mpc address`);
const [signer] = await hre.ethers.getSigners();
const tx = await signer.sendTransaction({
Expand All @@ -44,7 +40,7 @@ task("l2:update-mpc-address", "Update MPC address for SequencerSet contract")
});

task("l2:update-epoch-length", "Update epoch(aka span) length")
.addOptionalParam("length", "The new epoch length")
.addParam("length", "The new epoch length", "", types.int)
.setAction(async (args, hre) => {
if (!hre.network.tags["l2"]) {
throw new Error(`${hre.network.name} is not an l2`);
Expand Down
32 changes: 32 additions & 0 deletions ts-src/utils/params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const parseDuration = (durationString: string) => {
const regex = /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zA-Z]+)/g;
let totalSeconds = 0;

let match;
while ((match = regex.exec(durationString)) !== null) {
let value = parseFloat(match[1]);
let unit = match[2];

switch (unit.toLowerCase()) {
case "s":
totalSeconds += value;
break;
case "m":
totalSeconds += value * 60;
break;
case "h":
totalSeconds += value * 60 * 60;
break;
case "d":
totalSeconds += value * 24 * 60 * 60;
break;
case "w":
totalSeconds += value * 7 * 24 * 60 * 60;
break;
default:
throw new Error("Unsupported unit: " + unit);
}
}

return totalSeconds;
};

0 comments on commit 46408b8

Please sign in to comment.