Skip to content

Commit

Permalink
test: add tentative fork testing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kkirka committed Dec 14, 2023
1 parent 5fb603f commit d097749
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/fork/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -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;
47 changes: 47 additions & 0 deletions tests/fork/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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<N extends Network> = { [T in N]: number };
export type Addresses<N extends Network> = 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 = <N extends Network>(config: ForkConfig<N>, fn: (addresses: Addresses<N>) => 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);
};

0 comments on commit d097749

Please sign in to comment.