From 411160088329ee5f3cea4c318fe4929de931044f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 21 Nov 2024 17:26:52 -0300 Subject: [PATCH] feat(gre): working version for horizon with legacy contracts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- packages/hardhat-graph-protocol/src/config.ts | 42 ++++--- .../hardhat-graph-protocol/src/deployments.ts | 22 ++++ packages/hardhat-graph-protocol/src/gre.ts | 104 ++++-------------- .../sdk/deployments/horizon/address-book.ts | 8 +- .../src/sdk/deployments/horizon/index.ts | 4 +- .../src/sdk/deployments/horizon/types.ts | 10 +- .../src/type-extensions.ts | 67 +++++++++-- packages/hardhat-graph-protocol/src/types.ts | 24 ---- .../test/config.test.ts | 63 +++++++---- .../files/addresses-global-short.json | 0 .../files/addresses-network-short.json | 0 .../global-address-book/hardhat.config.ts | 6 +- .../invalid-address-book/hardhat.config.ts | 6 +- .../network-address-book/hardhat.config.ts | 12 +- .../fixtures/no-path-config/hardhat.config.ts | 6 +- .../fixtures/path-config/hardhat.config.ts | 6 +- .../hardhat-graph-protocol/test/gre.test.ts | 49 ++++----- .../hardhat-graph-protocol/test/helpers.ts | 30 +++-- packages/hardhat-graph-protocol/tsconfig.json | 2 +- packages/horizon/hardhat.config.ts | 6 +- 20 files changed, 258 insertions(+), 209 deletions(-) create mode 100644 packages/hardhat-graph-protocol/src/deployments.ts delete mode 100644 packages/hardhat-graph-protocol/src/types.ts create mode 100644 packages/hardhat-graph-protocol/test/fixtures/files/addresses-global-short.json create mode 100644 packages/hardhat-graph-protocol/test/fixtures/files/addresses-network-short.json diff --git a/packages/hardhat-graph-protocol/src/config.ts b/packages/hardhat-graph-protocol/src/config.ts index f93bc868e..3e5021b78 100644 --- a/packages/hardhat-graph-protocol/src/config.ts +++ b/packages/hardhat-graph-protocol/src/config.ts @@ -1,35 +1,49 @@ import fs from 'fs' + import { GraphPluginError } from './sdk/utils/error' import { logDebug } from './logger' +import { normalizePath } from './sdk/utils/path' -import type { GraphDeployment, GraphRuntimeEnvironmentOptions } from './types' +import type { GraphDeployment } from './deployments' +import type { GraphRuntimeEnvironmentOptions } from './type-extensions' import type { HardhatRuntimeEnvironment } from 'hardhat/types' -import { normalizePath } from './sdk/utils/path' export function getAddressBookPath( deployment: GraphDeployment, hre: HardhatRuntimeEnvironment, opts: GraphRuntimeEnvironmentOptions, ): string { + const optsPath = getPath(opts.deployments?.[deployment]) + const networkPath = getPath(hre.network.config.deployments?.[deployment]) + const globalPath = getPath(hre.config.graph?.deployments?.[deployment]) + logDebug(`== ${deployment} - Getting address book path`) logDebug(`Graph base dir: ${hre.config.paths.graph}`) - logDebug(`1) opts.addressBooks.[deployment]: ${opts.addressBooks?.[deployment]}`) - logDebug(`2) hre.network.config.addressBooks.[deployment]: ${hre.network.config?.addressBooks?.[deployment]}`) - logDebug(`3) hre.config.graph.addressBooks.[deployment]: ${hre.config.graph?.addressBooks?.[deployment]}`) - - let addressBookPath - = opts.addressBooks?.[deployment] ?? hre.network.config?.addressBooks?.[deployment] ?? hre.config.graph?.addressBooks?.[deployment] + logDebug(`1) opts: ${optsPath}`) + logDebug(`2) network: ${networkPath}`) + logDebug(`3) global: ${globalPath}`) + const addressBookPath = optsPath ?? networkPath ?? globalPath if (addressBookPath === undefined) { throw new GraphPluginError('Must set a an addressBook path!') } - addressBookPath = normalizePath(addressBookPath, hre.config.paths.graph) - - if (!fs.existsSync(addressBookPath)) { - throw new GraphPluginError(`Address book not found: ${addressBookPath}`) + const normalizedAddressBookPath = normalizePath(addressBookPath, hre.config.paths.graph) + if (!fs.existsSync(normalizedAddressBookPath)) { + throw new GraphPluginError(`Address book not found: ${normalizedAddressBookPath}`) } - logDebug(`Address book path found: ${addressBookPath}`) - return addressBookPath + logDebug(`Address book path found: ${normalizedAddressBookPath}`) + return normalizedAddressBookPath +} + +function getPath(value: string | { + addressBook: string +} | undefined): string | undefined { + if (typeof value === 'string') { + return value + } else if (value && typeof value == 'object') { + return value.addressBook + } + return } diff --git a/packages/hardhat-graph-protocol/src/deployments.ts b/packages/hardhat-graph-protocol/src/deployments.ts new file mode 100644 index 000000000..8074977b1 --- /dev/null +++ b/packages/hardhat-graph-protocol/src/deployments.ts @@ -0,0 +1,22 @@ +import type { GraphHorizonAddressBook, GraphHorizonContracts } from './sdk/deployments/horizon' + +// List of supported Graph deployments +const GraphDeploymentsList = [ + 'horizon', +] as const + +export type GraphDeployment = (typeof GraphDeploymentsList)[number] + +export type GraphDeploymentRuntimeEnvironmentMap = { + horizon: { + contracts: GraphHorizonContracts + addressBook: GraphHorizonAddressBook + } +} + +export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment { + return ( + typeof deployment === 'string' + && GraphDeploymentsList.includes(deployment as GraphDeployment) + ) +} diff --git a/packages/hardhat-graph-protocol/src/gre.ts b/packages/hardhat-graph-protocol/src/gre.ts index 5cc251138..a307583b8 100644 --- a/packages/hardhat-graph-protocol/src/gre.ts +++ b/packages/hardhat-graph-protocol/src/gre.ts @@ -1,12 +1,14 @@ import path from 'path' -import { GraphDeploymentsList, GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types' -import { logDebug, logWarn } from './logger' +import { assertGraphRuntimeEnvironment } from './type-extensions' import { getAddressBookPath } from './config' import { GraphHorizonAddressBook } from './sdk/deployments/horizon' import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' +import { isGraphDeployment } from './deployments' +import { logDebug } from './logger' import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types' +import type { GraphRuntimeEnvironmentOptions } from './type-extensions' export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly) => { const userPath = userConfig.paths?.graph @@ -26,113 +28,45 @@ export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly { - hre.graph = (opts: GraphRuntimeEnvironmentOptions = {}) => { + hre.graph = (opts: GraphRuntimeEnvironmentOptions = { deployments: {} }) => { logDebug('*** Initializing Graph Runtime Environment (GRE) ***') logDebug(`Main network: ${hre.network.name}`) - const provider = new HardhatEthersProvider(hre.network.provider, hre.network.name) const deployments = [ - ...Object.keys(opts.addressBooks ?? {}), - ...Object.keys(hre.network.config.addressBooks ?? {}), - ...Object.keys(hre.config.graph?.addressBooks ?? {}), - ] + ...Object.keys(opts.deployments ?? {}), + ...Object.keys(hre.network.config.deployments ?? {}), + ...Object.keys(hre.config.graph?.deployments ?? {}), + ].filter(v => isGraphDeployment(v)) logDebug(`Detected deployments: ${deployments.join(', ')}`) // Build the Graph Runtime Environment (GRE) for each deployment - const gre = {} as GraphRuntimeEnvironment + const provider = new HardhatEthersProvider(hre.network.provider, hre.network.name) + const greDeployments: Record = {} for (const deployment of deployments) { - if (!isGraphDeployment(deployment)) { - logWarn(`Invalid deployment: ${deployment}. Skipping...`) - continue - } - logDebug(`Initializing ${deployment} deployment...`) const addressBookPath = getAddressBookPath(deployment, hre, opts) let addressBook + switch (deployment) { case 'horizon': addressBook = new GraphHorizonAddressBook(addressBookPath, hre.network.config.chainId!) - gre.horizon = { + greDeployments.horizon = { addressBook: addressBook, contracts: addressBook.loadContracts(hre.network.config.chainId!, provider), } break - default: break } } + const gre = { + ...greDeployments, + provider, + chainId: async () => (await provider.getNetwork()).chainId, + } + assertGraphRuntimeEnvironment(gre) logDebug('GRE initialized successfully!') return gre } } - -// function buildGraphNetworkEnvironment( -// chainId: number, -// provider: EthersProviderWrapper | undefined, -// graphConfigPath: string | undefined, -// addressBookPath: string, -// isHHL1: boolean, -// enableTxLogging: boolean, -// secureAccounts: boolean, -// fork: boolean, -// getWallets: () => Promise, -// getWallet: (address: string) => Promise, -// unlockProvider: (caller: string) => Promise, -// ): GraphNetworkEnvironment | null { -// if (graphConfigPath === undefined) { -// logWarn( -// `No graph config file provided for chain: ${chainId}. ${ -// isHHL1 ? 'L2' : 'L1' -// } will not be initialized.`, -// ) -// return null -// } - -// if (provider === undefined) { -// logWarn( -// `No provider URL found for: ${chainId}. ${isHHL1 ? 'L2' : 'L1'} will not be initialized.`, -// ) -// return null -// } - -// // Upgrade provider to secure accounts if feature is enabled -// const getUpdatedProvider = async (caller: string) => -// secureAccounts ? await unlockProvider(caller) : provider - -// return { -// chainId: chainId, -// provider: provider, -// addressBook: lazyObject(() => new GraphNetworkAddressBook(addressBookPath, chainId)), -// graphConfig: lazyObject(() => { -// const config = readConfig(graphConfigPath, true) -// config.defaults = getDefaults(config, isHHL1) -// return config -// }), -// contracts: lazyObject(() => -// loadGraphNetworkContracts(addressBookPath, chainId, provider, undefined, { -// enableTxLogging, -// }), -// ), -// getWallets: lazyFunction(() => () => getWallets()), -// getWallet: lazyFunction(() => (address: string) => getWallet(address)), -// getDeployer: lazyFunction( -// () => async () => getDeployer(await getUpdatedProvider('getDeployer')), -// ), -// getNamedAccounts: lazyFunction( -// () => async () => -// getNamedAccounts( -// fork ? provider : await getUpdatedProvider('getNamedAccounts'), -// graphConfigPath, -// ), -// ), -// getTestAccounts: lazyFunction( -// () => async () => -// getTestAccounts(await getUpdatedProvider('getTestAccounts'), graphConfigPath), -// ), -// getAllAccounts: lazyFunction( -// () => async () => getAllAccounts(await getUpdatedProvider('getAllAccounts')), -// ), -// } -// } diff --git a/packages/hardhat-graph-protocol/src/sdk/deployments/horizon/address-book.ts b/packages/hardhat-graph-protocol/src/sdk/deployments/horizon/address-book.ts index 48b6e59ca..0ed701686 100644 --- a/packages/hardhat-graph-protocol/src/sdk/deployments/horizon/address-book.ts +++ b/packages/hardhat-graph-protocol/src/sdk/deployments/horizon/address-book.ts @@ -17,7 +17,7 @@ export class GraphHorizonAddressBook extends AddressBook Generator } - -export interface GraphHorizonRuntimeEnvironment { - contracts: GraphHorizonContracts - addressBook: GraphHorizonAddressBook -} diff --git a/packages/hardhat-graph-protocol/src/type-extensions.ts b/packages/hardhat-graph-protocol/src/type-extensions.ts index f8bfd7427..c7cf609d4 100644 --- a/packages/hardhat-graph-protocol/src/type-extensions.ts +++ b/packages/hardhat-graph-protocol/src/type-extensions.ts @@ -2,7 +2,48 @@ import 'hardhat/types/config' import 'hardhat/types/runtime' -import type { GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions } from './types' +import type { GraphDeployment, GraphDeploymentRuntimeEnvironmentMap } from './deployments' +import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' + +export type GraphRuntimeEnvironmentOptions = { + deployments?: { + [deployment in GraphDeployment]?: string | { + addressBook: string + } + } +} + +export type GraphRuntimeEnvironment = { + [deployment in keyof GraphDeploymentRuntimeEnvironmentMap]?: GraphDeploymentRuntimeEnvironmentMap[deployment] +} & { + provider: HardhatEthersProvider + chainId: () => Promise +} + +export function assertGraphRuntimeEnvironment( + obj: unknown, +): obj is GraphRuntimeEnvironment { + if (typeof obj !== 'object' || obj === null) return false + + const deployments = obj as Partial + + for (const deployment in deployments) { + const environment = deployments[deployment as keyof GraphDeploymentRuntimeEnvironmentMap] + if (!environment || typeof environment !== 'object') { + return false + } + } + + if (typeof (obj as GraphRuntimeEnvironment).provider !== 'object') { + return false + } + + if (typeof (obj as GraphRuntimeEnvironment).chainId !== 'function') { + return false + } + + return true +} declare module 'hardhat/types/runtime' { export interface HardhatRuntimeEnvironment { @@ -20,26 +61,34 @@ declare module 'hardhat/types/config' { } export interface HardhatNetworkConfig { - addressBooks?: { - [deployment: string]: string + deployments?: { + [deployment in GraphDeployment]?: string | { + addressBook: string + } } } export interface HardhatNetworkUserConfig { - addressBooks?: { - [deployment: string]: string + deployments?: { + [deployment in GraphDeployment]?: string | { + addressBook: string + } } } export interface HttpNetworkConfig { - addressBooks?: { - [deployment: string]: string + deployments?: { + [deployment in GraphDeployment]?: string | { + addressBook: string + } } } export interface HttpNetworkUserConfig { - addressBooks?: { - [deployment: string]: string + deployments?: { + [deployment in GraphDeployment]?: string | { + addressBook: string + } } } diff --git a/packages/hardhat-graph-protocol/src/types.ts b/packages/hardhat-graph-protocol/src/types.ts deleted file mode 100644 index 35e1069da..000000000 --- a/packages/hardhat-graph-protocol/src/types.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { GraphHorizonRuntimeEnvironment } from './sdk/deployments/horizon' - -export const GraphDeploymentsList = [ - 'horizon', -] as const - -export type GraphDeployment = (typeof GraphDeploymentsList)[number] - -export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment { - return ( - typeof deployment === 'string' - && GraphDeploymentsList.includes(deployment as GraphDeployment) - ) -} - -export type GraphRuntimeEnvironmentOptions = { - addressBooks?: { - [deployment in GraphDeployment]: string - } -} - -export type GraphRuntimeEnvironment = { - [deployment in GraphDeployment]: GraphHorizonRuntimeEnvironment -} diff --git a/packages/hardhat-graph-protocol/test/config.test.ts b/packages/hardhat-graph-protocol/test/config.test.ts index dbc84342c..3cd005c31 100644 --- a/packages/hardhat-graph-protocol/test/config.test.ts +++ b/packages/hardhat-graph-protocol/test/config.test.ts @@ -2,51 +2,74 @@ import path from 'path' import { expect } from 'chai' import { getAddressBookPath } from '../src/config' -import { useEnvironment } from './helpers' +import { loadHardhatContext } from './helpers' describe('GRE init functions', function () { // No address book - should throw describe('getAddressBookPath', function () { - useEnvironment('default-config', 'mainnet') - it('should throw if no address book is specified', function () { - expect(() => getAddressBookPath(this.hre, {})).to.throw('Must set a an addressBook path!') + this.hre = loadHardhatContext('default-config', 'mainnet') + expect(() => getAddressBookPath('horizon', this.hre, {})).to.throw('Must set a an addressBook path!') }) - }) - describe('getAddressBookPath', function () { - useEnvironment('network-address-book', 'mainnet') + it('should throw if address book doesn\'t exist', function () { + this.hre = loadHardhatContext('invalid-address-book', 'mainnet') + expect(() => getAddressBookPath('horizon', this.hre, {})).to.throw(/Address book not found: /) + }) // Address book via opts should be used it('should use opts parameter if available', function () { - const addressBook = getAddressBookPath(this.hre, { - addressBook: 'addresses-opt.json', + this.hre = loadHardhatContext('network-address-book', 'mainnet') + const addressBook = getAddressBookPath('horizon', this.hre, { + deployments: { + horizon: { + addressBook: 'addresses-opt.json', + }, + }, + }) + expect(path.basename(addressBook)).to.equal('addresses-opt.json') + }) + + it('should use opts parameter if available - shortcut syntax', function () { + this.hre = loadHardhatContext('network-address-book', 'mainnet') + const addressBook = getAddressBookPath('horizon', this.hre, { + deployments: { + horizon: 'addresses-opt.json', + }, }) expect(path.basename(addressBook)).to.equal('addresses-opt.json') }) // Address book via network config should be used it('should use HH network config', function () { - const addressBook = getAddressBookPath(this.hre, {}) + this.hre = loadHardhatContext('network-address-book', 'mainnet') + const addressBook = getAddressBookPath('horizon', this.hre, {}) expect(path.basename(addressBook)).to.equal('addresses-network.json') }) - }) - describe('getAddressBookPath', function () { - useEnvironment('global-address-book', 'mainnet') + it('should use HH network config - shortcut syntax', function () { + this.hre = loadHardhatContext('network-address-book', 'mainnet') + if (this.hre.network.config.deployments) { + this.hre.network.config.deployments.horizon = 'addresses-network-short.json' + } + const addressBook = getAddressBookPath('horizon', this.hre, {}) + expect(path.basename(addressBook)).to.equal('addresses-network-short.json') + }) // Address book via global config should be used it('should use HH global config', function () { - const addressBook = getAddressBookPath(this.hre, {}) + this.hre = loadHardhatContext('global-address-book', 'mainnet') + const addressBook = getAddressBookPath('horizon', this.hre, {}) expect(path.basename(addressBook)).to.equal('addresses-global.json') }) - }) - - describe('getAddressBookPath with a non existent address book', function () { - useEnvironment('invalid-address-book', 'mainnet') - it('should throw if address book doesn\'t exist', function () { - expect(() => getAddressBookPath(this.hre, {})).to.throw(/Address book not found: /) + it('should use HH global config - shortcut syntax', function () { + this.hre = loadHardhatContext('global-address-book', 'mainnet') + if (this.hre.config.graph.deployments) { + this.hre.config.graph.deployments.horizon = 'addresses-global-short.json' + } + const addressBook = getAddressBookPath('horizon', this.hre, {}) + expect(path.basename(addressBook)).to.equal('addresses-global-short.json') }) }) }) diff --git a/packages/hardhat-graph-protocol/test/fixtures/files/addresses-global-short.json b/packages/hardhat-graph-protocol/test/fixtures/files/addresses-global-short.json new file mode 100644 index 000000000..e69de29bb diff --git a/packages/hardhat-graph-protocol/test/fixtures/files/addresses-network-short.json b/packages/hardhat-graph-protocol/test/fixtures/files/addresses-network-short.json new file mode 100644 index 000000000..e69de29bb diff --git a/packages/hardhat-graph-protocol/test/fixtures/global-address-book/hardhat.config.ts b/packages/hardhat-graph-protocol/test/fixtures/global-address-book/hardhat.config.ts index b7224dfa4..1f7718d54 100644 --- a/packages/hardhat-graph-protocol/test/fixtures/global-address-book/hardhat.config.ts +++ b/packages/hardhat-graph-protocol/test/fixtures/global-address-book/hardhat.config.ts @@ -41,7 +41,11 @@ const config: HardhatUserConfig = { }, }, graph: { - addressBook: 'addresses-global.json', + deployments: { + horizon: { + addressBook: 'addresses-global.json', + }, + }, }, } diff --git a/packages/hardhat-graph-protocol/test/fixtures/invalid-address-book/hardhat.config.ts b/packages/hardhat-graph-protocol/test/fixtures/invalid-address-book/hardhat.config.ts index 995dacc17..e4569c04a 100644 --- a/packages/hardhat-graph-protocol/test/fixtures/invalid-address-book/hardhat.config.ts +++ b/packages/hardhat-graph-protocol/test/fixtures/invalid-address-book/hardhat.config.ts @@ -41,7 +41,11 @@ const config: HardhatUserConfig = { }, }, graph: { - addressBook: 'addresses-invalid.json', + deployments: { + horizon: { + addressBook: 'addresses-invalid.json', + }, + }, }, } diff --git a/packages/hardhat-graph-protocol/test/fixtures/network-address-book/hardhat.config.ts b/packages/hardhat-graph-protocol/test/fixtures/network-address-book/hardhat.config.ts index 51fd2341c..c75c71e4e 100644 --- a/packages/hardhat-graph-protocol/test/fixtures/network-address-book/hardhat.config.ts +++ b/packages/hardhat-graph-protocol/test/fixtures/network-address-book/hardhat.config.ts @@ -18,7 +18,11 @@ const config: HardhatUserConfig = { 'mainnet': { chainId: 1, url: `https://mainnet.infura.io/v3/123456`, - addressBook: 'addresses-network.json', + deployments: { + horizon: { + addressBook: 'addresses-network.json', + }, + }, }, 'arbitrum-one': { chainId: 42161, @@ -42,7 +46,11 @@ const config: HardhatUserConfig = { }, }, graph: { - addressBook: 'addresses-global.json', + deployments: { + horizon: { + addressBook: 'addresses-global.json', + }, + }, }, } diff --git a/packages/hardhat-graph-protocol/test/fixtures/no-path-config/hardhat.config.ts b/packages/hardhat-graph-protocol/test/fixtures/no-path-config/hardhat.config.ts index 0329f449e..c9199325a 100644 --- a/packages/hardhat-graph-protocol/test/fixtures/no-path-config/hardhat.config.ts +++ b/packages/hardhat-graph-protocol/test/fixtures/no-path-config/hardhat.config.ts @@ -38,7 +38,11 @@ const config: HardhatUserConfig = { }, }, graph: { - addressBook: 'addresses.json', + deployments: { + horizon: { + addressBook: 'addresses-hre.json', + }, + }, }, } diff --git a/packages/hardhat-graph-protocol/test/fixtures/path-config/hardhat.config.ts b/packages/hardhat-graph-protocol/test/fixtures/path-config/hardhat.config.ts index c8a4b55fc..836815e33 100644 --- a/packages/hardhat-graph-protocol/test/fixtures/path-config/hardhat.config.ts +++ b/packages/hardhat-graph-protocol/test/fixtures/path-config/hardhat.config.ts @@ -41,7 +41,11 @@ const config: HardhatUserConfig = { }, }, graph: { - addressBook: 'addresses-hre.json', + deployments: { + horizon: { + addressBook: 'addresses-hre.json', + }, + }, }, } diff --git a/packages/hardhat-graph-protocol/test/gre.test.ts b/packages/hardhat-graph-protocol/test/gre.test.ts index 4331ed1c4..d66a3fe75 100644 --- a/packages/hardhat-graph-protocol/test/gre.test.ts +++ b/packages/hardhat-graph-protocol/test/gre.test.ts @@ -1,57 +1,52 @@ import path from 'path' import { assert, expect } from 'chai' -import { useEnvironment } from './helpers' +import { loadHardhatContext, useHardhatProject } from './helpers' +import { GraphHorizonAddressBook } from '../src/sdk/deployments/horizon' describe('GRE usage', function () { describe('Project not using GRE', function () { - useEnvironment('default-config', 'mainnet') + useHardhatProject('default-config', 'mainnet') it('should throw when accessing hre.graph()', function () { expect(() => this.hre.graph()).to.throw() }) }) - describe(`Project using GRE: path-config`, function () { - useEnvironment('path-config', 'mainnet') - + describe(`Project using GRE - graph path`, function () { it('should add the graph path to the config', function () { + this.hre = loadHardhatContext('no-path-config', 'mainnet') assert.equal( this.hre.config.paths.graph, - path.join(__dirname, 'fixtures/files'), + path.join(__dirname, 'fixtures/no-path-config'), ) }) - }) - describe(`Project using GRE: no-path-config`, function () { - useEnvironment('no-path-config', 'mainnet') - - it('should add the graph path to the config', function () { + it('should add the graph path to the config from custom path', function () { + this.hre = loadHardhatContext('path-config', 'mainnet') assert.equal( this.hre.config.paths.graph, - path.join(__dirname, 'fixtures/no-path-config'), + path.join(__dirname, 'fixtures/files'), ) }) }) - describe(`Project using GRE: global-address-book`, function () { - useEnvironment('global-address-book', 'mainnet') + describe(`Project using GRE - deployments`, function () { + useHardhatProject('path-config', 'mainnet') - it('should use the global address book', function () { - assert.equal( - this.hre.graph().addressBook.file, - path.join(__dirname, 'fixtures/files/addresses-global.json'), - ) - }) - }) + it('should load Horizon deployment', function () { + const graph = this.hre.graph() + assert.isDefined(graph.horizon) + assert.isObject(graph.horizon) - describe(`Project using GRE: network-address-book`, function () { - useEnvironment('network-address-book', 'mainnet') + assert.isDefined(graph.horizon.contracts) + assert.isObject(graph.horizon.contracts) - it('should use the network address book', function () { - assert.equal( - this.hre.graph().addressBook.file, - path.join(__dirname, 'fixtures/files/addresses-network.json'), + assert.isDefined(graph.horizon.addressBook) + assert.isObject(graph.horizon.addressBook) + assert.instanceOf( + graph.horizon.addressBook, + GraphHorizonAddressBook, ) }) }) diff --git a/packages/hardhat-graph-protocol/test/helpers.ts b/packages/hardhat-graph-protocol/test/helpers.ts index 561e59c0e..1c7050e05 100644 --- a/packages/hardhat-graph-protocol/test/helpers.ts +++ b/packages/hardhat-graph-protocol/test/helpers.ts @@ -1,6 +1,6 @@ import path from 'path' -import { resetHardhatContext } from 'hardhat/plugins-testing' +import { resetHardhatContext as _resetHardhatContext } from 'hardhat/plugins-testing' import type { HardhatRuntimeEnvironment } from 'hardhat/types' declare module 'mocha' { @@ -9,18 +9,30 @@ declare module 'mocha' { } } -export function useEnvironment(fixtureProjectName: string, network?: string): void { +export function useHardhatProject(fixtureProjectName: string, network?: string): void { beforeEach('Loading hardhat environment', function () { - process.chdir(path.join(__dirname, 'fixtures', fixtureProjectName)) - - if (network !== undefined) { - process.env.HARDHAT_NETWORK = network - } - this.hre = require('hardhat') + this.hre = loadHardhatContext(fixtureProjectName, network) }) afterEach('Resetting hardhat', function () { resetHardhatContext() - delete process.env.HARDHAT_NETWORK }) } + +export function loadHardhatContext(fixtureProjectName: string, network?: string): HardhatRuntimeEnvironment { + resetHardhatContext() + delete process.env.HARDHAT_NETWORK + + process.chdir(path.join(__dirname, 'fixtures', fixtureProjectName)) + + if (network !== undefined) { + process.env.HARDHAT_NETWORK = network + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return require('hardhat') +} + +export function resetHardhatContext(): void { + _resetHardhatContext() + delete process.env.HARDHAT_NETWORK +} diff --git a/packages/hardhat-graph-protocol/tsconfig.json b/packages/hardhat-graph-protocol/tsconfig.json index 9ddee2404..18d89b091 100644 --- a/packages/hardhat-graph-protocol/tsconfig.json +++ b/packages/hardhat-graph-protocol/tsconfig.json @@ -11,5 +11,5 @@ "resolveJsonModule": true, "outDir": "dist", }, - "include": ["eslint.config.js", "src/**/*.ts"] + "include": ["eslint.config.js", "src/**/*.ts", "test/**/*.ts"], } diff --git a/packages/horizon/hardhat.config.ts b/packages/horizon/hardhat.config.ts index a1a177afd..dca9b466a 100644 --- a/packages/horizon/hardhat.config.ts +++ b/packages/horizon/hardhat.config.ts @@ -40,8 +40,10 @@ const config: HardhatUserConfig = { }, }, graph: { - addressBooks: { - horizon: 'addresses.json', + deployments: { + horizon: { + addressBook: 'addresses.json', + }, }, }, etherscan: {