Skip to content

Commit e99e106

Browse files
committed
modify interest model
1 parent b1a10d3 commit e99e106

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

x/lending/keeper/loan.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package keeper
22

33
import (
4-
"time"
5-
64
sdkmath "cosmossdk.io/math"
75
storetypes "cosmossdk.io/store/types"
86
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -216,10 +214,11 @@ func (k Keeper) GetCurrentInterest(ctx sdk.Context, loan *types.Loan) sdk.Coin {
216214

217215
case types.LoanStatus_Liquidated:
218216
liquidation := k.liquidationKeeper.GetLiquidation(ctx, loan.LiquidationId)
219-
interest = types.GetInterest(loan.Interest, time.Duration(loan.Maturity), loan.CreateAt.Unix(), liquidation.LiquidatedTime.Unix())
217+
interest = liquidation.DebtAmount.Amount.Sub(loan.BorrowAmount.Amount)
220218

221219
default:
222-
interest = types.GetInterest(loan.Interest, time.Duration(loan.Maturity), loan.CreateAt.Unix(), ctx.BlockTime().Unix())
220+
tranche, _ := types.GetTranche(k.GetPool(ctx, loan.PoolId).Tranches, loan.Maturity)
221+
interest = types.GetInterest(loan.BorrowAmount.Amount, loan.StartBorrowIndex, tranche.BorrowIndex)
223222
}
224223

225224
return sdk.NewCoin(loan.BorrowAmount.Denom, interest)

x/lending/keeper/msg_server_loan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (m msgServer) Apply(goCtx context.Context, msg *types.MsgApply) (*types.Msg
105105
repaymentEvent.Outcomes = []string{vault}
106106
m.dlcKeeper.SetEvent(ctx, repaymentEvent)
107107

108-
interest := msg.BorrowAmount.Amount.Mul(sdkmath.NewInt(int64(trancheConfig.BorrowAPR))).Mul(sdkmath.NewInt(trancheConfig.Maturity)).Quo(sdkmath.NewInt(int64(types.OneYear))).Quo(types.Permille)
108+
interest := types.GetTotalInterest(msg.BorrowAmount.Amount, tranche.BorrowIndex, trancheConfig.Maturity, trancheConfig.BorrowAPR, m.GetBlocksPerYear(ctx))
109109
protocolFee := interest.Mul(sdkmath.NewInt(int64(poolConfig.ReserveFactor))).Quo(types.Permille)
110110

111111
loan := &types.Loan{

x/lending/types/lending.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ func GetExchangeRate(totalAvailable sdkmath.Int, totalBorrowed sdkmath.Int, tota
2626
return sdkmath.LegacyNewDecFromInt(totalAvailable.Add(totalBorrowed)).Quo(totalSTokens.ToLegacyDec())
2727
}
2828

29-
// GetInterest calculates the loan interest based on the given params
30-
func GetInterest(totalInterest sdkmath.Int, maturity time.Duration, startTime int64, destTime int64) sdkmath.Int {
31-
elapsed := destTime - startTime
29+
// GetInterest calculates the loan interest based on the given borrow index
30+
func GetInterest(borrowAmount sdkmath.Int, startBorrowIndex sdkmath.LegacyDec, borrowIndex sdkmath.LegacyDec) sdkmath.Int {
31+
return borrowAmount.ToLegacyDec().Mul(borrowIndex).Quo(startBorrowIndex).TruncateInt().Sub(borrowAmount)
32+
}
33+
34+
// GetTotalInterest calculates the total loan interest based on the given params
35+
func GetTotalInterest(borrowAmount sdkmath.Int, startBorrowIndex sdkmath.LegacyDec, maturity int64, borrowAPR uint32, blocksPerYear uint64) sdkmath.Int {
36+
totalBlocks := uint64(maturity) * blocksPerYear / OneYear
37+
38+
borrowRatePerBlock := sdkmath.LegacyNewDec(int64(borrowAPR)).Quo(sdkmath.LegacyNewDec(1000)).Quo(sdkmath.LegacyNewDec(int64(blocksPerYear)))
39+
borrowIndexRatio := sdkmath.LegacyOneDec().Add(borrowRatePerBlock)
40+
41+
endBorrowIndex := startBorrowIndex.Mul(borrowIndexRatio.Power(totalBlocks))
3242

33-
return totalInterest.Mul(sdkmath.NewInt(elapsed)).Quo(sdkmath.NewInt(int64(maturity)))
43+
return GetInterest(borrowAmount, startBorrowIndex, endBorrowIndex)
3444
}
3545

3646
// GetLiquidationPrice calculates the liquidation price according to the liquidation LTV

0 commit comments

Comments
 (0)