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

Task for adding guards #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
101 changes: 101 additions & 0 deletions extensions/guards/addGuardians.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { task } from "hardhat/config";
import { GatewayV2 } from "typings/GatewayV2";
import { fetchJson } from "ethers/lib/utils";

/**
* USAGE
* npx hardhat add-guards --network [network] (--env [testnet|mainnet]) (--estimator estimator address)
* --env is optional, testnet by default
* --estimator is optional
*
* Example:
* npx hardhat add-guards --network goerli --env testnet --estimator 0x0408ff3B0A2cE1a0D5c25a0D4a387332f814Bd72
*/

interface IRelayer {
address: string;
}

enum EtherspotEnv {
testnet = "testnet",
mainnet = "mainnet",
}

interface TaskArgs {
env: EtherspotEnv;
estimator: string;
}

const EtherspotEnvUrls = {
[EtherspotEnv.testnet]: "https://qa-etherspot.pillarproject.io",
[EtherspotEnv.mainnet]: "https://etherspot.pillarproject.io",
};

const TASK_ADD_GUARDS = "add-guards";

task(TASK_ADD_GUARDS, "Add gw2 guardians")
.addParam("env", "Environment", EtherspotEnv.testnet)
.addOptionalParam("estimator", "Estimator adderss for the chain")
.setAction(async (args: TaskArgs, hre) => {
if (!EtherspotEnvUrls[args.env]) {
return console.error('Invalid env');
}
if (args.estimator && !hre.ethers.utils.isAddress(args.estimator)) {
return console.error('Invalid estimator');
}

const { network, ethers } = hre;
const gw2: GatewayV2 = await ethers.getContract("GatewayV2");

const relayers: IRelayer[] = await fetchJson(
EtherspotEnvUrls[args.env],
JSON.stringify({
operationName: null,
variables: {},
query: `{
gatewaySenders(chainId: ${network.config.chainId}) {
items {
address,
}
}
}`,
}),
).then(({ data }: any) => {
return data?.gatewaySenders?.items;
});

if (!relayers || !relayers.length) {
console.log("Relayers are empty");
return;
}
if (args.estimator) {
relayers.push({
address: args.estimator,
});
}

for (const relayer of relayers) {
const isGuardian = await gw2.isGuardian(relayer.address);
console.log(
`Relayer ${relayer.address} ${isGuardian ? "is" : "is not"} a guardian`,
);

if (!isGuardian) {
try {
console.log(`Adding a guardian...`);
const tx = await gw2.addGuardian(relayer.address);
console.log(`Waiting for the tx to settle`);
await tx.wait();
const isGuardian = await gw2.isGuardian(relayer.address);
console.log(
`Relayer ${relayer.address} ${
isGuardian ? "is" : "is not"
} a guardian`,
);
} catch (err) {
console.log("Tx failed", err);
return;
}
}
}
});
1 change: 1 addition & 0 deletions extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './deploy-diamonds';
import './test';
import './typings';
import './verify-all';
import './guards/addGuardians';

export * from './constants';
export * from './utils';