Skip to content

Commit

Permalink
🐛 auditor: fix due maturity penalties
Browse files Browse the repository at this point in the history
  • Loading branch information
itofarina committed Nov 21, 2024
1 parent 1c2ccdc commit 16d8b3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/auditor/accountLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export default function accountLiquidity(
for (const { position, maturity } of fixedBorrowPositions) {
const positionAssets = position.principal + position.fee;
totalDebt += positionAssets;
if (timestamp > maturity) totalDebt += mulWad(positionAssets, BigInt(timestamp) - maturity * penaltyRate);
if (timestamp > maturity) totalDebt += mulWad(positionAssets, (BigInt(timestamp) - maturity) * penaltyRate);
}
adjDebt += divWadUp(mulDivUp(totalDebt, usdPrice, baseUnit), adjustFactor);
adjDebt += adjustDebt(totalDebt, usdPrice, baseUnit, adjustFactor);
}

return { adjCollateral, adjDebt };
Expand All @@ -47,6 +47,10 @@ export function adjustCollateral(
return mulWad(mulDiv(floatingDepositAssets, usdPrice, baseUnit), adjustFactor);
}

export function adjustDebt(debt: bigint, usdPrice: bigint, baseUnit: bigint, adjustFactor: bigint): bigint {
return divWadUp(mulDivUp(debt, usdPrice, baseUnit), adjustFactor);
}

export function normalizeCollateral(
adjustedCollateral: bigint,
usdPrice: bigint,
Expand Down
22 changes: 19 additions & 3 deletions test/auditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, inject, it } from "vitest";

import { previewerAbi } from "./generated/contracts.js";
import anvilClient from "./utils/anvilClient.js";
import accountLiquidity, { normalizeCollateral, normalizeDebt } from "../src/auditor/accountLiquidity.js";
import accountLiquidity, { adjustDebt, normalizeCollateral, normalizeDebt } from "../src/auditor/accountLiquidity.js";
import borrowLimit from "../src/auditor/borrowLimit.js";
import healthFactor from "../src/auditor/healthFactor.js";
import withdrawLimit from "../src/auditor/withdrawLimit.js";
Expand All @@ -30,6 +30,10 @@ describe("with static data", () => {
maturity: BigInt(MATURITY_INTERVAL),
position: { principal: parseUnits("990", 6), fee: parseUnits("10", 6) },
},
{
maturity: BigInt(MATURITY_INTERVAL * 2),
position: { principal: parseUnits("990", 6), fee: parseUnits("10", 6) },
},
],
penaltyRate: parseUnits("0.000000052083333333", 18),
},
Expand All @@ -43,7 +47,7 @@ describe("with static data", () => {
floatingDepositAssets: parseUnits("1", 18),
fixedBorrowPositions: [
{
maturity: BigInt(MATURITY_INTERVAL),
maturity: BigInt(MATURITY_INTERVAL * 2),
position: { principal: parseUnits("0.9", 18), fee: parseUnits("0.1", 18) },
},
],
Expand All @@ -58,7 +62,7 @@ describe("with static data", () => {
mulDiv(parseUnits("10000", 6), parseUnits("0.91", 18), usdcBaseUnit) +
mulDiv(mulWad(parseUnits("1", 18), parseUnits("2500", 18)), parseUnits("0.86", 18), WAD),
debt:
divWadUp(parseUnits("2000", 18), parseUnits("0.91", 18)) +
divWadUp(parseUnits("3000", 18), parseUnits("0.91", 18)) +
divWadUp(parseUnits("5000", 18), parseUnits("0.86", 18)),
};
}
Expand All @@ -71,6 +75,18 @@ describe("with static data", () => {
expect(adjDebt).toBe(debt);
});

it("account liquidity with due maturity", () => {
let { debt } = exactlyAccountLiquidity();
const dueTime = 420;
const { adjDebt } = accountLiquidity(exactly, MATURITY_INTERVAL + dueTime);
const { penaltyRate, fixedBorrowPositions, usdPrice, decimals, adjustFactor } = exactly[0];
const positionAssets = fixedBorrowPositions[0].position.principal + fixedBorrowPositions[0].position.fee;
const penalties = mulWad(positionAssets, BigInt(dueTime) * penaltyRate);
debt += adjustDebt(penalties, usdPrice, 10n ** BigInt(decimals), adjustFactor);

expect(adjDebt).toBe(debt);
});

it("health factor", () => {
const { collateral, debt } = exactlyAccountLiquidity();

Expand Down

0 comments on commit 16d8b3b

Please sign in to comment.