Skip to content

Commit

Permalink
test: added providerCache for networks
Browse files Browse the repository at this point in the history
  • Loading branch information
pcheremu committed Mar 7, 2024
1 parent 3aa7bd8 commit e4fb462
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/integration-tests/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export enum Logger {
}

export enum Token {
// CUST_Address = "0x0928008B245A76E105E02C522b5d309c0887ecA5",// probably unused
customL2TokenName = "L2 ERC20 token",
customL2TokenSymbol = "L2",
customL2TokenDecimals = 18,
Expand Down
21 changes: 7 additions & 14 deletions packages/integration-tests/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { ethers } from "ethers";
import { promises as fs } from "fs";
import * as path from "path";
import * as request from "supertest";
import { setTimeout } from "timers/promises";
import { Provider } from "zksync-web3";

import { environment, localConfig } from "./config";
import { Logger } from "./constants";
import { getProviderForL1, getProviderForL2 } from "./provider";

import type { BaseProvider } from "@ethersproject/providers/src.ts/base-provider";

Expand Down Expand Up @@ -38,40 +40,31 @@ export class Helper {

async writeFile(filePath: string, fileName: string, data: string) {
const absoluteRoute = path.join(filePath, fileName);
try {
await fs.writeFile(absoluteRoute, data);
} catch {
console.log(`Cannot write: ${fileName} to ${filePath}`);
}
await fs.writeFile(absoluteRoute, data);
}

async readFile(filePath: string, fileName: string) {
const absoluteRoute = path.join(filePath, fileName);

try {
return await fs.readFile(absoluteRoute, { encoding: "utf-8" });
} catch {
console.log(`There is no the expected file: ${fileName} in ${filePath}`);
}
return await fs.readFile(absoluteRoute, { encoding: "utf-8" });
}

async getBalanceETH(walletAddress: string, layer: string) {
let network: string;
let provider: BaseProvider;
if (layer == "L1") {
network = localConfig.L1Network;
provider = ethers.getDefaultProvider(network);
provider = getProviderForL1(network);
} else if (layer == "L2") {
network = localConfig.L2Network;
provider = new Provider(network);
provider = getProviderForL2(network);
} else {
console.log(`Wrong layer: ${layer}`);
}
return ethers.utils.formatUnits(await provider.getBalance(walletAddress), "wei");
}

async delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
await setTimeout(ms);
}

async performBlockExplorerApiGetRequest(apiRoute: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: Unlicense
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IGreeter2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: Unlicense
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IGreeter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

Expand Down
21 changes: 21 additions & 0 deletions packages/integration-tests/src/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ethers } from "ethers";
import { Provider } from "zksync-web3";

import type { BaseProvider } from "@ethersproject/providers/src.ts/base-provider";

const providerCacheL1: { [key: string]: BaseProvider } = {};
const providerCacheL2: { [key: string]: Provider } = {};

export function getProviderForL2(network: string): Provider {
if (!providerCacheL2[network]) {
providerCacheL2[network] = new Provider(network);
}
return providerCacheL2[network];
}

export function getProviderForL1(network: string): BaseProvider {
if (!providerCacheL1[network]) {
providerCacheL1[network] = ethers.getDefaultProvider(network);
}
return providerCacheL1[network];
}
2 changes: 1 addition & 1 deletion packages/integration-tests/tests/api/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("API module: Account", () => {
expect(typeof response.body.result[0].isError).toStrictEqual("string");
expect(typeof response.body.result[0].txreceipt_status).toStrictEqual("string");
expect(typeof response.body.result[0].input).toStrictEqual("string");
expect(typeof response.body.result[0].contractAddress).toBeTruthy(); // can be null
expect(typeof response.body.result[0].contractAddress).toBeTruthy();
expect(typeof response.body.result[0].cumulativeGasUsed).toStrictEqual("string");
expect(typeof response.body.result[0].gasUsed).toStrictEqual("string");
expect(typeof response.body.result[0].confirmations).toStrictEqual("string");
Expand Down

0 comments on commit e4fb462

Please sign in to comment.