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: add fallbacks rpc urls #47

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions libs/providers/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./invalidArgument.exception";
export * from "./dataDecode.exception";
export * from "./multicallNotFound.exception";
export * from "./rpcUrlsEmpty.exception";
6 changes: 6 additions & 0 deletions libs/providers/src/exceptions/rpcUrlsEmpty.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class RpcUrlsEmpty extends Error {
constructor() {
super("RPC URLs array cannot be empty");
this.name = "RpcUrlsEmpty";
}
}
15 changes: 12 additions & 3 deletions libs/providers/src/providers/evmProvider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
DecodeAbiParametersReturnType,
encodeDeployData,
EstimateGasParameters,
fallback,
FallbackTransport,
GetBlockReturnType,
Hex,
http,
Expand All @@ -28,6 +30,7 @@ import {
DataDecodeException,
InvalidArgumentException,
MulticallNotFound,
RpcUrlsEmpty,
} from "@zkchainhub/providers/exceptions";
import { AbiWithConstructor } from "@zkchainhub/providers/types";

Expand All @@ -36,16 +39,22 @@ import { AbiWithConstructor } from "@zkchainhub/providers/types";
*/
@Injectable()
export class EvmProviderService {
private client: ReturnType<typeof createPublicClient<HttpTransport, Chain>>;
private client: ReturnType<
typeof createPublicClient<FallbackTransport<HttpTransport[]>, Chain>
>;

constructor(
rpcUrl: string,
rpcUrls: string[],
readonly chain: Chain,
@Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService,
) {
if (rpcUrls.length === 0) {
throw new RpcUrlsEmpty();
}

this.client = createPublicClient({
chain,
transport: http(rpcUrl),
transport: fallback(rpcUrls.map((rpcUrl) => http(rpcUrl))),
});
}

Expand Down
27 changes: 22 additions & 5 deletions libs/providers/src/providers/zkChainProvider.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Inject, Injectable, LoggerService } from "@nestjs/common";
import { WINSTON_MODULE_NEST_PROVIDER } from "nest-winston";
import { Chain, Client, createClient, http, HttpTransport } from "viem";
import {
Chain,
Client,
createClient,
fallback,
FallbackTransport,
http,
HttpTransport,
} from "viem";
import { GetL1BatchDetailsReturnType, PublicActionsL2, publicActionsL2 } from "viem/zksync";

import { InvalidArgumentException } from "@zkchainhub/providers/exceptions";
Expand All @@ -11,15 +19,24 @@ import { EvmProviderService } from "@zkchainhub/providers/providers/evmProvider.
* Acts as a wrapper around Viem library to provide methods to interact with ZK chains.
*/
export class ZKChainProviderService extends EvmProviderService {
private zkClient: Client<HttpTransport, Chain, undefined, undefined, PublicActionsL2>;
private zkClient: Client<
FallbackTransport<HttpTransport[]>,
Chain,
undefined,
undefined,
PublicActionsL2
>;

constructor(
rpcUrl: string,
rpcUrls: string[],
chain: Chain,
@Inject(WINSTON_MODULE_NEST_PROVIDER) logger: LoggerService,
) {
super(rpcUrl, chain, logger);
this.zkClient = createClient({ chain, transport: http(rpcUrl) }).extend(publicActionsL2());
super(rpcUrls, chain, logger);
this.zkClient = createClient({
chain,
transport: fallback(rpcUrls.map((rpcUrl) => http(rpcUrl))),
}).extend(publicActionsL2());
}

/**
Expand Down
16 changes: 13 additions & 3 deletions libs/providers/test/unit/providers/evmProvider.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { localhost } from "viem/chains";
import { Logger } from "winston";

import { EvmProviderService } from "@zkchainhub/providers";
import { DataDecodeException, MulticallNotFound } from "@zkchainhub/providers/exceptions";
import {
DataDecodeException,
MulticallNotFound,
RpcUrlsEmpty,
} from "@zkchainhub/providers/exceptions";
import {
arrayAbiFixture,
structAbiFixture,
Expand Down Expand Up @@ -48,8 +52,8 @@ describe("EvmProviderService", () => {
{
provide: EvmProviderService,
useFactory: () => {
const rpcUrl = "http://localhost:8545";
return new EvmProviderService(rpcUrl, mockChain, mockLogger as Logger);
const rpcUrls = ["http://localhost:8545"];
return new EvmProviderService(rpcUrls, mockChain, mockLogger as Logger);
},
},
],
Expand All @@ -63,6 +67,12 @@ describe("EvmProviderService", () => {
jest.resetModules();
});

it("throws RpcUrlsEmpty error if rpcUrls is empty", () => {
expect(() => {
new EvmProviderService([], mockChain, mockLogger as Logger);
}).toThrowError(RpcUrlsEmpty);
});

describe("getBalance", () => {
it("should return the balance of the specified address", async () => {
const address = "0x123456789";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GetL1BatchDetailsReturnType } from "viem/zksync";
import { Logger } from "winston";

import { ZKChainProviderService } from "@zkchainhub/providers";
import { InvalidArgumentException } from "@zkchainhub/providers/exceptions";
import { InvalidArgumentException, RpcUrlsEmpty } from "@zkchainhub/providers/exceptions";

export const mockLogger: Partial<Logger> = {
log: jest.fn(),
Expand All @@ -28,9 +28,9 @@ describe("ZKChainProviderService", () => {
{
provide: ZKChainProviderService,
useFactory: () => {
const rpcUrl = "http://localhost:8545";
const rpcUrls = ["http://localhost:8545"];
const chain = localhost;
return new ZKChainProviderService(rpcUrl, chain, mockLogger as Logger);
return new ZKChainProviderService(rpcUrls, chain, mockLogger as Logger);
},
},
],
Expand All @@ -43,6 +43,12 @@ describe("ZKChainProviderService", () => {
jest.clearAllMocks();
});

it("throws RpcUrlsEmpty error if rpcUrls is empty", () => {
expect(() => {
new ZKChainProviderService([], localhost, mockLogger as Logger);
}).toThrowError(RpcUrlsEmpty);
});

describe("avgBlockTime", () => {
it("should return the average block time over the given range", async () => {
const currentBlockNumber = 1000;
Expand Down