Skip to content

Commit cdf3fb4

Browse files
committed
feat: add subgraph service to gre
Signed-off-by: Tomás Migone <[email protected]>
1 parent a475c4a commit cdf3fb4

File tree

15 files changed

+266
-1243
lines changed

15 files changed

+266
-1243
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# hardhat-graph-protocol
2+
3+
4+
## Usage
5+
6+
Install with yarn
7+
8+
```bash
9+
yarn add --dev hardhat-graph-protocol
10+
11+
# From the monorepo
12+
yarn add --dev hardhat-graph-protocol@workspace:^x.y.z
13+
```
14+
15+
And add it to your `hardhat.config.ts`:
16+
17+
```ts
18+
import "hardhat-graph-protocol";
19+
20+
export default {
21+
...
22+
graph: {
23+
deployments: {
24+
horizon: require.resolve('@graphprotocol/horizon/addresses.json'),
25+
subgraphService: require.resolve('@graphprotocol/subgraph-service/addresses.json'),
26+
}
27+
},
28+
...
29+
};
30+
```
31+
32+
_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.

packages/hardhat-graph-protocol/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dependencies": {
3131
"@graphprotocol/contracts": "workspace:^7.0.0",
3232
"@graphprotocol/horizon": "workspace:^0.0.1",
33+
"@graphprotocol/subgraph-service": "workspace:^0.0.1",
3334
"@nomicfoundation/hardhat-ethers": "^3.0.8",
3435
"debug": "^4.3.7"
3536
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import type { GraphHorizonAddressBook, GraphHorizonContracts } from './sdk/deployments/horizon'
2+
import type { SubgraphServiceAddressBook, SubgraphServiceContracts } from './sdk/deployments/subgraph-service'
23

34
// List of supported Graph deployments
45
export const GraphDeploymentsList = [
56
'horizon',
7+
'subgraphService',
68
] as const
79

810
export type GraphDeploymentRuntimeEnvironmentMap = {
911
horizon: {
1012
contracts: GraphHorizonContracts
1113
addressBook: GraphHorizonAddressBook
1214
}
15+
subgraphService: {
16+
contracts: SubgraphServiceContracts
17+
addressBook: SubgraphServiceAddressBook
18+
}
1319
}

packages/hardhat-graph-protocol/src/gre.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import path from 'path'
22

33
import { getAddressBookPath } from './config'
4-
import { GraphHorizonAddressBook } from './sdk/deployments/horizon'
54
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
65
import { logDebug } from './logger'
76

7+
import { GraphHorizonAddressBook } from './sdk/deployments/horizon'
8+
import { SubgraphServiceAddressBook } from './sdk/deployments/subgraph-service'
9+
810
import { assertGraphRuntimeEnvironment, type GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types'
911
import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types'
1012

@@ -58,6 +60,13 @@ export const greExtendEnvironment = (hre: HardhatRuntimeEnvironment) => {
5860
contracts: addressBook.loadContracts(provider),
5961
}
6062
break
63+
case 'subgraphService':
64+
addressBook = new SubgraphServiceAddressBook(addressBookPath, chainId)
65+
greDeployments.subgraphService = {
66+
addressBook: addressBook,
67+
contracts: addressBook.loadContracts(provider),
68+
}
69+
break
6170
default:
6271
break
6372
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@ import { GraphHorizonAddressBook } from './address-book'
22

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

5-
export {
6-
GraphHorizonAddressBook,
7-
}
8-
5+
export { GraphHorizonAddressBook }
96
export type { GraphHorizonContractName, GraphHorizonContracts }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { logDebug, logError } from '../../../logger'
2+
import { Provider, Signer } from 'ethers'
3+
import { SubgraphServiceArtifactsMap, SubgraphServiceContractNameList } from './contracts'
4+
import { AddressBook } from '../../address-book'
5+
import { assertObject } from '../../utils/assertion'
6+
7+
import type { SubgraphServiceContractName, SubgraphServiceContracts } from './contracts'
8+
9+
export class SubgraphServiceAddressBook extends AddressBook<number, SubgraphServiceContractName> {
10+
isContractName(name: unknown): name is SubgraphServiceContractName {
11+
return (
12+
typeof name === 'string'
13+
&& SubgraphServiceContractNameList.includes(name as SubgraphServiceContractName)
14+
)
15+
}
16+
17+
loadContracts(
18+
signerOrProvider?: Signer | Provider,
19+
): SubgraphServiceContracts {
20+
logDebug('Loading Subgraph Service contracts...')
21+
22+
const contracts = this._loadContracts(
23+
SubgraphServiceArtifactsMap,
24+
signerOrProvider,
25+
)
26+
this._assertSubgraphServiceContracts(contracts)
27+
28+
// Aliases
29+
contracts.Curation = contracts.L2Curation
30+
contracts.GNS = contracts.L2GNS
31+
32+
return contracts
33+
}
34+
35+
_assertSubgraphServiceContracts(
36+
contracts: unknown,
37+
): asserts contracts is SubgraphServiceContracts {
38+
assertObject(contracts)
39+
40+
// Assert that all SubgraphServiceContracts were loaded
41+
for (const contractName of SubgraphServiceContractNameList) {
42+
if (!contracts[contractName]) {
43+
const errMessage = `Missing SubgraphService contract: ${contractName}`
44+
logError(errMessage)
45+
}
46+
}
47+
}
48+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import path from 'path'
2+
3+
import type {
4+
L2Curation,
5+
L2GNS,
6+
ServiceRegistry,
7+
SubgraphNFT,
8+
} from '@graphprotocol/contracts'
9+
10+
import type {
11+
DisputeManager,
12+
SubgraphService,
13+
} from '@graphprotocol/subgraph-service'
14+
import type { ContractList } from '../../lib/contract'
15+
16+
export const SubgraphServiceContractNameList = [
17+
// @graphprotocol/contracts
18+
'L2Curation',
19+
'L2GNS',
20+
'SubgraphNFT',
21+
'ServiceRegistry',
22+
23+
// @graphprotocol/subgraph-service
24+
'SubgraphService',
25+
'DisputeManager',
26+
] as const
27+
28+
const root = path.resolve(__dirname, '../../../../..') // hardhat-graph-protocol root
29+
export const CONTRACTS_ARTIFACTS_PATH = path.resolve(root, 'node_modules', '@graphprotocol/contracts/build/contracts')
30+
export const SUBGRAPH_SERVICE_ARTIFACTS_PATH = path.resolve(root, 'node_modules', '@graphprotocol/subgraph-service/build/contracts')
31+
32+
export const SubgraphServiceArtifactsMap = {
33+
// @graphprotocol/contracts
34+
L2Curation: CONTRACTS_ARTIFACTS_PATH,
35+
L2GNS: CONTRACTS_ARTIFACTS_PATH,
36+
SubgraphNFT: CONTRACTS_ARTIFACTS_PATH,
37+
ServiceRegistry: CONTRACTS_ARTIFACTS_PATH,
38+
39+
// @graphprotocol/subgraph-service
40+
SubgraphService: SUBGRAPH_SERVICE_ARTIFACTS_PATH,
41+
DisputeManager: SUBGRAPH_SERVICE_ARTIFACTS_PATH,
42+
} as const
43+
44+
export interface SubgraphServiceContracts extends ContractList<SubgraphServiceContractName> {
45+
// @graphprotocol/contracts
46+
L2Curation: L2Curation
47+
L2GNS: L2GNS
48+
SubgraphNFT: SubgraphNFT
49+
ServiceRegistry: ServiceRegistry
50+
51+
// @graphprotocol/subgraph-service
52+
SubgraphService: SubgraphService
53+
DisputeManager: DisputeManager
54+
55+
// Aliases
56+
Curation: L2Curation
57+
GNS: L2GNS
58+
}
59+
60+
export type SubgraphServiceContractName = (typeof SubgraphServiceContractNameList)[number]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { SubgraphServiceAddressBook } from './address-book'
2+
3+
import type { SubgraphServiceContractName, SubgraphServiceContracts } from './contracts'
4+
5+
export { SubgraphServiceAddressBook }
6+
export type { SubgraphServiceContractName, SubgraphServiceContracts }

0 commit comments

Comments
 (0)