From 1de2ffbab06578b9c35c89e9a05196f5c365c04f Mon Sep 17 00:00:00 2001 From: Debugger022 Date: Fri, 1 Dec 2023 19:37:51 +0530 Subject: [PATCH] refactor: update getBlockOrTimestampBasedDeploymentInfo() and removed redundancy --- deploy/007-deploy-pool-lens.ts | 2 +- deploy/009-deploy-vtokens.ts | 2 +- deploy/010-deploy-reward-distributors.ts | 2 +- deploy/014-riskfund-protocolshare.ts | 2 +- hardhat.config.ts | 13 ++++--------- helpers/deploymentConfig.ts | 8 +++++--- helpers/deploymentUtils.ts | 12 ++++++------ tests/hardhat/JumpRateModelV2.ts | 3 ++- tests/hardhat/Lens/PoolLens.ts | 4 ++-- tests/hardhat/Lens/RewardsSummary.ts | 5 +++-- tests/hardhat/PoolRegistry.ts | 8 -------- tests/hardhat/Rewards.ts | 5 +++-- tests/hardhat/Shortfall.ts | 5 +++-- tests/hardhat/UpgradedVToken.ts | 3 ++- tests/hardhat/WhitePaperInterestRateModel.ts | 3 ++- tests/hardhat/util/TokenTestHelpers.ts | 7 ++++--- 16 files changed, 40 insertions(+), 44 deletions(-) diff --git a/deploy/007-deploy-pool-lens.ts b/deploy/007-deploy-pool-lens.ts index 79c98a798..f5c564d01 100644 --- a/deploy/007-deploy-pool-lens.ts +++ b/deploy/007-deploy-pool-lens.ts @@ -8,7 +8,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const { deploy } = deployments; const { deployer } = await getNamedAccounts(); - const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre.network.config); + const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre); await deploy("PoolLens", { from: deployer, diff --git a/deploy/009-deploy-vtokens.ts b/deploy/009-deploy-vtokens.ts index 4a6834f40..c4ce1ff8c 100644 --- a/deploy/009-deploy-vtokens.ts +++ b/deploy/009-deploy-vtokens.ts @@ -20,7 +20,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const { deployer } = await getNamedAccounts(); const { tokensConfig, poolConfig, preconfiguredAddresses } = await getConfig(hre.network.name); - const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre.network.name); + const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre); const accessControlManagerAddress = await toAddress( preconfiguredAddresses.AccessControlManager || "AccessControlManager", diff --git a/deploy/010-deploy-reward-distributors.ts b/deploy/010-deploy-reward-distributors.ts index 21519b9dc..78e919cab 100644 --- a/deploy/010-deploy-reward-distributors.ts +++ b/deploy/010-deploy-reward-distributors.ts @@ -16,7 +16,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const maxLoopsLimit = 100; const { tokensConfig, poolConfig, preconfiguredAddresses } = await getConfig(hre.network.name); - const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre.network.name); + const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre); const accessControlAddress = await toAddress( preconfiguredAddresses.AccessControlManager || "AccessControlManager", diff --git a/deploy/014-riskfund-protocolshare.ts b/deploy/014-riskfund-protocolshare.ts index 49d75280c..b645ffee8 100644 --- a/deploy/014-riskfund-protocolshare.ts +++ b/deploy/014-riskfund-protocolshare.ts @@ -19,7 +19,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const { tokensConfig, preconfiguredAddresses } = await getConfig(hre.network.name); const usdt = await getUnderlyingToken("USDT", tokensConfig); - const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre.network.name); + const { isTimeBased, blocksPerYear } = getBlockOrTimestampBasedDeploymentInfo(hre); const poolRegistry = await ethers.getContract("PoolRegistry"); const deployerSigner = ethers.provider.getSigner(deployer); diff --git a/hardhat.config.ts b/hardhat.config.ts index a2ec719b5..a691fbfb4 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -179,22 +179,17 @@ const config: HardhatUserConfig = { url: process.env.ARCHIVE_NODE_sepolia || "https://ethereum-sepolia.blockpi.network/v1/rpc/public", chainId: 11155111, live: true, - timeout: 1200000, // 20 minutes + timeout: 1200000, // 20 minutes, + isTimeBased: false, accounts: process.env.DEPLOYER_PRIVATE_KEY ? [`0x${process.env.DEPLOYER_PRIVATE_KEY}`] : [], }, ethereum: { url: process.env.ARCHIVE_NODE_ethereum || "https://ethereum.blockpi.network/v1/rpc/public", chainId: 1, live: true, - timeout: 1200000, // 20 minutes - accounts: process.env.DEPLOYER_PRIVATE_KEY ? [`0x${process.env.DEPLOYER_PRIVATE_KEY}`] : [], - }, - sepolia: { - url: process.env.ARCHIVE_NODE_sepolia || "https://ethereum-sepolia.blockpi.network/v1/rpc/public", - chainId: 11155111, - live: true, - accounts: process.env.DEPLOYER_PRIVATE_KEY ? [`0x${process.env.DEPLOYER_PRIVATE_KEY}`] : [], + timeout: 1200000, // 20 minutes, isTimeBased: false, + accounts: process.env.DEPLOYER_PRIVATE_KEY ? [`0x${process.env.DEPLOYER_PRIVATE_KEY}`] : [], }, }, gasReporter: { diff --git a/helpers/deploymentConfig.ts b/helpers/deploymentConfig.ts index 66ccdd0d2..54fd9938f 100644 --- a/helpers/deploymentConfig.ts +++ b/helpers/deploymentConfig.ts @@ -30,7 +30,7 @@ export type DeploymentConfig = { export type DeploymentInfo = { isTimeBased: boolean; - blocksPerYear: string; + blocksPerYear: number; }; export type TokenConfig = { @@ -97,8 +97,9 @@ export enum InterestRateModels { const ANY_CONTRACT = ethers.constants.AddressZero; -const BSC_BLOCKS_PER_YEAR = 10_512_000; // assuming a block is mined every 3 seconds -const ETH_BLOCKS_PER_YEAR = 2_252_571; // assuming a block is mined every 14 seconds +export const BSC_BLOCKS_PER_YEAR = 10_512_000; // assuming a block is mined every 3 seconds +export const ETH_BLOCKS_PER_YEAR = 2_252_571; // assuming a block is mined every 14 seconds +export const SECONDS_PER_YEAR = 31_536_000; // seconds per year export type BlocksPerYear = { [key: string]: number; @@ -110,6 +111,7 @@ export const blocksPerYear: BlocksPerYear = { bscmainnet: BSC_BLOCKS_PER_YEAR, sepolia: ETH_BLOCKS_PER_YEAR, ethereum: ETH_BLOCKS_PER_YEAR, + isTimeBased: SECONDS_PER_YEAR, }; export const SEPOLIA_MULTISIG = "0x94fa6078b6b8a26f0b6edffbe6501b22a10470fb"; diff --git a/helpers/deploymentUtils.ts b/helpers/deploymentUtils.ts index 68097884d..6f9c0533a 100644 --- a/helpers/deploymentUtils.ts +++ b/helpers/deploymentUtils.ts @@ -8,6 +8,7 @@ import { RewardConfig, TokenConfig, VTokenConfig, + blocksPerYear, getTokenConfig, } from "./deploymentConfig"; @@ -134,10 +135,9 @@ export const getUnregisteredRewardsDistributors = async ( ); }; -export const getBlockOrTimestampBasedDeploymentInfo = (config: any): DeploymentInfo => { - const isTimeBased = config.isTimeBased; - if (isTimeBased) { - return { isTimeBased: true, blocksPerYear: "31536000" }; - } - return { isTimeBased: false, blocksPerYear: "10512000" }; +export const getBlockOrTimestampBasedDeploymentInfo = (hre: any): DeploymentInfo => { + const isTimeBased = hre.network.config.isTimeBased; + const blocksPerYearKey = isTimeBased ? "isTimeBased" : hre.network.name; + + return { isTimeBased: isTimeBased, blocksPerYear: blocksPerYear[blocksPerYearKey] }; }; diff --git a/tests/hardhat/JumpRateModelV2.ts b/tests/hardhat/JumpRateModelV2.ts index 4e622d08f..3db11c278 100644 --- a/tests/hardhat/JumpRateModelV2.ts +++ b/tests/hardhat/JumpRateModelV2.ts @@ -4,6 +4,7 @@ import BigNumber from "bignumber.js"; import chai from "chai"; import { ethers } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../helpers/deploymentConfig"; import { convertToUnit } from "../../helpers/utils"; import { AccessControlManager, JumpRateModelV2 } from "../../typechain"; import { getDescription } from "./util/descriptionHelpers"; @@ -26,7 +27,7 @@ for (const isTimeBased of [false, true]) { const jumpMultiplierPerYear = convertToUnit(2, 18); const description = getDescription(isTimeBased); - const slotsPerYear = isTimeBased ? 31536000 : 10512000; + const slotsPerYear = isTimeBased ? SECONDS_PER_YEAR : BSC_BLOCKS_PER_YEAR; describe(`${description}Jump rate model tests`, async () => { const fixture = async () => { diff --git a/tests/hardhat/Lens/PoolLens.ts b/tests/hardhat/Lens/PoolLens.ts index 4ca60d05f..82711bc85 100644 --- a/tests/hardhat/Lens/PoolLens.ts +++ b/tests/hardhat/Lens/PoolLens.ts @@ -5,7 +5,7 @@ import { BigNumberish, Signer } from "ethers"; import { parseUnits } from "ethers/lib/utils"; import { ethers, upgrades } from "hardhat"; -import { blocksPerYear } from "../../../helpers/deploymentConfig"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../../helpers/deploymentConfig"; import { AccessControlManager, Beacon, @@ -48,7 +48,7 @@ const assertVTokenMetadata = (vTokenMetadataActual: any, vTokenMetadataExpected: for (const isTimeBased of [false, true]) { const description = getDescription(isTimeBased); - const slotsPerYear = isTimeBased ? 31536000 : 10512000; + const slotsPerYear = isTimeBased ? SECONDS_PER_YEAR : BSC_BLOCKS_PER_YEAR; describe(`${description}PoolLens`, async () => { let poolRegistry: PoolRegistry; diff --git a/tests/hardhat/Lens/RewardsSummary.ts b/tests/hardhat/Lens/RewardsSummary.ts index 047bbcb47..7cf08c11c 100644 --- a/tests/hardhat/Lens/RewardsSummary.ts +++ b/tests/hardhat/Lens/RewardsSummary.ts @@ -4,6 +4,7 @@ import chai from "chai"; import { BigNumber, Signer } from "ethers"; import { ethers } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../../helpers/deploymentConfig"; import { convertToUnit } from "../../../helpers/utils"; import { Comptroller, MockToken, PoolLens, PoolLens__factory, RewardsDistributor, VToken } from "../../../typechain"; import { getDescription } from "../util/descriptionHelpers"; @@ -27,7 +28,7 @@ let poolLens: MockContract; let account: Signer; let startBlock: number; let isTimeBased = false; // for block based contracts -let blocksPerYear = 10512000; // for block based contracts +let blocksPerYear = BSC_BLOCKS_PER_YEAR; // for block based contracts type RewardsFixtire = { comptroller: FakeContract; @@ -163,7 +164,7 @@ const timeBasedRewardsFixture = async (): Promise => { const poolLensFactory = await smock.mock("PoolLens"); isTimeBased = true; - blocksPerYear = 31536000; + blocksPerYear = SECONDS_PER_YEAR; poolLens = await poolLensFactory.deploy(isTimeBased, blocksPerYear); const startBlock = (await ethers.provider.getBlock("latest")).number; diff --git a/tests/hardhat/PoolRegistry.ts b/tests/hardhat/PoolRegistry.ts index 2a373ae89..37f1bceae 100644 --- a/tests/hardhat/PoolRegistry.ts +++ b/tests/hardhat/PoolRegistry.ts @@ -145,8 +145,6 @@ describe("PoolRegistry: Tests", function () { initialExchangeRateMantissa: parseUnits("1", 18), admin: owner.address, beacon: vTokenBeacon, - isTimeBased: false, - blocksPerYear: 10512000, }); await mockWBTC.faucet(INITIAL_SUPPLY); @@ -169,8 +167,6 @@ describe("PoolRegistry: Tests", function () { initialExchangeRateMantissa: parseUnits("1", 18), admin: owner.address, beacon: vTokenBeacon, - isTimeBased: false, - blocksPerYear: 10512000, }); await mockDAI.faucet(INITIAL_SUPPLY); @@ -193,8 +189,6 @@ describe("PoolRegistry: Tests", function () { initialExchangeRateMantissa: parseUnits("1", 28), // underlying.decimals + 18 - vToken.decimals admin: owner.address, beacon: vTokenBeacon, - isTimeBased: false, - blocksPerYear: 10512000, }); // Enter Markets @@ -376,8 +370,6 @@ describe("PoolRegistry: Tests", function () { initialExchangeRateMantissa: parseUnits("1", 28), // underlying.decimals + 18 - vToken.decimals admin: user.address, beacon: vTokenBeacon, - isTimeBased: false, - blocksPerYear: 10512000, }); await feeToken.allocateTo(user.address, INITIAL_SUPPLY); diff --git a/tests/hardhat/Rewards.ts b/tests/hardhat/Rewards.ts index 3f544e704..20b6062c6 100644 --- a/tests/hardhat/Rewards.ts +++ b/tests/hardhat/Rewards.ts @@ -5,6 +5,7 @@ import { expect } from "chai"; import { parseUnits } from "ethers/lib/utils"; import { ethers, upgrades } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../helpers/deploymentConfig"; import { convertToUnit } from "../../helpers/utils"; import { AccessControlManager, @@ -36,7 +37,7 @@ let xvs: MockToken; let fakePriceOracle: FakeContract; let fakeAccessControlManager: FakeContract; const maxLoopsLimit = 150; -let blocksPerYear = 10512000; // for block based contracts +let blocksPerYear = BSC_BLOCKS_PER_YEAR; // for block based contracts async function rewardsFixture(isTimeBased: boolean) { [root] = await ethers.getSigners(); @@ -98,7 +99,7 @@ async function rewardsFixture(isTimeBased: boolean) { ); if (isTimeBased) { - blocksPerYear = 31536000; + blocksPerYear = SECONDS_PER_YEAR; } // Deploy VTokens diff --git a/tests/hardhat/Shortfall.ts b/tests/hardhat/Shortfall.ts index bc3041991..2952333a6 100644 --- a/tests/hardhat/Shortfall.ts +++ b/tests/hardhat/Shortfall.ts @@ -7,6 +7,7 @@ import { constants } from "ethers"; import { parseUnits } from "ethers/lib/utils"; import { ethers, upgrades } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../helpers/deploymentConfig"; import { AddressOne, convertToUnit } from "../../helpers/utils"; import { AccessControlManager, @@ -48,7 +49,7 @@ let fakePriceOracle: FakeContract; const riskFundBalance = "10000"; const minimumPoolBadDebt = "10000"; let isTimeBased = false; // for block based contracts -let blocksPerYear = 10512000; // for block based contracts +let blocksPerYear = BSC_BLOCKS_PER_YEAR; // for block based contracts let poolAddress; /** @@ -182,7 +183,7 @@ async function shortfallFixture() { async function timeBasedhortfallFixture() { isTimeBased = true; - blocksPerYear = 31536000; + blocksPerYear = SECONDS_PER_YEAR; const MockBUSD = await ethers.getContractFactory("MockToken"); mockBUSD = await MockBUSD.deploy("BUSD", "BUSD", 18); await mockBUSD.faucet(convertToUnit(100000, 18)); diff --git a/tests/hardhat/UpgradedVToken.ts b/tests/hardhat/UpgradedVToken.ts index 2ed11634f..1d7171ce3 100644 --- a/tests/hardhat/UpgradedVToken.ts +++ b/tests/hardhat/UpgradedVToken.ts @@ -3,6 +3,7 @@ import { expect } from "chai"; import { parseUnits } from "ethers/lib/utils"; import { ethers, upgrades } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../helpers/deploymentConfig"; import { convertToUnit } from "../../helpers/utils"; import { AccessControlManager, @@ -27,7 +28,7 @@ let fakeAccessControlManager: FakeContract; for (const isTimeBased of [false, true]) { const description = getDescription(isTimeBased); - const slotsPerYear = isTimeBased ? 31536000 : 10512000; + const slotsPerYear = isTimeBased ? SECONDS_PER_YEAR : BSC_BLOCKS_PER_YEAR; describe(`${description}UpgradedVToken: Tests`, function () { /** diff --git a/tests/hardhat/WhitePaperInterestRateModel.ts b/tests/hardhat/WhitePaperInterestRateModel.ts index fa41e6d6a..07caa4cb6 100644 --- a/tests/hardhat/WhitePaperInterestRateModel.ts +++ b/tests/hardhat/WhitePaperInterestRateModel.ts @@ -4,6 +4,7 @@ import BigNumber from "bignumber.js"; import chai from "chai"; import { ethers } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR, SECONDS_PER_YEAR } from "../../helpers/deploymentConfig"; import { convertToUnit } from "../../helpers/utils"; import { WhitePaperInterestRateModel } from "../../typechain"; import { getDescription } from "./util/descriptionHelpers"; @@ -21,7 +22,7 @@ for (const isTimeBased of [false, true]) { const baseRatePerYear = convertToUnit(2, 12); const multiplierPerYear = convertToUnit(4, 14); const description: string = getDescription(isTimeBased); - const slotsPerYear = isTimeBased ? 31536000 : 10512000; + const slotsPerYear = isTimeBased ? SECONDS_PER_YEAR : BSC_BLOCKS_PER_YEAR; describe(`${description}White Paper interest rate model tests`, () => { const fixture = async () => { diff --git a/tests/hardhat/util/TokenTestHelpers.ts b/tests/hardhat/util/TokenTestHelpers.ts index bc2e6bf34..8ec6699a0 100644 --- a/tests/hardhat/util/TokenTestHelpers.ts +++ b/tests/hardhat/util/TokenTestHelpers.ts @@ -4,6 +4,7 @@ import { BigNumber, BigNumberish, Signer } from "ethers"; import { parseUnits } from "ethers/lib/utils"; import { ethers, upgrades } from "hardhat"; +import { BSC_BLOCKS_PER_YEAR } from "../../../helpers/deploymentConfig"; import { AccessControlManager, Comptroller, @@ -74,7 +75,7 @@ export type AnyVTokenFactory = VTokenHarness__factory | VToken__factory; export const deployVTokenBeacon = async ( { kind }: { kind: string } = { kind: "VToken" }, isTimeBased: boolean = false, - blocksPerYear: BigNumberish = 10512000, + blocksPerYear: BigNumberish = BSC_BLOCKS_PER_YEAR, ): Promise => { const VToken = await ethers.getContractFactory(kind); const vTokenBeacon = (await upgrades.deployBeacon(VToken, { @@ -110,10 +111,10 @@ const deployVTokenDependencies = async ( { kind }, params.isTimeBased || false, - params.blocksPerYear || 10512000, + params.blocksPerYear || BSC_BLOCKS_PER_YEAR, )), isTimeBased: params.isTimeBased || false, - blocksPerYear: params.blocksPerYear || 10512000, + blocksPerYear: params.blocksPerYear || BSC_BLOCKS_PER_YEAR, }; };