From d097749f0f35a228f50f85cedaaaad0234af423f Mon Sep 17 00:00:00 2001 From: Kirill Kuvshinov Date: Thu, 14 Dec 2023 15:11:23 +0300 Subject: [PATCH] test: add tentative fork testing utils --- tests/fork/utils/constants.ts | 13 ++++++++++ tests/fork/utils/index.ts | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/fork/utils/constants.ts create mode 100644 tests/fork/utils/index.ts diff --git a/tests/fork/utils/constants.ts b/tests/fork/utils/constants.ts new file mode 100644 index 00000000..eb971402 --- /dev/null +++ b/tests/fork/utils/constants.ts @@ -0,0 +1,13 @@ +export const ADDRESSES = { + bscmainnet: { + BUSD: "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56", + USDT: "0x55d398326f99059fF775485246999027B3197955", + PancakeSwapRouter: "0x13f4EA83D0bd40E75C8222255bc855a974568Dd4", + MoveDebtDelegate: "0x89621C48EeC04A85AfadFD37d32077e65aFe2226", + Unitroller: "0xfD36E2c2a6789Db23113685031d7F16329158384", + NormalTimelock: "0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396", + vUSDT: "0xfD5840Cd36d94D7229439859C0112a4185BC0255", + vBUSD: "0x95c78222B3D6e262426483D42CfA53685A67Ab9D", + }, + bsctestnet: {}, +} as const; diff --git a/tests/fork/utils/index.ts b/tests/fork/utils/index.ts new file mode 100644 index 00000000..983b4891 --- /dev/null +++ b/tests/fork/utils/index.ts @@ -0,0 +1,47 @@ +import { impersonateAccount, setBalance } from "@nomicfoundation/hardhat-network-helpers"; +import { NumberLike } from "@nomicfoundation/hardhat-network-helpers/dist/src/types"; +import { ethers } from "hardhat"; +import hre from "hardhat"; + +import { ADDRESSES } from "./constants"; + +export type Network = keyof typeof ADDRESSES; +export type ForkConfig = { [T in N]: number }; +export type Addresses = typeof ADDRESSES[N]; + +export const resetFork = async (network: Network, blockNumber: number) => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: process.env[`ARCHIVE_NODE_${network}`], + blockNumber, + }, + }, + ], + }); +}; + +export const forking = (config: ForkConfig, fn: (addresses: Addresses) => void) => { + if (!process.env.FORK || process.env.FORK === "false") { + return; + } + const config_ = Object.entries(config) as [N, number][]; + config_.forEach(([network, blockNumber]) => { + describe(`Forking ${network} at block #${blockNumber}`, () => { + before(async () => { + await resetFork(network, blockNumber); + }); + fn(ADDRESSES[network]); + }); + }); +}; + +export const initMainnetUser = async (user: string, balance?: NumberLike) => { + await impersonateAccount(user); + if (balance !== undefined) { + await setBalance(user, balance); + } + return ethers.getSigner(user); +};