Skip to content

Commit

Permalink
✨ irm: allow curve A and B injection
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Mar 20, 2024
1 parent e187577 commit 3810a91
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/interest-rate-model/Parameters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ export default interface IRMParameters {
fixedAllocation: bigint;
maxRate: bigint;
}

export type IRMBaseParameters = Prettify<
Pick<IRMParameters, "maxUtilization" | "naturalUtilization" | "sigmoidSpeed" | "growthSpeed"> &
(
| (Pick<IRMParameters, "minRate" | "naturalRate"> & { curveA?: undefined; curveB?: undefined })
| { curveA: bigint; curveB: bigint }
)
>;

export type IRMFloatingParameters = Prettify<IRMBaseParameters & Pick<IRMParameters, "maxRate">>;

type Prettify<T> = {
[K in keyof T]: Prettify<T[K]>;
} & {}; // eslint-disable-line @typescript-eslint/ban-types
20 changes: 12 additions & 8 deletions src/interest-rate-model/baseRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ import MAX_UINT256 from "../fixed-point-math/MAX_UINT256.js";
import WAD from "../fixed-point-math/WAD.js";
import expWad from "../fixed-point-math/expWad.js";
import lnWad from "../fixed-point-math/lnWad.js";
import type IRMParameters from "./Parameters.d.ts";
import type { IRMBaseParameters } from "./Parameters.d.ts";

const EXP_THRESHOLD = 135_305_999_368_893_231_588n;

export default function baseRate(
uFloating: bigint,
uGlobal: bigint,
{ minRate, naturalRate, maxUtilization, naturalUtilization, sigmoidSpeed, growthSpeed }: IRMParameters,
{ maxUtilization, naturalUtilization, sigmoidSpeed, growthSpeed, ...p }: IRMBaseParameters,
) {
if (uFloating > uGlobal) throw new Error("UTILIZATION_EXCEEDED");
if (uGlobal >= WAD) return MAX_UINT256;

const curveA =
(((naturalRate * expWad((growthSpeed * lnWad(WAD - naturalUtilization / 2n)) / WAD) - 1n) / WAD + 1n - minRate) *
(maxUtilization - naturalUtilization) *
maxUtilization) /
(naturalUtilization * WAD);
const curveB = minRate - (curveA * WAD) / maxUtilization;
p.curveA === undefined
? (((p.naturalRate * expWad((growthSpeed * lnWad(WAD - naturalUtilization / 2n)) / WAD) - 1n) / WAD +
1n -
p.minRate) *
(maxUtilization - naturalUtilization) *
maxUtilization) /
(naturalUtilization * WAD)
: p.curveA;
const curveB = p.curveB === undefined ? p.minRate - (curveA * WAD) / maxUtilization : p.curveB;

const r = (curveA * WAD) / (maxUtilization - uFloating) + curveB;
if (uGlobal === 0n) return r;
Expand All @@ -37,4 +41,4 @@ export default function baseRate(
}

export { default as WAD } from "../fixed-point-math/WAD.js";
export type { default as IRMParameters } from "./Parameters.d.ts";
export type { default as IRMParameters, IRMBaseParameters } from "./Parameters.d.ts";
6 changes: 3 additions & 3 deletions src/interest-rate-model/floatingRate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type IRMParameters from "./Parameters.d.ts";
import type { IRMFloatingParameters } from "./Parameters.d.ts";
import baseRate from "./baseRate.js";

export default function floatingRate(uFloating: bigint, uGlobal: bigint, parameters: IRMParameters) {
export default function floatingRate(uFloating: bigint, uGlobal: bigint, parameters: IRMFloatingParameters) {
const { maxRate } = parameters;
const base = baseRate(uFloating, uGlobal, parameters);
return base > maxRate ? maxRate : base;
}

export { default as WAD } from "../fixed-point-math/WAD.js";
export type { default as IRMParameters } from "./Parameters.d.ts";
export type { default as IRMParameters, IRMFloatingParameters } from "./Parameters.d.ts";

0 comments on commit 3810a91

Please sign in to comment.