-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: l2 metrics api integration #64
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import { isAddress } from "viem"; | ||
import { z } from "zod"; | ||
|
||
const stringToJSONSchema = z.string().transform((str, ctx): z.infer<ReturnType<typeof Object>> => { | ||
try { | ||
return JSON.parse(str); | ||
} catch (e) { | ||
ctx.addIssue({ code: "custom", message: "Invalid JSON" }); | ||
return z.NEVER; | ||
} | ||
}); | ||
|
||
const addressArraySchema = z | ||
.string() | ||
.transform((str) => str.split(",")) | ||
|
@@ -11,20 +20,21 @@ const addressSchema = z.string().refine((address) => isAddress(address), { | |
message: "Must be a valid Address", | ||
}); | ||
|
||
const urlArraySchema = z | ||
.string() | ||
.transform((str) => str.split(",")) | ||
.refine((urls) => urls.every((url) => z.string().url().safeParse(url).success), { | ||
message: "Must be a comma-separated list of valid URLs", | ||
}); | ||
const urlArraySchema = z.array(z.string().url()); | ||
Comment on lines
-14
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noice |
||
|
||
const urlArrayMapSchema = z.record( | ||
z.union([z.coerce.number().int(), z.string().regex(/^\d+$/)]), // key: number or string number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super edge case but would the key There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it's fine, chain ids are numbers actually but JSON doesn't allow numbers as key type so if you write, "04" to us it's the same as "4" |
||
urlArraySchema, | ||
); | ||
|
||
const baseSchema = z.object({ | ||
PORT: z.coerce.number().positive().default(3000), | ||
BRIDGE_HUB_ADDRESS: addressSchema, | ||
SHARED_BRIDGE_ADDRESS: addressSchema, | ||
STATE_MANAGER_ADDRESSES: addressArraySchema, | ||
ENVIRONMENT: z.enum(["mainnet", "testnet", "local"]).default("mainnet"), | ||
L1_RPC_URLS: urlArraySchema, | ||
L1_RPC_URLS: stringToJSONSchema.pipe(urlArraySchema), | ||
L2_RPC_URLS_MAP: stringToJSONSchema.pipe(urlArrayMapSchema).default("{}"), | ||
PRICING_SOURCE: z.enum(["dummy", "coingecko"]).default("dummy"), | ||
DUMMY_PRICE: z.coerce.number().optional(), | ||
COINGECKO_API_KEY: z.string().optional(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { inspect } from "util"; | ||
import { caching } from "cache-manager"; | ||
|
||
import { EvmProvider } from "@zkchainhub/chain-providers"; | ||
import { EvmProvider, ZKChainProvider } from "@zkchainhub/chain-providers"; | ||
import { MetadataProviderFactory } from "@zkchainhub/metadata"; | ||
import { L1MetricsService } from "@zkchainhub/metrics"; | ||
import { L1MetricsService, L2MetricsService } from "@zkchainhub/metrics"; | ||
import { PricingProviderFactory } from "@zkchainhub/pricing"; | ||
import { Logger } from "@zkchainhub/shared"; | ||
import { ChainId, Logger } from "@zkchainhub/shared"; | ||
|
||
import { App } from "./app.js"; | ||
import { config } from "./common/config/index.js"; | ||
|
@@ -40,7 +40,22 @@ const main = async (): Promise<void> => { | |
metadataProvider, | ||
logger, | ||
); | ||
const metricsController = new MetricsController(l1MetricsService, metadataProvider, logger); | ||
|
||
const l2ChainsConfigMap = config.l2; | ||
const l2MetricsMap = new Map<ChainId, L2MetricsService>(); | ||
|
||
for (const [chainId, rpcUrls] of Object.entries(l2ChainsConfigMap)) { | ||
const provider = new ZKChainProvider(rpcUrls, logger); | ||
const metricsService = new L2MetricsService(provider, logger); | ||
l2MetricsMap.set(BigInt(chainId), metricsService); | ||
} | ||
|
||
Comment on lines
+47
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was wondering if it would be better to encapsulate this logic inside the L2MetricsService and you just pass the map as parameter. But this approach seems to be more decoupled. 👍 |
||
const metricsController = new MetricsController( | ||
l1MetricsService, | ||
l2MetricsMap, | ||
metadataProvider, | ||
logger, | ||
); | ||
const metricsRouter = new MetricsRouter(metricsController, logger); | ||
|
||
const app = new App(config, [metricsRouter], logger); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we are already parsing a json don't you think having an array of rpcs is better ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced also L1_RPC_URLS to also be a json array since it's easier to read