Skip to content

Commit

Permalink
minor refactor and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikasr committed Feb 20, 2024
1 parent b7c10ae commit 70c805a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
20 changes: 10 additions & 10 deletions x/auctionsV2/keeper/bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, auctionID uint64, bidder s
if bid.Amount.GTE(auctionData.DebtToken.Amount) {
bid.Amount = auctionData.DebtToken.Amount
fullBid = true

}
_, collateralTokenQuanitity, _ := k.vault.GetAmountOfOtherToken(ctx, auctionData.DebtAssetId, debtPrice, bid.Amount, auctionData.CollateralAssetId, auctionData.CollateralTokenAuctionPrice)
//From auction bonus quantity , use the available quantity to calculate the collateral value
_, collateralTokenQuanitityForBonus, _ := k.vault.GetAmountOfOtherToken(ctx, auctionData.DebtAssetId, debtPrice, auctionData.BonusAmount, auctionData.CollateralAssetId, auctionData.CollateralTokenAuctionPrice)
//Checking if the auction bonus and the collateral to be given to user isnt more than available colalteral
totalCollateralTokenQuanitity := collateralTokenQuanitity.Add(collateralTokenQuanitityForBonus)
//If user has sent a bigger bid than the target amount ,
if fullBid || !totalCollateralTokenQuanitity.LTE(auctionData.CollateralToken.Amount) {

if fullBid {
_, collateralTokenQuantity, _ := k.vault.GetAmountOfOtherToken(ctx, auctionData.DebtAssetId, debtPrice, bid.Amount, auctionData.CollateralAssetId, auctionData.CollateralTokenAuctionPrice)
//From auction bonus quantity , use the available quantity to calculate the collateral value
_, collateralTokenQuanitityForBonus, _ := k.vault.GetAmountOfOtherToken(ctx, auctionData.DebtAssetId, debtPrice, auctionData.BonusAmount, auctionData.CollateralAssetId, auctionData.CollateralTokenAuctionPrice)
//Checking if the auction bonus and the collateral to be given to user isnt more than available colalteral
totalCollateralTokenQuanitity := collateralTokenQuantity.Add(collateralTokenQuanitityForBonus)
//If user has sent a bigger bid than the target amount ,

if !totalCollateralTokenQuanitity.LTE(auctionData.CollateralToken.Amount) {
//This means that there is less collateral available .
leftOverCollateral := auctionData.CollateralToken.Amount
_, debtTokenAgainstLeftOverCollateral, _ := k.vault.GetAmountOfOtherToken(ctx, auctionData.CollateralAssetId, auctionData.CollateralTokenAuctionPrice, leftOverCollateral.Sub(collateralTokenQuanitityForBonus), auctionData.DebtAssetId, debtPrice)
_, debtTokenAgainstLeftOverCollateral, _ := k.vault.GetAmountOfOtherToken(ctx, auctionData.CollateralAssetId, auctionData.CollateralTokenAuctionPrice, leftOverCollateral, auctionData.DebtAssetId, debtPrice)
bid.Amount = debtTokenAgainstLeftOverCollateral
totalCollateralTokenQuanitity = leftOverCollateral
//Amount to call from reserve account for adjusting the auction target debt
Expand All @@ -64,7 +64,7 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, auctionID uint64, bidder s
//Updating the protocol was in loss struct
err := k.LiquidationsV2.WithdrawAppReserveFundsFn(ctx, auctionData.AppId, auctionData.DebtAssetId, debtGettingLeft)
if err != nil {
return bidId, err
return 0, types.ErrorInsufficientReserveBalance
}
}
//Take Debt Token from user ,
Expand Down
1 change: 0 additions & 1 deletion x/auctionsV2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ func (s *KeeperTestSuite) TestLiquidateBorrows() {
s.Require().Equal(lockedVault[0].OriginalVaultId, beforeBorrow.ID)
s.Require().Equal(lockedVault[0].ExtendedPairId, beforeBorrow.PairID)
s.Require().Equal(lockedVault[0].Owner, beforeLend.Owner)
s.Require().Equal(lockedVault[0].CollateralToken.Amount, beforeBorrow.AmountIn.Amount)
s.Require().Equal(lockedVault[0].DebtToken.Amount, beforeBorrow.AmountOut.Amount)
s.Require().Equal(lockedVault[0].TargetDebt.Amount, lockedVault[0].DebtToken.Amount.Add(sdk.NewDecFromInt(beforeBorrow.AmountOut.Amount).Mul(newDec("0.05")).TruncateInt()))
s.Require().Equal(lockedVault[0].FeeToBeCollected, sdk.NewDecFromInt(beforeBorrow.AmountOut.Amount).Mul(newDec("0.05")).TruncateInt())
Expand Down
1 change: 1 addition & 0 deletions x/auctionsV2/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ var (
ErrAuctionLookupTableNotFound = sdkerrors.Register(ModuleName, 714, "auctionLookupTable not found")
ErrorUnableToSetNetFees = sdkerrors.Register(ModuleName, 715, "Unable To set net fees collected after auction closed")
ErrorInGettingLockedVault = sdkerrors.Register(ModuleName, 716, "error in bid dutch auction - locked vault not found")
ErrorInsufficientReserveBalance = sdkerrors.Register(ModuleName, 717, "Insufficient Reserve Balance for this transaction")
)
1 change: 1 addition & 0 deletions x/liquidationsV2/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type VaultKeeper interface {
DeleteUserVaultExtendedPairMapping(ctx sdk.Context, address string, appID uint64, pairVaultID uint64)
DeleteAddressFromAppExtendedPairVaultMapping(ctx sdk.Context, extendedPairID uint64, userVaultID uint64, appMappingID uint64)
SetVault(ctx sdk.Context, vault types.Vault)
GetAmountOfOtherToken(ctx sdk.Context, id1 uint64, rate1 sdk.Dec, amt1 sdk.Int, id2 uint64, rate2 sdk.Dec) (sdk.Dec, sdk.Int, error)
}

type MarketKeeper interface {
Expand Down
14 changes: 11 additions & 3 deletions x/liquidationsV2/keeper/liquidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,17 @@ func (k Keeper) UpdateLockedBorrows(ctx sdk.Context, borrow lendtypes.BorrowAsse
//Calculating Liquidation Fees
feesToBeCollected := sdk.NewDecFromInt(borrow.AmountOut.Amount).Mul(assetRatesStats.LiquidationPenalty).TruncateInt()

// for upgrade: v14
// this code is to deduct the bonus amount from amount in of lend position of the user

debtToken, _ := k.market.GetTwa(ctx, pair.AssetOut)
debtPrice := sdk.NewDecFromInt(sdk.NewInt(int64(debtToken.Twa)))

collateralToken, _ := k.market.GetTwa(ctx, pair.AssetIn)
collateralPrice := sdk.NewDecFromInt(sdk.NewInt(int64(collateralToken.Twa)))
//Calculating auction bonus to be given
auctionBonusToBeGiven := sdk.NewDecFromInt(borrow.AmountOut.Amount).Mul(assetRatesStats.LiquidationBonus).TruncateInt()
_, bonusTokenQuantity, _ := k.vault.GetAmountOfOtherToken(ctx, pair.AssetOut, debtPrice, auctionBonusToBeGiven, pair.AssetIn, collateralPrice)

err := k.bank.SendCoinsFromModuleToModule(ctx, pool.ModuleName, auctionsV2types.ModuleName, sdk.NewCoins(sdk.NewCoin(assetIn.Denom, borrow.AmountIn.Amount)))
if err != nil {
Expand All @@ -383,7 +392,7 @@ func (k Keeper) UpdateLockedBorrows(ctx sdk.Context, borrow lendtypes.BorrowAsse
return err
}

err = k.CreateLockedVault(ctx, borrow.ID, borrow.PairID, owner, sdk.NewCoin(assetIn.Denom, borrow.AmountIn.Amount), borrow.AmountOut, borrow.AmountIn, borrow.AmountOut, currentCollateralizationRatio, appID, isInternalkeeper, liquidator, "", feesToBeCollected, auctionBonusToBeGiven, "lend", whitelistingData.IsDutchActivated, false, pair.AssetIn, pair.AssetOut)
err = k.CreateLockedVault(ctx, borrow.ID, borrow.PairID, owner, sdk.NewCoin(assetIn.Denom, borrow.AmountIn.Amount.Sub(bonusTokenQuantity)), borrow.AmountOut, borrow.AmountIn, borrow.AmountOut, currentCollateralizationRatio, appID, isInternalkeeper, liquidator, "", feesToBeCollected, auctionBonusToBeGiven, "lend", whitelistingData.IsDutchActivated, false, pair.AssetIn, pair.AssetOut)
if err != nil {
return err
}
Expand Down Expand Up @@ -721,8 +730,7 @@ func (k Keeper) MsgLiquidateExternal(ctx sdk.Context, from string, appID uint64,
func (k Keeper) MsgCloseDutchAuctionForBorrow(ctx sdk.Context, liquidationData types.LockedVault, auctionData auctionsV2types.Auction) error {
// send money back to the debt pool (assetOut pool)
// liquidation penalty to the reserve and interest to the pool
// send token to the bidder
// if cross pool borrow, settle the transit asset to it's native pool
// if cross pool borrow, settle the transit asset to its native pool
// close the borrow and update the stats

borrowPos, _ := k.lend.GetBorrow(ctx, liquidationData.OriginalVaultId)
Expand Down

0 comments on commit 70c805a

Please sign in to comment.