Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: add debug based logger to GRE
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Aug 15, 2022
commit 1343eac3968e08c125795c879971b12a11c77d6b
82 changes: 56 additions & 26 deletions gre/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ import { HardhatRuntimeEnvironment } from 'hardhat/types/runtime'
import { HttpNetworkConfig } from 'hardhat/types/config'

import { GraphRuntimeEnvironmentOptions } from './type-extensions'
import { GREPluginError } from './helpers/errors'
import GraphChains from './helpers/chains'
import { GREPluginError } from './helpers/error'
import GraphNetwork from './helpers/network'

import debug from 'debug'
const log = debug('hardhat:graphprotocol:gre')
import { logDebug } from './logger'

interface GREChainData {
interface GREChains {
l1ChainId: number
l2ChainId: number
isHHL1: boolean
isHHL2: boolean
}

interface GREProviderData {
interface GREProviders {
l1Provider: providers.JsonRpcProvider
l2Provider: providers.JsonRpcProvider
}

interface GREGraphConfigData {
interface GREGraphConfigs {
l1GraphConfigPath: string | undefined
l2GraphConfigPath: string | undefined
}
Expand All @@ -33,6 +32,10 @@ export function getAddressBookPath(
hre: HardhatRuntimeEnvironment,
opts: GraphRuntimeEnvironmentOptions,
): string {
logDebug('== Getting address book path')
logDebug(`1) opts.addressBookPath: ${opts.addressBook}`)
logDebug(`2) hre.config.graph.addressBook: ${hre.config.graph.addressBook}`)

const addressBookPath = opts.addressBook ?? hre.config.graph.addressBook

if (addressBookPath === undefined) {
Expand All @@ -43,33 +46,36 @@ export function getAddressBookPath(
throw new GREPluginError(`Address book not found: ${addressBookPath}`)
}

logDebug(`Address book path found: ${addressBookPath}`)
return addressBookPath
}

export function getChains(mainChainId: number | undefined): GREChainData {
if (!GraphChains.isSupported(mainChainId)) {
const supportedChains = GraphChains.chains.join(',')
export function getChains(mainChainId: number | undefined): GREChains {
logDebug('== Getting chain ids')
logDebug(`Hardhat chain id: ${mainChainId}`)

if (!GraphNetwork.isSupported(mainChainId)) {
const supportedChains = GraphNetwork.chains.join(',')
throw new GREPluginError(
`Chain ${mainChainId} is not supported! Supported chainIds: ${supportedChains}.`,
)
}

// If mainChainId is supported there is a supported counterpart chainId so both chains are not undefined

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
mainChainId = mainChainId!

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const secondaryChainId = GraphChains.counterpart(mainChainId)!
const secondaryChainId = GraphNetwork.counterpart(mainChainId)!
logDebug(`Secondary chain id: ${secondaryChainId}`)

const isHHL1 = GraphChains.isL1(mainChainId)
const isHHL2 = GraphChains.isL2(mainChainId)
const isHHL1 = GraphNetwork.isL1(mainChainId)
const isHHL2 = GraphNetwork.isL2(mainChainId)
const l1ChainId = isHHL1 ? mainChainId : secondaryChainId
const l2ChainId = isHHL2 ? mainChainId : secondaryChainId

log(`Hardhat chain id: ${mainChainId}`)
log(`L1 chain id: ${l1ChainId} - Is HHL1: ${isHHL1}`)
log(`L2 chain id: ${l2ChainId} - Is HHL2: ${isHHL2}`)
logDebug(`L1 chain id: ${l1ChainId} - Is HHL1: ${isHHL1}`)
logDebug(`L2 chain id: ${l2ChainId} - Is HHL2: ${isHHL2}`)

return {
l1ChainId,
Expand All @@ -83,19 +89,26 @@ export function getProviders(
hre: HardhatRuntimeEnvironment,
l1ChainId: number,
l2ChainId: number,
): GREProviderData {
const l1Network = getNetworkByChainId(hre.config.networks, l1ChainId) as HttpNetworkConfig
const l2Network = getNetworkByChainId(hre.config.networks, l2ChainId) as HttpNetworkConfig
): GREProviders {
logDebug('== Getting providers')

const l1Network = getNetworkConfig(hre.config.networks, l1ChainId) as HttpNetworkConfig
const l2Network = getNetworkConfig(hre.config.networks, l2ChainId) as HttpNetworkConfig

for (const network of [l1Network, l2Network]) {
if (network === undefined || network.url === undefined) {
throw new GREPluginError(`Must set a provider url for chain ${l1ChainId}!`)
if (network === undefined) {
throw new GREPluginError(`L1 or L2 network not found in hardhat config!`)
} else if (network.url === undefined) {
throw new GREPluginError(`Must set a provider url for chain ${network.chainId}!`)
}
}

const l1Provider = new providers.JsonRpcProvider(l1Network.url)
const l2Provider = new providers.JsonRpcProvider(l2Network.url)

logDebug(`L1 provider url: ${l1Network.url}`)
logDebug(`L2 provider url: ${l2Network.url}`)

return {
l1Provider,
l2Provider,
Expand All @@ -108,9 +121,11 @@ export function getGraphConfigPaths(
l1ChainId: number,
l2ChainId: number,
isHHL1: boolean,
): GREGraphConfigData {
const l1Network = getNetworkByChainId(hre.config.networks, l1ChainId)
const l2Network = getNetworkByChainId(hre.config.networks, l2ChainId)
): GREGraphConfigs {
logDebug('== Getting graph config paths')

const l1Network = getNetworkConfig(hre.config.networks, l1ChainId)
const l2Network = getNetworkConfig(hre.config.networks, l2ChainId)

// Priority is as follows:
// - hre.graph() init parameter l1GraphConfigPath/l2GraphConfigPath
Expand All @@ -123,25 +138,40 @@ export function getGraphConfigPaths(
l1Network?.graphConfig ??
hre.config.graph.l1GraphConfig

logDebug(`> L1 graph config`)
logDebug(`1) opts.l1GraphConfig: ${opts.l1GraphConfig}`)
logDebug(`2) opts.graphConfig: ${isHHL1 ? opts.graphConfig : undefined}`)
logDebug(`3) l1Network.graphConfig: ${l1Network?.graphConfig}`)
logDebug(`4) hre.config.graph.l1GraphConfig: ${hre.config.graph.l1GraphConfig}`)

const l2GraphConfigPath =
opts.l2GraphConfig ??
(!isHHL1 ? opts.graphConfig : undefined) ??
l2Network?.graphConfig ??
hre.config.graph.l2GraphConfig

logDebug(`> L2 graph config`)
logDebug(`1) opts.l2GraphConfig: ${opts.l2GraphConfig}`)
logDebug(`2) opts.graphConfig: ${!isHHL1 ? opts.graphConfig : undefined}`)
logDebug(`3) l2Network.graphConfig: ${l2Network?.graphConfig}`)
logDebug(`4) hre.config.graph.l2GraphConfig: ${hre.config.graph.l2GraphConfig}`)

for (const configPath of [l1GraphConfigPath, l2GraphConfigPath]) {
if (configPath !== undefined && !fs.existsSync(configPath)) {
throw new GREPluginError(`Graph config file not found: ${configPath}`)
}
}

logDebug(`L1 graph config path: ${l1GraphConfigPath}`)
logDebug(`L2 graph config path: ${l2GraphConfigPath}`)

return {
l1GraphConfigPath: l1GraphConfigPath,
l2GraphConfigPath: l2GraphConfigPath,
}
}

function getNetworkByChainId(networks: NetworksConfig, chainId: number): NetworkConfig | undefined {
function getNetworkConfig(networks: NetworksConfig, chainId: number): NetworkConfig | undefined {
return Object.keys(networks)
.map((n) => networks[n])
.find((n) => n.chainId === chainId)
Expand Down
12 changes: 8 additions & 4 deletions gre/gre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { providers } from 'ethers'
import { getChains, getProviders, getAddressBookPath, getGraphConfigPaths } from './config'
import { getDeployer, getNamedAccounts, getTestAccounts } from './accounts'
import { logDebug, logWarn } from './logger'

// Graph Runtime Environment (GRE) extensions for the HRE
extendEnvironment((hre: HardhatRuntimeEnvironment) => {
Expand Down Expand Up @@ -50,6 +51,9 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => {
l2: l2Graph,
}

logDebug('GRE initialized successfully!')
logDebug(`Main network: L${isHHL1 ? '1' : '2'}`)
logDebug(`Secondary network: ${gre.l2 !== null ? (isHHL1 ? 'L2' : 'L1') : 'not initialized'}`)
return gre
}
})
Expand All @@ -62,10 +66,10 @@ function buildGraphNetworkEnvironment(
isHHL1: boolean,
): GraphNetworkEnvironment | null {
if (graphConfigPath === undefined) {
console.warn(
`No graph config file provided for chain: ${chainId}. L${
isHHL1 ? '2' : '1'
} graph object will not be initialized.`,
logWarn(
`No graph config file provided for chain: ${chainId}. ${
isHHL1 ? 'L2' : 'L1'
} will not be initialized.`,
)
return null
}
Expand Down
2 changes: 2 additions & 0 deletions gre/helpers/errors.ts → gre/helpers/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { HardhatPluginError } from 'hardhat/plugins'
import { logError } from '../logger'

export class GREPluginError extends HardhatPluginError {
constructor(message: string) {
super('GraphRuntimeEnvironment', message)
logError(message)
}
}
5 changes: 2 additions & 3 deletions gre/helpers/chains.ts → gre/helpers/network.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class MapWithGetKey<K> extends Map<K, K> {
getKey(value: K): K | undefined {
for (const [k, v] of this.entries()) {
console.log(k, v, value)
for (const [k] of this.entries()) {
if (k === value) {
return k
}
Expand Down Expand Up @@ -30,7 +29,7 @@ export const l1ToL2 = (chainId: number): number | undefined => chainMap.get(chai
export const l2ToL1 = (chainId: number): number | undefined => chainMap.getKey(chainId)
export const counterpart = (chainId: number): number | undefined => {
if (!isSupported(chainId)) return
return isL1(chainId) ? l2ToL1(chainId) : l1ToL2(chainId)
return isL1(chainId) ? l1ToL2(chainId) : l2ToL1(chainId)
}

export default {
Expand Down
1 change: 1 addition & 0 deletions gre/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ const LOG_BASE = 'hardhat:gre'

export const logDebug = debug(`${LOG_BASE}:debug`)
export const logWarn = debug(`${LOG_BASE}:warn`)
export const logError = debug(`${LOG_BASE}:error`)