From c2f4d880a255bda6cadb6370d0e775ac2d3398fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 28 Jul 2022 11:27:06 +0200 Subject: [PATCH 1/4] fix: simplify GRE account management MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- tasks/deployment/accounts.ts | 8 +++--- tasks/gre.ts | 48 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/tasks/deployment/accounts.ts b/tasks/deployment/accounts.ts index da30366b5..d26102beb 100644 --- a/tasks/deployment/accounts.ts +++ b/tasks/deployment/accounts.ts @@ -10,22 +10,22 @@ task('migrate:accounts', '[localhost] Creates protocol accounts and saves them i throw new Error('This task can only be run on localhost network') } - const { graphConfig, getNamedAccounts, getDeployer } = hre.graph({ + const { graphConfig, getDeployer } = hre.graph({ graphConfig: taskArgs.graphConfig, }) console.log('> Generating addresses') const deployer = await getDeployer() - const { + const [ + , arbitrator, governor, authority, availabilityOracle, pauseGuardian, allocationExchangeOwner, - } = await getNamedAccounts() - console.log(await getNamedAccounts()) + ] = await hre.ethers.getSigners() console.log(`- Deployer: ${deployer.address}`) console.log(`- Arbitrator: ${arbitrator.address}`) diff --git a/tasks/gre.ts b/tasks/gre.ts index f2a062747..d2427e7bb 100644 --- a/tasks/gre.ts +++ b/tasks/gre.ts @@ -7,7 +7,7 @@ import { loadContracts } from '../cli/contracts' import { getItemValue, readConfig } from '../cli/config' import { Account, GREOptions, NamedAccounts } from './type-extensions' import fs from 'fs' -import { Signer, VoidSigner } from 'ethers' +import { Signer } from 'ethers' // Graph Runtime Environment (GRE) extensions for the HRE extendEnvironment((hre: HardhatRuntimeEnvironment) => { @@ -34,35 +34,46 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => { ] const getTestAccounts = async (): Promise => { - const accounts = [] - const signers: Signer[] = await hre.ethers.getSigners() + // Get list of privileged accounts we don't want as test accounts + const namedAccounts = await getNamedAccounts() + const blacklist = namedAccountList.map((a) => { + const account = namedAccounts[a] as Account + return account.address + }) + blacklist.push((await getDeployer()).address) + + // Get signers from provider and filter out blacklisted accounts + let signers: Signer[] = await hre.ethers.getSigners() + signers = signers.filter(async (s) => { + const address = await s.getAddress() + return !blacklist.includes(address) + }) - // Skip deployer and named accounts - for (let i = namedAccountList.length + 1; i < signers.length; i++) { - accounts.push({ signer: signers[i], address: await signers[i].getAddress() }) + // Build accounts + const accounts = [] + for (const signer of signers) { + accounts.push({ signer: signer, address: await signer.getAddress() }) } + return accounts } - // Returns void signers. Upgrades to signer on loca networks. const getNamedAccounts = async (): Promise => { const namedAccounts = namedAccountList.reduce((acc, name) => { const address = getItemValue(readConfig(graphConfigPath, true), `general/${name}`) - - if (chainId === '1337') { - const signer = hre.ethers.provider.getSigner(address) - acc[name] = { signer, address: address } - } else { - const signer = new VoidSigner(address) - acc[name] = { signer, address: signer.address } - } - + const signer = hre.ethers.provider.getSigner(address) + acc[name] = { signer, address: address } return acc }, {} as NamedAccounts) return namedAccounts } + const getDeployer = async () => { + const signer = hre.ethers.provider.getSigner(0) + return { signer, address: await signer.getAddress() } + } + return { addressBook: lazyObject(() => getAddressBook(addressBookPath, chainId)), graphConfig: lazyObject(() => readConfig(graphConfigPath, true)), @@ -71,10 +82,7 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => { ), getNamedAccounts: lazyFunction(() => getNamedAccounts), getTestAccounts: lazyFunction(() => getTestAccounts), - getDeployer: lazyFunction(() => async () => { - const signer = hre.ethers.provider.getSigner(0) - return { signer, address: await signer.getAddress() } - }), + getDeployer: lazyFunction(() => getDeployer), } } }) From b93458c2f5bc73904c3282596f954a6620d87631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 28 Jul 2022 12:11:15 +0200 Subject: [PATCH 2/4] chore: use hardhat SignerWithAddress instead of Account MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- tasks/deployment/ownership.ts | 8 ++++---- tasks/deployment/unpause.ts | 2 +- tasks/gre.ts | 33 +++++++++++++-------------------- tasks/type-extensions.d.ts | 23 +++++++++-------------- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/tasks/deployment/ownership.ts b/tasks/deployment/ownership.ts index c5a872806..83b4e6485 100644 --- a/tasks/deployment/ownership.ts +++ b/tasks/deployment/ownership.ts @@ -23,10 +23,10 @@ task( console.log(`- Governor: ${governor.address}`) const txs: ContractTransaction[] = [] - txs.push(await contracts.GraphToken.connect(governor.signer).acceptOwnership()) - txs.push(await contracts.Controller.connect(governor.signer).acceptOwnership()) - txs.push(await contracts.GraphProxyAdmin.connect(governor.signer).acceptOwnership()) - txs.push(await contracts.SubgraphNFT.connect(governor.signer).acceptOwnership()) + txs.push(await contracts.GraphToken.connect(governor).acceptOwnership()) + txs.push(await contracts.Controller.connect(governor).acceptOwnership()) + txs.push(await contracts.GraphProxyAdmin.connect(governor).acceptOwnership()) + txs.push(await contracts.SubgraphNFT.connect(governor).acceptOwnership()) await Promise.all(txs.map((tx) => tx.wait())) console.log('Done!') diff --git a/tasks/deployment/unpause.ts b/tasks/deployment/unpause.ts index fa5767de5..7f0b71f57 100644 --- a/tasks/deployment/unpause.ts +++ b/tasks/deployment/unpause.ts @@ -12,7 +12,7 @@ task('migrate:unpause', 'Unpause protocol') const { governor } = await getNamedAccounts() console.log('> Unpausing protocol') - const tx = await contracts.Controller.connect(governor.signer).setPaused(false) + const tx = await contracts.Controller.connect(governor).setPaused(false) await tx.wait() console.log('Done!') }) diff --git a/tasks/gre.ts b/tasks/gre.ts index d2427e7bb..342a13797 100644 --- a/tasks/gre.ts +++ b/tasks/gre.ts @@ -5,9 +5,9 @@ import { lazyFunction, lazyObject } from 'hardhat/plugins' import { getAddressBook } from '../cli/address-book' import { loadContracts } from '../cli/contracts' import { getItemValue, readConfig } from '../cli/config' -import { Account, GREOptions, NamedAccounts } from './type-extensions' +import { GREOptions, NamedAccounts } from './type-extensions' import fs from 'fs' -import { Signer } from 'ethers' +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' // Graph Runtime Environment (GRE) extensions for the HRE extendEnvironment((hre: HardhatRuntimeEnvironment) => { @@ -33,45 +33,38 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => { 'allocationExchangeOwner', ] - const getTestAccounts = async (): Promise => { + const getTestAccounts = async (): Promise => { // Get list of privileged accounts we don't want as test accounts const namedAccounts = await getNamedAccounts() const blacklist = namedAccountList.map((a) => { - const account = namedAccounts[a] as Account + const account = namedAccounts[a] as SignerWithAddress return account.address }) blacklist.push((await getDeployer()).address) - // Get signers from provider and filter out blacklisted accounts - let signers: Signer[] = await hre.ethers.getSigners() + // Get signers and filter out blacklisted accounts + let signers: SignerWithAddress[] = await hre.ethers.getSigners() signers = signers.filter(async (s) => { - const address = await s.getAddress() - return !blacklist.includes(address) + return !blacklist.includes(s.address) }) - // Build accounts - const accounts = [] - for (const signer of signers) { - accounts.push({ signer: signer, address: await signer.getAddress() }) - } - - return accounts + return signers } const getNamedAccounts = async (): Promise => { - const namedAccounts = namedAccountList.reduce((acc, name) => { + const namedAccounts = namedAccountList.reduce(async (accP, name) => { + const acc = await accP const address = getItemValue(readConfig(graphConfigPath, true), `general/${name}`) - const signer = hre.ethers.provider.getSigner(address) - acc[name] = { signer, address: address } + acc[name] = await hre.ethers.getSigner(address) return acc - }, {} as NamedAccounts) + }, Promise.resolve({} as NamedAccounts)) return namedAccounts } const getDeployer = async () => { const signer = hre.ethers.provider.getSigner(0) - return { signer, address: await signer.getAddress() } + return hre.ethers.getSigner(await signer.getAddress()) } return { diff --git a/tasks/type-extensions.d.ts b/tasks/type-extensions.d.ts index 400be724f..5dbaa09ab 100644 --- a/tasks/type-extensions.d.ts +++ b/tasks/type-extensions.d.ts @@ -1,4 +1,4 @@ -import { Signer } from 'ethers' +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' import { AddressBook } from '../cli/address-book' import { NetworkContracts } from '../cli/contracts' @@ -7,18 +7,13 @@ export interface GREOptions { graphConfig?: string } -export interface Account { - readonly signer: Signer - readonly address: string -} - export interface NamedAccounts { - arbitrator: Account - governor: Account - authority: Account - availabilityOracle: Account - pauseGuardian: Account - allocationExchangeOwner: Account + arbitrator: SignerWithAddress + governor: SignerWithAddress + authority: SignerWithAddress + availabilityOracle: SignerWithAddress + pauseGuardian: SignerWithAddress + allocationExchangeOwner: SignerWithAddress } declare module 'hardhat/types/runtime' { @@ -28,8 +23,8 @@ declare module 'hardhat/types/runtime' { graphConfig: any addressBook: AddressBook getNamedAccounts: () => Promise - getTestAccounts: () => Promise - getDeployer: () => Promise + getTestAccounts: () => Promise + getDeployer: () => Promise } } } From 23b41c7bdbf15f763df3941b9606e39f6c5a61e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 28 Jul 2022 13:33:07 +0200 Subject: [PATCH 3/4] fix: getTestAccounts blacklist not working properly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- tasks/gre.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/gre.ts b/tasks/gre.ts index 342a13797..890367110 100644 --- a/tasks/gre.ts +++ b/tasks/gre.ts @@ -44,7 +44,7 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => { // Get signers and filter out blacklisted accounts let signers: SignerWithAddress[] = await hre.ethers.getSigners() - signers = signers.filter(async (s) => { + signers = signers.filter((s) => { return !blacklist.includes(s.address) }) From 516e8d88d183c9e840c976c7cb077eb255f0b9b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 28 Jul 2022 15:25:56 +0200 Subject: [PATCH 4/4] fix: clarify variable naming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- tasks/gre.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tasks/gre.ts b/tasks/gre.ts index 890367110..41028e958 100644 --- a/tasks/gre.ts +++ b/tasks/gre.ts @@ -52,11 +52,11 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => { } const getNamedAccounts = async (): Promise => { - const namedAccounts = namedAccountList.reduce(async (accP, name) => { - const acc = await accP + const namedAccounts = namedAccountList.reduce(async (accountsPromise, name) => { + const accounts = await accountsPromise const address = getItemValue(readConfig(graphConfigPath, true), `general/${name}`) - acc[name] = await hre.ethers.getSigner(address) - return acc + accounts[name] = await hre.ethers.getSigner(address) + return accounts }, Promise.resolve({} as NamedAccounts)) return namedAccounts