-
Notifications
You must be signed in to change notification settings - Fork 0
/
liquidation.go
428 lines (362 loc) · 14 KB
/
liquidation.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package keeper
import (
"sort"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elysium-station/black/x/hard/types"
)
// LiqData holds liquidation-related data
type LiqData struct {
price sdk.Dec
ltv sdk.Dec
conversionFactor sdkmath.Int
}
// AttemptKeeperLiquidation enables a keeper to liquidate an individual borrower's position
func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress, borrower sdk.AccAddress) error {
deposit, found := k.GetDeposit(ctx, borrower)
if !found {
return types.ErrDepositNotFound
}
borrow, found := k.GetBorrow(ctx, borrower)
if !found {
return types.ErrBorrowNotFound
}
// Call incentive hooks
k.BeforeDepositModified(ctx, deposit)
k.BeforeBorrowModified(ctx, borrow)
k.SyncBorrowInterest(ctx, borrower)
k.SyncSupplyInterest(ctx, borrower)
deposit, found = k.GetDeposit(ctx, borrower)
if !found {
return types.ErrDepositNotFound
}
borrow, found = k.GetBorrow(ctx, borrower)
if !found {
return types.ErrBorrowNotFound
}
isWithinRange, err := k.IsWithinValidLtvRange(ctx, deposit, borrow)
if err != nil {
return err
}
if isWithinRange {
return errorsmod.Wrapf(types.ErrBorrowNotLiquidatable, "position is within valid LTV range")
}
// Sending coins to auction module with keeper address getting % of the profits
borrowDenoms := getDenoms(borrow.Amount)
depositDenoms := getDenoms(deposit.Amount)
err = k.SeizeDeposits(ctx, keeper, deposit, borrow, depositDenoms, borrowDenoms)
if err != nil {
return err
}
deposit.Amount = sdk.NewCoins()
k.DeleteDeposit(ctx, deposit)
k.AfterDepositModified(ctx, deposit)
borrow.Amount = sdk.NewCoins()
k.DeleteBorrow(ctx, borrow)
k.AfterBorrowModified(ctx, borrow)
return nil
}
// SeizeDeposits seizes a list of deposits and sends them to auction
func (k Keeper) SeizeDeposits(ctx sdk.Context, keeper sdk.AccAddress, deposit types.Deposit,
borrow types.Borrow, dDenoms, bDenoms []string,
) error {
liqMap, err := k.LoadLiquidationData(ctx, deposit, borrow)
if err != nil {
return err
}
// Seize % of every deposit and send to the keeper
keeperRewardCoins := sdk.Coins{}
for _, depCoin := range deposit.Amount {
mm, _ := k.GetMoneyMarket(ctx, depCoin.Denom)
keeperReward := mm.KeeperRewardPercentage.MulInt(depCoin.Amount).TruncateInt()
if keeperReward.GT(sdk.ZeroInt()) {
// Send keeper their reward
keeperCoin := sdk.NewCoin(depCoin.Denom, keeperReward)
keeperRewardCoins = append(keeperRewardCoins, keeperCoin)
}
}
if !keeperRewardCoins.Empty() {
if err := k.DecrementSuppliedCoins(ctx, keeperRewardCoins); err != nil {
return err
}
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, keeper, keeperRewardCoins); err != nil {
return err
}
}
// All deposit amounts not given to keeper as rewards are eligible to be auctioned off
aucDeposits := deposit.Amount.Sub(keeperRewardCoins...)
// Build valuation map to hold deposit coin USD valuations
depositCoinValues := types.NewValuationMap()
for _, deposit := range aucDeposits {
dData := liqMap[deposit.Denom]
dCoinUsdValue := sdk.NewDecFromInt(deposit.Amount).Quo(sdk.NewDecFromInt(dData.conversionFactor)).Mul(dData.price)
depositCoinValues.Increment(deposit.Denom, dCoinUsdValue)
}
// Build valuation map to hold borrow coin USD valuations
borrowCoinValues := types.NewValuationMap()
for _, bCoin := range borrow.Amount {
bData := liqMap[bCoin.Denom]
bCoinUsdValue := sdk.NewDecFromInt(bCoin.Amount).Quo(sdk.NewDecFromInt(bData.conversionFactor)).Mul(bData.price)
borrowCoinValues.Increment(bCoin.Denom, bCoinUsdValue)
}
// Loan-to-Value ratio after sending keeper their reward
depositUsdValue := depositCoinValues.Sum()
if depositUsdValue.IsZero() {
// Deposit value can be zero if params.KeeperRewardPercent is 1.0, or all deposit asset prices are zero.
// In this case the full deposit will be sent to the keeper and no auctions started.
return nil
}
ltv := borrowCoinValues.Sum().Quo(depositUsdValue)
liquidatedCoins, err := k.StartAuctions(ctx, deposit.Depositor, borrow.Amount, aucDeposits, depositCoinValues, borrowCoinValues, ltv, liqMap)
// If some coins were liquidated and sent to auction prior to error, still need to emit liquidation event
if !liquidatedCoins.Empty() {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeHardLiquidation,
sdk.NewAttribute(types.AttributeKeyLiquidatedOwner, deposit.Depositor.String()),
sdk.NewAttribute(types.AttributeKeyLiquidatedCoins, liquidatedCoins.String()),
sdk.NewAttribute(types.AttributeKeyKeeper, keeper.String()),
sdk.NewAttribute(types.AttributeKeyKeeperRewardCoins, keeperRewardCoins.String()),
),
)
}
// Returns nil if there's no error
return err
}
// StartAuctions attempts to start auctions for seized assets
func (k Keeper) StartAuctions(ctx sdk.Context, borrower sdk.AccAddress, borrows, deposits sdk.Coins,
depositCoinValues, borrowCoinValues types.ValuationMap, ltv sdk.Dec, liqMap map[string]LiqData,
) (sdk.Coins, error) {
// Sort keys to ensure deterministic behavior
bKeys := borrowCoinValues.GetSortedKeys()
dKeys := depositCoinValues.GetSortedKeys()
// Set up auction constants
returnAddrs := []sdk.AccAddress{borrower}
weights := []sdkmath.Int{sdkmath.NewInt(100)}
debt := sdk.NewCoin("debt", sdk.ZeroInt())
macc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
maccCoins := k.bankKeeper.SpendableCoins(ctx, macc.GetAddress())
var liquidatedCoins sdk.Coins
for _, bKey := range bKeys {
bValue := borrowCoinValues.Get(bKey)
maxLotSize := bValue.Quo(ltv)
for _, dKey := range dKeys {
dValue := depositCoinValues.Get(dKey)
if maxLotSize.Equal(sdk.ZeroDec()) {
break // exit out of the loop if we have cleared the full amount
}
if dValue.GTE(maxLotSize) { // We can start an auction for the whole borrow amount]
bid := sdk.NewCoin(bKey, borrows.AmountOf(bKey))
lotSize := maxLotSize.MulInt(liqMap[dKey].conversionFactor).Quo(liqMap[dKey].price)
if lotSize.TruncateInt().Equal(sdk.ZeroInt()) {
continue
}
lot := sdk.NewCoin(dKey, lotSize.TruncateInt())
insufficientLotFunds := false
if lot.Amount.GT(maccCoins.AmountOf(dKey)) {
insufficientLotFunds = true
lot = sdk.NewCoin(lot.Denom, maccCoins.AmountOf(dKey))
}
// Sanity check that we can deliver coins to the liquidator account
if deposits.AmountOf(dKey).LT(lot.Amount) {
return liquidatedCoins, types.ErrInsufficientCoins
}
// Start auction: bid = full borrow amount, lot = maxLotSize
_, err := k.auctionKeeper.StartCollateralAuction(ctx, types.ModuleAccountName, lot, bid, returnAddrs, weights, debt)
if err != nil {
return liquidatedCoins, err
}
// Decrement supplied coins and decrement borrowed coins optimistically
err = k.DecrementSuppliedCoins(ctx, sdk.Coins{lot})
if err != nil {
return liquidatedCoins, err
}
err = k.DecrementBorrowedCoins(ctx, sdk.Coins{bid})
if err != nil {
return liquidatedCoins, err
}
// Add lot to liquidated coins
liquidatedCoins = liquidatedCoins.Add(lot)
// Update USD valuation maps
borrowCoinValues.SetZero(bKey)
depositCoinValues.Decrement(dKey, maxLotSize)
// Update deposits, borrows
borrows = borrows.Sub(bid)
if insufficientLotFunds {
deposits = deposits.Sub(sdk.NewCoin(dKey, deposits.AmountOf(dKey)))
} else {
deposits = deposits.Sub(lot)
}
// Update max lot size
maxLotSize = sdk.ZeroDec()
} else { // We can only start an auction for the partial borrow amount
maxBid := dValue.Mul(ltv)
bidSize := maxBid.MulInt(liqMap[bKey].conversionFactor).Quo(liqMap[bKey].price)
bid := sdk.NewCoin(bKey, bidSize.TruncateInt())
lot := sdk.NewCoin(dKey, deposits.AmountOf(dKey))
if bid.Amount.Equal(sdk.ZeroInt()) || lot.Amount.Equal(sdk.ZeroInt()) {
continue
}
insufficientLotFunds := false
if lot.Amount.GT(maccCoins.AmountOf(dKey)) {
insufficientLotFunds = true
lot = sdk.NewCoin(lot.Denom, maccCoins.AmountOf(dKey))
}
// Sanity check that we can deliver coins to the liquidator account
if deposits.AmountOf(dKey).LT(lot.Amount) {
return liquidatedCoins, types.ErrInsufficientCoins
}
// Start auction: bid = maxBid, lot = whole deposit amount
_, err := k.auctionKeeper.StartCollateralAuction(ctx, types.ModuleAccountName, lot, bid, returnAddrs, weights, debt)
if err != nil {
return liquidatedCoins, err
}
// Decrement supplied coins and decrement borrowed coins optimistically
err = k.DecrementSuppliedCoins(ctx, sdk.Coins{lot})
if err != nil {
return liquidatedCoins, err
}
err = k.DecrementBorrowedCoins(ctx, sdk.Coins{bid})
if err != nil {
return liquidatedCoins, err
}
// Add lot to liquidated coins
liquidatedCoins = liquidatedCoins.Add(lot)
// Update variables to account for partial auction
borrowCoinValues.Decrement(bKey, maxBid)
depositCoinValues.SetZero(dKey)
borrows = borrows.Sub(bid)
if insufficientLotFunds {
deposits = deposits.Sub(sdk.NewCoin(dKey, deposits.AmountOf(dKey)))
} else {
deposits = deposits.Sub(lot)
}
// Update max lot size
maxLotSize = borrowCoinValues.Get(bKey).Quo(ltv)
}
}
}
// Send any remaining deposit back to the original borrower
for _, dKey := range dKeys {
remaining := deposits.AmountOf(dKey)
if remaining.GT(sdk.ZeroInt()) {
returnCoin := sdk.NewCoins(sdk.NewCoin(dKey, remaining))
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, returnCoin)
if err != nil {
return liquidatedCoins, err
}
}
}
return liquidatedCoins, nil
}
// IsWithinValidLtvRange compares a borrow and deposit to see if it's within a valid LTV range at current prices
func (k Keeper) IsWithinValidLtvRange(ctx sdk.Context, deposit types.Deposit, borrow types.Borrow) (bool, error) {
liqMap, err := k.LoadLiquidationData(ctx, deposit, borrow)
if err != nil {
return false, err
}
totalBorrowableUSDAmount := sdk.ZeroDec()
for _, depCoin := range deposit.Amount {
lData := liqMap[depCoin.Denom]
usdValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(lData.conversionFactor)).Mul(lData.price)
borrowableUSDAmountForDeposit := usdValue.Mul(lData.ltv)
totalBorrowableUSDAmount = totalBorrowableUSDAmount.Add(borrowableUSDAmountForDeposit)
}
totalBorrowedUSDAmount := sdk.ZeroDec()
for _, coin := range borrow.Amount {
lData := liqMap[coin.Denom]
usdValue := sdk.NewDecFromInt(coin.Amount).Quo(sdk.NewDecFromInt(lData.conversionFactor)).Mul(lData.price)
totalBorrowedUSDAmount = totalBorrowedUSDAmount.Add(usdValue)
}
// Check if the user's has borrowed more than they're allowed to
if totalBorrowedUSDAmount.GT(totalBorrowableUSDAmount) {
return false, nil
}
return true, nil
}
// GetStoreLTV calculates the user's current LTV based on their deposits/borrows in the store
// and does not include any outsanding interest.
func (k Keeper) GetStoreLTV(ctx sdk.Context, addr sdk.AccAddress) (sdk.Dec, error) {
// Fetch deposits and parse coin denoms
deposit, found := k.GetDeposit(ctx, addr)
if !found {
return sdk.ZeroDec(), nil
}
// Fetch borrow balances and parse coin denoms
borrow, found := k.GetBorrow(ctx, addr)
if !found {
return sdk.ZeroDec(), nil
}
return k.CalculateLtv(ctx, deposit, borrow)
}
// CalculateLtv calculates the potential LTV given a user's deposits and borrows.
// The boolean returned indicates if the LTV should be added to the store's LTV index.
func (k Keeper) CalculateLtv(ctx sdk.Context, deposit types.Deposit, borrow types.Borrow) (sdk.Dec, error) {
// Load required liquidation data for every deposit/borrow denom
liqMap, err := k.LoadLiquidationData(ctx, deposit, borrow)
if err != nil {
return sdk.ZeroDec(), nil
}
// Build valuation map to hold deposit coin USD valuations
depositCoinValues := types.NewValuationMap()
for _, depCoin := range deposit.Amount {
dData := liqMap[depCoin.Denom]
dCoinUsdValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(dData.conversionFactor)).Mul(dData.price)
depositCoinValues.Increment(depCoin.Denom, dCoinUsdValue)
}
// Build valuation map to hold borrow coin USD valuations
borrowCoinValues := types.NewValuationMap()
for _, bCoin := range borrow.Amount {
bData := liqMap[bCoin.Denom]
bCoinUsdValue := sdk.NewDecFromInt(bCoin.Amount).Quo(sdk.NewDecFromInt(bData.conversionFactor)).Mul(bData.price)
borrowCoinValues.Increment(bCoin.Denom, bCoinUsdValue)
}
// User doesn't have any deposits, catch divide by 0 error
sumDeposits := depositCoinValues.Sum()
if sumDeposits.Equal(sdk.ZeroDec()) {
return sdk.ZeroDec(), nil
}
// Loan-to-Value ratio
return borrowCoinValues.Sum().Quo(sumDeposits), nil
}
// LoadLiquidationData returns liquidation data, deposit, borrow
func (k Keeper) LoadLiquidationData(ctx sdk.Context, deposit types.Deposit, borrow types.Borrow) (map[string]LiqData, error) {
liqMap := make(map[string]LiqData)
borrowDenoms := getDenoms(borrow.Amount)
depositDenoms := getDenoms(deposit.Amount)
denoms := removeDuplicates(borrowDenoms, depositDenoms)
// Load required liquidation data for every deposit/borrow denom
for _, denom := range denoms {
mm, found := k.GetMoneyMarket(ctx, denom)
if !found {
return liqMap, errorsmod.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", denom)
}
priceData, err := k.pricefeedKeeper.GetCurrentPrice(ctx, mm.SpotMarketID)
if err != nil {
return liqMap, err
}
liqMap[denom] = LiqData{priceData.Price, mm.BorrowLimit.LoanToValue, mm.ConversionFactor}
}
return liqMap, nil
}
func getDenoms(coins sdk.Coins) []string {
denoms := []string{}
for _, coin := range coins {
denoms = append(denoms, coin.Denom)
}
return denoms
}
func removeDuplicates(one []string, two []string) []string {
check := make(map[string]int)
fullList := append(one, two...)
res := []string{}
for _, val := range fullList {
check[val] = 1
}
for key := range check {
res = append(res, key)
}
sort.Strings(res)
return res
}