Skip to content

Commit

Permalink
feat: add gre/sdk with support for horizon
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Nov 20, 2024
1 parent 9ae3410 commit 13ea6ca
Show file tree
Hide file tree
Showing 39 changed files with 3,066 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"packages/contracts",
"packages/data-edge",
"packages/eslint-graph-config",
"packages/hardhat-graph-protocol",
"packages/horizon",
"packages/sdk",
"packages/solhint-graph-config",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-graph-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ This repository contains shared linting and formatting rules for TypeScript proj
## Installation

```bash
yarn add --dev eslint eslint-graph-config
yarn add --dev eslint@^8.56.0 eslint-graph-config
```

For projects on this monorepo, you can use the following command to install the package:

```bash
yarn add --dev eslint eslint-graph-config@workspace:^x.y.z
yarn add --dev eslint@^8.56.0 eslint-graph-config@workspace:^x.y.z
```

To enable the rules, you need to create an `eslint.config.js` file in the root of your project with the following content:
Expand Down
5 changes: 5 additions & 0 deletions packages/hardhat-graph-protocol/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": "ts-node/register/files",
"ignore": ["test/fixtures/**/*"],
"timeout": 6000
}
14 changes: 14 additions & 0 deletions packages/hardhat-graph-protocol/eslint.config.js
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',
},
},
]
52 changes: 52 additions & 0 deletions packages/hardhat-graph-protocol/package.json
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"
}
}
35 changes: 35 additions & 0 deletions packages/hardhat-graph-protocol/src/config.ts
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
}
138 changes: 138 additions & 0 deletions packages/hardhat-graph-protocol/src/gre.ts
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')),
// ),
// }
// }
10 changes: 10 additions & 0 deletions packages/hardhat-graph-protocol/src/index.ts
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)
7 changes: 7 additions & 0 deletions packages/hardhat-graph-protocol/src/logger.ts
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`)
Loading

0 comments on commit 13ea6ca

Please sign in to comment.