-
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.
feat: add gre/sdk with support for horizon
Signed-off-by: Tomás Migone <[email protected]>
- Loading branch information
Showing
39 changed files
with
3,066 additions
and
18 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
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,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,52 @@ | ||
{ | ||
"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/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"lint": "eslint '**/*.{js,ts}' --fix", | ||
"test": "mocha --exit --recursive 'test/**/*.test.ts'" | ||
}, | ||
"files": [ | ||
"dist/", | ||
"src/", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"dependencies": { | ||
"@graphprotocol/contracts": "workspace:^7.0.0", | ||
"@graphprotocol/horizon": "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,35 @@ | ||
import fs from 'fs' | ||
import { GraphPluginError } from './sdk/utils/error' | ||
import { logDebug } from './logger' | ||
|
||
import type { GraphDeployment, GraphRuntimeEnvironmentOptions } from './types' | ||
import type { HardhatRuntimeEnvironment } from 'hardhat/types' | ||
import { normalizePath } from './sdk/utils/path' | ||
|
||
export function getAddressBookPath( | ||
deployment: GraphDeployment, | ||
hre: HardhatRuntimeEnvironment, | ||
opts: GraphRuntimeEnvironmentOptions, | ||
): string { | ||
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] | ||
|
||
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}`) | ||
} | ||
|
||
logDebug(`Address book path found: ${addressBookPath}`) | ||
return addressBookPath | ||
} |
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,138 @@ | ||
import path from 'path' | ||
|
||
import { GraphDeploymentsList, GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types' | ||
import { logDebug, logWarn } from './logger' | ||
import { getAddressBookPath } from './config' | ||
import { GraphHorizonAddressBook } from './sdk/deployments/horizon' | ||
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' | ||
|
||
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 = {}) => { | ||
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 ?? {}), | ||
] | ||
logDebug(`Detected deployments: ${deployments.join(', ')}`) | ||
|
||
// Build the Graph Runtime Environment (GRE) for each deployment | ||
const gre = {} as GraphRuntimeEnvironment | ||
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 = { | ||
addressBook: addressBook, | ||
contracts: addressBook.loadContracts(hre.network.config.chainId!, provider), | ||
} | ||
break | ||
|
||
default: | ||
break | ||
} | ||
} | ||
|
||
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<Wallet[]>, | ||
// getWallet: (address: string) => Promise<Wallet>, | ||
// unlockProvider: (caller: string) => Promise<EthersProviderWrapper>, | ||
// ): 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')), | ||
// ), | ||
// } | ||
// } |
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.