Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Export discover method (#90)
Browse files Browse the repository at this point in the history
* Exported discovery cli

* Added changest

* Did some renaming

* Updated changelog

* Moved cli commands' implementations to separate files

* Added changeset

* Updated changeset
  • Loading branch information
aminlatifi authored Nov 2, 2023
1 parent 7fa5e3c commit bb28144
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 96 deletions.
6 changes: 6 additions & 0 deletions packages/discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @l2beat/discovery

## 0.21.4

### Patch Changes

- Export discover method to allow running `discovery discover` command directy from the package

## 0.21.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@l2beat/discovery",
"description": "L2Beat discovery - engine & tooling utilized for keeping an eye on L2s",
"version": "0.21.3",
"version": "0.21.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
Expand Down
94 changes: 8 additions & 86 deletions packages/discovery/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { assert, Logger } from '@l2beat/backend-tools'
import { providers } from 'ethers'
import { Logger } from '@l2beat/backend-tools'

import { discoverCommand } from './cli/discoverCommand'
import { handleCli } from './cli/handleCli'
import {
DiscoveryCliConfig,
getDiscoveryCliConfig,
} from './config/config.discovery'
import { ConfigReader } from './discovery/config/ConfigReader'
import { dryRunDiscovery, runDiscovery } from './discovery/runDiscovery'
import { runInversion } from './inversion/runInversion'
import { singleDiscovery } from './singleDiscovery'
import { ChainId } from './utils/ChainId'
import { EtherscanLikeClient } from './utils/EtherscanLikeClient'
import { HttpClient } from './utils/HttpClient'
import { invertCommand } from './cli/invertCommand'
import { singleDiscoveryCommand } from './cli/singleDiscoveryCommand'
import { getDiscoveryCliConfig } from './config/config.discovery'

main().catch((e) => {
console.error(e)
Expand All @@ -24,77 +16,7 @@ async function main(): Promise<void> {
const config = getDiscoveryCliConfig(cli)
const logger = Logger.DEBUG

await discover(config, logger)
await invert(config, logger)
await singleDiscovery(config, logger)
}

async function discover(
config: DiscoveryCliConfig,
logger: Logger,
): Promise<void> {
if (!config.discovery) {
return
}
const discoverConfig = config.discovery
const chainConfig = config.chain

assert(
chainConfig.chainId === discoverConfig.chainId,
'Chain config does not match discovery config! Update "discovery.config" file or config.json of your project',
)

const http = new HttpClient()
const provider = new providers.StaticJsonRpcProvider(chainConfig.rpcUrl)
const etherscanClient = EtherscanLikeClient.createForDiscovery(
http,
chainConfig.etherscanUrl,
chainConfig.etherscanApiKey,
chainConfig.etherscanUnsupported,
)
const configReader = new ConfigReader()

if (discoverConfig.dryRun) {
logger = logger.for('DryRun')
logger.info('Starting')

await dryRunDiscovery(
provider,
etherscanClient,
config.chain.multicall,
configReader,
discoverConfig,
)
return
}

logger = logger.for('Discovery')
logger.info('Starting discovery...\n')
logger.info(`Project: ${discoverConfig.project}`)
logger.info(`Chain: ${ChainId.getName(discoverConfig.chainId)}\n`)
await runDiscovery(
provider,
etherscanClient,
config.chain.multicall,
configReader,
discoverConfig,
)
}

async function invert(
config: DiscoveryCliConfig,
logger: Logger,
): Promise<void> {
if (!config.invert) {
return
}

const { project, useMermaidMarkup, chainId } = config.invert

const configReader = new ConfigReader()

logger = logger.for('Inversion')
logger.info('Starting')

await runInversion(project, configReader, useMermaidMarkup, chainId)
await discoverCommand(config, logger)
await invertCommand(config, logger)
await singleDiscoveryCommand(config, logger)
}
86 changes: 86 additions & 0 deletions packages/discovery/src/cli/discoverCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { assert, Logger } from '@l2beat/backend-tools'
import { providers } from 'ethers'

import {
DiscoveryCliConfig,
DiscoveryModuleConfig,
getDiscoveryCliConfig,
} from '../config/config.discovery'
import { ConfigReader } from '../discovery/config/ConfigReader'
import { dryRunDiscovery, runDiscovery } from '../discovery/runDiscovery'
import { ChainId } from '../utils/ChainId'
import { EtherscanLikeClient } from '../utils/EtherscanLikeClient'
import { HttpClient } from '../utils/HttpClient'

export async function discoverCommand(
config: DiscoveryCliConfig,
logger: Logger,
): Promise<void> {
if (!config.discovery) {
return
}
const discoverConfig = config.discovery
const chainConfig = config.chain

assert(
chainConfig.chainId === discoverConfig.chainId,
'Chain config does not match discovery config! Update "discovery.config" file or config.json of your project',
)

const http = new HttpClient()
const provider = new providers.StaticJsonRpcProvider(chainConfig.rpcUrl)
const etherscanClient = EtherscanLikeClient.createForDiscovery(
http,
chainConfig.etherscanUrl,
chainConfig.etherscanApiKey,
chainConfig.etherscanUnsupported,
)
const configReader = new ConfigReader()

if (discoverConfig.dryRun) {
logger = logger.for('DryRun')
logger.info('Starting')

await dryRunDiscovery(
provider,
etherscanClient,
config.chain.multicall,
configReader,
discoverConfig,
)
return
}

logger = logger.for('Discovery')
logger.info('Starting discovery...\n')
logger.info(`Project: ${discoverConfig.project}`)
logger.info(`Chain: ${ChainId.getName(discoverConfig.chainId)}\n`)
await runDiscovery(
provider,
etherscanClient,
config.chain.multicall,
configReader,
discoverConfig,
)
}

/**
* Expose the discover command as a method exported by the package.
*/
export function discover(
config: DiscoveryModuleConfig,
logger: Logger = Logger.DEBUG,
): Promise<void> {
const cliConfig = getDiscoveryCliConfig({
mode: 'discover',
project: config.project,
chain: config.chainId,
dryRun: config.dryRun === true,
dev: config.dev === true,
sourcesFolder: config.sourcesFolder,
discoveryFilename: config.discoveryFilename,
blockNumber: config.blockNumber,
})

return discoverCommand(cliConfig, logger)
}
23 changes: 23 additions & 0 deletions packages/discovery/src/cli/invertCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Logger } from '@l2beat/backend-tools'

import { DiscoveryCliConfig } from '../config/config.discovery'
import { ConfigReader } from '../discovery/config/ConfigReader'
import { runInversion } from '../inversion/runInversion'

export async function invertCommand(
config: DiscoveryCliConfig,
logger: Logger,
): Promise<void> {
if (!config.invert) {
return
}

const { project, useMermaidMarkup, chainId } = config.invert

const configReader = new ConfigReader()

logger = logger.for('Inversion')
logger.info('Starting')

await runInversion(project, configReader, useMermaidMarkup, chainId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { mkdirp } from 'mkdirp'
import { dirname } from 'path'
import { rimraf } from 'rimraf'

import { DiscoveryCliConfig } from './config/config.discovery'
import { DiscoveryConfig } from './discovery/config/DiscoveryConfig'
import { DiscoveryLogger } from './discovery/DiscoveryLogger'
import { getSourceName } from './discovery/output/saveDiscoveryResult'
import { toDiscoveryOutput } from './discovery/output/toDiscoveryOutput'
import { discover as discovery } from './discovery/runDiscovery'
import { EtherscanLikeClient } from './utils/EtherscanLikeClient'
import { HttpClient } from './utils/HttpClient'
import { DiscoveryCliConfig } from '../config/config.discovery'
import { DiscoveryConfig } from '../discovery/config/DiscoveryConfig'
import { DiscoveryLogger } from '../discovery/DiscoveryLogger'
import { getSourceName } from '../discovery/output/saveDiscoveryResult'
import { toDiscoveryOutput } from '../discovery/output/toDiscoveryOutput'
import { discover as discovery } from '../discovery/runDiscovery'
import { EtherscanLikeClient } from '../utils/EtherscanLikeClient'
import { HttpClient } from '../utils/HttpClient'

export async function singleDiscovery(
export async function singleDiscoveryCommand(
config: DiscoveryCliConfig,
logger: Logger,
): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions packages/discovery/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { discover } from './cli/discoverCommand'
export { AddressAnalyzer } from './discovery/analysis/AddressAnalyzer'
export { ConfigReader } from './discovery/config/ConfigReader'
export { DiscoveryConfig } from './discovery/config/DiscoveryConfig'
Expand Down

0 comments on commit bb28144

Please sign in to comment.