Skip to content

Commit

Permalink
Merge branch 'horizon' into tmigone/post-trust-changes-payments
Browse files Browse the repository at this point in the history
  • Loading branch information
tmigone authored Jan 27, 2025
2 parents f056410 + c3c94ca commit 36d203f
Show file tree
Hide file tree
Showing 140 changed files with 2,710 additions and 36,969 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ tx-builder-*.json

# Hardhat Ignition
**/chain-31337/
**/chain-1377/
**/horizon-localhost/
**/horizon-hardhat/
!**/ignition/**/artifacts/
2 changes: 1 addition & 1 deletion packages/eslint-graph-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default [
'@typescript-eslint/no-inferrable-types': 'warn',
'@typescript-eslint/no-empty-function': 'warn',
'no-only-tests/no-only-tests': 'error',
'no-secrets/no-secrets': ['error', { tolerance: 4.1 }],
'no-secrets/no-secrets': ['error', { tolerance: 5.1 }],
'sort-imports': [
'warn', {
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
Expand Down
17 changes: 14 additions & 3 deletions packages/hardhat-graph-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@
],
"author": "Tomás Migone <[email protected]>",
"license": "MIT",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"main": "./dist/src/index.js",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"default": "./dist/src/index.js"
},
"./sdk": {
"types": "./src/sdk/index.ts",
"default": "./src/sdk/index.ts"
}
},
"types": "./dist/src/index.d.ts",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
Expand All @@ -33,7 +43,8 @@
"@graphprotocol/horizon": "workspace:^0.0.1",
"@graphprotocol/subgraph-service": "workspace:^0.0.1",
"@nomicfoundation/hardhat-ethers": "^3.0.8",
"debug": "^4.3.7"
"debug": "^4.3.7",
"json5": "^2.2.3"
},
"devDependencies": {
"@types/chai": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { logDebug, logError } from '../../../logger'
import { Provider, Signer } from 'ethers'
import { AddressBook } from '../../address-book'
import { assertObject } from '../../utils/assertion'
import { Contract } from 'ethers'
import { loadArtifact } from '../../lib/artifact'
import { mergeABIs } from '../../utils/abi'

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

Expand All @@ -23,6 +26,20 @@ export class GraphHorizonAddressBook extends AddressBook<number, GraphHorizonCon
GraphHorizonArtifactsMap,
signerOrProvider,
)

// Handle HorizonStaking specially to include extension functions
if (contracts.HorizonStaking) {
const stakingOverride = new Contract(
this.getEntry('HorizonStaking').address,
mergeABIs(
loadArtifact('HorizonStaking', GraphHorizonArtifactsMap.HorizonStaking).abi,
loadArtifact('HorizonStakingExtension', GraphHorizonArtifactsMap.HorizonStaking).abi,
),
signerOrProvider,
)
contracts.HorizonStaking = stakingOverride
}

this._assertGraphHorizonContracts(contracts)

// Aliases
Expand Down
71 changes: 71 additions & 0 deletions packages/hardhat-graph-protocol/src/sdk/ignition/ignition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
require('json5/lib/register')

import fs from 'fs'
import path from 'path'

export function loadConfig(configPath: string, prefix: string, networkName: string): any {
const configFileCandidates = [
path.join(require.main?.path ?? '', configPath, `${prefix}.${networkName}.json5`),
path.join(require.main?.path ?? '', configPath, `${prefix}.default.json5`),
]

const configFile = configFileCandidates.find(file => fs.existsSync(file))
if (!configFile) {
throw new Error(
`Config file not found. Tried:\n${configFileCandidates.map(f => `- ${f}`).join('\n')}`,
)
}

return removeNFromBigInts(require(configFile))
}

export function saveAddressBook(
contracts: any,
chainId: number | undefined,
addressBook = 'addresses.json',
): Record<string, Record<string, string>> {
if (!chainId) {
throw new Error('Chain ID is required')
}

// Use different address book for local networks - this one can be gitignored
if ([1377, 31337].includes(chainId)) {
addressBook = 'addresses-local.json'
}

const output = fs.existsSync(addressBook)
? JSON.parse(fs.readFileSync(addressBook, 'utf8'))
: {}

output[chainId] = output[chainId] || {}

// Extract contract names and addresses
Object.entries(contracts).forEach(([contractName, contract]: [string, any]) => {
output[chainId][contractName] = contract.target
})

// Write to output file
const outputDir = path.dirname(addressBook)
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}

fs.writeFileSync(addressBook, JSON.stringify(output, null, 2))

return output as Record<string, Record<string, string>>
}

// Ignition requires "n" suffix for bigints, but not here
function removeNFromBigInts(obj: any): any {
if (typeof obj === 'string') {
return obj.replace(/(\d+)n/g, '$1')
} else if (Array.isArray(obj)) {
return obj.map(removeNFromBigInts)
} else if (typeof obj === 'object' && obj !== null) {
for (const key in obj) {
obj[key] = removeNFromBigInts(obj[key])
}
}
return obj
}
3 changes: 3 additions & 0 deletions packages/hardhat-graph-protocol/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { loadConfig, saveAddressBook } from './ignition/ignition'

export const IgnitionHelper = { saveAddressBook, loadConfig }
8 changes: 8 additions & 0 deletions packages/hardhat-graph-protocol/src/sdk/utils/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function mergeABIs(abi1: any[], abi2: any[]) {
for (const item of abi2) {
if (abi1.find((v) => v.name === item.name) === undefined) {
abi1.push(item)
}
}
return abi1
}
35 changes: 29 additions & 6 deletions packages/horizon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,41 @@

Graph Horizon is the next evolution of the Graph Protocol.

## Deployment
## Configuration

We use Hardhat Ignition to deploy the contracts. To build and deploy Graph Horizon run the following commands:
The following environment variables might be required:

- `ETHERSCAN_API_KEY`: Etherscan API key

You can set them using Hardhat:

```bash
npx hardhat vars set ETHERSCAN_API_KEY
```

## Build

```bash
yarn install
yarn build
npx hardhat ignition deploy ./ignition/modules/horizon.ts \
--parameters ./ignition/configs/horizon.hardhat.json5 \
--network hardhat
```

You can use any network defined in `hardhat.config.ts` by replacing `hardhat` with the network name.
## Deploy

### New deployment
To deploy Graph Horizon from scratch run the following command:

```bash
npx hardhat run scripts/deploy.ts --network hardhat
```

Note that this will deploy a standalone version of Graph Horizon contracts, meaning the Subgraph Service or any other data service will not be deployed. If you want to deploy the Subgraph Service please refer to the [Subgraph Service README](../subgraph-service/README.md) for deploy instructions.

### Upgrade deployment
To upgrade an existing deployment of the original Graph Protocol to Graph Horizon, run the following command:

```bash
npx hardhat run scripts/migrate.ts --network hardhat
```

Note that this will deploy a standalone version of Graph Horizon contracts, meaning the Subgraph Service or any other data service will not be deployed. If you want to deploy the Subgraph Service please refer to the [Subgraph Service README](../subgraph-service/README.md) for deploy instructions.
Loading

0 comments on commit 36d203f

Please sign in to comment.