Skip to content

Commit

Permalink
🪛👷‍♂️: Adding checkWire task (#151)
Browse files Browse the repository at this point in the history
Co-authored-by: Ján Jakub Naništa <[email protected]>
  • Loading branch information
sirarthurmoney and janjakubnanista authored Jan 8, 2024
1 parent a97bb50 commit b206cda
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 45 deletions.
7 changes: 7 additions & 0 deletions .changeset/healthy-beans-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@layerzerolabs/ua-devtools-evm-hardhat-test": patch
"@layerzerolabs/ua-devtools-evm-hardhat": patch
"@layerzerolabs/ua-devtools": patch
---

Adding checkWire task
17 changes: 9 additions & 8 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
---
name: Bug report
about: Create a report to help us improve
title: 'bug: [bug summary]'
title: "bug: [bug summary]"
labels: bug
assignees: ''

assignees: ""
---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

1. Run '...'
4. See error
2. See error

**Expected behavior**
A clear and concise description of what you expected to happen.
Expand All @@ -22,10 +22,11 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.

**Environment (please complete the following information):**
- OS: [e.g. macOS]
- OS Version [e.g. 22]
- VM: [e.g. node, deno]
- VM Version: [e.g. 18.16.0]

- OS: [e.g. macOS]
- OS Version [e.g. 22]
- VM: [e.g. node, deno]
- VM Version: [e.g. 18.16.0]

**Additional context**
Add any other context about the problem here.
1 change: 1 addition & 0 deletions packages/ua-devtools-evm-hardhat/src/constants/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const TASK_LZ_WIRE_OAPP = 'lz:oapp:wire'
export const TASK_LZ_GET_DEFAULT_CONFIG = 'lz:oapp:getDefaultConfig'
export const TASK_LZ_GET_OAPP_CONFIG = 'lz:oapp:getOAppConfig'
export const TASK_LZ_CHECK_WIRE_OAPP = 'lz:oapp:checkWire'
57 changes: 57 additions & 0 deletions packages/ua-devtools-evm-hardhat/src/tasks/oapp/checkWire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ActionType } from 'hardhat/types'
import { task, types } from 'hardhat/config'
import { createLogger, setDefaultLogLevel } from '@layerzerolabs/io-devtools'
import { printRecords } from '@layerzerolabs/io-devtools/swag'
import { TASK_LZ_CHECK_WIRE_OAPP } from '@/constants/tasks'
import { printLogo } from '@layerzerolabs/io-devtools/swag'
import { OAppOmniGraph } from '@layerzerolabs/ua-devtools'
import { createConnectedContractFactory } from '@layerzerolabs/devtools-evm-hardhat'
import { createOAppFactory } from '@layerzerolabs/ua-devtools-evm'
import { checkOAppPeers } from '@layerzerolabs/ua-devtools'
import { validateAndTransformOappConfig } from '@/utils/taskHelpers'
import { getNetworkNameForEid } from '@layerzerolabs/devtools-evm-hardhat'
import { printBoolean } from '@layerzerolabs/io-devtools'

interface TaskArgs {
oappConfig: string
logLevel?: string
}

export const checkWire: ActionType<TaskArgs> = async ({ oappConfig: oappConfigPath, logLevel = 'info' }) => {
printLogo()

// We'll set the global logging level to get as much info as needed
setDefaultLogLevel(logLevel)

// And we'll create a logger for ourselves
const logger = createLogger()
const graph: OAppOmniGraph = await validateAndTransformOappConfig(oappConfigPath, logger)

// At this point we are ready read data from the OApp
logger.verbose(`Reading peers from OApps`)
const contractFactory = createConnectedContractFactory()
const oAppFactory = createOAppFactory(contractFactory)

try {
const peers = await checkOAppPeers(graph, oAppFactory)

const formattedPeers = peers.map((peer) => ({
'From network': getNetworkNameForEid(peer.vector.from.eid),
'To network': getNetworkNameForEid(peer.vector.to.eid),
Connected: printBoolean(peer.hasPeer),
}))

printRecords(formattedPeers)

return peers
} catch (error) {
throw new Error(`An error occurred while getting the OApp configuration: ${error}`)
}
}

task(
TASK_LZ_CHECK_WIRE_OAPP,
'outputs visual console table to show current state of oapp connections via configuration'
)
.addParam('oappConfig', 'Path to your LayerZero OApp config', './layerzero.config.js', types.string)
.setAction(checkWire)
1 change: 1 addition & 0 deletions packages/ua-devtools-evm-hardhat/src/tasks/oapp/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './wire'
import './checkWire'
import './getDefaultConfig'
import './getOAppConfig'
35 changes: 2 additions & 33 deletions packages/ua-devtools-evm-hardhat/src/tasks/oapp/wire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ import type { ActionType } from 'hardhat/types'
import { TASK_LZ_WIRE_OAPP } from '@/constants/tasks'
import {
createLogger,
createConfigLoader,
setDefaultLogLevel,
promptToContinue,
printJson,
pluralizeNoun,
printBoolean,
} from '@layerzerolabs/io-devtools'
import { OAppOmniGraphHardhat, OAppOmniGraphHardhatSchema } from '@/oapp'
import { OAppOmniGraph, configureOApp } from '@layerzerolabs/ua-devtools'
import { createOAppFactory } from '@layerzerolabs/ua-devtools-evm'
import {
OmniGraphBuilderHardhat,
createConnectedContractFactory,
createSignerFactory,
formatOmniTransaction,
} from '@layerzerolabs/devtools-evm-hardhat'
import { createSignAndSend, OmniTransaction } from '@layerzerolabs/devtools'
import { resolve } from 'path'
import { createProgressBar, printLogo, printRecords, render } from '@layerzerolabs/io-devtools/swag'
import { validateAndTransformOappConfig } from '@/utils/taskHelpers'

interface TaskArgs {
oappConfig: string
Expand All @@ -40,35 +37,7 @@ const action: ActionType<TaskArgs> = async ({ oappConfig: oappConfigPath, logLev

// And we'll create a logger for ourselves
const logger = createLogger()

// Now we create our config loader
const configLoader = createConfigLoader<OAppOmniGraphHardhat>(OAppOmniGraphHardhatSchema)

// At this point we have a correctly typed config in the hardhat format
const hardhatGraph: OAppOmniGraphHardhat = await configLoader(resolve(oappConfigPath))

// We'll also print out the whole config for verbose loggers
logger.verbose(`Config file '${oappConfigPath}' has correct structure`)
logger.debug(`The hardhat config is:\n\n${printJson(hardhatGraph)}`)

// What we need to do now is transform the config from hardhat format to the generic format
// with addresses instead of contractNames
logger.verbose(`Transforming '${oappConfigPath}' from hardhat-specific format to generic format`)
let graph: OAppOmniGraph
try {
// The transformation is achieved using a builder that also validates the resulting graph
// (i.e. makes sure that all the contracts exist and connections are valid)
const builder = await OmniGraphBuilderHardhat.fromConfig(hardhatGraph)

// We only need the graph so we throw away the builder
graph = builder.graph
} catch (error) {
throw new Error(`Config from file '${oappConfigPath}' is invalid: ${error}`)
}

// Show more detailed logs to interested users
logger.verbose(`Transformed '${oappConfigPath}' from hardhat-specific format to generic format`)
logger.debug(`The resulting config is:\n\n${printJson(graph)}`)
const graph: OAppOmniGraph = await validateAndTransformOappConfig(oappConfigPath, logger)

// At this point we are ready to create the list of transactions
logger.verbose(`Creating a list of wiring transactions`)
Expand Down
44 changes: 43 additions & 1 deletion packages/ua-devtools-evm-hardhat/src/utils/taskHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { Address } from '@layerzerolabs/devtools'
import { Timeout, Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-devtools'
import { createConnectedContractFactory, getEidForNetworkName } from '@layerzerolabs/devtools-evm-hardhat'
import {
createConnectedContractFactory,
getEidForNetworkName,
OmniGraphBuilderHardhat,
} from '@layerzerolabs/devtools-evm-hardhat'
import { createConfigLoader, printJson } from '@layerzerolabs/io-devtools'
import { createEndpointFactory } from '@layerzerolabs/protocol-devtools-evm'
import { OAppOmniGraphHardhat, OAppOmniGraphHardhatSchema } from '@/oapp'
import { resolve } from 'path'
import { OAppOmniGraph } from '@layerzerolabs/ua-devtools'
import { Logger } from '@layerzerolabs/io-devtools'

export async function getSendConfig(
localNetworkName: string,
Expand Down Expand Up @@ -62,3 +71,36 @@ export async function getReceiveConfig(
const receiveUlnConfig = await localReceiveUlnSDK.getUlnConfig(remoteEid)
return [receiveLibrary, receiveUlnConfig, receiveLibraryTimeout]
}

export async function validateAndTransformOappConfig(oappConfigPath: string, logger: Logger): Promise<OAppOmniGraph> {
// Now we create our config loader
const configLoader = createConfigLoader<OAppOmniGraphHardhat>(OAppOmniGraphHardhatSchema)

// At this point we have a correctly typed config in the hardhat format
const hardhatGraph: OAppOmniGraphHardhat = await configLoader(resolve(oappConfigPath))

// We'll also print out the whole config for verbose loggers
logger.verbose(`Config file '${oappConfigPath}' has correct structure`)
logger.debug(`The hardhat config is:\n\n${printJson(hardhatGraph)}`)

// What we need to do now is transform the config from hardhat format to the generic format
// with addresses instead of contractNames
logger.verbose(`Transforming '${oappConfigPath}' from hardhat-specific format to generic format`)
let graph: OAppOmniGraph
try {
// The transformation is achieved using a builder that also validates the resulting graph
// (i.e. makes sure that all the contracts exist and connections are valid)
const builder = await OmniGraphBuilderHardhat.fromConfig(hardhatGraph)

// We only need the graph so we throw away the builder
graph = builder.graph
} catch (error) {
throw new Error(`Config from file '${oappConfigPath}' is invalid: ${error}`)
}

// Show more detailed logs to interested users
logger.verbose(`Transformed '${oappConfigPath}' from hardhat-specific format to generic format`)
logger.debug(`The resulting config is:\n\n${printJson(graph)}`)

return graph
}
13 changes: 13 additions & 0 deletions packages/ua-devtools/src/oapp/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { OAppFactory, OAppOmniGraph, OAppPeers } from '@/oapp/types'

export type OAppRead = (graph: OAppOmniGraph, createSdk: OAppFactory) => Promise<OAppPeers[]>

export const checkOAppPeers: OAppRead = async (graph, createSdk): Promise<OAppPeers[]> => {
return await Promise.all(
graph.connections.map(async ({ vector }): Promise<OAppPeers> => {
const sdk = await createSdk(vector.from)
const hasPeer = await sdk.hasPeer(vector.to.eid, vector.to.address)
return { vector: vector, hasPeer }
})
)
}
2 changes: 1 addition & 1 deletion packages/ua-devtools/src/oapp/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { flattenTransactions, type OmniTransaction } from '@layerzerolabs/devtools'
import type { OAppFactory, OAppOmniGraph } from './types'
import { OAppFactory, OAppOmniGraph } from './types'
import { createModuleLogger, printBoolean } from '@layerzerolabs/io-devtools'
import { formatOmniVector, isDeepEqual } from '@layerzerolabs/devtools'
import { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-devtools'
Expand Down
1 change: 1 addition & 0 deletions packages/ua-devtools/src/oapp/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './check'
export * from './config'
export * from './schema'
export * from './types'
6 changes: 6 additions & 0 deletions packages/ua-devtools/src/oapp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
OmniGraph,
OmniPoint,
OmniTransaction,
OmniVector,
} from '@layerzerolabs/devtools'
import type { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-devtools'

Expand Down Expand Up @@ -40,6 +41,11 @@ export interface OAppEdgeConfig {
receiveConfig?: OAppReceiveConfig
}

export interface OAppPeers {
vector: OmniVector
hasPeer: boolean
}

export type OAppOmniGraph = OmniGraph<unknown, OAppEdgeConfig | undefined>

export type OAppFactory<TOApp extends IOApp = IOApp, TOmniPoint = OmniPoint> = Factory<[TOmniPoint], TOApp>
Loading

0 comments on commit b206cda

Please sign in to comment.