Skip to content

Commit

Permalink
fix: nativeTokenGateway, liquidation and reduceReserves fork tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Debugger022 committed May 2, 2024
1 parent b022b57 commit a47277f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
19 changes: 3 additions & 16 deletions tests/hardhat/Fork/NativeTokenGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import { Comptroller, ERC20, NativeTokenGateway, VToken } from "../../../typecha
import { initMainnetUser, setForkBlock } from "./utils";

const ADMIN = "0x285960C5B22fD66A736C7136967A3eB15e93CC67";
const COMPTROLLER_BEACON = "0xAE2C3F21896c02510aA187BdA0791cDA77083708";
const VTOKEN_BEACON = "0xfc08aADC7a1A93857f6296C3fb78aBA1d286533a";
const COMPTROLLER_ADDRESS = "0x687a01ecF6d3907658f7A7c714749fAC32336D1B";
const POOL_REGISTRY = "0x61CAff113CCaf05FFc6540302c37adcf077C5179";
const VWETH = "0x7c8ff7d2A1372433726f879BD945fFb250B94c65";
const USDT = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
const VUSDT = "0x8C3e3821259B82fFb32B2450A95d2dcbf161C24E";

const USER1 = "0xf89d7b9c864f589bbF53a82105107622B35EaA40";
const USER2 = "0x974CaA59e49682CdA0AD2bbe82983419A2ECC400";
const BLOCK_NUMBER = 19139865;
const BLOCK_NUMBER = 19781700;

async function configureTimeLock() {
impersonatedTimeLock = await initMainnetUser(ADMIN, ethers.utils.parseUnits("2"));
Expand All @@ -44,16 +41,6 @@ async function setup() {
usdt = await ethers.getContractAt("@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20", USDT);

const comptroller = await ethers.getContractAt("Comptroller", COMPTROLLER_ADDRESS);
const newComptrollerFactory = await ethers.getContractFactory("Comptroller");
const newComptrollerImplementation = await newComptrollerFactory.deploy(POOL_REGISTRY);
const beaconContract = await ethers.getContractAt("UpgradeableBeacon", COMPTROLLER_BEACON);

const newVToken = await ethers.getContractFactory("VToken");
const newVTokenImplementation = await newVToken.deploy();
const vTokenBeaconContract = await ethers.getContractAt("UpgradeableBeacon", VTOKEN_BEACON);

await beaconContract.connect(impersonatedTimeLock).upgradeTo(newComptrollerImplementation.address);
await vTokenBeaconContract.connect(impersonatedTimeLock).upgradeTo(newVTokenImplementation.address);

vusdt = await ethers.getContractAt("VToken", VUSDT);
vweth = await ethers.getContractAt("VToken", VWETH);
Expand Down Expand Up @@ -94,7 +81,7 @@ if (FORK && FORKED_NETWORK === "ethereum") {
const balanceAfterSupplying = await vweth.balanceOf(await user1.getAddress());
await expect(balanceAfterSupplying.sub(balanceBeforeSupplying).toString()).to.closeTo(
parseUnits("10", 8),
parseUnits("1", 5),
parseUnits("1", 7),
);
await expect(tx).to.changeEtherBalances([user1], [supplyAmount.mul(-1)]);
});
Expand All @@ -115,7 +102,7 @@ if (FORK && FORKED_NETWORK === "ethereum") {

await expect(ethBalanceAfter.sub(ethBalanceBefore)).to.closeTo(redeemAmount, parseUnits("1", 16));

expect(await vweth.balanceOf(await user1.getAddress())).to.eq(0);
expect(await vweth.balanceOf(await user1.getAddress())).to.closeTo(0, 10);
});
});

Expand Down
25 changes: 13 additions & 12 deletions tests/hardhat/Fork/liquidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,24 @@ if (FORK) {
const liquidatorSeizeTokens = Math.floor(seizeTokens - protocolSeizeTokens);
const reserveIncrease = (protocolSeizeTokens * exchangeRateCollateralNew) / 1e18;
const borrowBalanceNew = await vTOKEN2.borrowBalanceStored(ACC2);
expect(borrowBalancePrev - maxClose).closeTo(borrowBalanceNew, 100);
expect(borrowBalancePrev.sub(maxClose)).closeTo(borrowBalanceNew, 200);

expect(vTOKEN1BalAcc2Prev - vTOKEN1BalAcc2New).to.closeTo(Math.floor(seizeTokens), 100);
expect(vTOKEN1BalAcc2Prev.sub(vTOKEN1BalAcc2New)).to.closeTo(Math.floor(seizeTokens), 1000);

expect(vTOKEN1BalAcc1New - vTOKEN1BalAcc1Prev).to.closeTo(liquidatorSeizeTokens, 100);
expect(vTOKEN1BalAcc1New.sub(vTOKEN1BalAcc1Prev)).to.closeTo(liquidatorSeizeTokens, 1000);
const psrBalanceNew = await token1.balanceOf(PSR);

const psrAndreservesSumNew = psrBalanceNew.add(totalReservesToken1New);
const difference = psrAndreservesSumNew.sub(psrAndreservesSumPrev);

expect(difference.toString()).to.closeTo(reserveIncrease.toString(), parseUnits("0.00003", 18));
expect(difference.toString()).to.closeTo(reserveIncrease.toString(), parseUnits("0.03", 18));
});
});

describe("Liquidate from Comptroller", async () => {
const mintAmount: BigNumberish = convertToUnit(1, 15);
const token2BorrowAmount: BigNumberish = convertToUnit(1, 15);

beforeEach(async () => {
await setupBeforeEach(mintAmount, token2BorrowAmount);
});
Expand Down Expand Up @@ -352,10 +353,10 @@ if (FORK) {

// repayAmount will be calculated after accruing interest and then using borrowBalanceStored to get the repayAmount.
const NetworkRespectiveRepayAmounts = {
bsctestnet: 1000000047610436,
sepolia: 1000000137700483,
bscmainnet: 1000000034788981,
ethereum: 1000000076103691,
bsctestnet: 1000000048189327,
sepolia: 1000000138102913,
bscmainnet: 1000000020807824,
ethereum: 1000000262400462,
};

const repayAmount = NetworkRespectiveRepayAmounts[FORKED_NETWORK];
Expand All @@ -378,11 +379,11 @@ if (FORK) {
const protocolSeizeTokens = Math.floor((seizeTokens * 5) / 100);
const reserveIncrease = (protocolSeizeTokens * exchangeRateCollateralNew) / 1e18;

expect(vTOKEN1BalAcc2Prev - vTOKEN1BalAcc2New).to.closeTo(Math.floor(seizeTokens), 100);
expect(vTOKEN1BalAcc1New - vTOKEN1BalAcc1Prev).to.closeTo(liquidatorSeizeTokens, 1);
expect(totalReservesToken1New - totalReservesToken1Prev).to.closeTo(
expect(vTOKEN1BalAcc2Prev.sub(vTOKEN1BalAcc2New)).to.closeTo(Math.floor(seizeTokens), 100);
expect(vTOKEN1BalAcc1New.sub(vTOKEN1BalAcc1Prev)).to.closeTo(liquidatorSeizeTokens, 1);
expect(totalReservesToken1New.sub(totalReservesToken1Prev)).to.closeTo(
Math.round(reserveIncrease),
parseUnits("0.0003", 18),
parseUnits("1", 17),
);
});
});
Expand Down
6 changes: 4 additions & 2 deletions tests/hardhat/Fork/reduceReservesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { smock } from "@defi-wonderland/smock";
import { mine } from "@nomicfoundation/hardhat-network-helpers";
import chai from "chai";
import { BigNumber, BigNumberish, Signer } from "ethers";
import { parseUnits } from "ethers/lib/utils";
import { ethers } from "hardhat";

import { convertToUnit } from "../../../helpers/utils";
Expand Down Expand Up @@ -161,9 +160,12 @@ if (FORK) {
const reduceAmount = totalReservesOld.mul(50).div(100);
const protocolShareBalanceOld = await token1.balanceOf(PSR);

const psrBalanceBefore = await token1.balanceOf(PSR);
await vTOKEN1.reduceReserves(reduceAmount);
const psrBalanceAfter = await token1.balanceOf(PSR);

totalReservesCurrent = await vTOKEN1.totalReserves();
expect(totalReservesOld.sub(totalReservesCurrent)).to.closeTo(reduceAmount, parseUnits("0.0000002", 19));
expect(psrBalanceAfter.sub(psrBalanceBefore)).to.be.equal(reduceAmount);

const protocolShareBalanceNew = await token1.balanceOf(PSR);
expect(protocolShareBalanceNew.sub(protocolShareBalanceOld)).equals(reduceAmount);
Expand Down

0 comments on commit a47277f

Please sign in to comment.