Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xkenj1 committed Jul 22, 2024
1 parent 0e3bfb0 commit 5580e92
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
5 changes: 3 additions & 2 deletions libs/providers/src/providers.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Module } from "@nestjs/common";

import { EvmProviderService } from "./providers";
import { ZKChainProviderService } from "./providers/zkChainProvider.service";

/**
* Module for managing provider services.
* This module exports Services for interacting with EVM-based blockchains.
*/
@Module({
providers: [EvmProviderService],
exports: [EvmProviderService],
providers: [EvmProviderService, ZKChainProviderService],
exports: [EvmProviderService, ZKChainProviderService],
})
export class ProvidersModule {}
2 changes: 1 addition & 1 deletion libs/providers/src/providers/evmProvider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class EvmProviderService {

/**
* Retrieves the current block number.
* @returns {Promise<bigint>} Latest block number.
* @returns {Promise<GetBlockReturnType>} Latest block number.
*/
async getBlockByNumber(blockNumber: number): Promise<GetBlockReturnType> {
return this.client.getBlock({ blockNumber: BigInt(blockNumber) });
Expand Down
1 change: 1 addition & 0 deletions libs/providers/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./evmProvider.service";
export * from "./zkChainProvider.service";
16 changes: 7 additions & 9 deletions libs/providers/src/providers/zkChainProvider.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,42 @@ describe("ZKChainProviderService", () => {

describe("tps", () => {
it("should return the transactions per second (TPS)", async () => {
const currentBatchNumber = "0x3e8"; // 1000 in hexadecimal
const currentBatchNumberNum = parseInt(currentBatchNumber, 16);
const currentBatchNumber = 1000; // 1000 in hexadecimal
const currentBatchDetails = { l2TxCount: 200, timestamp: 123234345 };
const prevBatchDetails = { timestamp: 123123123 };

jest.spyOn(zkProvider, "getL1BatchNumber").mockResolvedValue(currentBatchNumber);
jest.spyOn(zkProvider, "getL1BatchDetails").mockImplementation((batchNumber) => {
if (batchNumber === currentBatchNumberNum) {
if (batchNumber === currentBatchNumber) {
return Promise.resolve(
currentBatchDetails as unknown as GetL1BatchDetailsReturnType,
);
} else if (batchNumber === currentBatchNumberNum - 1) {
} else if (batchNumber === currentBatchNumber - 1) {
return Promise.resolve(
prevBatchDetails as unknown as GetL1BatchDetailsReturnType,
);
}
return Promise.reject(new Error("Block number not found"));
});
const tps = await zkProvider.tps();
expect(tps).toBe(0.002);
expect(tps).toBe(0.0017982053910197623);
expect(zkProvider.getL1BatchNumber).toHaveBeenCalled();
expect(zkProvider.getL1BatchDetails).toHaveBeenCalledWith(1000);
expect(zkProvider.getL1BatchDetails).toHaveBeenCalledWith(999);
});

it("should handle the case when there are no transactions", async () => {
const currentBatchNumber = "0x3e8"; // 1000 in hexadecimal
const currentBatchNumberNum = parseInt(currentBatchNumber, 16);
const currentBatchNumber = 1000; // 1000 in hexadecimal
const currentBatchDetails = { l2TxCount: 0, timestamp: 123234345 };
const prevBatchDetails = { timestamp: 123123123 };

jest.spyOn(zkProvider, "getL1BatchNumber").mockResolvedValue(currentBatchNumber);
jest.spyOn(zkProvider, "getL1BatchDetails").mockImplementation((batchNumber) => {
if (batchNumber === currentBatchNumberNum) {
if (batchNumber === currentBatchNumber) {
return Promise.resolve(
currentBatchDetails as unknown as GetL1BatchDetailsReturnType,
);
} else if (batchNumber === currentBatchNumberNum - 1) {
} else if (batchNumber === currentBatchNumber - 1) {
return Promise.resolve(
prevBatchDetails as unknown as GetL1BatchDetailsReturnType,
);
Expand Down
10 changes: 5 additions & 5 deletions libs/providers/src/providers/zkChainProvider.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from "@nestjs/common";
import { InvalidArgumentException } from "@packages/providers/exceptions";
import { EvmProviderService } from "@packages/providers/providers/evmProvider.service";
import { Chain, Client, createClient, Hex, http, HttpTransport } from "viem";
import { Chain, Client, createClient, http, HttpTransport } from "viem";
import { GetL1BatchDetailsReturnType, PublicActionsL2, publicActionsL2 } from "viem/zksync";

@Injectable()
Expand Down Expand Up @@ -29,8 +29,8 @@ export class ZKChainProviderService extends EvmProviderService {
* Retrieves the current L1 batch number.
* @returns Current L1 batch number.
*/
async getL1BatchNumber(): Promise<Hex> {
return this.zkClient.getL1BatchNumber();
async getL1BatchNumber(): Promise<number> {
return parseInt((await this.zkClient.getL1BatchNumber()).toString(), 16);
}

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ export class ZKChainProviderService extends EvmProviderService {
* @returns TPS value.
*/
async tps(): Promise<number> {
const currentBatchNumber = parseInt((await this.getL1BatchNumber()).toString(), 16);
const currentBatchNumber = await this.getL1BatchNumber();
const [currentBatch, prevBatch] = await Promise.all([
this.getL1BatchDetails(currentBatchNumber),
this.getL1BatchDetails(currentBatchNumber - 1),
Expand All @@ -67,6 +67,6 @@ export class ZKChainProviderService extends EvmProviderService {
const txCount = currentBatch.l2TxCount;
const blockTime = currentBatch.timestamp - prevBatch.timestamp;

return parseFloat((txCount / blockTime).toFixed(3));
return txCount / blockTime;
}
}

0 comments on commit 5580e92

Please sign in to comment.