Skip to content

Commit

Permalink
Add hardhat tasks to lint factories and registrars
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetson committed Dec 3, 2023
1 parent 4e161cb commit 25d9021
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions packages/hardhat/tasks/setFactory.ts
Original file line number Diff line number Diff line change
@@ -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<Args[]>((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!`);
}
});
53 changes: 53 additions & 0 deletions packages/hardhat/tasks/setRegistrar.ts
Original file line number Diff line number Diff line change
@@ -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<Args[]>((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!`);
}
});

0 comments on commit 25d9021

Please sign in to comment.