Skip to content

Commit

Permalink
fix(market): fix supply to utilization edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Aug 2, 2024
1 parent b11ee66 commit 7fc42e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 14 additions & 0 deletions packages/blue-sdk/src/market/MarketUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ describe("MarketUtils", () => {
90_0000000000000000n,
),
).toEqual(0n);

expect(
MarketUtils.getSupplyToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: 0n },
0n,
),
).toEqual(0n);

expect(
MarketUtils.getSupplyToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: 1n },
0n,
),
).toEqual(MathLib.MAX_UINT_256);
});

it("should calculate the withdraw volume to reach utilization", () => {
Expand Down
15 changes: 6 additions & 9 deletions packages/blue-sdk/src/market/MarketUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,22 @@ export namespace MarketUtils {
* @param utilization The target utilization rate (scaled by WAD).
*/
export function getSupplyToUtilization(
{
totalSupplyAssets,
totalBorrowAssets,
}: {
market: {
totalSupplyAssets: BigIntish;
totalBorrowAssets: BigIntish;
},
utilization: BigIntish,
) {
utilization = BigInt(utilization);
totalBorrowAssets = BigInt(totalBorrowAssets);
if (utilization === 0n) {
if (totalBorrowAssets === 0n) return totalSupplyAssets;
if (getUtilization(market) === 0n) return 0n;

return 0n;
return MathLib.MAX_UINT_256;
}

return MathLib.zeroFloorSub(
MathLib.wDivUp(totalBorrowAssets, utilization),
totalSupplyAssets,
MathLib.wDivUp(market.totalBorrowAssets, utilization),
market.totalSupplyAssets,
);
}

Expand All @@ -176,6 +172,7 @@ export namespace MarketUtils {
utilization: BigIntish,
) {
utilization = BigInt(utilization);
totalSupplyAssets = BigInt(totalSupplyAssets);
totalBorrowAssets = BigInt(totalBorrowAssets);
if (utilization === 0n) {
if (totalBorrowAssets === 0n) return totalSupplyAssets;
Expand Down

0 comments on commit 7fc42e7

Please sign in to comment.