-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1071 from graphprotocol/horizon-gre
- Loading branch information
Showing
56 changed files
with
2,511 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": "ts-node/register/files", | ||
"ignore": ["test/fixtures/**/*"], | ||
"timeout": 6000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# hardhat-graph-protocol | ||
|
||
|
||
## Usage | ||
|
||
Install with yarn | ||
|
||
```bash | ||
yarn add --dev hardhat-graph-protocol | ||
|
||
# From the monorepo | ||
yarn add --dev hardhat-graph-protocol@workspace:^x.y.z | ||
``` | ||
|
||
And add it to your `hardhat.config.ts`: | ||
|
||
```ts | ||
import "hardhat-graph-protocol"; | ||
|
||
export default { | ||
... | ||
graph: { | ||
deployments: { | ||
horizon: require.resolve('@graphprotocol/horizon/addresses.json'), | ||
subgraphService: require.resolve('@graphprotocol/subgraph-service/addresses.json'), | ||
} | ||
}, | ||
... | ||
}; | ||
``` | ||
|
||
_Note_: When using the plugin from within this monorepo TypeScript fails to properly apply the type extension typings. This is a known issue and can be worked around by adding a `types/hardhat-graph-protocol.d.ts` file with the same content as the `type-extensions.ts` file in this repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const config = require('eslint-graph-config') | ||
|
||
module.exports = [ | ||
...config.default, | ||
{ | ||
rules: { | ||
'@typescript-eslint/no-unsafe-assignment': 'off', | ||
'@typescript-eslint/no-var-requires': 'off', | ||
'@typescript-eslint/no-unsafe-call': 'off', | ||
'@typescript-eslint/no-unsafe-member-access': 'off', | ||
'@typescript-eslint/no-unsafe-argument': 'off', | ||
}, | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "hardhat-graph-protocol", | ||
"version": "0.0.1", | ||
"description": "A hardhat plugin that extends the runtime environment to inject additional functionality related to the usage of the Graph Protocol.", | ||
"keywords": [ | ||
"ethereum", | ||
"smart-contracts", | ||
"hardhat", | ||
"hardhat-plugin", | ||
"graph", | ||
"graph-protocol", | ||
"horizon" | ||
], | ||
"author": "Tomás Migone <[email protected]>", | ||
"license": "MIT", | ||
"main": "dist/src/index.js", | ||
"types": "dist/src/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"clean": "rm -rf dist", | ||
"lint": "eslint '**/*.{js,ts}' --fix", | ||
"test": "mocha --exit --recursive 'test/**/*.test.ts'", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"files": [ | ||
"dist/", | ||
"src/", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"dependencies": { | ||
"@graphprotocol/contracts": "workspace:^7.0.0", | ||
"@graphprotocol/horizon": "workspace:^0.0.1", | ||
"@graphprotocol/subgraph-service": "workspace:^0.0.1", | ||
"@nomicfoundation/hardhat-ethers": "^3.0.8", | ||
"debug": "^4.3.7" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.0.0", | ||
"@types/debug": "^4.1.12", | ||
"@types/mocha": "^10.0.9", | ||
"chai": "^4.0.0", | ||
"eslint": "^8.56.0", | ||
"eslint-graph-config": "workspace:^0.0.1", | ||
"ethers": "^6.13.4", | ||
"hardhat": "^2.22.16", | ||
"mocha": "^10.8.2", | ||
"ts-node": "^8.0.0", | ||
"typescript": "^5.6.3" | ||
}, | ||
"peerDependencies": { | ||
"ethers": "^6.13.4", | ||
"hardhat": "^2.22.16" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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 { HardhatRuntimeEnvironment } from 'hardhat/types' | ||
|
||
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(`Getting address book path...`) | ||
logDebug(`Graph base dir: ${hre.config.paths.graph}`) | ||
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!') | ||
} | ||
|
||
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: ${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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { GraphHorizonAddressBook, GraphHorizonContracts } from './sdk/deployments/horizon' | ||
import type { SubgraphServiceAddressBook, SubgraphServiceContracts } from './sdk/deployments/subgraph-service' | ||
|
||
// List of supported Graph deployments | ||
export const GraphDeploymentsList = [ | ||
'horizon', | ||
'subgraphService', | ||
] as const | ||
|
||
export type GraphDeploymentRuntimeEnvironmentMap = { | ||
horizon: { | ||
contracts: GraphHorizonContracts | ||
addressBook: GraphHorizonAddressBook | ||
} | ||
subgraphService: { | ||
contracts: SubgraphServiceContracts | ||
addressBook: SubgraphServiceAddressBook | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import path from 'path' | ||
|
||
import { getAddressBookPath } from './config' | ||
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' | ||
import { logDebug } from './logger' | ||
|
||
import { GraphHorizonAddressBook } from './sdk/deployments/horizon' | ||
import { SubgraphServiceAddressBook } from './sdk/deployments/subgraph-service' | ||
|
||
import { assertGraphRuntimeEnvironment, type GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types' | ||
import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types' | ||
|
||
export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => { | ||
const userPath = userConfig.paths?.graph | ||
|
||
let newPath: string | ||
if (userPath === undefined) { | ||
newPath = config.paths.root | ||
} else { | ||
if (path.isAbsolute(userPath)) { | ||
newPath = userPath | ||
} else { | ||
newPath = path.normalize(path.join(config.paths.root, userPath)) | ||
} | ||
} | ||
|
||
config.paths.graph = newPath | ||
} | ||
|
||
export const greExtendEnvironment = (hre: HardhatRuntimeEnvironment) => { | ||
hre.graph = (opts: GraphRuntimeEnvironmentOptions = { deployments: {} }) => { | ||
logDebug('*** Initializing Graph Runtime Environment (GRE) ***') | ||
logDebug(`Main network: ${hre.network.name}`) | ||
const chainId = hre.network.config.chainId | ||
if (chainId === undefined) { | ||
throw new Error('Please define chainId in your Hardhat network configuration') | ||
} | ||
logDebug(`Chain Id: ${chainId}`) | ||
|
||
const deployments = [ | ||
...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 provider = new HardhatEthersProvider(hre.network.provider, hre.network.name) | ||
const greDeployments: Record<string, unknown> = {} | ||
for (const deployment of deployments) { | ||
logDebug(`== Initializing deployment: ${deployment} ==`) | ||
const addressBookPath = getAddressBookPath(deployment, hre, opts) | ||
let addressBook | ||
|
||
switch (deployment) { | ||
case 'horizon': | ||
addressBook = new GraphHorizonAddressBook(addressBookPath, chainId) | ||
greDeployments.horizon = { | ||
addressBook: addressBook, | ||
contracts: addressBook.loadContracts(provider), | ||
} | ||
break | ||
case 'subgraphService': | ||
addressBook = new SubgraphServiceAddressBook(addressBookPath, chainId) | ||
greDeployments.subgraphService = { | ||
addressBook: addressBook, | ||
contracts: addressBook.loadContracts(provider), | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
|
||
const gre = { | ||
...greDeployments, | ||
provider, | ||
chainId, | ||
} | ||
assertGraphRuntimeEnvironment(gre) | ||
logDebug('GRE initialized successfully!') | ||
return gre | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { extendConfig, extendEnvironment } from 'hardhat/config' | ||
import { greExtendConfig, greExtendEnvironment } from './gre' | ||
|
||
// This import is needed to let the TypeScript compiler know that it should include your type | ||
// extensions in your npm package's types file. | ||
import './type-extensions' | ||
|
||
// ** Graph Runtime Environment (GRE) extensions for the HRE ** | ||
extendConfig(greExtendConfig) | ||
extendEnvironment(greExtendEnvironment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import debug from 'debug' | ||
|
||
const LOG_BASE = 'hardhat:graph' | ||
|
||
export const logDebug = debug(`${LOG_BASE}:debug`) | ||
export const logWarn = debug(`${LOG_BASE}:warn`) | ||
export const logError = debug(`${LOG_BASE}:error`) |
Oops, something went wrong.