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: gas info #38

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 5 additions & 25 deletions libs/metrics/src/l1/l1MetricsService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { isNativeError } from "util/types";
import { Inject, Injectable, LoggerService } from "@nestjs/common";
import { WINSTON_MODULE_NEST_PROVIDER } from "nest-winston";
import {
encodeFunctionData,
erc20Abi,
formatGwei,
formatUnits,
parseEther,
zeroAddress,
} from "viem";
import { encodeFunctionData, erc20Abi, formatGwei, parseEther, zeroAddress } from "viem";

import { L1ProviderException } from "@zkchainhub/metrics/exceptions/provider.exception";
import { bridgeHubAbi, sharedBridgeAbi } from "@zkchainhub/metrics/l1/abis";
Expand All @@ -19,7 +12,6 @@ import { AbiWithAddress, ChainId, L1_CONTRACTS, vitalikAddress } from "@zkchainh
import { ETH, WETH } from "@zkchainhub/shared/tokens/tokens";

const ONE_ETHER = parseEther("1");
const ETHER_DECIMALS = 18;

/**
* Acts as a wrapper around Viem library to provide methods to interact with an EVM-based blockchain.
Expand Down Expand Up @@ -100,10 +92,10 @@ export class L1MetricsService {
}

return {
gasPriceInGwei: Number(formatGwei(gasPrice)),
ethPrice: ethPriceInUsd,
ethTransferGas: Number(ethTransferGasCost),
erc20TransferGas: Number(erc20TransferGasCost),
gasPriceInGwei: formatGwei(gasPrice),
ethPrice: ethPriceInUsd?.toString(),
ethTransferGas: ethTransferGasCost.toString(),
erc20TransferGas: erc20TransferGasCost.toString(),
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 just had a conversation with @0xnigir1 about this and talked about calling the .toString() method on the controller, separating:

  • Business logic on the service side by letting the service methods return the "raw" values
  • Handling the serialization (aka calling .toString()) on the controller side.

What do you think?

This way we avoid leaking controller's responsibilities into services, which might cause a bigint → string → bigint situation if some other part of the application wants to consume this service and use the raw bigint values.

Copy link
Collaborator

Choose a reason for hiding this comment

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

i am in. Yea, makes totally sense since metrics module will not interact with end users, our entry point for endusers will be the api , so makes totally sense to make serialization in there.

good catch @0xyaco

};
} catch (e: unknown) {
if (isNativeError(e)) {
Expand All @@ -113,18 +105,6 @@ export class L1MetricsService {
}
}

/**
* Calculates the transaction value in USD based on the gas used, gas price, and ether price.
* Formula: (txGas * gasPriceInWei/1e18) * etherPrice
* @param txGas - The amount of gas used for the transaction.
* @param gasPrice - The price of gas in wei.
* @param etherPrice - The current price of ether in USD.
* @returns The transaction value in USD.
*/
private transactionInUsd(txGas: bigint, gasPriceInWei: bigint, etherPrice: number): number {
return Number(formatUnits(txGas * gasPriceInWei, ETHER_DECIMALS)) * etherPrice;
}

//TODO: Implement feeParams.
async feeParams(_chainId: number): Promise<{
batchOverheadL1Gas: number;
Expand Down
8 changes: 4 additions & 4 deletions libs/metrics/src/types/gasInfo.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type GasInfo = {
gasPriceInGwei: number;
ethPrice?: number; // USD
ethTransferGas: number; // units of gas
erc20TransferGas: number; // units of gas
gasPriceInGwei: string;
ethPrice?: string; // USD
ethTransferGas: string; // units of gas
erc20TransferGas: string; // units of gas
};
14 changes: 7 additions & 7 deletions libs/metrics/test/unit/l1/l1MetricsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ describe("L1MetricsService", () => {
expect(mockGetTokenPrices).toHaveBeenCalledWith([ETH.coingeckoId]);

expect(result).toEqual({
gasPriceInGwei: 50,
ethPrice: 2000,
ethTransferGas: 21000,
erc20TransferGas: 65000,
gasPriceInGwei: "50",
ethPrice: "2000",
ethTransferGas: "21000",
erc20TransferGas: "65000",
});
});

Expand All @@ -175,10 +175,10 @@ describe("L1MetricsService", () => {

// Assertions
expect(result).toEqual({
gasPriceInGwei: 50,
gasPriceInGwei: "50",
ethPrice: undefined,
ethTransferGas: 21000,
erc20TransferGas: 65000,
ethTransferGas: "21000",
erc20TransferGas: "65000",
});
expect(mockEstimateGas).toHaveBeenCalledTimes(2);
expect(mockEstimateGas).toHaveBeenNthCalledWith(1, {
Expand Down