Skip to content

Commit

Permalink
feat(market): add liquidity to utilization getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Aug 2, 2024
1 parent ca98ff8 commit bd4f046
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 25 deletions.
3 changes: 1 addition & 2 deletions packages/blue-api-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@
},
"publishConfig": {
"access": "public"
},
"gitHead": "17b1a46190f11620ae969927aec1c891f9752ca0"
}
}
9 changes: 6 additions & 3 deletions packages/blue-sdk-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
},
"peerDependencies": {
"@morpho-org/blue-sdk": "workspace:^",
"@morpho-org/morpho-ts": "workspace:^"
"@morpho-org/morpho-ts": "workspace:^",
"ethers": "^6.12.1",
"ethers-multicall-provider": "^6.3.0",
"ethers-types": "^3.17.1",
"rxjs": "^7.8.1"
},
"publishConfig": {
"access": "public"
Expand All @@ -67,6 +71,5 @@
"ts"
],
"preset": "ts-jest"
},
"gitHead": "17b1a46190f11620ae969927aec1c891f9752ca0"
}
}
3 changes: 1 addition & 2 deletions packages/blue-sdk-viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@
},
"publishConfig": {
"access": "public"
},
"gitHead": "17b1a46190f11620ae969927aec1c891f9752ca0"
}
}
3 changes: 1 addition & 2 deletions packages/blue-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@
"ts"
],
"preset": "ts-jest"
},
"gitHead": "17b1a46190f11620ae969927aec1c891f9752ca0"
}
}
32 changes: 24 additions & 8 deletions packages/blue-sdk/src/market/Market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ export class Market implements InputMarket {

let borrowRate = 0n;
let { rateAtTarget } = this;
if (this.rateAtTarget != null) {
if (rateAtTarget != null) {
const { avgBorrowRate, endRateAtTarget } =
AdaptiveCurveIrmLib.getBorrowRate(
this.utilization,
this.rateAtTarget,
rateAtTarget,
elapsed,
);

Expand Down Expand Up @@ -351,19 +351,35 @@ export class Market implements InputMarket {
}

/**
* Returns the liquidity available to borrow until the market reach the given utilization rate.
* Returns the volume to supply until the market gets the closest to the given utilization rate.
* @param utilization The target utilization rate (scaled by WAD).
*/
public getSupplyLiquidityToUtilization(utilization: bigint) {
return MarketUtils.getSupplyLiquidityToUtilization(this, utilization);
public getSupplyToUtilization(utilization: bigint) {
return MarketUtils.getSupplyToUtilization(this, utilization);
}

/**
* Returns the liquidity available to borrow until the market reach the given utilization rate.
* Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
* @param utilization The target utilization rate (scaled by WAD).
*/
public getBorrowLiquidityToUtilization(utilization: bigint) {
return MarketUtils.getBorrowLiquidityToUtilization(this, utilization);
public getWithdrawToUtilization(utilization: bigint) {
return MarketUtils.getWithdrawToUtilization(this, utilization);
}

/**
* Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
* @param utilization The target utilization rate (scaled by WAD).
*/
public getBorrowToUtilization(utilization: bigint) {
return MarketUtils.getBorrowToUtilization(this, utilization);
}

/**
* Returns the volume to repay until the market gets the closest to the given utilization rate.
* @param utilization The target utilization rate (scaled by WAD).
*/
public getRepayToUtilization(utilization: bigint) {
return MarketUtils.getRepayToUtilization(this, utilization);
}

/**
Expand Down
72 changes: 72 additions & 0 deletions packages/blue-sdk/src/market/MarketUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MathLib } from "../maths";
import { MarketUtils } from "./MarketUtils";

const market = {
Expand All @@ -20,4 +21,75 @@ describe("MarketUtils", () => {
1043841336116910229n,
);
});

it("should calculate the supply volume to reach utilization", () => {
expect(
MarketUtils.getSupplyToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: MathLib.WAD },
90_0000000000000000n,
),
).toEqual(11_1111111111111112n);

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

it("should calculate the withdraw volume to reach utilization", () => {
expect(
MarketUtils.getWithdrawToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: MathLib.WAD },
90_0000000000000000n,
),
).toEqual(0n);

expect(
MarketUtils.getWithdrawToUtilization(
{ totalSupplyAssets: 2n * MathLib.WAD, totalBorrowAssets: MathLib.WAD },
90_0000000000000000n,
),
).toEqual(88_8888888888888888n);

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

it("should calculate the borrow volume to reach utilization", () => {
expect(
MarketUtils.getBorrowToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: MathLib.WAD },
90_0000000000000000n,
),
).toEqual(0n);

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

it("should calculate the repay volume to reach utilization", () => {
expect(
MarketUtils.getRepayToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: MathLib.WAD },
90_0000000000000000n,
),
).toEqual(10_0000000000000000n);

expect(
MarketUtils.getRepayToUtilization(
{ totalSupplyAssets: MathLib.WAD, totalBorrowAssets: 0n },
90_0000000000000000n,
),
).toEqual(0n);
});
});
58 changes: 54 additions & 4 deletions packages/blue-sdk/src/market/MarketUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,40 @@ export namespace MarketUtils {
}

/**
* Returns the liquidity available to withdraw until the market reach the given utilization rate.
* Returns the volume to supply until the market gets the closest to the given utilization rate.
* @param market The market state.
* @param utilization The target utilization rate (scaled by WAD).
*/
export function getSupplyLiquidityToUtilization(
export function getSupplyToUtilization(
{
totalSupplyAssets,
totalBorrowAssets,
}: {
totalSupplyAssets: BigIntish;
totalBorrowAssets: BigIntish;
},
utilization: BigIntish,
) {
utilization = BigInt(utilization);
totalBorrowAssets = BigInt(totalBorrowAssets);
if (utilization === 0n) {
if (totalBorrowAssets === 0n) return totalSupplyAssets;

return 0n;
}

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

/**
* Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
* @param market The market state.
* @param utilization The target utilization rate (scaled by WAD).
*/
export function getWithdrawToUtilization(
{
totalSupplyAssets,
totalBorrowAssets,
Expand All @@ -161,11 +190,11 @@ export namespace MarketUtils {
}

/**
* Returns the liquidity available to borrow until the market reach the given utilization rate.
* Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
* @param market The market state.
* @param utilization The target utilization rate (scaled by WAD).
*/
export function getBorrowLiquidityToUtilization(
export function getBorrowToUtilization(
{
totalSupplyAssets,
totalBorrowAssets,
Expand All @@ -181,6 +210,27 @@ export namespace MarketUtils {
);
}

/**
* Returns the volume to repay until the market gets the closest to the given utilization rate.
* @param market The market state.
* @param utilization The target utilization rate (scaled by WAD).
*/
export function getRepayToUtilization(
{
totalSupplyAssets,
totalBorrowAssets,
}: {
totalSupplyAssets: BigIntish;
totalBorrowAssets: BigIntish;
},
utilization: BigIntish,
) {
return MathLib.zeroFloorSub(
totalBorrowAssets,
MathLib.wMulDown(totalSupplyAssets, utilization),
);
}

export function getCollateralPower(
collateral: BigIntish,
{ lltv }: { lltv: BigIntish },
Expand Down
33 changes: 33 additions & 0 deletions packages/blue-sdk/src/maths/AdaptiveCurveIrmLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,37 @@ export namespace AdaptiveCurveIrmLib {
endRateAtTarget,
};
}

export function getUtilizationAtBorrowRate(
borrowRate: BigIntish,
rateAtTarget: BigIntish,
) {
borrowRate = BigInt(borrowRate);
rateAtTarget = BigInt(rateAtTarget);

if (borrowRate >= rateAtTarget) {
const maxBorrowRate = MathLib.wMulDown(rateAtTarget, CURVE_STEEPNESS);

return MathLib.min(
MathLib.WAD,
TARGET_UTILIZATION +
MathLib.mulDivDown(
MathLib.WAD - TARGET_UTILIZATION,
borrowRate - rateAtTarget,
maxBorrowRate - rateAtTarget,
),
);
}

const minBorrowRate = MathLib.wDivDown(rateAtTarget, CURVE_STEEPNESS);

return MathLib.max(
0n,
MathLib.mulDivDown(
TARGET_UTILIZATION,
borrowRate - minBorrowRate,
rateAtTarget - minBorrowRate,
),
);
}
}
3 changes: 1 addition & 2 deletions packages/morpho-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@
},
"publishConfig": {
"access": "public"
},
"gitHead": "17b1a46190f11620ae969927aec1c891f9752ca0"
}
}
3 changes: 1 addition & 2 deletions packages/morpho-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@
"ts"
],
"preset": "ts-jest"
},
"gitHead": "17b1a46190f11620ae969927aec1c891f9752ca0"
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,10 @@ __metadata:
peerDependencies:
"@morpho-org/blue-sdk": "workspace:^"
"@morpho-org/morpho-ts": "workspace:^"
ethers: ^6.12.1
ethers-multicall-provider: ^6.3.0
ethers-types: ^3.17.1
rxjs: ^7.8.1
languageName: unknown
linkType: soft

Expand Down

0 comments on commit bd4f046

Please sign in to comment.