Skip to content

Commit

Permalink
feat: use metadata provider to fetch chains metadata on api
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Aug 27, 2024
1 parent 4d1f202 commit d8c57e6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const main = async (): Promise<void> => {
metadataProvider,
logger,
);
const metricsController = new MetricsController(l1MetricsService, logger);
const metricsController = new MetricsController(l1MetricsService, metadataProvider, logger);
const metricsRouter = new MetricsRouter(metricsController, logger);

const app = new App(config, [metricsRouter], logger);
Expand Down
12 changes: 8 additions & 4 deletions apps/api/src/metrics/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import { BigNumber } from "bignumber.js";

import { IMetadataProvider } from "@zkchainhub/metadata";
import { L1MetricsService } from "@zkchainhub/metrics";
import { ILogger, zkChainsMetadata } from "@zkchainhub/shared";
import { ILogger } from "@zkchainhub/shared";

import { EcosystemInfo, ZKChainInfo, ZkChainMetadata } from "../dto/response/index.js";
import { ChainNotFound } from "../exceptions/index.js";

export class MetricsController {
constructor(
private readonly l1MetricsService: L1MetricsService,
private readonly metadataProvider: IMetadataProvider,
private readonly logger: ILogger,
) {}

async getEcosystem(): Promise<EcosystemInfo> {
const [l1Tvl, gasInfo, chainIds] = await Promise.all([
const [l1Tvl, gasInfo, chainIds, chainsMetadata] = await Promise.all([
this.l1MetricsService.l1Tvl(),
this.l1MetricsService.ethGasInfo(),
this.l1MetricsService.getChainIds(),
this.metadataProvider.getChainsMetadata(),
]);

const zkChains = await Promise.all(
chainIds.map(async (chainId) => {
const metadata = zkChainsMetadata.get(chainId);
const metadata = chainsMetadata.get(chainId);
const tvl = (await this.l1MetricsService.tvl(chainId))
.reduce((acc, curr) => {
return acc.plus(BigNumber(curr.amountUsd || 0));
Expand Down Expand Up @@ -62,7 +65,8 @@ export class MetricsController {

async getChain(chainId: number) {
const chainIdBn = BigInt(chainId);
const metadata = zkChainsMetadata.get(chainIdBn);
const chainsMetadata = await this.metadataProvider.getChainsMetadata();
const metadata = chainsMetadata.get(chainIdBn);
const ecosystemChainIds = await this.l1MetricsService.getChainIds();

if (!ecosystemChainIds.includes(chainIdBn)) {
Expand Down

0 comments on commit d8c57e6

Please sign in to comment.