/
repay.go
170 lines (144 loc) · 5.97 KB
/
repay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package keeper
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/incubus-network/nemo/x/hard/types"
)
// Repay borrowed funds
func (k Keeper) Repay(ctx sdk.Context, sender, owner sdk.AccAddress, coins sdk.Coins) error {
// Check borrow exists here to avoid duplicating store read in ValidateRepay
borrow, found := k.GetBorrow(ctx, owner)
if !found {
return types.ErrBorrowNotFound
}
// Call incentive hook
k.BeforeBorrowModified(ctx, borrow)
// Sync borrow interest so loan is up-to-date
k.SyncBorrowInterest(ctx, owner)
// Refresh borrow after syncing interest
borrow, _ = k.GetBorrow(ctx, owner)
// cap the repayment by what's available to repay (the borrow amount)
payment, err := k.CalculatePaymentAmount(borrow.Amount, coins)
if err != nil {
return err
}
// Validate that sender holds coins for repayment
err = k.ValidateRepay(ctx, sender, owner, payment)
if err != nil {
return err
}
// Sends coins from user to Hard module account
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleAccountName, payment)
if err != nil {
return err
}
// If any coin denoms have been completely repaid reset the denom's borrow index factor
for _, coin := range payment {
if coin.Amount.Equal(borrow.Amount.AmountOf(coin.Denom)) {
borrowIndex, removed := borrow.Index.RemoveInterestFactor(coin.Denom)
if !removed {
return errorsmod.Wrapf(types.ErrInvalidIndexFactorDenom, "%s", coin.Denom)
}
borrow.Index = borrowIndex
}
}
// Update user's borrow in store
borrow.Amount = borrow.Amount.Sub(payment...)
if borrow.Amount.Empty() {
k.DeleteBorrow(ctx, borrow)
} else {
k.SetBorrow(ctx, borrow)
}
// Update total borrowed amount
err = k.DecrementBorrowedCoins(ctx, payment)
if err != nil {
return err
}
// Call incentive hook
k.AfterBorrowModified(ctx, borrow)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeHardRepay,
sdk.NewAttribute(types.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyOwner, owner.String()),
sdk.NewAttribute(types.AttributeKeyRepayCoins, payment.String()),
),
)
return nil
}
// ValidateRepay validates a requested loan repay
func (k Keeper) ValidateRepay(ctx sdk.Context, sender, owner sdk.AccAddress, coins sdk.Coins) error {
assetPriceCache := map[string]sdk.Dec{}
// Get the total USD value of user's existing borrows
existingBorrowUSDValue := sdk.ZeroDec()
existingBorrow, found := k.GetBorrow(ctx, owner)
if found {
for _, coin := range existingBorrow.Amount {
moneyMarket, found := k.GetMoneyMarket(ctx, coin.Denom)
if !found {
return errorsmod.Wrapf(types.ErrMarketNotFound, "no money market found for denom %s", coin.Denom)
}
assetPrice, ok := assetPriceCache[coin.Denom]
if !ok { // Fetch current asset price and store in local cache
assetPriceInfo, err := k.pricefeedKeeper.GetCurrentPrice(ctx, moneyMarket.SpotMarketID)
if err != nil {
return errorsmod.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
}
assetPriceCache[coin.Denom] = assetPriceInfo.Price
assetPrice = assetPriceInfo.Price
}
// Calculate this borrow coin's USD value and add it to the total previous borrowed USD value
coinUSDValue := sdk.NewDecFromInt(coin.Amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor)).Mul(assetPrice)
existingBorrowUSDValue = existingBorrowUSDValue.Add(coinUSDValue)
}
}
senderCoins := k.bankKeeper.SpendableCoins(ctx, sender)
repayTotalUSDValue := sdk.ZeroDec()
for _, repayCoin := range coins {
// Check that sender holds enough tokens to make the proposed payment
if senderCoins.AmountOf(repayCoin.Denom).LT(repayCoin.Amount) {
return errorsmod.Wrapf(types.ErrInsufficientBalanceForRepay, "account can only repay up to %s%s", senderCoins.AmountOf(repayCoin.Denom), repayCoin.Denom)
}
moneyMarket, found := k.GetMoneyMarket(ctx, repayCoin.Denom)
if !found {
return errorsmod.Wrapf(types.ErrMarketNotFound, "no money market found for denom %s", repayCoin.Denom)
}
// Calculate this coin's USD value and add it to the repay's total USD value
assetPrice, ok := assetPriceCache[repayCoin.Denom]
if !ok { // Fetch current asset price and store in local cache
assetPriceInfo, err := k.pricefeedKeeper.GetCurrentPrice(ctx, moneyMarket.SpotMarketID)
if err != nil {
return errorsmod.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
}
assetPriceCache[repayCoin.Denom] = assetPriceInfo.Price
assetPrice = assetPriceInfo.Price
}
coinUSDValue := sdk.NewDecFromInt(repayCoin.Amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor)).Mul(assetPrice)
repayTotalUSDValue = repayTotalUSDValue.Add(coinUSDValue)
}
// If the proposed repayment would results in a borrowed USD value below the minimum borrow USD value, reject it.
// User can overpay their loan to close it out, but underpaying by such a margin that the USD value is in an
// invalid range is not allowed
// Unless the user is fully repaying their loan
proposedBorrowNewUSDValue := existingBorrowUSDValue.Sub(repayTotalUSDValue)
isFullRepayment := coins.IsEqual(existingBorrow.Amount)
if proposedBorrowNewUSDValue.LT(k.GetMinimumBorrowUSDValue(ctx)) && !isFullRepayment {
return errorsmod.Wrapf(types.ErrBelowMinimumBorrowValue, "the proposed borrow's USD value $%s is below the minimum borrow limit $%s", proposedBorrowNewUSDValue, k.GetMinimumBorrowUSDValue(ctx))
}
return nil
}
// CalculatePaymentAmount prevents overpayment when repaying borrowed coins
func (k Keeper) CalculatePaymentAmount(owed sdk.Coins, payment sdk.Coins) (sdk.Coins, error) {
repayment := sdk.Coins{}
if !payment.DenomsSubsetOf(owed) {
return repayment, types.ErrInvalidRepaymentDenom
}
for _, coin := range payment {
if coin.Amount.GT(owed.AmountOf(coin.Denom)) {
repayment = append(repayment, sdk.NewCoin(coin.Denom, owed.AmountOf(coin.Denom)))
} else {
repayment = append(repayment, coin)
}
}
return repayment, nil
}