Skip to content

Commit 4111600

Browse files
committed
feat(gre): working version for horizon with legacy contracts
Signed-off-by: Tomás Migone <[email protected]>
1 parent 13ea6ca commit 4111600

File tree

20 files changed

+258
-209
lines changed

20 files changed

+258
-209
lines changed
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
11
import fs from 'fs'
2+
23
import { GraphPluginError } from './sdk/utils/error'
34
import { logDebug } from './logger'
5+
import { normalizePath } from './sdk/utils/path'
46

5-
import type { GraphDeployment, GraphRuntimeEnvironmentOptions } from './types'
7+
import type { GraphDeployment } from './deployments'
8+
import type { GraphRuntimeEnvironmentOptions } from './type-extensions'
69
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
7-
import { normalizePath } from './sdk/utils/path'
810

911
export function getAddressBookPath(
1012
deployment: GraphDeployment,
1113
hre: HardhatRuntimeEnvironment,
1214
opts: GraphRuntimeEnvironmentOptions,
1315
): string {
16+
const optsPath = getPath(opts.deployments?.[deployment])
17+
const networkPath = getPath(hre.network.config.deployments?.[deployment])
18+
const globalPath = getPath(hre.config.graph?.deployments?.[deployment])
19+
1420
logDebug(`== ${deployment} - Getting address book path`)
1521
logDebug(`Graph base dir: ${hre.config.paths.graph}`)
16-
logDebug(`1) opts.addressBooks.[deployment]: ${opts.addressBooks?.[deployment]}`)
17-
logDebug(`2) hre.network.config.addressBooks.[deployment]: ${hre.network.config?.addressBooks?.[deployment]}`)
18-
logDebug(`3) hre.config.graph.addressBooks.[deployment]: ${hre.config.graph?.addressBooks?.[deployment]}`)
19-
20-
let addressBookPath
21-
= opts.addressBooks?.[deployment] ?? hre.network.config?.addressBooks?.[deployment] ?? hre.config.graph?.addressBooks?.[deployment]
22+
logDebug(`1) opts: ${optsPath}`)
23+
logDebug(`2) network: ${networkPath}`)
24+
logDebug(`3) global: ${globalPath}`)
2225

26+
const addressBookPath = optsPath ?? networkPath ?? globalPath
2327
if (addressBookPath === undefined) {
2428
throw new GraphPluginError('Must set a an addressBook path!')
2529
}
2630

27-
addressBookPath = normalizePath(addressBookPath, hre.config.paths.graph)
28-
29-
if (!fs.existsSync(addressBookPath)) {
30-
throw new GraphPluginError(`Address book not found: ${addressBookPath}`)
31+
const normalizedAddressBookPath = normalizePath(addressBookPath, hre.config.paths.graph)
32+
if (!fs.existsSync(normalizedAddressBookPath)) {
33+
throw new GraphPluginError(`Address book not found: ${normalizedAddressBookPath}`)
3134
}
3235

33-
logDebug(`Address book path found: ${addressBookPath}`)
34-
return addressBookPath
36+
logDebug(`Address book path found: ${normalizedAddressBookPath}`)
37+
return normalizedAddressBookPath
38+
}
39+
40+
function getPath(value: string | {
41+
addressBook: string
42+
} | undefined): string | undefined {
43+
if (typeof value === 'string') {
44+
return value
45+
} else if (value && typeof value == 'object') {
46+
return value.addressBook
47+
}
48+
return
3549
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { GraphHorizonAddressBook, GraphHorizonContracts } from './sdk/deployments/horizon'
2+
3+
// List of supported Graph deployments
4+
const GraphDeploymentsList = [
5+
'horizon',
6+
] as const
7+
8+
export type GraphDeployment = (typeof GraphDeploymentsList)[number]
9+
10+
export type GraphDeploymentRuntimeEnvironmentMap = {
11+
horizon: {
12+
contracts: GraphHorizonContracts
13+
addressBook: GraphHorizonAddressBook
14+
}
15+
}
16+
17+
export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment {
18+
return (
19+
typeof deployment === 'string'
20+
&& GraphDeploymentsList.includes(deployment as GraphDeployment)
21+
)
22+
}
Lines changed: 19 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import path from 'path'
22

3-
import { GraphDeploymentsList, GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types'
4-
import { logDebug, logWarn } from './logger'
3+
import { assertGraphRuntimeEnvironment } from './type-extensions'
54
import { getAddressBookPath } from './config'
65
import { GraphHorizonAddressBook } from './sdk/deployments/horizon'
76
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
7+
import { isGraphDeployment } from './deployments'
8+
import { logDebug } from './logger'
89

910
import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types'
11+
import type { GraphRuntimeEnvironmentOptions } from './type-extensions'
1012

1113
export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
1214
const userPath = userConfig.paths?.graph
@@ -26,113 +28,45 @@ export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly<Hard
2628
}
2729

2830
export const greExtendEnvironment = (hre: HardhatRuntimeEnvironment) => {
29-
hre.graph = (opts: GraphRuntimeEnvironmentOptions = {}) => {
31+
hre.graph = (opts: GraphRuntimeEnvironmentOptions = { deployments: {} }) => {
3032
logDebug('*** Initializing Graph Runtime Environment (GRE) ***')
3133
logDebug(`Main network: ${hre.network.name}`)
3234

33-
const provider = new HardhatEthersProvider(hre.network.provider, hre.network.name)
3435
const deployments = [
35-
...Object.keys(opts.addressBooks ?? {}),
36-
...Object.keys(hre.network.config.addressBooks ?? {}),
37-
...Object.keys(hre.config.graph?.addressBooks ?? {}),
38-
]
36+
...Object.keys(opts.deployments ?? {}),
37+
...Object.keys(hre.network.config.deployments ?? {}),
38+
...Object.keys(hre.config.graph?.deployments ?? {}),
39+
].filter(v => isGraphDeployment(v))
3940
logDebug(`Detected deployments: ${deployments.join(', ')}`)
4041

4142
// Build the Graph Runtime Environment (GRE) for each deployment
42-
const gre = {} as GraphRuntimeEnvironment
43+
const provider = new HardhatEthersProvider(hre.network.provider, hre.network.name)
44+
const greDeployments: Record<string, unknown> = {}
4345
for (const deployment of deployments) {
44-
if (!isGraphDeployment(deployment)) {
45-
logWarn(`Invalid deployment: ${deployment}. Skipping...`)
46-
continue
47-
}
48-
4946
logDebug(`Initializing ${deployment} deployment...`)
5047
const addressBookPath = getAddressBookPath(deployment, hre, opts)
5148
let addressBook
49+
5250
switch (deployment) {
5351
case 'horizon':
5452
addressBook = new GraphHorizonAddressBook(addressBookPath, hre.network.config.chainId!)
55-
gre.horizon = {
53+
greDeployments.horizon = {
5654
addressBook: addressBook,
5755
contracts: addressBook.loadContracts(hre.network.config.chainId!, provider),
5856
}
5957
break
60-
6158
default:
6259
break
6360
}
6461
}
6562

63+
const gre = {
64+
...greDeployments,
65+
provider,
66+
chainId: async () => (await provider.getNetwork()).chainId,
67+
}
68+
assertGraphRuntimeEnvironment(gre)
6669
logDebug('GRE initialized successfully!')
6770
return gre
6871
}
6972
}
70-
71-
// function buildGraphNetworkEnvironment(
72-
// chainId: number,
73-
// provider: EthersProviderWrapper | undefined,
74-
// graphConfigPath: string | undefined,
75-
// addressBookPath: string,
76-
// isHHL1: boolean,
77-
// enableTxLogging: boolean,
78-
// secureAccounts: boolean,
79-
// fork: boolean,
80-
// getWallets: () => Promise<Wallet[]>,
81-
// getWallet: (address: string) => Promise<Wallet>,
82-
// unlockProvider: (caller: string) => Promise<EthersProviderWrapper>,
83-
// ): GraphNetworkEnvironment | null {
84-
// if (graphConfigPath === undefined) {
85-
// logWarn(
86-
// `No graph config file provided for chain: ${chainId}. ${
87-
// isHHL1 ? 'L2' : 'L1'
88-
// } will not be initialized.`,
89-
// )
90-
// return null
91-
// }
92-
93-
// if (provider === undefined) {
94-
// logWarn(
95-
// `No provider URL found for: ${chainId}. ${isHHL1 ? 'L2' : 'L1'} will not be initialized.`,
96-
// )
97-
// return null
98-
// }
99-
100-
// // Upgrade provider to secure accounts if feature is enabled
101-
// const getUpdatedProvider = async (caller: string) =>
102-
// secureAccounts ? await unlockProvider(caller) : provider
103-
104-
// return {
105-
// chainId: chainId,
106-
// provider: provider,
107-
// addressBook: lazyObject(() => new GraphNetworkAddressBook(addressBookPath, chainId)),
108-
// graphConfig: lazyObject(() => {
109-
// const config = readConfig(graphConfigPath, true)
110-
// config.defaults = getDefaults(config, isHHL1)
111-
// return config
112-
// }),
113-
// contracts: lazyObject(() =>
114-
// loadGraphNetworkContracts(addressBookPath, chainId, provider, undefined, {
115-
// enableTxLogging,
116-
// }),
117-
// ),
118-
// getWallets: lazyFunction(() => () => getWallets()),
119-
// getWallet: lazyFunction(() => (address: string) => getWallet(address)),
120-
// getDeployer: lazyFunction(
121-
// () => async () => getDeployer(await getUpdatedProvider('getDeployer')),
122-
// ),
123-
// getNamedAccounts: lazyFunction(
124-
// () => async () =>
125-
// getNamedAccounts(
126-
// fork ? provider : await getUpdatedProvider('getNamedAccounts'),
127-
// graphConfigPath,
128-
// ),
129-
// ),
130-
// getTestAccounts: lazyFunction(
131-
// () => async () =>
132-
// getTestAccounts(await getUpdatedProvider('getTestAccounts'), graphConfigPath),
133-
// ),
134-
// getAllAccounts: lazyFunction(
135-
// () => async () => getAllAccounts(await getUpdatedProvider('getAllAccounts')),
136-
// ),
137-
// }
138-
// }

packages/hardhat-graph-protocol/src/sdk/deployments/horizon/address-book.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class GraphHorizonAddressBook extends AddressBook<number, GraphHorizonCon
1717
chainId: number,
1818
signerOrProvider?: Signer | Provider,
1919
): GraphHorizonContracts {
20-
logDebug('Loading Graph Network contracts...')
20+
logDebug('Loading Graph Horizon contracts...')
2121
const artifactsPath = path.resolve('node_modules', '@graphprotocol/contracts/build/contracts')
2222

2323
const contracts = this._loadContracts(
@@ -26,6 +26,10 @@ export class GraphHorizonAddressBook extends AddressBook<number, GraphHorizonCon
2626
)
2727
assertGraphHorizonContracts(contracts, chainId)
2828

29+
// Aliases
30+
contracts.GraphToken = contracts.L2GraphToken
31+
contracts.GraphTokenGateway = contracts.L2GraphTokenGateway
32+
2933
// Iterator
3034
contracts[Symbol.iterator] = function* () {
3135
for (const key of Object.keys(this)) {
@@ -61,7 +65,7 @@ function assertGraphHorizonContracts(
6165
)
6266
}
6367

64-
// Assert that all GraphNetworkContracts were loaded
68+
// Assert that all GraphHorizonContracts were loaded
6569
for (const contractName of GraphHorizonContractNameList) {
6670
if (!contracts[contractName]) {
6771
const errMessage = `Missing GraphHorizon contract: ${contractName} for chainId ${chainId}`
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { GraphHorizonAddressBook } from './address-book'
22

3-
import type { GraphHorizonContractName, GraphHorizonContracts, GraphHorizonRuntimeEnvironment } from './types'
3+
import type { GraphHorizonContractName, GraphHorizonContracts } from './types'
44

55
export {
66
GraphHorizonAddressBook,
77
}
88

9-
export type { GraphHorizonContractName, GraphHorizonContracts, GraphHorizonRuntimeEnvironment }
9+
export type { GraphHorizonContractName, GraphHorizonContracts }

packages/hardhat-graph-protocol/src/sdk/deployments/horizon/types.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
} from '@graphprotocol/contracts'
99
import type { Contract } from 'ethers'
1010
import type { ContractList } from '../lib/contract'
11-
import type { GraphHorizonAddressBook } from './address-book'
1211

1312
export const GraphHorizonContractNameList = [
1413
'GraphProxyAdmin',
@@ -28,8 +27,8 @@ export interface GraphHorizonContracts extends ContractList<GraphHorizonContract
2827
RewardsManager: RewardsManager & Contract
2928
GraphProxyAdmin: GraphProxyAdmin & Contract
3029
Controller: Controller & Contract
31-
L2GraphToken?: L2GraphToken & Contract
32-
L2GraphTokenGateway?: L2GraphTokenGateway & Contract
30+
L2GraphToken: L2GraphToken & Contract
31+
L2GraphTokenGateway: L2GraphTokenGateway & Contract
3332

3433
// Aliases
3534
GraphToken: L2GraphToken
@@ -38,8 +37,3 @@ export interface GraphHorizonContracts extends ContractList<GraphHorizonContract
3837
// Iterator
3938
[Symbol.iterator]: () => Generator<Contract, void, void>
4039
}
41-
42-
export interface GraphHorizonRuntimeEnvironment {
43-
contracts: GraphHorizonContracts
44-
addressBook: GraphHorizonAddressBook
45-
}

packages/hardhat-graph-protocol/src/type-extensions.ts

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,48 @@
22
import 'hardhat/types/config'
33
import 'hardhat/types/runtime'
44

5-
import type { GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions } from './types'
5+
import type { GraphDeployment, GraphDeploymentRuntimeEnvironmentMap } from './deployments'
6+
import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
7+
8+
export type GraphRuntimeEnvironmentOptions = {
9+
deployments?: {
10+
[deployment in GraphDeployment]?: string | {
11+
addressBook: string
12+
}
13+
}
14+
}
15+
16+
export type GraphRuntimeEnvironment = {
17+
[deployment in keyof GraphDeploymentRuntimeEnvironmentMap]?: GraphDeploymentRuntimeEnvironmentMap[deployment]
18+
} & {
19+
provider: HardhatEthersProvider
20+
chainId: () => Promise<bigint>
21+
}
22+
23+
export function assertGraphRuntimeEnvironment(
24+
obj: unknown,
25+
): obj is GraphRuntimeEnvironment {
26+
if (typeof obj !== 'object' || obj === null) return false
27+
28+
const deployments = obj as Partial<GraphDeploymentRuntimeEnvironmentMap>
29+
30+
for (const deployment in deployments) {
31+
const environment = deployments[deployment as keyof GraphDeploymentRuntimeEnvironmentMap]
32+
if (!environment || typeof environment !== 'object') {
33+
return false
34+
}
35+
}
36+
37+
if (typeof (obj as GraphRuntimeEnvironment).provider !== 'object') {
38+
return false
39+
}
40+
41+
if (typeof (obj as GraphRuntimeEnvironment).chainId !== 'function') {
42+
return false
43+
}
44+
45+
return true
46+
}
647

748
declare module 'hardhat/types/runtime' {
849
export interface HardhatRuntimeEnvironment {
@@ -20,26 +61,34 @@ declare module 'hardhat/types/config' {
2061
}
2162

2263
export interface HardhatNetworkConfig {
23-
addressBooks?: {
24-
[deployment: string]: string
64+
deployments?: {
65+
[deployment in GraphDeployment]?: string | {
66+
addressBook: string
67+
}
2568
}
2669
}
2770

2871
export interface HardhatNetworkUserConfig {
29-
addressBooks?: {
30-
[deployment: string]: string
72+
deployments?: {
73+
[deployment in GraphDeployment]?: string | {
74+
addressBook: string
75+
}
3176
}
3277
}
3378

3479
export interface HttpNetworkConfig {
35-
addressBooks?: {
36-
[deployment: string]: string
80+
deployments?: {
81+
[deployment in GraphDeployment]?: string | {
82+
addressBook: string
83+
}
3784
}
3885
}
3986

4087
export interface HttpNetworkUserConfig {
41-
addressBooks?: {
42-
[deployment: string]: string
88+
deployments?: {
89+
[deployment in GraphDeployment]?: string | {
90+
addressBook: string
91+
}
4392
}
4493
}
4594

0 commit comments

Comments
 (0)