Skip to content

Commit

Permalink
feat: add subgraph service to gre
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 22, 2024
1 parent a475c4a commit cdf3fb4
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 1,243 deletions.
32 changes: 32 additions & 0 deletions packages/hardhat-graph-protocol/README.md
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.
1 change: 1 addition & 0 deletions packages/hardhat-graph-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"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"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/hardhat-graph-protocol/src/deployment-list.ts
Original file line number Diff line number Diff line change
@@ -1,13 +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
}
}
11 changes: 10 additions & 1 deletion packages/hardhat-graph-protocol/src/gre.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from 'path'

import { getAddressBookPath } from './config'
import { GraphHorizonAddressBook } from './sdk/deployments/horizon'
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'

Expand Down Expand Up @@ -58,6 +60,13 @@ export const greExtendEnvironment = (hre: HardhatRuntimeEnvironment) => {
contracts: addressBook.loadContracts(provider),
}
break
case 'subgraphService':
addressBook = new SubgraphServiceAddressBook(addressBookPath, chainId)
greDeployments.subgraphService = {
addressBook: addressBook,
contracts: addressBook.loadContracts(provider),
}
break
default:
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import { GraphHorizonAddressBook } from './address-book'

import type { GraphHorizonContractName, GraphHorizonContracts } from './contracts'

export {
GraphHorizonAddressBook,
}

export { GraphHorizonAddressBook }
export type { GraphHorizonContractName, GraphHorizonContracts }
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { logDebug, logError } from '../../../logger'
import { Provider, Signer } from 'ethers'
import { SubgraphServiceArtifactsMap, SubgraphServiceContractNameList } from './contracts'
import { AddressBook } from '../../address-book'
import { assertObject } from '../../utils/assertion'

import type { SubgraphServiceContractName, SubgraphServiceContracts } from './contracts'

export class SubgraphServiceAddressBook extends AddressBook<number, SubgraphServiceContractName> {
isContractName(name: unknown): name is SubgraphServiceContractName {
return (
typeof name === 'string'
&& SubgraphServiceContractNameList.includes(name as SubgraphServiceContractName)
)
}

loadContracts(
signerOrProvider?: Signer | Provider,
): SubgraphServiceContracts {
logDebug('Loading Subgraph Service contracts...')

const contracts = this._loadContracts(
SubgraphServiceArtifactsMap,
signerOrProvider,
)
this._assertSubgraphServiceContracts(contracts)

// Aliases
contracts.Curation = contracts.L2Curation
contracts.GNS = contracts.L2GNS

return contracts
}

_assertSubgraphServiceContracts(
contracts: unknown,
): asserts contracts is SubgraphServiceContracts {
assertObject(contracts)

// Assert that all SubgraphServiceContracts were loaded
for (const contractName of SubgraphServiceContractNameList) {
if (!contracts[contractName]) {
const errMessage = `Missing SubgraphService contract: ${contractName}`
logError(errMessage)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import path from 'path'

import type {
L2Curation,
L2GNS,
ServiceRegistry,
SubgraphNFT,
} from '@graphprotocol/contracts'

import type {
DisputeManager,
SubgraphService,
} from '@graphprotocol/subgraph-service'
import type { ContractList } from '../../lib/contract'

export const SubgraphServiceContractNameList = [
// @graphprotocol/contracts
'L2Curation',
'L2GNS',
'SubgraphNFT',
'ServiceRegistry',

// @graphprotocol/subgraph-service
'SubgraphService',
'DisputeManager',
] as const

const root = path.resolve(__dirname, '../../../../..') // hardhat-graph-protocol root
export const CONTRACTS_ARTIFACTS_PATH = path.resolve(root, 'node_modules', '@graphprotocol/contracts/build/contracts')
export const SUBGRAPH_SERVICE_ARTIFACTS_PATH = path.resolve(root, 'node_modules', '@graphprotocol/subgraph-service/build/contracts')

export const SubgraphServiceArtifactsMap = {
// @graphprotocol/contracts
L2Curation: CONTRACTS_ARTIFACTS_PATH,
L2GNS: CONTRACTS_ARTIFACTS_PATH,
SubgraphNFT: CONTRACTS_ARTIFACTS_PATH,
ServiceRegistry: CONTRACTS_ARTIFACTS_PATH,

// @graphprotocol/subgraph-service
SubgraphService: SUBGRAPH_SERVICE_ARTIFACTS_PATH,
DisputeManager: SUBGRAPH_SERVICE_ARTIFACTS_PATH,
} as const

export interface SubgraphServiceContracts extends ContractList<SubgraphServiceContractName> {
// @graphprotocol/contracts
L2Curation: L2Curation
L2GNS: L2GNS
SubgraphNFT: SubgraphNFT
ServiceRegistry: ServiceRegistry

// @graphprotocol/subgraph-service
SubgraphService: SubgraphService
DisputeManager: DisputeManager

// Aliases
Curation: L2Curation
GNS: L2GNS
}

export type SubgraphServiceContractName = (typeof SubgraphServiceContractNameList)[number]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SubgraphServiceAddressBook } from './address-book'

import type { SubgraphServiceContractName, SubgraphServiceContracts } from './contracts'

export { SubgraphServiceAddressBook }
export type { SubgraphServiceContractName, SubgraphServiceContracts }
Loading

0 comments on commit cdf3fb4

Please sign in to comment.