From 25d9021e9b1c802b2f654931f638a81df1eb56e5 Mon Sep 17 00:00:00 2001 From: Medet Ahmetson Date: Sun, 3 Dec 2023 11:57:51 +0700 Subject: [PATCH] Add hardhat tasks to lint factories and registrars --- packages/hardhat/hardhat.config.ts | 2 + packages/hardhat/tasks/setFactory.ts | 57 ++++++++++++++++++++++++++ packages/hardhat/tasks/setRegistrar.ts | 53 ++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 packages/hardhat/tasks/setFactory.ts create mode 100644 packages/hardhat/tasks/setRegistrar.ts diff --git a/packages/hardhat/hardhat.config.ts b/packages/hardhat/hardhat.config.ts index 81b502e..25191bc 100644 --- a/packages/hardhat/hardhat.config.ts +++ b/packages/hardhat/hardhat.config.ts @@ -5,6 +5,8 @@ import "@nomicfoundation/hardhat-toolbox"; import "hardhat-deploy"; import "@matterlabs/hardhat-zksync-solc"; import "@matterlabs/hardhat-zksync-verify"; +import "./tasks/setFactory"; +import "./tasks/setRegistrar"; // If not set, it uses ours Alchemy's default API key. // You can get your own at https://dashboard.alchemyapi.io diff --git a/packages/hardhat/tasks/setFactory.ts b/packages/hardhat/tasks/setFactory.ts new file mode 100644 index 0000000..fd159d6 --- /dev/null +++ b/packages/hardhat/tasks/setFactory.ts @@ -0,0 +1,57 @@ +import { task } from "hardhat/config"; +import { chainIds, contractAddress, supportedNetworkParams, zeroAddr } from "../scripts/params"; + +type Args = { + chainId: string; + selector: string; + factory: string; +}; + +task("set-factory") + .setDescription("Lints the factories to the Registrar") + .setAction(async function (taskArgs, hre) { + const chainId = await hre.getChainId(); + + const Registrar = await hre.ethers.getContractFactory("Registrar", { + libraries: { + SourceNftLib: contractAddress(chainId, "SourceNftLib"), + }, + }); + const registrarAddr = contractAddress(chainId, "Registrar"); + if (registrarAddr.length === 0) { + throw `No 'Registrar' found in ${chainId}`; + } + const registrar = Registrar.attach(registrarAddr); + console.log(`Registrar '${registrarAddr}' in ${chainId}`); + + const destFactories = chainIds(chainId).reduce((destFactories, destChainId) => { + const addr = contractAddress(destChainId, "LinkedFactory"); + if (addr.length == 0) { + return destFactories; + } + const destArg: Args = { + chainId: destChainId, + selector: supportedNetworkParams[destChainId].selector, + factory: addr, + }; + destFactories.push(destArg); + return destFactories; + }, []); + + if (destFactories.length === 0) { + console.log(`No factories to set`); + return; + } + console.log(`There are ${destFactories.length} factories to set`); + for (const factory of destFactories) { + const networkParams = await registrar.functions["destNetworks"](factory.selector); + if (networkParams.factory !== zeroAddr) { + console.log(`Skipping ${factory.chainId} chain's factory as set`); + continue; + } + const tx = await registrar.functions["setFactory(uint64,address)"](factory.selector, factory.factory); + console.log(`Setting factory for ${factory.chainId}, deploying ${tx.hash}...`); + await tx.wait(); + console.log(`Setting factory for ${factory.chainId} completed!`); + } + }); diff --git a/packages/hardhat/tasks/setRegistrar.ts b/packages/hardhat/tasks/setRegistrar.ts new file mode 100644 index 0000000..dc71025 --- /dev/null +++ b/packages/hardhat/tasks/setRegistrar.ts @@ -0,0 +1,53 @@ +import { task } from "hardhat/config"; +import { chainIds, contractAddress, supportedNetworkParams, zeroAddr } from "../scripts/params"; + +type Args = { + chainId: string; + selector: string; + registrar: string; +}; + +task("set-registrar") + .setDescription("Lints the registrar to the LinkedFactory") + .setAction(async function (taskArgs, hre) { + const chainId = await hre.getChainId(); + + const LinkedFactory = await hre.ethers.getContractFactory("LinkedFactory", {}); + const linkedFactoryAddr = contractAddress(chainId, "LinkedFactory"); + if (linkedFactoryAddr.length === 0) { + throw `No 'LinkedFactory' found in ${chainId}`; + } + const linkedFactory = LinkedFactory.attach(linkedFactoryAddr); + console.log(`LinkedFactory '${linkedFactoryAddr}' in ${chainId}`); + + const destRegistrars = chainIds(chainId).reduce((destRegistrars, destChainId) => { + const addr = contractAddress(destChainId, "Registrar"); + if (addr.length == 0) { + return destRegistrars; + } + const destArg: Args = { + chainId: destChainId, + selector: supportedNetworkParams[destChainId].selector, + registrar: addr, + }; + destRegistrars.push(destArg); + return destRegistrars; + }, []); + + if (destRegistrars.length === 0) { + console.log(`No registrars to set`); + return; + } + console.log(`There are ${destRegistrars.length} registrars to set`); + for (const registrar of destRegistrars) { + const networkParams = await linkedFactory.functions["destNetworks"](registrar.selector); + if (networkParams.registrar !== zeroAddr) { + console.log(`Skipping ${registrar.chainId} chain's factory as set`); + continue; + } + const tx = await linkedFactory.functions["setRegistrar(uint64,address)"](registrar.selector, registrar.registrar); + console.log(`Setting registrar for ${registrar.chainId}, deploying ${tx.hash}...`); + await tx.wait(); + console.log(`Setting registrar for ${registrar.chainId} completed!`); + } + });