From e4fb4624d82abd329895a1cb6e8afc0803397bd3 Mon Sep 17 00:00:00 2001 From: pcheremu Date: Thu, 7 Mar 2024 19:58:00 +0100 Subject: [PATCH] test: added providerCache for networks --- packages/integration-tests/src/constants.ts | 1 - packages/integration-tests/src/helper.ts | 21 +++++++------------ .../src/playbook/contracts/Caller.sol | 2 +- .../src/playbook/contracts/Middle.sol | 2 +- .../src/playbook/contracts/MyERC20.sol | 2 +- packages/integration-tests/src/provider.ts | 21 +++++++++++++++++++ .../tests/api/accounts.test.ts | 2 +- 7 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 packages/integration-tests/src/provider.ts diff --git a/packages/integration-tests/src/constants.ts b/packages/integration-tests/src/constants.ts index 1ad300052c..e46f4a17c5 100644 --- a/packages/integration-tests/src/constants.ts +++ b/packages/integration-tests/src/constants.ts @@ -51,7 +51,6 @@ export enum Logger { } export enum Token { - // CUST_Address = "0x0928008B245A76E105E02C522b5d309c0887ecA5",// probably unused customL2TokenName = "L2 ERC20 token", customL2TokenSymbol = "L2", customL2TokenDecimals = 18, diff --git a/packages/integration-tests/src/helper.ts b/packages/integration-tests/src/helper.ts index 9b599171ae..4c6cded977 100644 --- a/packages/integration-tests/src/helper.ts +++ b/packages/integration-tests/src/helper.ts @@ -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"; @@ -38,21 +40,12 @@ 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) { @@ -60,10 +53,10 @@ export class Helper { 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}`); } @@ -71,7 +64,7 @@ export class Helper { } async delay(ms: number) { - return new Promise((resolve) => setTimeout(resolve, ms)); + await setTimeout(ms); } async performBlockExplorerApiGetRequest(apiRoute: string) { diff --git a/packages/integration-tests/src/playbook/contracts/Caller.sol b/packages/integration-tests/src/playbook/contracts/Caller.sol index 7028ee5148..d8a7ca96ce 100644 --- a/packages/integration-tests/src/playbook/contracts/Caller.sol +++ b/packages/integration-tests/src/playbook/contracts/Caller.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: Unlicense +// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IGreeter2 { diff --git a/packages/integration-tests/src/playbook/contracts/Middle.sol b/packages/integration-tests/src/playbook/contracts/Middle.sol index 6a45f3e3f0..311d227246 100644 --- a/packages/integration-tests/src/playbook/contracts/Middle.sol +++ b/packages/integration-tests/src/playbook/contracts/Middle.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: Unlicense +// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IGreeter { diff --git a/packages/integration-tests/src/playbook/contracts/MyERC20.sol b/packages/integration-tests/src/playbook/contracts/MyERC20.sol index 5eb5a63d78..5821702080 100644 --- a/packages/integration-tests/src/playbook/contracts/MyERC20.sol +++ b/packages/integration-tests/src/playbook/contracts/MyERC20.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED +// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; diff --git a/packages/integration-tests/src/provider.ts b/packages/integration-tests/src/provider.ts new file mode 100644 index 0000000000..1a31d6f666 --- /dev/null +++ b/packages/integration-tests/src/provider.ts @@ -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]; +} diff --git a/packages/integration-tests/tests/api/accounts.test.ts b/packages/integration-tests/tests/api/accounts.test.ts index 819654f7e6..4ac7e0c211 100644 --- a/packages/integration-tests/tests/api/accounts.test.ts +++ b/packages/integration-tests/tests/api/accounts.test.ts @@ -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");