Skip to content

Commit

Permalink
chore: remove duplication in getting contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
tamtamchik committed Oct 4, 2024
1 parent cd9b0d7 commit ede02e7
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 49 deletions.
5 changes: 0 additions & 5 deletions lib/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ export async function loadContract<ContractType extends BaseContract>(
return (await addContractHelperFields(result, name)) as unknown as LoadedContract<ContractType>;
}

export async function getContractAt(name: string, address: string): Promise<LoadedContract> {
const contract = await ethers.getContractAt(name, address);
return await addContractHelperFields(contract, name);
}

export async function getContractPath(contractName: string) {
const artifact = await artifacts.readArtifact(contractName);
return artifact.sourceName;
Expand Down
10 changes: 2 additions & 8 deletions lib/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { ContractFactory, ContractTransactionReceipt } from "ethers";
import { ethers } from "hardhat";

import {
addContractHelperFields,
DeployedContract,
getContractAt,
getContractPath,
LoadedContract,
} from "lib/contract";
import { addContractHelperFields, DeployedContract, getContractPath, loadContract, LoadedContract } from "lib/contract";
import { ConvertibleToString, cy, gr, log, yl } from "lib/log";
import { incrementGasUsed, Sk, updateObjectInState } from "lib/state-file";

Expand Down Expand Up @@ -212,7 +206,7 @@ export async function updateProxyImplementation(

const implementation = await deployContract(artifactName, constructorArgs, proxyOwner);

const proxy = await getContractAt(PROXY_CONTRACT_NAME, proxyAddress);
const proxy = await loadContract(PROXY_CONTRACT_NAME, proxyAddress);
await makeTx(proxy, "proxy__upgradeTo", [implementation.address], { from: proxyOwner });

updateObjectInState(nameInState, {
Expand Down
4 changes: 2 additions & 2 deletions scripts/scratch/steps/0020-deploy-aragon-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from "hardhat";

import { DAOFactory, ENS } from "typechain-types";

import { getContractAt, loadContract, LoadedContract } from "lib/contract";
import { loadContract, LoadedContract } from "lib/contract";
import { deployImplementation, deployWithoutProxy, makeTx } from "lib/deploy";
import { assignENSName } from "lib/ens";
import { findEvents } from "lib/event";
Expand Down Expand Up @@ -33,7 +33,7 @@ async function deployAPM(
log(`Using APMRegistry: ${cy(apmAddress)}`);
log.emptyLine();

const apmRegistry = await getContractAt("APMRegistry", apmAddress);
const apmRegistry = await loadContract("APMRegistry", apmAddress);

return {
apmRegistry,
Expand Down
4 changes: 2 additions & 2 deletions scripts/scratch/steps/0060-create-app-repos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { readNetworkState, setValueInState, Sk } from "lib/state-file";

Expand All @@ -11,7 +11,7 @@ export async function main() {
const deployer = (await ethers.provider.getSigner()).address;
const state = readNetworkState({ deployer });

const template = await getContractAt("LidoTemplate", state[Sk.lidoTemplate].address);
const template = await loadContract("LidoTemplate", state[Sk.lidoTemplate].address);

// Create Lido app repos
const lidoAppsReceipt = await makeTx(
Expand Down
6 changes: 3 additions & 3 deletions scripts/scratch/steps/0070-deploy-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ethers } from "hardhat";

import { ERCProxy, EVMScriptRegistryFactory, Kernel } from "typechain-types";

import { getContractAt, getContractPath, loadContract, LoadedContract } from "lib/contract";
import { getContractPath, loadContract, LoadedContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { findEvents, findEventsWithInterfaces } from "lib/event";
import { cy, log, yl } from "lib/log";
Expand Down Expand Up @@ -192,7 +192,7 @@ async function saveStateFromNewDAOTx(newDAOReceipt: ContractTransactionReceipt)
appName = Sk.aragonEvmScriptRegistry;
}

const proxy = await getContractAt(proxyContractName, e.args.proxy);
const proxy = await loadContract(proxyContractName, e.args.proxy);

state[appName] = {
...state[appName],
Expand Down Expand Up @@ -225,7 +225,7 @@ export async function main() {
const deployer = (await ethers.provider.getSigner()).address;
const state = readNetworkState({ deployer });

const template = await getContractAt("LidoTemplate", state[Sk.lidoTemplate].address);
const template = await loadContract("LidoTemplate", state[Sk.lidoTemplate].address);
if (state[Sk.lidoTemplate].deployBlock) {
log(`Using LidoTemplate deploy block: ${yl(state.lidoTemplate.deployBlock)}`);
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/scratch/steps/0080-issue-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib";
import { makeTx } from "lib/deploy";
import { cy, log, yl } from "lib/log";
import { readNetworkState, Sk } from "lib/state-file";
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function main() {
log(` Total batches:`, yl(totalTransactions));
log.emptyLine();

const template = await getContractAt("LidoTemplate", state[Sk.lidoTemplate].address);
const template = await loadContract("LidoTemplate", state[Sk.lidoTemplate].address);
let endTotalSupply = 0n;

// Issue tokens in batches
Expand Down
4 changes: 2 additions & 2 deletions scripts/scratch/steps/0100-gate-seal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib";
import { makeTx } from "lib/deploy";
import { findEvents } from "lib/event";
import { cy, log } from "lib/log";
Expand All @@ -26,7 +26,7 @@ export async function main() {

// Create new GateSeal instance
const sealableContracts = [state.withdrawalQueueERC721.proxy.address, state.validatorsExitBusOracle.proxy.address];
const gateSealFactory = await getContractAt("IGateSealFactory", state[Sk.gateSeal].factoryAddress);
const gateSealFactory = await loadContract("IGateSealFactory", state[Sk.gateSeal].factoryAddress);

const receipt = await makeTx(
gateSealFactory,
Expand Down
4 changes: 2 additions & 2 deletions scripts/scratch/steps/0110-finalize-dao.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { readNetworkState, Sk } from "lib/state-file";

export async function main() {
const deployer = (await ethers.provider.getSigner()).address;
const state = readNetworkState({ deployer });

const template = await getContractAt("LidoTemplate", state[Sk.lidoTemplate].address);
const template = await loadContract("LidoTemplate", state[Sk.lidoTemplate].address);

// Finalize the DAO by calling the finalizeDAO function on the template
await makeTx(
Expand Down
18 changes: 9 additions & 9 deletions scripts/scratch/steps/0120-initialize-non-aragon-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { readNetworkState, Sk } from "lib/state-file";
import { en0x } from "lib/string";
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function main() {
"0x" +
ethers.AbiCoder.defaultAbiCoder().encode(["string"], [nodeOperatorsRegistryParams.stakingModuleTypeId]).slice(-64);

const nodeOperatorsRegistry = await getContractAt("NodeOperatorsRegistry", nodeOperatorsRegistryAddress);
const nodeOperatorsRegistry = await loadContract("NodeOperatorsRegistry", nodeOperatorsRegistryAddress);
await makeTx(
nodeOperatorsRegistry,
"initialize",
Expand All @@ -51,20 +51,20 @@ export async function main() {

// Initialize Lido
const bootstrapInitBalance = 10n; // wei
const lido = await getContractAt("Lido", lidoAddress);
const lido = await loadContract("Lido", lidoAddress);
await makeTx(lido, "initialize", [lidoLocatorAddress, eip712StETHAddress], {
value: bootstrapInitBalance,
from: deployer,
});

// Initialize LegacyOracle
const legacyOracle = await getContractAt("LegacyOracle", legacyOracleAddress);
const legacyOracle = await loadContract("LegacyOracle", legacyOracleAddress);
await makeTx(legacyOracle, "initialize", [lidoLocatorAddress, hashConsensusForAccountingAddress], { from: deployer });

const zeroLastProcessingRefSlot = 0;

// Initialize AccountingOracle
const accountingOracle = await getContractAt("AccountingOracle", accountingOracleAddress);
const accountingOracle = await loadContract("AccountingOracle", accountingOracleAddress);
await makeTx(
accountingOracle,
"initializeWithoutMigration",
Expand All @@ -78,7 +78,7 @@ export async function main() {
);

// Initialize ValidatorsExitBusOracle
const validatorsExitBusOracle = await getContractAt("ValidatorsExitBusOracle", ValidatorsExitBusOracleAddress);
const validatorsExitBusOracle = await loadContract("ValidatorsExitBusOracle", ValidatorsExitBusOracleAddress);
await makeTx(
validatorsExitBusOracle,
"initialize",
Expand All @@ -92,7 +92,7 @@ export async function main() {
);

// Initialize WithdrawalQueue
const withdrawalQueue = await getContractAt("WithdrawalQueueERC721", withdrawalQueueAddress);
const withdrawalQueue = await loadContract("WithdrawalQueueERC721", withdrawalQueueAddress);
await makeTx(withdrawalQueue, "initialize", [withdrawalQueueAdmin], { from: deployer });

// Set WithdrawalQueue base URI if provided
Expand All @@ -106,13 +106,13 @@ export async function main() {

// Initialize StakingRouter
const withdrawalCredentials = `0x010000000000000000000000${withdrawalVaultAddress.slice(2)}`;
const stakingRouter = await getContractAt("StakingRouter", stakingRouterAddress);
const stakingRouter = await loadContract("StakingRouter", stakingRouterAddress);
await makeTx(stakingRouter, "initialize", [stakingRouterAdmin, lidoAddress, withdrawalCredentials], {
from: deployer,
});

// Set OracleDaemonConfig parameters
const oracleDaemonConfig = await getContractAt("OracleDaemonConfig", oracleDaemonConfigAddress);
const oracleDaemonConfig = await loadContract("OracleDaemonConfig", oracleDaemonConfigAddress);
const CONFIG_MANAGER_ROLE = await oracleDaemonConfig.getFunction("CONFIG_MANAGER_ROLE")();
await makeTx(oracleDaemonConfig, "grantRole", [CONFIG_MANAGER_ROLE, testnetAdmin], { from: testnetAdmin });

Expand Down
10 changes: 5 additions & 5 deletions scripts/scratch/steps/0130-grant-roles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { log } from "lib/log";
import { readNetworkState, Sk } from "lib/state-file";
Expand All @@ -20,7 +20,7 @@ export async function main() {
const depositSecurityModuleAddress = state[Sk.depositSecurityModule].address;

// StakingRouter
const stakingRouter = await getContractAt("StakingRouter", stakingRouterAddress);
const stakingRouter = await loadContract("StakingRouter", stakingRouterAddress);
await makeTx(
stakingRouter,
"grantRole",
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function main() {

// ValidatorsExitBusOracle
if (gateSealAddress) {
const validatorsExitBusOracle = await getContractAt("ValidatorsExitBusOracle", validatorsExitBusOracleAddress);
const validatorsExitBusOracle = await loadContract("ValidatorsExitBusOracle", validatorsExitBusOracleAddress);
await makeTx(
validatorsExitBusOracle,
"grantRole",
Expand All @@ -61,7 +61,7 @@ export async function main() {
}

// WithdrawalQueue
const withdrawalQueue = await getContractAt("WithdrawalQueueERC721", withdrawalQueueAddress);
const withdrawalQueue = await loadContract("WithdrawalQueueERC721", withdrawalQueueAddress);
if (gateSealAddress) {
await makeTx(withdrawalQueue, "grantRole", [await withdrawalQueue.getFunction("PAUSE_ROLE")(), gateSealAddress], {
from: deployer,
Expand All @@ -83,7 +83,7 @@ export async function main() {
);

// Burner
const burner = await getContractAt("Burner", burnerAddress);
const burner = await loadContract("Burner", burnerAddress);
// NB: REQUEST_BURN_SHARES_ROLE is already granted to Lido in Burner constructor
await makeTx(
burner,
Expand Down
6 changes: 3 additions & 3 deletions scripts/scratch/steps/0140-plug-curated-staking-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { streccak } from "lib/keccak";
import { readNetworkState, Sk } from "lib/state-file";
Expand All @@ -15,8 +15,8 @@ export async function main() {
const state = readNetworkState({ deployer });

// Get contract instances
const stakingRouter = await getContractAt("StakingRouter", state.stakingRouter.proxy.address);
const nodeOperatorsRegistry = await getContractAt(
const stakingRouter = await loadContract("StakingRouter", state.stakingRouter.proxy.address);
const nodeOperatorsRegistry = await loadContract(
"NodeOperatorsRegistry",
state[Sk.appNodeOperatorsRegistry].proxy.address,
);
Expand Down
8 changes: 4 additions & 4 deletions scripts/scratch/steps/0150-transfer-roles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "hardhat";

import { getContractAt } from "lib/contract";
import { loadContract } from "lib/contract";
import { makeTx } from "lib/deploy";
import { readNetworkState, Sk } from "lib/state-file";

Expand All @@ -26,7 +26,7 @@ export async function main() {
];

for (const contract of ozAdminTransfers) {
const contractInstance = await getContractAt(contract.name, contract.address);
const contractInstance = await loadContract(contract.name, contract.address);
await makeTx(contractInstance, "grantRole", [DEFAULT_ADMIN_ROLE, agent], { from: deployer });
await makeTx(contractInstance, "renounceRole", [DEFAULT_ADMIN_ROLE, deployer], { from: deployer });
}
Expand All @@ -41,13 +41,13 @@ export async function main() {
];

for (const proxyAddress of ossifiableProxyAdminChanges) {
const proxy = await getContractAt("OssifiableProxy", proxyAddress);
const proxy = await loadContract("OssifiableProxy", proxyAddress);
await makeTx(proxy, "proxy__changeAdmin", [agent], { from: deployer });
}

// Change DepositSecurityModule admin if not using predefined address
if (state[Sk.depositSecurityModule].deployParameters.usePredefinedAddressInstead === null) {
const depositSecurityModule = await getContractAt("DepositSecurityModule", state.depositSecurityModule.address);
const depositSecurityModule = await loadContract("DepositSecurityModule", state.depositSecurityModule.address);
await makeTx(depositSecurityModule, "setOwner", [agent], { from: deployer });
}
}
7 changes: 5 additions & 2 deletions scripts/upgrade/steps/0000-deploy-locator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { assert } from "chai";
import { ethers } from "hardhat";

import { deployImplementation, getContractAt, LoadedContract, log, readNetworkState, Sk } from "lib";
import { log } from "lib";
import { loadContract, LoadedContract } from "lib/contract";
import { deployImplementation } from "lib/deploy";
import { readNetworkState, Sk } from "lib/state-file";

const VIEW_NAMES_AND_CTOR_ARGS = [
"accountingOracle",
Expand Down Expand Up @@ -56,7 +59,7 @@ export async function main(): Promise<void> {

const state = readNetworkState();
const locatorAddress = state[Sk.lidoLocator].proxy.address;
const locator = await getContractAt("LidoLocator", locatorAddress);
const locator = await loadContract("LidoLocator", locatorAddress);

const ctorArgs = await getConstructorArgs(locator);
if (Object.keys(g_newAddresses).length === 0) {
Expand Down

0 comments on commit ede02e7

Please sign in to comment.