Skip to content

Commit e0883f1

Browse files
committed
Refactor loan repayment calculations in getBrokerUserPositions to include total penalty in outstanding amounts. Update related types and market user data to accommodate new totalPenalty field, ensuring accurate financial data representation across the SDK.
1 parent 9bdde41 commit e0883f1

10 files changed

Lines changed: 101 additions & 14 deletions

File tree

packages/moolah-lending-sdk/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @lista-dao/moolah-lending-sdk
22

3+
## 1.0.8
4+
5+
### Patch Changes
6+
7+
- Refactor loan repayment calculations in getBrokerUserPositions to include total penalty in outstanding amounts. Update related types and market user data to accommodate new totalPenalty field, ensuring accurate financial data representation across the SDK.
8+
- Updated dependencies
9+
- @lista-dao/moolah-sdk-core@1.0.9
10+
311
## 1.0.7
412

513
### Patch Changes

packages/moolah-lending-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lista-dao/moolah-lending-sdk",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"type": "module",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import type { Address, PublicClient } from "viem";
3+
4+
import { getBrokerUserPositions } from "../../../read/broker/getBrokerUserPositions.js";
5+
6+
const mockReadContract = vi.fn();
7+
const mockPublicClient = {
8+
readContract: mockReadContract,
9+
} as unknown as PublicClient;
10+
11+
const BROKER = "0x1111111111111111111111111111111111111111" as Address;
12+
const RATE_CALCULATOR =
13+
"0x2222222222222222222222222222222222222222" as Address;
14+
const USER = "0x3333333333333333333333333333333333333333" as Address;
15+
const RAY = 10n ** 27n;
16+
17+
describe("getBrokerUserPositions - dynamic debt", () => {
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
});
21+
22+
it("computes dynamic outstanding with normalizedDebt * rate / 1e27", async () => {
23+
const principal = 17061806491632102441n;
24+
const normalizedDebt = 16878807815476167930n;
25+
const rate = 1011624315990879851775155575n;
26+
const expectedOutstanding = (normalizedDebt * rate) / RAY;
27+
28+
mockReadContract.mockImplementation(async ({ functionName }) => {
29+
switch (functionName) {
30+
case "userFixedPositions":
31+
return [];
32+
case "getFixedTerms":
33+
return [];
34+
case "userDynamicPosition":
35+
return { principal, normalizedDebt };
36+
case "getRate":
37+
return rate;
38+
default:
39+
throw new Error(`Unexpected function: ${String(functionName)}`);
40+
}
41+
});
42+
43+
const result = await getBrokerUserPositions(
44+
mockPublicClient,
45+
BROKER,
46+
RATE_CALCULATOR,
47+
USER,
48+
18,
49+
);
50+
51+
expect(result.dynamicOutstanding).not.toBeNull();
52+
expect(result.dynamicOutstanding?.roundDown(18).numerator).toBe(
53+
expectedOutstanding,
54+
);
55+
expect(result.totalOutstanding.roundDown(18).numerator).toBe(
56+
expectedOutstanding,
57+
);
58+
});
59+
});

packages/moolah-lending-sdk/src/read/broker/getBrokerUserPositions.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Decimal,
44
LENDING_BROKER_ABI,
55
BROKER_RATE_CALCULATOR_ABI,
6-
calculateDynamicLoanRepayment,
76
calculateFixedLoanRepayment,
87
type BrokerUserPositionsData,
98
type FixedLoanPosition,
@@ -81,19 +80,18 @@ export async function getBrokerUserPositions(
8180
let dynamicOutstanding: Decimal | null = null;
8281

8382
if (dynamicPosition?.principal && dynamicPosition.principal > 0n) {
84-
const { totalRepay } = calculateDynamicLoanRepayment(
85-
{
86-
principal: dynamicPosition.principal,
87-
normalizedDebt: dynamicPosition.normalizedDebt,
88-
rate: dynamicRate,
89-
},
83+
const normalizedDebt = new Decimal(
84+
dynamicPosition.normalizedDebt ?? dynamicPosition.principal,
9085
loanDecimals,
9186
);
92-
dynamicOutstanding = totalRepay.roundDown(loanDecimals);
87+
dynamicOutstanding = normalizedDebt
88+
.mul(new Decimal(dynamicRate, 27))
89+
.roundDown(loanDecimals);
9390
}
9491

9592
// Calculate fixed positions data
9693
let fixedOutstanding = Decimal.ZERO;
94+
let totalPenalty = Decimal.ZERO;
9795
let totalOutstanding = Decimal.ZERO;
9896
let weightedSum = Decimal.ZERO;
9997

@@ -119,9 +117,18 @@ export async function getBrokerUserPositions(
119117
return;
120118
}
121119

122-
const { totalRepay } = calculateFixedLoanRepayment(position);
120+
const {
121+
principal: remainPrincipal,
122+
interest,
123+
penalty,
124+
} = calculateFixedLoanRepayment(position);
125+
const totalRepayNoPenalty = new Decimal(
126+
remainPrincipal + interest,
127+
loanDecimals,
128+
);
123129

124-
fixedOutstanding = fixedOutstanding.add(totalRepay);
130+
fixedOutstanding = fixedOutstanding.add(totalRepayNoPenalty);
131+
totalPenalty = totalPenalty.add(new Decimal(penalty, loanDecimals));
125132

126133
const duration =
127134
BigInt(position.end ?? 0n) > BigInt(position.start ?? 0n)
@@ -132,8 +139,8 @@ export async function getBrokerUserPositions(
132139
termRateByDuration.get(duration.toString()) ??
133140
new Decimal(normalizeAprRate(position.apr), 27);
134141

135-
totalOutstanding = totalOutstanding.add(totalRepay);
136-
weightedSum = weightedSum.add(totalRepay.mul(normalizedFixedRate));
142+
totalOutstanding = totalOutstanding.add(totalRepayNoPenalty);
143+
weightedSum = weightedSum.add(totalRepayNoPenalty.mul(normalizedFixedRate));
137144
});
138145

139146
// Calculate weighted borrow rate
@@ -149,6 +156,7 @@ export async function getBrokerUserPositions(
149156
dynamicRatePercent: currentFlexibleRate,
150157
dynamicOutstanding,
151158
fixedOutstanding,
159+
totalPenalty,
152160
totalOutstanding,
153161
weightedBorrowRate,
154162
termRateByDuration,

packages/moolah-lending-sdk/src/read/market/getMarketUserData.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export async function getMarketUserData(
107107
fixedTermData && !fixedTermData.weightedBorrowRate.isZero()
108108
? fixedTermData.weightedBorrowRate
109109
: marketExtraInfo.borrowRate;
110+
const totalPenalty = fixedTermData?.totalPenalty ?? Decimal.ZERO;
110111

111112
const _getExtraRepayAmount = () => {
112113
const now = BigInt(Math.round(Date.now() / 1000));
@@ -166,6 +167,7 @@ export async function getMarketUserData(
166167
collateral,
167168
borrowShares,
168169
borrowed: finalBorrowed,
170+
totalPenalty,
169171
rawBorrowed: borrowed,
170172
borrowRate: finalBorrowRate,
171173
loanable,

packages/moolah-sdk-core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @lista-dao/moolah-sdk-core
22

3+
## 1.0.9
4+
5+
### Patch Changes
6+
7+
- Refactor loan repayment calculations in getBrokerUserPositions to include total penalty in outstanding amounts. Update related types and market user data to accommodate new totalPenalty field, ensuring accurate financial data representation across the SDK.
8+
39
## 1.0.8
410

511
### Patch Changes

packages/moolah-sdk-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lista-dao/moolah-sdk-core",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"type": "module",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",

packages/moolah-sdk-core/src/brokerToUserFixedTermData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function brokerPositionsToUserFixedTermData(
1414
dynamicOutstanding: data.dynamicOutstanding ?? Decimal.ZERO,
1515
fixedOutstanding: data.fixedOutstanding,
1616
totalBorrowed: data.totalOutstanding,
17+
totalPenalty: data.totalPenalty,
1718
weightedBorrowRate: data.weightedBorrowRate,
1819
};
1920
}

packages/moolah-sdk-core/src/types/broker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface BrokerUserPositionsData {
3434
dynamicRatePercent: Decimal | null;
3535
dynamicOutstanding: Decimal | null;
3636
fixedOutstanding: Decimal;
37+
totalPenalty: Decimal;
3738
totalOutstanding: Decimal;
3839
weightedBorrowRate: Decimal;
3940
termRateByDuration: Map<string, Decimal>;

packages/moolah-sdk-core/src/types/market.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export interface MarketUserData {
204204
collateral: Decimal;
205205
borrowShares: bigint;
206206
borrowed: Decimal;
207+
totalPenalty: Decimal;
207208
rawBorrowed: Decimal;
208209
borrowRate: Decimal;
209210
loanable: Decimal;
@@ -282,5 +283,6 @@ export interface UserFixedTermData {
282283
dynamicOutstanding: Decimal;
283284
fixedOutstanding: Decimal;
284285
totalBorrowed: Decimal;
286+
totalPenalty: Decimal;
285287
weightedBorrowRate: Decimal;
286288
}

0 commit comments

Comments
 (0)