Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lend module changes #423

Merged
merged 20 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ func New(
app.AccountKeeper,
&app.AssetKeeper,
&app.MarketKeeper,
&app.EsmKeeper,
)

app.EsmKeeper = esmkeeper.NewKeeper(
Expand Down
1 change: 1 addition & 0 deletions proto/comdex/lend/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,6 @@ service Query {
rpc QueryAuctionParams(QueryAuctionParamRequest) returns (QueryAuctionParamResponse) {
option (google.api.http).get = "/comdex/lend/v1beta1/auctionparams/{app_id}";
}

}

4 changes: 2 additions & 2 deletions scripts/comdex_local_setup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ def CreateState():
exit("Invalid liquidity pair configs")
CreateLiquidityPair(liquidityPair[0], liquidityPair[1], liquidityPair[2])
Vote("yes")

for liquidityPool in LIQUIDITY_POOLS:
if len(liquidityPool) != 3:
exit("Invalid liquidity pool configs")
CreateLiquidityPool(liquidityPool[0], liquidityPool[1], liquidityPool[2])

for assetRate in ADD_ASSET_RATES:
if len(assetRate) != 2:
exit("Invalid add asset rate configs")
Expand Down
2 changes: 1 addition & 1 deletion x/asset/keeper/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (k Keeper) UpdateAssetRecords(ctx sdk.Context, msg types.Asset) error {
if msg.Name != "" {
var IsLetter = regexp.MustCompile(`^[A-Z]+$`).MatchString

if (!IsLetter(msg.Name) || len(msg.Name) > 10){
if !IsLetter(msg.Name) || len(msg.Name) > 10 {
return types.ErrorNameDidNotMeetCriterion
}
}
Expand Down
24 changes: 12 additions & 12 deletions x/auction/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
}
}

lockedVaults := k.GetLockedVaults(ctx)
//lockedVaults := k.GetLockedVaults(ctx)

if len(lockedVaults) > 0 {
// err3 := k.DutchActivator(ctx, lockedVaults)
// if err3 != nil {
// ctx.Logger().Error("error in dutch activator")
// }

err5 := k.LendDutchActivator(ctx, lockedVaults)
if err5 != nil {
ctx.Logger().Error("error in lend dutch activator")
}
}
//if len(lockedVaults) > 0 {
// err3 := k.DutchActivator(ctx, lockedVaults)
// if err3 != nil {
// ctx.Logger().Error("error in dutch activator")
// }
//
// err5 := k.LendDutchActivator(ctx, lockedVaults)
// if err5 != nil {
// ctx.Logger().Error("error in lend dutch activator")
// }
//}

apps, appsFound := k.GetApps(ctx)

Expand Down
2 changes: 1 addition & 1 deletion x/auction/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,4 @@ func queryFilterDutchAuctions() *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "filter-dutch-auctions")
return cmd
}
}
3 changes: 2 additions & 1 deletion x/auction/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expected

import (
assettypes "github.com/comdex-official/comdex/x/asset/types"
auctiontypes "github.com/comdex-official/comdex/x/auction/types"
"github.com/comdex-official/comdex/x/collector/types"
esmtypes "github.com/comdex-official/comdex/x/esm/types"
lendtypes "github.com/comdex-official/comdex/x/lend/types"
Expand Down Expand Up @@ -37,7 +38,7 @@ type LiquidationKeeper interface {
SetLockedVault(ctx sdk.Context, lockedVault liquidationtypes.LockedVault)
DeleteLockedVault(ctx sdk.Context, appID, id uint64)
CreateLockedVaultHistory(ctx sdk.Context, lockedVault liquidationtypes.LockedVault) error
UnLiquidateLockedBorrows(ctx sdk.Context, appID, id uint64) error
UnLiquidateLockedBorrows(ctx sdk.Context, appID, id uint64, dutchAuction auctiontypes.DutchAuction) error
}

type AssetKeeper interface {
Expand Down
4 changes: 2 additions & 2 deletions x/auction/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ func (k Keeper) ModuleBalance(ctx sdk.Context, moduleName string, denom string)
func (k Keeper) UpdateReserveBalances(ctx sdk.Context, assetID uint64, moduleName string, payment sdk.Coin, inc bool) error {
return k.lend.UpdateReserveBalances(ctx, assetID, moduleName, payment, inc)
}
func (k Keeper) UnLiquidateLockedBorrows(ctx sdk.Context, appID, id uint64) error {
return k.liquidation.UnLiquidateLockedBorrows(ctx, appID, id)
func (k Keeper) UnLiquidateLockedBorrows(ctx sdk.Context, appID, id uint64, dutchAuction auctiontypes.DutchAuction) error {
return k.liquidation.UnLiquidateLockedBorrows(ctx, appID, id, dutchAuction)
}

func (k Keeper) SetLend(ctx sdk.Context, lend lendtypes.LendAsset) {
Expand Down
166 changes: 85 additions & 81 deletions x/auction/keeper/dutch_lend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,58 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) LendDutchActivator(ctx sdk.Context, lockedVaults []liquidationtypes.LockedVault) error {
for _, lockedVault := range lockedVaults {
if lockedVault.Kind != nil {
if !lockedVault.IsAuctionInProgress {
extendedPair, found := k.GetLendPair(ctx, lockedVault.ExtendedPairId)
if !found {
return auctiontypes.ErrorInvalidPair
}
assetIn, _ := k.GetAsset(ctx, extendedPair.AssetIn)

assetOut, _ := k.GetAsset(ctx, extendedPair.AssetOut)

assetInPrice, found := k.GetPriceForAsset(ctx, assetIn.Id)
if !found {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
continue
}
assetOutPrice, found := k.GetPriceForAsset(ctx, assetOut.Id)
if !found {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
continue
}
//assetInPrice is the collateral price
////Here collateral to be auctioned is received in ucollateral*uusd so inorder to get back amount we divide with uusd of assetIn
AssetInPrice := sdk.NewDecFromInt(sdk.NewIntFromUint64(assetInPrice))
if AssetInPrice.Equal(sdk.ZeroDec()) {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
continue
}
AssetOutPrice := sdk.NewDecFromInt(sdk.NewIntFromUint64(assetOutPrice))
if AssetOutPrice.Equal(sdk.ZeroDec()) {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
continue
}
outflowToken := sdk.NewCoin(assetIn.Denom, lockedVault.CollateralToBeAuctioned.Quo(AssetInPrice).TruncateInt())
inflowToken := sdk.NewCoin(assetOut.Denom, lockedVault.CollateralToBeAuctioned.Quo(AssetOutPrice).TruncateInt())

AssetRatesStats, found := k.GetAssetRatesStats(ctx, extendedPair.AssetIn)
if !found {
ctx.Logger().Error(auctiontypes.ErrorAssetRates.Error(), lockedVault.LockedVaultId)
continue
}
liquidationPenalty := AssetRatesStats.LiquidationPenalty

err1 := k.StartLendDutchAuction(ctx, outflowToken, inflowToken, lockedVault.AppId, assetOut.Id, assetIn.Id, lockedVault.LockedVaultId, lockedVault.Owner, liquidationPenalty)
if err1 != nil {
ctx.Logger().Error(auctiontypes.ErrorInStartDutchAuction.Error(), lockedVault.LockedVaultId)
continue
}
func (k Keeper) LendDutchActivator(ctx sdk.Context, lockedVault liquidationtypes.LockedVault) error {

if lockedVault.Kind != nil {
if !lockedVault.IsAuctionInProgress {
extendedPair, found := k.GetLendPair(ctx, lockedVault.ExtendedPairId)
if !found {
return auctiontypes.ErrorInvalidPair
}
assetIn, _ := k.GetAsset(ctx, extendedPair.AssetIn)

assetOut, _ := k.GetAsset(ctx, extendedPair.AssetOut)

assetInPrice, found := k.GetPriceForAsset(ctx, assetIn.Id)
if !found {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
return nil
}
assetOutPrice, found := k.GetPriceForAsset(ctx, assetOut.Id)
if !found {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
return nil
}
//assetInPrice is the collateral price
////Here collateral to be auctioned is received in ucollateral*uusd so inorder to get back amount we divide with uusd of assetIn
AssetInPrice := sdk.NewDecFromInt(sdk.NewIntFromUint64(assetInPrice))
if AssetInPrice.Equal(sdk.ZeroDec()) {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
return nil
}
AssetOutPrice := sdk.NewDecFromInt(sdk.NewIntFromUint64(assetOutPrice))
if AssetOutPrice.Equal(sdk.ZeroDec()) {
ctx.Logger().Error(auctiontypes.ErrorPrices.Error(), lockedVault.LockedVaultId)
return nil
}
outflowToken := sdk.NewCoin(assetIn.Denom, lockedVault.CollateralToBeAuctioned.Quo(AssetInPrice).TruncateInt())
inflowToken := sdk.NewCoin(assetOut.Denom, lockedVault.CollateralToBeAuctioned.Quo(AssetOutPrice).TruncateInt())

AssetRatesStats, found := k.GetAssetRatesStats(ctx, extendedPair.AssetIn)
if !found {
ctx.Logger().Error(auctiontypes.ErrorAssetRates.Error(), lockedVault.LockedVaultId)
return nil
}
liquidationPenalty := AssetRatesStats.LiquidationPenalty

err1 := k.StartLendDutchAuction(ctx, outflowToken, inflowToken, lockedVault.AppId, assetOut.Id, assetIn.Id, lockedVault.LockedVaultId, lockedVault.Owner, liquidationPenalty)
if err1 != nil {
ctx.Logger().Error(auctiontypes.ErrorInStartDutchAuction.Error(), lockedVault.LockedVaultId)
return nil
}
}
}

return nil
}

Expand Down Expand Up @@ -96,15 +96,21 @@ func (k Keeper) StartLendDutchAuction(
if !found {
return auctiontypes.ErrorInvalidAuctionParams
}

BorrowMetaData := lockedVault.GetBorrowMetaData()
LendPos, _ := k.GetLend(ctx, BorrowMetaData.LendingId)
pool, _ := k.GetPool(ctx, LendPos.PoolID)
err := k.SendCoinsFromModuleToModule(ctx, pool.ModuleName, auctiontypes.ModuleName, sdk.NewCoins(outFlowToken))
if err != nil {
return err
assetStat, _ := k.GetAssetRatesStats(ctx, LendPos.AssetID)
cAsset, _ := k.GetAsset(ctx, assetStat.CAssetID)
if outFlowToken.Amount.GT(sdk.ZeroInt()) {
err := k.SendCoinsFromModuleToModule(ctx, pool.ModuleName, auctiontypes.ModuleName, sdk.NewCoins(outFlowToken))
if err != nil {
return err
}
err = k.BurnCoins(ctx, pool.ModuleName, sdk.NewCoin(cAsset.Denom, outFlowToken.Amount))
if err != nil {
return err
}
}

outFlowTokenPrice, found = k.GetPriceForAsset(ctx, assetOutID)
if !found {
return auctiontypes.ErrorPrices
Expand Down Expand Up @@ -212,7 +218,8 @@ func (k Keeper) PlaceLendDutchAuctionBid(ctx sdk.Context, appID, auctionMappingI
if !found {
return auctiontypes.ErrorInvalidExtendedPairVault
}

assetStats, _ := k.GetAssetRatesStats(ctx, ExtendedPairVault.AssetIn)
assetOutPool, _ := k.GetPool(ctx, ExtendedPairVault.AssetOutPoolID)
//dust is in usd * 10*-6 (uusd)
dust := sdk.NewIntFromUint64(ExtendedPairVault.MinUsdValueLeft)
//here subtracting current amount and slice to get amount left in auction and also converting it to usd * 10**-12
Expand All @@ -239,11 +246,17 @@ func (k Keeper) PlaceLendDutchAuctionBid(ctx sdk.Context, appID, auctionMappingI
if err != nil {
return err
}
err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, bidder, sdk.NewCoins(outFlowTokenCoin))
// sending inflow token back to the pool
err = k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, assetOutPool.ModuleName, sdk.NewCoins(inFlowTokenCoin))
if err != nil {
return err
}

// calculating additional auction bonus to the bidder

auctionBonus := slice.ToDec().Mul(assetStats.LiquidationBonus)
totalAmountToBidder := sdk.NewCoin(auction.OutflowTokenInitAmount.Denom, slice.Add(auctionBonus.TruncateInt()))

biddingID, err := k.CreateNewDutchLendBid(ctx, appID, auctionMappingID, auctionID, bidder.String(), inFlowTokenCoin, outFlowTokenCoin)
if err != nil {
return err
Expand All @@ -261,27 +274,22 @@ func (k Keeper) PlaceLendDutchAuctionBid(ctx sdk.Context, appID, auctionMappingI
//collateral not over but target cmst reached then send remaining collateral to owner
//if inflow token current amount >= InflowTokenTargetAmount
if auction.InflowTokenCurrentAmount.IsGTE(auction.InflowTokenTargetAmount) {
//send left overcollateral to vault owner as target cmst reached and also

total := auction.OutflowTokenCurrentAmount
err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, sdk.AccAddress(lockedVault.Owner), sdk.NewCoins(total))
err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, bidder, sdk.NewCoins(totalAmountToBidder))
if err != nil {
return err
}

err = k.SetDutchLendAuction(ctx, auction)
if err != nil {
return err
}

//remove dutch auction

err = k.CloseDutchLendAuction(ctx, auction)
if err != nil {
return err
}
} else if auction.OutflowTokenCurrentAmount.Amount.IsZero() && auction.InflowTokenCurrentAmount.IsLT(auction.InflowTokenTargetAmount) { //entire collateral sold out

// take requiredAmount from reserve-pool
requiredAmount := auction.InflowTokenTargetAmount.Sub(auction.InflowTokenCurrentAmount)
//get reserve balance if the requiredAmount is available in the reserves or not
Expand All @@ -299,6 +307,11 @@ func (k Keeper) PlaceLendDutchAuctionBid(ctx sdk.Context, appID, auctionMappingI
if err != nil {
return err
}
err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, bidder, sdk.NewCoins(totalAmountToBidder))
if err != nil {
return err
}

err = k.SetDutchLendAuction(ctx, auction)
if err != nil {
return err
Expand All @@ -311,6 +324,11 @@ func (k Keeper) PlaceLendDutchAuctionBid(ctx sdk.Context, appID, auctionMappingI
}
} else if auction.OutflowTokenCurrentAmount.Amount.IsZero() { //entire collateral sold out

err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, bidder, sdk.NewCoins(totalAmountToBidder))
if err != nil {
return err
}

err = k.SetDutchLendAuction(ctx, auction)
if err != nil {
return err
Expand Down Expand Up @@ -383,24 +401,9 @@ func (k Keeper) CloseDutchLendAuction(
return auctiontypes.ErrorVaultNotFound
}

//logic to send the coins back to pool
//calculate penalty
penaltyCoin := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())

// send penalty

pairID := lockedVault.ExtendedPairId
lendPair, _ := k.GetLendPair(ctx, pairID)
inFlowTokenAssetID := lendPair.AssetOut

err := k.UpdateReserveBalances(ctx, inFlowTokenAssetID, auctiontypes.ModuleName, penaltyCoin, true)
if err != nil {
return err
}

lockedVault.AmountIn = lockedVault.AmountIn.Sub(dutchAuction.OutflowTokenInitAmount.Amount)
//set sell of history in locked vault
err = k.CreateLockedVaultHistory(ctx, lockedVault)
err := k.CreateLockedVaultHistory(ctx, lockedVault)
if err != nil {
return err
}
Expand All @@ -411,6 +414,7 @@ func (k Keeper) CloseDutchLendAuction(
k.SetLend(ctx, lendPos)

lockedVault.AmountOut = lockedVault.AmountOut.Sub(dutchAuction.InflowTokenTargetAmount.Amount)
lockedVault.UpdatedAmountOut = lockedVault.UpdatedAmountOut.Sub(dutchAuction.InflowTokenTargetAmount.Amount)
if lockedVault.AmountOut.LTE(sdk.ZeroInt()) {
lockedVault.AmountOut = sdk.ZeroInt()
}
Expand All @@ -432,15 +436,15 @@ func (k Keeper) CloseDutchLendAuction(
if err != nil {
return err
}
err = k.DeleteDutchLendAuction(ctx, dutchAuction)
err = k.SetHistoryDutchLendAuction(ctx, dutchAuction)
if err != nil {
return err
}
err = k.SetHistoryDutchLendAuction(ctx, dutchAuction)
err = k.UnLiquidateLockedBorrows(ctx, lockedVault.AppId, lockedVault.LockedVaultId, dutchAuction)
if err != nil {
return err
}
err = k.UnLiquidateLockedBorrows(ctx, lockedVault.AppId, lockedVault.LockedVaultId)
err = k.DeleteDutchLendAuction(ctx, dutchAuction)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/auction/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (q QueryServer) QueryFilterDutchAuctions(c context.Context, req *types.Quer
check = true
}

if accumulate && check{
if accumulate && check {
items = append(items, item)
}

Expand All @@ -495,4 +495,4 @@ func (q QueryServer) QueryFilterDutchAuctions(c context.Context, req *types.Quer
Auctions: items,
Pagination: pagination,
}, nil
}
}
Loading