Skip to content

Commit

Permalink
feat(flash-swap): add test for repay underlying equal to seized under…
Browse files Browse the repository at this point in the history
…lying

feat(constants): add swap repay liquidation incentive
  • Loading branch information
scorpion9979 committed Nov 5, 2021
1 parent d86fc14 commit 6ebee5b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/constants/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export const H_TOKEN_MATURITY_THREE_MONTHS: BigNumber = getNow().add(getDaysInSe
export const LIQUIDATION_INCENTIVES = {
default: toBn("1.10"),
lowerBound: toBn("1.00"),
swapRepay: toBn("1.003009027081243731"),
upperBound: toBn("1.50"),
};
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,66 @@ export function shouldBehaveLikeUniswapV2Call(): void {
const oneHourAgo: BigNumber = getNow().sub(3600);
await this.contracts.hToken.connect(this.signers.admin).__godMode_setMaturity(oneHourAgo);
});
// context("when the repay underlying amount is equal to the seized underlying amount", function () {});

context("when the repay underlying amount is equal to the seized underlying amount", function () {
let expectedProfitUnderlyingAmount: BigNumber;
let expectedRepayUnderlyingAmount: BigNumber;
let seizableUnderlyingAmount: BigNumber;

beforeEach(async function () {
// Mint 0.3% more USDC.
const addedUnderlyingAmount: BigNumber = feeUnderlyingAmount;
await this.contracts.usdc.__godMode_mint(this.signers.borrower.address, addedUnderlyingAmount);

// Deposit the USDC in the vault.
await this.contracts.balanceSheet
.connect(this.signers.borrower)
.depositCollateral(this.contracts.usdc.address, addedUnderlyingAmount);

// Set the liquidation incentive to 0.3%.
await this.contracts.fintroller
.connect(this.signers.admin)
.setLiquidationIncentive(this.contracts.usdc.address, LIQUIDATION_INCENTIVES.swapRepay);

// Calculate the amounts necessary for running the tests.
expectedRepayUnderlyingAmount = swapUnderlyingAmount.mul(1000).div(997).add(1);
seizableUnderlyingAmount = await this.contracts.balanceSheet.getSeizableCollateralAmount(
this.contracts.hToken.address,
repayHTokenAmount,
this.contracts.usdc.address,
);
expectedProfitUnderlyingAmount = seizableUnderlyingAmount.sub(expectedRepayUnderlyingAmount);
});

it("flash swaps USDC via and makes no USDC profit", async function () {
const to: string = this.contracts.underlyingFlashUniswapV2.address;
const oldUsdcBalance = await this.contracts.usdc.balanceOf(this.signers.liquidator.address);
await this.contracts.uniswapV2Pair
.connect(this.signers.liquidator)
.swap(token0Amount, token1Amount, to, data);
const newUsdcBalance = await this.contracts.usdc.balanceOf(this.signers.liquidator.address);
expect(newUsdcBalance).to.equal(oldUsdcBalance);
});

it("emits a FlashSwapUnderlyingAndLiquidateBorrow event", async function () {
const to: string = this.contracts.underlyingFlashUniswapV2.address;
const contractCall = this.contracts.uniswapV2Pair
.connect(this.signers.liquidator)
.swap(token0Amount, token1Amount, to, data);
await expect(contractCall)
.to.emit(this.contracts.underlyingFlashUniswapV2, "FlashSwapUnderlyingAndLiquidateBorrow")
.withArgs(
this.signers.liquidator.address,
this.signers.borrower.address,
this.contracts.hToken.address,
swapUnderlyingAmount,
seizableUnderlyingAmount,
expectedRepayUnderlyingAmount,
Zero,
expectedProfitUnderlyingAmount,
);
});
});

context("when the repay underlying amount is less than the seized underlying amount", function () {
let expectedProfitUnderlyingAmount: BigNumber;
Expand Down Expand Up @@ -257,11 +316,6 @@ export function shouldBehaveLikeUniswapV2Call(): void {
.connect(this.signers.liquidator)
.swap(token0Amount, token1Amount, to, data);
const newUsdcBalance = await this.contracts.usdc.balanceOf(this.signers.liquidator.address);
console.log({
oldUsdcBalance: oldUsdcBalance.toString(),
expectedProfitUnderlyingAmount: expectedProfitUnderlyingAmount.toString(),
newUsdcBalance: newUsdcBalance.toString(),
});
expect(newUsdcBalance.sub(expectedProfitUnderlyingAmount)).to.equal(oldUsdcBalance);
});

Expand Down

0 comments on commit 6ebee5b

Please sign in to comment.