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

Commit

Permalink
Use Multicall for SimpleStorageHandler (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsidzej authored Oct 20, 2023
1 parent 0f7094e commit a25b194
Show file tree
Hide file tree
Showing 36 changed files with 815 additions and 104 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.18.0

### Minor Changes

- Add multicall support and use multicall for SimpleStorageHandler

## 0.17.2

### 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.17.2",
"version": "0.18.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
Expand Down
9 changes: 8 additions & 1 deletion packages/discovery/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async function discover(
await dryRunDiscovery(
provider,
etherscanClient,
config.chain.multicall,
configReader,
discoverConfig,
)
Expand All @@ -71,7 +72,13 @@ async function discover(
logger.info('Starting discovery...\n')
logger.info(`Project: ${discoverConfig.project}`)
logger.info(`Chain: ${ChainId.getName(discoverConfig.chainId)}\n`)
await runDiscovery(provider, etherscanClient, configReader, discoverConfig)
await runDiscovery(
provider,
etherscanClient,
config.chain.multicall,
configReader,
discoverConfig,
)
}

async function invert(
Expand Down
14 changes: 14 additions & 0 deletions packages/discovery/src/config/config.discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getEnv } from '@l2beat/backend-tools'
import { config as dotenv } from 'dotenv'

import { CliParameters } from '../cli/getCliParameters'
import { multicallConfig } from '../discovery/provider/multicall/MulticallConfig'
import { MulticallConfig } from '../discovery/provider/multicall/types'
import { ChainId } from '../utils/ChainId'
import { EthereumAddress } from '../utils/EthereumAddress'
import { EtherscanUnsupportedMethods } from '../utils/EtherscanLikeClient'
Expand Down Expand Up @@ -57,6 +59,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_ETHEREUM_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.ethereum,
etherscanApiKey: env.string('DISCOVERY_ETHEREUM_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.etherscan.io/api',
}
Expand All @@ -67,6 +70,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_ARBITRUM_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.arbitrum,
etherscanApiKey: env.string('DISCOVERY_ARBITRUM_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.arbiscan.io/api',
}
Expand All @@ -77,6 +81,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_OPTIMISM_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.optimism,
etherscanApiKey: env.string('DISCOVERY_OPTIMISM_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api-optimistic.etherscan.io/api',
}
Expand All @@ -87,6 +92,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_POLYGON_POS_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.polygon_pos,
etherscanApiKey: env.string('DISCOVERY_POLYGON_POS_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.polygonscan.com/api',
}
Expand All @@ -97,6 +103,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_BSC_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.bsc,
etherscanApiKey: env.string('DISCOVERY_BSC_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.bscscan.com/api',
}
Expand All @@ -107,6 +114,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_AVALANCHE_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.avalanche,
etherscanApiKey: env.string('DISCOVERY_AVALANCHE_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.snowtrace.io/api',
}
Expand All @@ -117,6 +125,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_CELO_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.celo,
etherscanApiKey: env.string('DISCOVERY_CELO_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.celoscan.io/api',
etherscanUnsupported: {
Expand All @@ -130,6 +139,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_LINEA_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.linea,
etherscanApiKey: env.string('DISCOVERY_LINEA_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.lineascan.build/api',
}
Expand All @@ -140,6 +150,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_BASE_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.base,
etherscanApiKey: env.string('DISCOVERY_BASE_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.basescan.org/api',
}
Expand All @@ -150,6 +161,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_POLYGON_ZKEVM_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.polygon_zkevm,
etherscanApiKey: env.string(
'DISCOVERY_POLYGON_ZKEVM_ETHERSCAN_API_KEY',
),
Expand All @@ -162,6 +174,7 @@ function getChainConfig(chainId: ChainId): DiscoveryChainConfig {
rpcGetLogsMaxRange: env.optionalInteger(
'DISCOVERY_GNOSIS_RPC_GETLOGS_MAX_RANGE',
),
multicall: multicallConfig.gnosis,
etherscanApiKey: env.string('DISCOVERY_GNOSIS_ETHERSCAN_API_KEY'),
etherscanUrl: 'https://api.gnosisscan.io/api',
}
Expand Down Expand Up @@ -199,6 +212,7 @@ export interface DiscoveryChainConfig {
chainId: ChainId
rpcUrl: string
rpcGetLogsMaxRange?: number
multicall: MulticallConfig
etherscanApiKey: string
etherscanUrl: string
etherscanUnsupported?: EtherscanUnsupportedMethods
Expand Down
6 changes: 5 additions & 1 deletion packages/discovery/src/discovery/engine/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import { DiscoveryConfig } from '../config/DiscoveryConfig'
import { DiscoveryLogger } from '../DiscoveryLogger'
import { HandlerExecutor } from '../handlers/HandlerExecutor'
import { DiscoveryProvider } from '../provider/DiscoveryProvider'
import { MulticallClient } from '../provider/multicall/MulticallClient'
import { MulticallConfig } from '../provider/multicall/types'
import { ProxyDetector } from '../proxies/ProxyDetector'
import { SourceCodeService } from '../source/SourceCodeService'
import { DiscoveryEngine } from './DiscoveryEngine'

export async function discover(
provider: DiscoveryProvider,
config: DiscoveryConfig,
multicallConfig: MulticallConfig,
logger: DiscoveryLogger,
blockNumber: number,
): Promise<Analysis[]> {
const proxyDetector = new ProxyDetector(provider, logger)
const sourceCodeService = new SourceCodeService(provider)
const handlerExecutor = new HandlerExecutor(provider, logger)
const multicallClient = new MulticallClient(provider, multicallConfig)
const handlerExecutor = new HandlerExecutor(provider, multicallClient, logger)
const addressAnalyzer = new AddressAnalyzer(
provider,
proxyDetector,
Expand Down
21 changes: 20 additions & 1 deletion packages/discovery/src/discovery/handlers/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { ContractValue } from '@l2beat/discovery-types'
import { EthereumAddress } from '../../utils/EthereumAddress'
import { DiscoveryLogger } from '../DiscoveryLogger'
import { DiscoveryProvider } from '../provider/DiscoveryProvider'
import {
MulticallRequest,
MulticallResponse,
} from '../provider/multicall/types'

export interface HandlerResult {
field: string
Expand All @@ -11,7 +15,7 @@ export interface HandlerResult {
ignoreRelative?: boolean
}

export interface Handler {
interface BaseHandler {
field: string
dependencies: string[]
logger?: DiscoveryLogger
Expand All @@ -22,3 +26,18 @@ export interface Handler {
previousResults: Record<string, HandlerResult | undefined>,
): Promise<HandlerResult>
}

export interface ClassicHandler extends BaseHandler {
multicallable?: false
}

export interface MulticallableHandler extends BaseHandler {
multicallable: true
encode(
address: EthereumAddress,
previousResults: Record<string, HandlerResult | undefined>,
): MulticallRequest[]
decode: (result: MulticallResponse[]) => HandlerResult
}

export type Handler = MulticallableHandler | ClassicHandler
3 changes: 3 additions & 0 deletions packages/discovery/src/discovery/handlers/HandlerExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EthereumAddress } from '../../utils/EthereumAddress'
import { ContractOverrides } from '../config/DiscoveryOverrides'
import { DiscoveryLogger } from '../DiscoveryLogger'
import { DiscoveryProvider } from '../provider/DiscoveryProvider'
import { MulticallClient } from '../provider/multicall/MulticallClient'
import { executeHandlers } from './executeHandlers'
import { getHandlers } from './getHandlers'
import { getValuesAndErrors } from './getValuesAndErrors'
Expand All @@ -12,6 +13,7 @@ import { HandlerResult } from './Handler'
export class HandlerExecutor {
constructor(
private readonly provider: DiscoveryProvider,
private readonly multicallClient: MulticallClient,
private readonly logger: DiscoveryLogger,
) {}

Expand All @@ -28,6 +30,7 @@ export class HandlerExecutor {
const handlers = getHandlers(abi, overrides, this.logger)
const results = await executeHandlers(
this.provider,
this.multicallClient,
handlers,
address,
blockNumber,
Expand Down
Loading

0 comments on commit a25b194

Please sign in to comment.