Skip to content

Commit 0dc7743

Browse files
committed
Refactor loan repayment logic in Moolah SDK to clarify the use of normalizedDebt for debt calculations while ensuring the original principal is returned. Update tests to reflect these changes and improve accuracy in repayment computations.
1 parent be3cdd7 commit 0dc7743

4 files changed

Lines changed: 23 additions & 18 deletions

File tree

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.12
4+
5+
### Patch Changes
6+
7+
- Refactor loan repayment logic in Moolah SDK to clarify the use of normalizedDebt for debt calculations while ensuring the original principal is returned. Update tests to reflect these changes and improve accuracy in repayment computations.
8+
39
## 1.0.11
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.11",
3+
"version": "1.0.12",
44
"type": "module",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",

packages/moolah-sdk-core/src/__tests__/loan.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@ describe("calculateDynamicLoanRepayment", () => {
8787
expect(result.interest.valueOf()).toBeGreaterThan(0);
8888
});
8989

90-
it("should use normalizedDebt if provided", () => {
90+
it("should use normalizedDebt for debt calculation but return original principal", () => {
9191
const result = calculateDynamicLoanRepayment({
9292
principal: 1000n * DECIMAL_SCALE,
93-
normalizedDebt: 1100n * DECIMAL_SCALE, // With accrued interest
93+
normalizedDebt: 1100n * DECIMAL_SCALE, // Normalized debt used for debt calculation
9494
rate: RATE_INDEX_5_PERCENT,
9595
});
9696

97-
expect(result.principal.numerator).toBe(1100n * DECIMAL_SCALE);
97+
// principal in result is always the original principal
98+
expect(result.principal.numerator).toBe(1000n * DECIMAL_SCALE);
99+
// totalRepay should be higher because normalizedDebt (1100) is used for calculation
100+
expect(result.totalRepay.valueOf()).toBeGreaterThan(1100);
98101
});
99102

100103
it("should handle zero rate (index = 1.0)", () => {

packages/moolah-sdk-core/src/calculations/loan.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,30 @@ export function calculateDynamicLoanRepayment(
5656
position.normalizedDebt ?? position.principal,
5757
loanDecimals,
5858
);
59+
const principal = new Decimal(position.principal, loanDecimals);
5960

6061
// rate is the cumulative borrow index in RAY format (27 decimals)
62+
// actualDebt = normalizedDebt × (rate / RATE_SCALE_27)
6163
const rateIndex = new Decimal(position.rate, 27);
62-
63-
// Current debt = normalized debt × rate index
6464
const currentDebt = normalizedDebt.mul(rateIndex);
6565

66-
// Principal is the normalized debt
67-
const principal = normalizedDebt;
68-
69-
// Interest = current debt - principal
66+
// Calculate interest = currentDebt - original principal
7067
const currentInterest = currentDebt.sub(principal);
7168

72-
// Buffer = 10% of current interest
73-
const bufferRate = new Decimal(10n ** 17n, 18); // 0.1 in 18 decimals
74-
const buffer = currentInterest.mul(bufferRate);
75-
76-
// Total repay = current debt + buffer
69+
// Add 10% of interest as buffer (excess is refunded)
70+
const BUFFER_RATE = new Decimal(1n, 1); // 0.1 = 10%
71+
const buffer = currentInterest.mul(BUFFER_RATE);
7772
const totalRepay = currentDebt.add(buffer);
78-
79-
// Total interest including buffer
8073
const interest = totalRepay.sub(principal);
8174

75+
// Accumulated rate = rateIndex - 1
76+
const accumulatedRate = rateIndex.sub(Decimal.ONE);
77+
8278
return {
8379
totalRepay,
8480
principal,
8581
interest,
86-
rate: rateIndex,
82+
rate: accumulatedRate,
8783
};
8884
}
8985

0 commit comments

Comments
 (0)