Skip to content

Commit b44769d

Browse files
authored
feat: refactor graph runtime environment hardhat extension (#636)
* feat: refactor graph runtime environment hardhat extension Signed-off-by: Tomás Migone <[email protected]>
1 parent bcb9876 commit b44769d

File tree

8 files changed

+57
-70
lines changed

8 files changed

+57
-70
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ MNEMONIC=
22
ETHERSCAN_API_KEY=
33
INFURA_KEY=
44
ADDRESS_BOOK="addresses.json"
5-
GRAPH_CONFIG=""config/graph.mainnet.yml""
5+
GRAPH_CONFIG="config/graph.mainnet.yml"
66
PROVIDER_URL="http://localhost:8545"

cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MNEMONIC=
1111
ETHERSCAN_API_KEY=
1212
INFURA_KEY=
1313
ADDRESS_BOOK="addresses.json"
14-
GRAPH_CONFIG=""config/graph.mainnet.yml""
14+
GRAPH_CONFIG="config/graph.mainnet.yml"
1515
PROVIDER_URL="http://localhost:8545"
1616
```
1717

tasks/deployment/config.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { task } from 'hardhat/config'
22
import '@nomiclabs/hardhat-ethers'
33
import { cliOpts } from '../../cli/defaults'
4-
import { readConfig, writeConfig } from '../../cli/config'
4+
import { writeConfig } from '../../cli/config'
55
import YAML from 'yaml'
66

77
import { Scalar, YAMLMap } from 'yaml/types'
8-
import { HardhatRuntimeEnvironment } from 'hardhat/types'
9-
import fs from 'fs'
108
import { confirm } from '../../cli/helpers'
9+
import { NetworkContracts } from '../../cli/contracts'
1110

1211
interface Contract {
1312
name: string
@@ -100,10 +99,6 @@ task('update-config', 'Update graph config parameters with onchain data')
10099
const dryRun = taskArgs.dryRun
101100
const skipConfirmation = taskArgs.skipConfirmation
102101

103-
if (!fs.existsSync(configFile)) {
104-
throw new Error(`Could not find config file: ${configFile}`)
105-
}
106-
107102
console.log('## Update graph config ##')
108103
console.log(`Network: ${networkName}`)
109104
console.log(`Config file: ${configFile}\n`)
@@ -117,18 +112,18 @@ task('update-config', 'Update graph config parameters with onchain data')
117112
if (!sure) return
118113
}
119114

120-
const graphConfig = readConfig(configFile, true)
115+
const { graphConfig, contracts } = hre.graph({ graphConfig: configFile })
121116

122117
// general parameters
123118
console.log(`> General`)
124119
for (const param of generalParams) {
125-
await updateGeneralParams(hre, param, graphConfig)
120+
await updateGeneralParams(contracts, param, graphConfig)
126121
}
127122

128123
// contracts parameters
129124
for (const contract of contractList) {
130125
console.log(`> ${contract.name}`)
131-
await updateContractParams(hre, contract, graphConfig)
126+
await updateContractParams(contracts, contract, graphConfig)
132127
}
133128

134129
if (dryRun) {
@@ -140,24 +135,24 @@ task('update-config', 'Update graph config parameters with onchain data')
140135
})
141136

142137
const updateGeneralParams = async (
143-
hre: HardhatRuntimeEnvironment,
138+
contracts: NetworkContracts,
144139
param: GeneralParam,
145140
config: YAML.Document.Parsed,
146141
) => {
147-
const value = await hre.contracts[param.contract][param.name]()
142+
const value = await contracts[param.contract][param.name]()
148143
const updated = updateItem(config, `general/${param.name}`, value)
149144
if (updated) {
150145
console.log(`\t- Updated ${param.name} to ${value}`)
151146
}
152147
}
153148

154149
const updateContractParams = async (
155-
hre: HardhatRuntimeEnvironment,
150+
contracts: NetworkContracts,
156151
contract: Contract,
157152
config: YAML.Document.Parsed,
158153
) => {
159154
for (const param of contract.initParams) {
160-
let value = await hre.contracts[contract.name][param.getter ?? param.name]()
155+
let value = await contracts[contract.name][param.getter ?? param.name]()
161156
if (param.type === 'BigNumber') {
162157
if (param.format === 'number') {
163158
value = value.toNumber()

tasks/gre.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
1-
import { Contract } from 'ethers'
21
import { HardhatRuntimeEnvironment } from 'hardhat/types'
32
import { extendEnvironment } from 'hardhat/config'
43
import { lazyObject } from 'hardhat/plugins'
5-
import '@nomiclabs/hardhat-ethers'
64

7-
import { cliOpts } from '../cli/defaults'
85
import { getAddressBook } from '../cli/address-book'
9-
import { loadContracts, NetworkContracts } from '../cli/contracts'
6+
import { loadContracts } from '../cli/contracts'
7+
import { readConfig } from '../cli/config'
8+
import { GREOptions } from './type-extensions'
9+
import fs from 'fs'
1010

1111
// Graph Runtime Environment (GRE) extensions for the HRE
12-
13-
declare module 'hardhat/types/runtime' {
14-
export interface HardhatRuntimeEnvironment {
15-
contracts: NetworkContracts
16-
}
17-
}
18-
19-
interface ConsoleNetworkContracts extends NetworkContracts {
20-
connect: () => void
21-
}
22-
2312
extendEnvironment((hre: HardhatRuntimeEnvironment) => {
24-
hre['contracts'] = lazyObject(() => {
25-
const chainId = hre.network.config.chainId.toString()
26-
const provider = hre.ethers.provider
27-
const addressBook = getAddressBook(cliOpts.addressBook.default, chainId)
28-
const contracts = loadContracts(addressBook, provider) as ConsoleNetworkContracts
13+
hre.graph = (opts: GREOptions = {}) => {
14+
const chainId = hre.network.config.chainId?.toString() ?? '1337'
15+
const addressBookPath = opts.addressBook ?? process.env.ADDRESS_BOOK
16+
const graphConfigPath = opts.graphConfig ?? process.env.GRAPH_CONFIG
2917

30-
// Connect contracts to a signing account
31-
contracts.connect = async function (n = 0) {
32-
const accounts = await hre.ethers.getSigners()
33-
const senderAccount = accounts[n]
34-
console.log(`> Sender set to ${senderAccount.address}`)
35-
for (const [k, contract] of Object.entries(contracts)) {
36-
if (contract instanceof Contract) {
37-
contracts[k] = contract.connect(senderAccount)
38-
}
39-
}
18+
if (!fs.existsSync(addressBookPath)) {
19+
throw new Error(`Address book not found: ${addressBookPath}`)
4020
}
4121

42-
return contracts
43-
})
44-
hre['provider'] = lazyObject(() => hre.ethers.provider)
45-
hre['accounts'] = function () {
46-
return hre.ethers.getSigners()
22+
if (!fs.existsSync(graphConfigPath)) {
23+
throw new Error(`Graph config not found: ${graphConfigPath}`)
24+
}
25+
26+
return {
27+
addressBook: lazyObject(() => getAddressBook(addressBookPath, chainId)),
28+
graphConfig: lazyObject(() => readConfig(graphConfigPath, true)),
29+
contracts: lazyObject(() =>
30+
loadContracts(getAddressBook(addressBookPath, chainId), hre.ethers.provider),
31+
),
32+
}
4733
}
4834
})

tasks/test-upgrade.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from 'fs'
2-
32
import { task } from 'hardhat/config'
43

54
function saveProxyAddresses(data) {

tasks/type-extensions.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { AddressBook } from '../cli/address-book'
2+
import { NetworkContracts } from '../cli/contracts'
3+
4+
interface GREOptions {
5+
addressBook?: string
6+
graphConfig?: string
7+
}
8+
9+
declare module 'hardhat/types/runtime' {
10+
export interface HardhatRuntimeEnvironment {
11+
graph: (opts?: GREOptions) => {
12+
contracts: NetworkContracts
13+
graphConfig: any
14+
addressBook: AddressBook
15+
}
16+
}
17+
}

tasks/verify/verify.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import { isFullyQualifiedName, parseFullyQualifiedName } from 'hardhat/utils/con
55
import { TASK_COMPILE } from 'hardhat/builtin-tasks/task-names'
66
import { loadEnv } from '../../cli/env'
77
import { cliOpts } from '../../cli/defaults'
8-
import { getAddressBook } from '../../cli/address-book'
9-
import { getContractConfig, readConfig } from '../../cli/config'
8+
import { getContractConfig } from '../../cli/config'
109
import { Wallet } from 'ethers'
11-
import { NomicLabsHardhatPluginError } from 'hardhat/plugins'
1210
import fs from 'fs'
1311
import path from 'path'
14-
import { HardhatRuntimeEnvironment } from 'hardhat/types'
12+
import { HardhatRuntimeEnvironment } from 'hardhat/types/runtime'
1513

1614
task('sourcify', 'Verifies contract on sourcify')
1715
.addPositionalParam('address', 'Address of the smart contract to verify', undefined, types.string)
@@ -46,7 +44,7 @@ task('sourcifyAll', 'Verifies all contracts on sourcify')
4644
throw new Error('Cannot verify contracts without a network')
4745
}
4846
console.log(`> Verifying all contracts on chain ${chainName}[${chainId}]...`)
49-
const addressBook = getAddressBook(cliOpts.addressBook.default, chainId.toString())
47+
const { addressBook } = hre.graph({ addressBook: _args.addressBook })
5048

5149
for (const contractName of addressBook.listEntries()) {
5250
console.log(`\n> Verifying contract ${contractName}...`)
@@ -86,8 +84,10 @@ task('verifyAll', 'Verifies all contracts on etherscan')
8684
}
8785

8886
console.log(`> Verifying all contracts on chain ${chainName}[${chainId}]...`)
89-
const addressBook = getAddressBook(args.addressBook, chainId.toString())
90-
const graphConfig = readConfig(args.graphConfig)
87+
const { addressBook, graphConfig } = hre.graph({
88+
addressBook: args.addressBook,
89+
graphConfig: args.graphConfig,
90+
})
9191

9292
const accounts = await hre.ethers.getSigners()
9393
const env = await loadEnv(args, accounts[0] as unknown as Wallet)

tsconfig.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,5 @@
88
"resolveJsonModule": true,
99
"esModuleInterop": true
1010
},
11-
"exclude": ["dist", "node_modules"],
12-
"files": [
13-
"./hardhat.config.ts",
14-
"./scripts/**/*.ts",
15-
"./test/**/*.ts",
16-
"node_modules/@nomiclabs/hardhat-ethers/internal/type-extensions.d.ts",
17-
"node_modules/@nomiclabs/hardhat-etherscan/dist/src/type-extensions.d.ts",
18-
"node_modules/@nomiclabs/hardhat-waffle/dist/src/type-extensions.d.ts",
19-
"node_modules/@typechain/hardhat/dist/type-extensions.d.ts",
20-
"./index.d.ts"
21-
]
11+
"include": ["hardhat.config.ts", "index.d.ts", "scripts/**/*.ts", "test/**/*.ts", "tasks/**/*.ts"]
2212
}

0 commit comments

Comments
 (0)