Skip to content
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: batches info l1 metric #41

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/metrics/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./invalidChainId.exception";
export * from "./l1MetricsService.exception";
6 changes: 6 additions & 0 deletions libs/metrics/src/exceptions/invalidChainId.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class InvalidChainId extends Error {
constructor(message: string) {
super(message);
this.name = "InvalidChainId";
}
}
6 changes: 6 additions & 0 deletions libs/metrics/src/exceptions/l1MetricsService.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class L1MetricsServiceException extends Error {
constructor(message: string) {
super(message);
this.name = "L1MetricsServiceException";
}
}
13 changes: 0 additions & 13 deletions libs/metrics/src/exceptions/provider.exception.ts

This file was deleted.

1 change: 1 addition & 0 deletions libs/metrics/src/l1/abis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./bridgeHub.abi";
export * from "./diamondProxy.abi";
export * from "./sharedBridge.abi";
export * from "./tokenBalances.abi";
89 changes: 66 additions & 23 deletions libs/metrics/src/l1/l1MetricsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import {
zeroAddress,
} from "viem";

import { L1ProviderException } from "@zkchainhub/metrics/exceptions/provider.exception";
import { bridgeHubAbi, sharedBridgeAbi } from "@zkchainhub/metrics/l1/abis";
import { tokenBalancesAbi } from "@zkchainhub/metrics/l1/abis/tokenBalances.abi";
import { InvalidChainId, L1MetricsServiceException } from "@zkchainhub/metrics/exceptions";
import {
bridgeHubAbi,
diamondProxyAbi,
sharedBridgeAbi,
tokenBalancesAbi,
} from "@zkchainhub/metrics/l1/abis";
import { tokenBalancesBytecode } from "@zkchainhub/metrics/l1/bytecode";
import { AssetTvl, GasInfo } from "@zkchainhub/metrics/types";
import { IPricingService, PRICING_PROVIDER } from "@zkchainhub/pricing";
import { EvmProviderService } from "@zkchainhub/providers";
import { AbiWithAddress, ChainId, L1_CONTRACTS, vitalikAddress } from "@zkchainhub/shared";
import { ETH_TOKEN_ADDRESS } from "@zkchainhub/shared/constants/addresses";
import { BatchesInfo, ChainId, L1_CONTRACTS, vitalikAddress } from "@zkchainhub/shared";
import { ETH_TOKEN_ADDRESS } from "@zkchainhub/shared/constants";
import {
erc20Tokens,
isNativeToken,
Expand All @@ -38,15 +42,15 @@ const ONE_ETHER = parseEther("1");
*/
@Injectable()
export class L1MetricsService {
private readonly bridgeHub: Readonly<AbiWithAddress> = {
private readonly bridgeHub = {
abi: bridgeHubAbi,
address: L1_CONTRACTS.BRIDGE_HUB,
};
private readonly sharedBridge: Readonly<AbiWithAddress> = {
private readonly sharedBridge = {
abi: sharedBridgeAbi,
address: L1_CONTRACTS.SHARED_BRIDGE,
};
private readonly diamondContracts: Map<ChainId, AbiWithAddress> = new Map();
private readonly diamondContracts: Map<ChainId, Address> = new Map();

constructor(
private readonly evmProviderService: EvmProviderService,
Expand Down Expand Up @@ -146,18 +150,58 @@ export class L1MetricsService {
return { ethBalance: balances[addresses.length]!, addressesBalance: balances.slice(0, -1) };
}

//TODO: Implement getBatchesInfo.
async getBatchesInfo(
_chainId: number,
): Promise<{ commited: number; verified: number; proved: number }> {
return { commited: 100, verified: 100, proved: 100 };
/**
* Retrieves the information about the batches from L2 chain
* @param chainId - The chain id for which to get the batches info
* @returns commits, verified and executed batches
*/
async getBatchesInfo(chainId: ChainId): Promise<BatchesInfo> {
let diamondProxyAddress: Address | undefined = this.diamondContracts.get(chainId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the PR but you got me wondering, why is the type of the diamondContracts keys (ie ChainId) a number? Any tech/business specific reason to not use bigints?

Copy link
Collaborator Author

@0xkenj1 0xkenj1 Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought there was no need for using bigints for chainId since they are not gona be huge numbers, but probably its better to keep types consistent with returned values from blockchain, i will change it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xkenj1 we can add this revisions to tech debt because i had the same thought as you, maybe the chainId argument can be a bigint directly too 🤔? (then we can remove the isInteger validation)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, we wont need that validation, i will fix this one here but will added it to linear to tackle it before moving to api integration


if (!diamondProxyAddress) {
diamondProxyAddress = await this.evmProviderService.readContract(
this.bridgeHub.address,
this.bridgeHub.abi,
"getHyperchain",
[chainId],
);
if (diamondProxyAddress == zeroAddress) {
throw new InvalidChainId(`Chain ID ${chainId} doesn't exist on the ecosystem`);
}
this.diamondContracts.set(chainId, diamondProxyAddress);
}

const [commited, verified, executed] = await this.evmProviderService.multicall({
contracts: [
{
address: diamondProxyAddress,
abi: diamondProxyAbi,
functionName: "getTotalBatchesCommitted",
args: [],
} as const,
{
address: diamondProxyAddress,
abi: diamondProxyAbi,
functionName: "getTotalBatchesVerified",
args: [],
} as const,
{
address: diamondProxyAddress,
abi: diamondProxyAbi,
functionName: "getTotalBatchesExecuted",
args: [],
} as const,
],
allowFailure: false,
});
return { commited, verified, executed };
}

/**
* Retrieves the Total Value Locked for {chainId} by L1 token
* @returns A Promise that resolves to an array of AssetTvl objects representing the TVL for each asset.
*/
async tvl(chainId: number): Promise<AssetTvl[]> {
async tvl(chainId: ChainId): Promise<AssetTvl[]> {
const erc20Addresses = erc20Tokens.map((token) => token.contractAddress);

const balances = await this.fetchTokenBalancesByChain(chainId, erc20Addresses);
Expand All @@ -176,23 +220,22 @@ export class L1MetricsService {
* @param addresses - An array of addresses for which to fetch the token balances.
* @returns A promise that resolves to an object containing the ETH balance and an array of address balances.
*/
private async fetchTokenBalancesByChain(chainId: number, addresses: Address[]) {
const chainIdBn = BigInt(chainId);
private async fetchTokenBalancesByChain(chainId: ChainId, addresses: Address[]) {
const balances = await this.evmProviderService.multicall({
contracts: [
...addresses.map((tokenAddress) => {
return {
address: this.sharedBridge.address,
abi: sharedBridgeAbi,
abi: this.sharedBridge.abi,
functionName: "chainBalance",
args: [chainIdBn, tokenAddress],
args: [chainId, tokenAddress],
} as const;
}),
{
address: this.sharedBridge.address,
abi: sharedBridgeAbi,
abi: this.sharedBridge.abi,
functionName: "chainBalance",
args: [chainIdBn, ETH_TOKEN_ADDRESS],
args: [chainId, ETH_TOKEN_ADDRESS],
} as const,
],
allowFailure: false,
Expand All @@ -202,7 +245,7 @@ export class L1MetricsService {
}

//TODO: Implement chainType.
async chainType(_chainId: number): Promise<"validium" | "rollup"> {
async chainType(_chainId: ChainId): Promise<"validium" | "rollup"> {
return "rollup";
}

Expand Down Expand Up @@ -254,12 +297,12 @@ export class L1MetricsService {
if (isNativeError(e)) {
this.logger.error(`Failed to get gas information: ${e.message}`);
}
throw new L1ProviderException("Failed to get gas information from L1.");
throw new L1MetricsServiceException("Failed to get gas information from L1.");
}
}

//TODO: Implement feeParams.
async feeParams(_chainId: number): Promise<{
async feeParams(_chainId: ChainId): Promise<{
batchOverheadL1Gas: number;
maxPubdataPerBatch: number;
maxL2GasPerBatch: number;
Expand Down
Loading