2020-12-15 17:38:14 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-02-19 21:05:17 +00:00
|
|
|
"sort"
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-12-15 17:38:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LiqData holds liquidation-related data
|
|
|
|
type LiqData struct {
|
|
|
|
price sdk.Dec
|
|
|
|
ltv sdk.Dec
|
|
|
|
conversionFactor sdk.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// AttemptKeeperLiquidation enables a keeper to liquidate an individual borrower's position
|
2021-01-07 21:40:25 +00:00
|
|
|
func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress, borrower sdk.AccAddress) error {
|
2021-01-21 13:52:09 +00:00
|
|
|
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)
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
k.SyncBorrowInterest(ctx, borrower)
|
2021-01-13 11:59:39 +00:00
|
|
|
k.SyncSupplyInterest(ctx, borrower)
|
2020-12-18 01:12:48 +00:00
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
deposit, found = k.GetDeposit(ctx, borrower)
|
2020-12-18 16:05:21 +00:00
|
|
|
if !found {
|
2021-01-07 21:40:25 +00:00
|
|
|
return types.ErrDepositNotFound
|
2020-12-18 16:05:21 +00:00
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
borrow, found = k.GetBorrow(ctx, borrower)
|
2020-12-18 01:12:48 +00:00
|
|
|
if !found {
|
2021-01-07 21:40:25 +00:00
|
|
|
return types.ErrBorrowNotFound
|
2020-12-18 01:12:48 +00:00
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
isWithinRange, err := k.IsWithinValidLtvRange(ctx, deposit, borrow)
|
|
|
|
if err != nil {
|
2021-01-07 21:40:25 +00:00
|
|
|
return err
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
2020-12-21 17:28:41 +00:00
|
|
|
if isWithinRange {
|
2021-01-07 21:40:25 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrBorrowNotLiquidatable, "position is within valid LTV range")
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
// Sending coins to auction module with keeper address getting % of the profits
|
2020-12-21 17:28:41 +00:00
|
|
|
borrowDenoms := getDenoms(borrow.Amount)
|
|
|
|
depositDenoms := getDenoms(deposit.Amount)
|
|
|
|
err = k.SeizeDeposits(ctx, keeper, deposit, borrow, depositDenoms, borrowDenoms)
|
2020-12-15 17:38:14 +00:00
|
|
|
if err != nil {
|
2021-01-07 21:40:25 +00:00
|
|
|
return err
|
2020-12-18 01:12:48 +00:00
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
2021-02-08 12:23:37 +00:00
|
|
|
k.DeleteDeposit(ctx, deposit)
|
|
|
|
k.DeleteBorrow(ctx, borrow)
|
2021-01-07 21:40:25 +00:00
|
|
|
return nil
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeizeDeposits seizes a list of deposits and sends them to auction
|
2020-12-21 17:28:41 +00:00
|
|
|
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
|
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
|
|
|
// Seize % of every deposit and send to the keeper
|
2021-02-03 22:23:17 +00:00
|
|
|
keeperRewardCoins := sdk.Coins{}
|
2020-12-18 16:05:21 +00:00
|
|
|
for _, depCoin := range deposit.Amount {
|
2021-02-03 22:23:17 +00:00
|
|
|
mm, _ := k.GetMoneyMarket(ctx, depCoin.Denom)
|
2021-02-09 20:33:58 +00:00
|
|
|
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)
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
2021-02-03 22:23:17 +00:00
|
|
|
}
|
|
|
|
if !keeperRewardCoins.Empty() {
|
|
|
|
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, keeper, keeperRewardCoins)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 22:23:17 +00:00
|
|
|
// All deposit amounts not given to keeper as rewards are eligible to be auctioned off
|
|
|
|
aucDeposits := deposit.Amount.Sub(keeperRewardCoins)
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
// 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()
|
2020-12-21 17:28:41 +00:00
|
|
|
for _, bCoin := range borrow.Amount {
|
2020-12-15 17:38:14 +00:00
|
|
|
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
|
|
|
|
ltv := borrowCoinValues.Sum().Quo(depositCoinValues.Sum())
|
|
|
|
|
2021-02-03 22:23:17 +00:00
|
|
|
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()),
|
|
|
|
),
|
|
|
|
)
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
2021-02-03 22:23:17 +00:00
|
|
|
// Returns nil if there's no error
|
|
|
|
return err
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartAuctions attempts to start auctions for seized assets
|
|
|
|
func (k Keeper) StartAuctions(ctx sdk.Context, borrower sdk.AccAddress, borrows, deposits sdk.Coins,
|
2021-02-03 22:23:17 +00:00
|
|
|
depositCoinValues, borrowCoinValues types.ValuationMap, ltv sdk.Dec, liqMap map[string]LiqData) (sdk.Coins, error) {
|
2020-12-15 17:38:14 +00:00
|
|
|
// Sort keys to ensure deterministic behavior
|
|
|
|
bKeys := borrowCoinValues.GetSortedKeys()
|
|
|
|
dKeys := depositCoinValues.GetSortedKeys()
|
|
|
|
|
|
|
|
// Set up auction constants
|
|
|
|
returnAddrs := []sdk.AccAddress{borrower}
|
|
|
|
weights := []sdk.Int{sdk.NewInt(100)}
|
|
|
|
debt := sdk.NewCoin("debt", sdk.ZeroInt())
|
|
|
|
|
2021-02-01 21:13:17 +00:00
|
|
|
macc := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
|
|
|
maccCoins := macc.SpendableCoins(ctx.BlockTime())
|
|
|
|
|
2021-02-03 22:23:17 +00:00
|
|
|
var liquidatedCoins sdk.Coins
|
2020-12-15 17:38:14 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:13:17 +00:00
|
|
|
if dValue.GTE(maxLotSize) { // We can start an auction for the whole borrow amount]
|
2020-12-15 17:38:14 +00:00
|
|
|
bid := sdk.NewCoin(bKey, borrows.AmountOf(bKey))
|
2021-02-01 21:13:17 +00:00
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
lotSize := maxLotSize.MulInt(liqMap[dKey].conversionFactor).Quo(liqMap[dKey].price)
|
2021-02-01 21:13:17 +00:00
|
|
|
if lotSize.TruncateInt().Equal(sdk.ZeroInt()) {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
lot := sdk.NewCoin(dKey, lotSize.TruncateInt())
|
2021-02-01 21:13:17 +00:00
|
|
|
|
|
|
|
insufficientLotFunds := false
|
|
|
|
if lot.Amount.GT(maccCoins.AmountOf(dKey)) {
|
|
|
|
insufficientLotFunds = true
|
|
|
|
lot = sdk.NewCoin(lot.Denom, maccCoins.AmountOf(dKey))
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
// Sanity check that we can deliver coins to the liquidator account
|
|
|
|
if deposits.AmountOf(dKey).LT(lot.Amount) {
|
2021-02-03 22:23:17 +00:00
|
|
|
return liquidatedCoins, types.ErrInsufficientCoins
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start auction: bid = full borrow amount, lot = maxLotSize
|
2021-02-09 20:33:58 +00:00
|
|
|
_, err := k.auctionKeeper.StartCollateralAuction(ctx, types.ModuleAccountName, lot, bid, returnAddrs, weights, debt)
|
2020-12-15 17:38:14 +00:00
|
|
|
if err != nil {
|
2021-02-03 22:23:17 +00:00
|
|
|
return liquidatedCoins, err
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
2021-02-12 20:07:32 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-02-09 20:32:28 +00:00
|
|
|
|
2021-02-03 22:23:17 +00:00
|
|
|
// Add lot to liquidated coins
|
|
|
|
liquidatedCoins = liquidatedCoins.Add(lot)
|
2020-12-15 17:38:14 +00:00
|
|
|
|
|
|
|
// Update USD valuation maps
|
|
|
|
borrowCoinValues.SetZero(bKey)
|
|
|
|
depositCoinValues.Decrement(dKey, maxLotSize)
|
|
|
|
// Update deposits, borrows
|
|
|
|
borrows = borrows.Sub(sdk.NewCoins(bid))
|
2021-02-01 21:13:17 +00:00
|
|
|
if insufficientLotFunds {
|
|
|
|
deposits = deposits.Sub(sdk.NewCoins(sdk.NewCoin(dKey, deposits.AmountOf(dKey))))
|
|
|
|
} else {
|
|
|
|
deposits = deposits.Sub(sdk.NewCoins(lot))
|
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:13:17 +00:00
|
|
|
insufficientLotFunds := false
|
|
|
|
if lot.Amount.GT(maccCoins.AmountOf(dKey)) {
|
|
|
|
insufficientLotFunds = true
|
|
|
|
lot = sdk.NewCoin(lot.Denom, maccCoins.AmountOf(dKey))
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
// Sanity check that we can deliver coins to the liquidator account
|
|
|
|
if deposits.AmountOf(dKey).LT(lot.Amount) {
|
2021-02-03 22:23:17 +00:00
|
|
|
return liquidatedCoins, types.ErrInsufficientCoins
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start auction: bid = maxBid, lot = whole deposit amount
|
2021-02-09 20:33:58 +00:00
|
|
|
_, err := k.auctionKeeper.StartCollateralAuction(ctx, types.ModuleAccountName, lot, bid, returnAddrs, weights, debt)
|
2020-12-15 17:38:14 +00:00
|
|
|
if err != nil {
|
2021-02-03 22:23:17 +00:00
|
|
|
return liquidatedCoins, err
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
2021-02-12 20:07:32 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-02-09 20:32:28 +00:00
|
|
|
|
2021-02-03 22:23:17 +00:00
|
|
|
// Add lot to liquidated coins
|
|
|
|
liquidatedCoins = liquidatedCoins.Add(lot)
|
2020-12-15 17:38:14 +00:00
|
|
|
|
|
|
|
// Update variables to account for partial auction
|
|
|
|
borrowCoinValues.Decrement(bKey, maxBid)
|
|
|
|
depositCoinValues.SetZero(dKey)
|
2021-02-01 21:13:17 +00:00
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
borrows = borrows.Sub(sdk.NewCoins(bid))
|
2021-02-01 21:13:17 +00:00
|
|
|
if insufficientLotFunds {
|
|
|
|
deposits = deposits.Sub(sdk.NewCoins(sdk.NewCoin(dKey, deposits.AmountOf(dKey))))
|
|
|
|
} else {
|
|
|
|
deposits = deposits.Sub(sdk.NewCoins(lot))
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
// 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.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, returnCoin)
|
|
|
|
if err != nil {
|
2021-02-03 22:23:17 +00:00
|
|
|
return liquidatedCoins, err
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 22:23:17 +00:00
|
|
|
return liquidatedCoins, nil
|
2020-12-15 17:38:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:12:48 +00:00
|
|
|
// GetStoreLTV calculates the user's current LTV based on their deposits/borrows in the store
|
|
|
|
// and does not include any outsanding interest.
|
2021-01-07 21:40:25 +00:00
|
|
|
func (k Keeper) GetStoreLTV(ctx sdk.Context, addr sdk.AccAddress) (sdk.Dec, error) {
|
2020-12-16 21:08:29 +00:00
|
|
|
// Fetch deposits and parse coin denoms
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit, found := k.GetDeposit(ctx, addr)
|
|
|
|
if !found {
|
2021-01-07 21:40:25 +00:00
|
|
|
return sdk.ZeroDec(), nil
|
2020-12-18 16:05:21 +00:00
|
|
|
}
|
2020-12-16 21:08:29 +00:00
|
|
|
|
|
|
|
// Fetch borrow balances and parse coin denoms
|
2020-12-21 17:28:41 +00:00
|
|
|
borrow, found := k.GetBorrow(ctx, addr)
|
2020-12-18 01:12:48 +00:00
|
|
|
if !found {
|
2021-01-07 21:40:25 +00:00
|
|
|
return sdk.ZeroDec(), nil
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
return k.CalculateLtv(ctx, deposit, borrow)
|
|
|
|
}
|
2020-12-16 21:08:29 +00:00
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
// 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.
|
2021-01-07 21:40:25 +00:00
|
|
|
func (k Keeper) CalculateLtv(ctx sdk.Context, deposit types.Deposit, borrow types.Borrow) (sdk.Dec, error) {
|
2020-12-16 21:08:29 +00:00
|
|
|
// Load required liquidation data for every deposit/borrow denom
|
2020-12-21 17:28:41 +00:00
|
|
|
liqMap, err := k.LoadLiquidationData(ctx, deposit, borrow)
|
|
|
|
if err != nil {
|
2021-01-07 21:40:25 +00:00
|
|
|
return sdk.ZeroDec(), nil
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build valuation map to hold deposit coin USD valuations
|
|
|
|
depositCoinValues := types.NewValuationMap()
|
2020-12-18 16:05:21 +00:00
|
|
|
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)
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build valuation map to hold borrow coin USD valuations
|
|
|
|
borrowCoinValues := types.NewValuationMap()
|
2020-12-21 17:28:41 +00:00
|
|
|
for _, bCoin := range borrow.Amount {
|
2020-12-16 21:08:29 +00:00
|
|
|
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()) {
|
2021-01-07 21:40:25 +00:00
|
|
|
return sdk.ZeroDec(), nil
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loan-to-Value ratio
|
2021-01-07 21:40:25 +00:00
|
|
|
return borrowCoinValues.Sum().Quo(sumDeposits), nil
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
// 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)
|
2020-12-16 21:08:29 +00:00
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
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, sdkerrors.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}
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
2020-12-21 17:28:41 +00:00
|
|
|
|
|
|
|
return liqMap, nil
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-02-19 21:05:17 +00:00
|
|
|
sort.Strings(res)
|
2020-12-15 17:38:14 +00:00
|
|
|
return res
|
|
|
|
}
|