diff --git a/x/auction/expected/keeper.go b/x/auction/expected/keeper.go index d12218fac..e56aab637 100644 --- a/x/auction/expected/keeper.go +++ b/x/auction/expected/keeper.go @@ -55,6 +55,7 @@ type VaultKeeper interface { //BurnCAssets(ctx sdk.Context, moduleName string, collateralDenom string, denom string, amount sdk.Int) error GetAppExtendedPairVaultMapping(ctx sdk.Context, appMappingId uint64) (appExtendedPairVaultData vaultttypes.AppExtendedPairVaultMapping, found bool) SetAppExtendedPairVaultMapping(ctx sdk.Context, appExtendedPairVaultData vaultttypes.AppExtendedPairVaultMapping) error + UpdateCollateralLockedAmountLockerMapping(ctx sdk.Context, valutLookupData vaultttypes.AppExtendedPairVaultMapping, extendedPairId uint64, amount sdk.Int, changeType bool) } type CollectorKeeper interface { diff --git a/x/auction/keeper/alias.go b/x/auction/keeper/alias.go index 1d3c93f4c..ce216d225 100644 --- a/x/auction/keeper/alias.go +++ b/x/auction/keeper/alias.go @@ -163,3 +163,7 @@ func (k *Keeper) GetAuctionMappingForApp(ctx sdk.Context, appId uint64) (collect func (k *Keeper) SetAuctionMappingForApp(ctx sdk.Context, records ...types.CollectorAuctionLookupTable) error { return k.collector.SetAuctionMappingForApp(ctx, records...) } + +func (k *Keeper) UpdateCollateralLockedAmountLockerMapping(ctx sdk.Context, valutLookupData vaultttypes.AppExtendedPairVaultMapping, extendedPairId uint64, amount sdk.Int, changeType bool) { + k.vault.UpdateCollateralLockedAmountLockerMapping(ctx, valutLookupData, extendedPairId, amount, changeType) +} diff --git a/x/auction/keeper/auction.go b/x/auction/keeper/auction.go index 23aef1557..658f072f2 100644 --- a/x/auction/keeper/auction.go +++ b/x/auction/keeper/auction.go @@ -1,9 +1,10 @@ package keeper import ( - "github.com/comdex-official/comdex/x/auction/types" "time" + "github.com/comdex-official/comdex/x/auction/types" + vaulttypes "github.com/comdex-official/comdex/x/vault/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -439,7 +440,10 @@ func (k Keeper) RestartDebtAuction( if status == auctiontypes.NoAuction { return nil } - auctionParams := k.GetParams(ctx) + auctionParams, found := k.GetAuctionParams(ctx, appId) + if !found { + return auctiontypes.ErrorInvalidAuctionParams + } debtAuction.ExpectedUserToken = inflowToken debtAuction.EndTime = ctx.BlockTime().Add(time.Second * time.Duration(auctionParams.AuctionDurationSeconds)) err := k.SetDebtAuction(ctx, debtAuction) @@ -458,7 +462,10 @@ func (k Keeper) RestartSurplusAuction( if status == auctiontypes.NoAuction { return nil } - auctionParams := k.GetParams(ctx) + auctionParams, found := k.GetAuctionParams(ctx, appId) + if !found { + return auctiontypes.ErrorInvalidAuctionParams + } surplusAuction.InflowToken = inflowToken surplusAuction.Bid = inflowToken surplusAuction.EndTime = ctx.BlockTime().Add(time.Second * time.Duration(auctionParams.AuctionDurationSeconds)) @@ -472,7 +479,10 @@ func (k Keeper) RestartSurplusAuction( //get all app ids and call RestartDutchAuctions with app id func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appId uint64) error { dutchAuctions := k.GetDutchAuctions(ctx, appId) - auctionParams := k.GetParams(ctx) + auctionParams, found := k.GetAuctionParams(ctx, appId) + if !found { + return auctiontypes.ErrorInvalidAuctionParams + } // SET current price of inflow token and outflow token for _, dutchAuction := range dutchAuctions { lockedVault, found := k.GetLockedVault(ctx, dutchAuction.LockedVaultId) @@ -538,7 +548,10 @@ func (k Keeper) StartSurplusAuction( assetInId, assetOutId uint64, ) error { - auctionParams := k.GetParams(ctx) + auctionParams, found := k.GetAuctionParams(ctx, appId) + if !found { + return auctiontypes.ErrorInvalidAuctionParams + } auction := auctiontypes.SurplusAuction{ OutflowToken: outflowToken, InflowToken: inflowToken, @@ -573,7 +586,10 @@ func (k Keeper) StartDebtAuction( assetInId, assetOutId uint64, ) error { - auctionParams := k.GetParams(ctx) + auctionParams, found := k.GetAuctionParams(ctx, appId) + if !found { + return auctiontypes.ErrorInvalidAuctionParams + } auction := auctiontypes.DebtAuction{ AuctionedToken: auctionToken, ExpectedMintedToken: auctionToken, @@ -641,7 +657,10 @@ func (k Keeper) StartDutchAuction( if err != nil { return err } - auctionParams := k.GetParams(ctx) + auctionParams, found := k.GetAuctionParams(ctx, appId) + if !found { + return auctiontypes.ErrorInvalidAuctionParams + } //need to get real price instead of hard coding //calculate target amount of cmst to collect if auctiontypes.TestFlag != 1 { @@ -953,6 +972,7 @@ func (k Keeper) CloseDutchAuction( //Transfer balance from collector module to auction module requiredAmount := lockedVault.AmountOut.Sub(burnToken.Amount) + _, err := k.GetAmountFromCollector(ctx, dutchAuction.AppId, dutchAuction.AssetInId, requiredAmount) if err != nil { @@ -966,17 +986,22 @@ func (k Keeper) CloseDutchAuction( } //burn the burn tokens - err := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, tokenminttypes.ModuleName, sdk.NewCoins(burnToken)) + err := k.BurnCoins(ctx, auctiontypes.ModuleName, burnToken) if err != nil { return err } + ExtendedPairVault, found := k.GetPairsVault(ctx, lockedVault.ExtendedPairId) + if !found { + return auctiontypes.ErrorInvalidExtendedPairVault + } - err = k.tokenmint.BurnTokensForApp(ctx, dutchAuction.AppId, dutchAuction.AssetInId, burnToken.Amount) - if err != nil { - - return err + appExtendedPairVaultData, found := k.GetAppExtendedPairVaultMapping(ctx, ExtendedPairVault.AppMappingId) + if !found { + return sdkerrors.ErrNotFound } + k.UpdateCollateralLockedAmountLockerMapping(ctx, appExtendedPairVaultData, ExtendedPairVault.Id, burnToken.Amount, false) + //send penalty err = k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(sdk.NewCoin(burnToken.Denom, penaltyAmount))) if err != nil { diff --git a/x/auction/keeper/store.go b/x/auction/keeper/store.go index 7071c5c22..ed1c4dcaf 100644 --- a/x/auction/keeper/store.go +++ b/x/auction/keeper/store.go @@ -101,8 +101,13 @@ func (k *Keeper) SetUserBiddingID(ctx sdk.Context, id uint64) { store.Set(key, value) } -func (k *Keeper) GetAuctionType(ctx sdk.Context, auctionTypeId uint64) (string, error) { - params := k.GetParams(ctx) +func (k *Keeper) GetAuctionType(ctx sdk.Context, auctionTypeId uint64, appId uint64) (string, error) { + + params, found := k.GetAuctionParams(ctx, appId) + + if !found { + return "", auctiontypes.ErrorInvalidAuctionParams + } if auctionTypeId == params.SurplusId { return auctiontypes.SurplusString, nil } else if auctionTypeId == params.DebtId { @@ -134,7 +139,7 @@ func (k *Keeper) GetAllAuctions(ctx sdk.Context) (auctions []auctiontypes.Surplu //SURPLUS func (k *Keeper) SetSurplusAuction(ctx sdk.Context, auction auctiontypes.SurplusAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -148,7 +153,7 @@ func (k *Keeper) SetSurplusAuction(ctx sdk.Context, auction auctiontypes.Surplus } func (k *Keeper) SetHistorySurplusAuction(ctx sdk.Context, auction auctiontypes.SurplusAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -162,7 +167,7 @@ func (k *Keeper) SetHistorySurplusAuction(ctx sdk.Context, auction auctiontypes. } func (k *Keeper) DeleteSurplusAuction(ctx sdk.Context, auction auctiontypes.SurplusAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -175,7 +180,7 @@ func (k *Keeper) DeleteSurplusAuction(ctx sdk.Context, auction auctiontypes.Surp } func (k *Keeper) GetSurplusAuction(ctx sdk.Context, appId, auctionMappingId, auctionId uint64) (auction auctiontypes.SurplusAuction, err error) { - auctionType, err := k.GetAuctionType(ctx, auctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auctionMappingId, appId) if err != nil { return auction, err } @@ -193,7 +198,7 @@ func (k *Keeper) GetSurplusAuction(ctx sdk.Context, appId, auctionMappingId, auc } func (k *Keeper) GetHistorySurplusAuction(ctx sdk.Context, appId, auctionMappingId, auctionId uint64) (auction auctiontypes.SurplusAuction, err error) { - auctionType, err := k.GetAuctionType(ctx, auctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auctionMappingId, appId) if err != nil { return auction, err } @@ -247,7 +252,7 @@ func (k *Keeper) GetHistorySurplusAuctions(ctx sdk.Context, appId uint64) (aucti } func (k *Keeper) SetSurplusUserBidding(ctx sdk.Context, userBiddings auctiontypes.SurplusBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -261,7 +266,7 @@ func (k *Keeper) SetSurplusUserBidding(ctx sdk.Context, userBiddings auctiontype } func (k *Keeper) SetHistorySurplusUserBidding(ctx sdk.Context, userBiddings auctiontypes.SurplusBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -275,7 +280,7 @@ func (k *Keeper) SetHistorySurplusUserBidding(ctx sdk.Context, userBiddings auct } func (k *Keeper) DeleteSurplusUserBidding(ctx sdk.Context, userBiddings auctiontypes.SurplusBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -356,7 +361,7 @@ func (k *Keeper) GetHistorySurplusUserBiddings(ctx sdk.Context, bidder string, a //DEBT func (k *Keeper) SetDebtAuction(ctx sdk.Context, auction auctiontypes.DebtAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -370,7 +375,7 @@ func (k *Keeper) SetDebtAuction(ctx sdk.Context, auction auctiontypes.DebtAuctio } func (k *Keeper) SetHistoryDebtAuction(ctx sdk.Context, auction auctiontypes.DebtAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -384,7 +389,7 @@ func (k *Keeper) SetHistoryDebtAuction(ctx sdk.Context, auction auctiontypes.Deb } func (k *Keeper) DeleteDebtAuction(ctx sdk.Context, auction auctiontypes.DebtAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -397,7 +402,7 @@ func (k *Keeper) DeleteDebtAuction(ctx sdk.Context, auction auctiontypes.DebtAuc } func (k *Keeper) GetDebtAuction(ctx sdk.Context, appId, auctionMappingId, auctionId uint64) (auction auctiontypes.DebtAuction, err error) { - auctionType, err := k.GetAuctionType(ctx, auctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auctionMappingId, appId) if err != nil { return auction, err } @@ -414,7 +419,7 @@ func (k *Keeper) GetDebtAuction(ctx sdk.Context, appId, auctionMappingId, auctio } func (k *Keeper) GetHistoryDebtAuction(ctx sdk.Context, appId, auctionMappingId, auctionId uint64) (auction auctiontypes.DebtAuction, err error) { - auctionType, err := k.GetAuctionType(ctx, auctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auctionMappingId, appId) if err != nil { return auction, err } @@ -467,7 +472,7 @@ func (k *Keeper) GetHistoryDebtAuctions(ctx sdk.Context, appId uint64) (auctions } func (k *Keeper) SetDebtUserBidding(ctx sdk.Context, userBiddings auctiontypes.DebtBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -481,7 +486,7 @@ func (k *Keeper) SetDebtUserBidding(ctx sdk.Context, userBiddings auctiontypes.D } func (k *Keeper) SetHistoryDebtUserBidding(ctx sdk.Context, userBiddings auctiontypes.DebtBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -495,7 +500,7 @@ func (k *Keeper) SetHistoryDebtUserBidding(ctx sdk.Context, userBiddings auction } func (k *Keeper) DeleteDebtUserBidding(ctx sdk.Context, userBiddings auctiontypes.DebtBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -572,7 +577,7 @@ func (k *Keeper) GetHistoryDebtUserBiddings(ctx sdk.Context, bidder string, appI //DUTCH func (k *Keeper) SetDutchAuction(ctx sdk.Context, auction auctiontypes.DutchAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -587,7 +592,7 @@ func (k *Keeper) SetDutchAuction(ctx sdk.Context, auction auctiontypes.DutchAuct } func (k *Keeper) SetHistoryDutchAuction(ctx sdk.Context, auction auctiontypes.DutchAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -601,7 +606,7 @@ func (k *Keeper) SetHistoryDutchAuction(ctx sdk.Context, auction auctiontypes.Du } func (k *Keeper) DeleteDutchAuction(ctx sdk.Context, auction auctiontypes.DutchAuction) error { - auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auction.AuctionMappingId, auction.AppId) if err != nil { return err } @@ -614,8 +619,10 @@ func (k *Keeper) DeleteDutchAuction(ctx sdk.Context, auction auctiontypes.DutchA } func (k *Keeper) GetDutchAuction(ctx sdk.Context, appId, auctionMappingId, auctionId uint64) (auction auctiontypes.DutchAuction, err error) { - auctionType, err := k.GetAuctionType(ctx, auctionMappingId) + + auctionType, err := k.GetAuctionType(ctx, auctionMappingId, appId) if err != nil { + return auction, err } var ( @@ -623,6 +630,7 @@ func (k *Keeper) GetDutchAuction(ctx sdk.Context, appId, auctionMappingId, aucti key = auctiontypes.AuctionKey(appId, auctionType, auctionId) value = store.Get(key) ) + if value == nil { return auction, sdkerrors.ErrNotFound } @@ -632,7 +640,7 @@ func (k *Keeper) GetDutchAuction(ctx sdk.Context, appId, auctionMappingId, aucti } func (k *Keeper) GetHistoryDutchAuction(ctx sdk.Context, appId, auctionMappingId, auctionId uint64) (auction auctiontypes.DutchAuction, err error) { - auctionType, err := k.GetAuctionType(ctx, auctionMappingId) + auctionType, err := k.GetAuctionType(ctx, auctionMappingId, appId) if err != nil { return auction, err } @@ -686,7 +694,7 @@ func (k *Keeper) GetHistoryDutchAuctions(ctx sdk.Context, appId uint64) (auction } func (k *Keeper) SetDutchUserBidding(ctx sdk.Context, userBiddings auctiontypes.DutchBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -700,7 +708,7 @@ func (k *Keeper) SetDutchUserBidding(ctx sdk.Context, userBiddings auctiontypes. } func (k *Keeper) SetHistoryDutchUserBidding(ctx sdk.Context, userBiddings auctiontypes.DutchBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -714,7 +722,7 @@ func (k *Keeper) SetHistoryDutchUserBidding(ctx sdk.Context, userBiddings auctio } func (k *Keeper) DeleteDutchUserBidding(ctx sdk.Context, userBiddings auctiontypes.DutchBiddings) error { - auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId) + auctionType, err := k.GetAuctionType(ctx, userBiddings.AuctionMappingId, userBiddings.AppId) if err != nil { return err } @@ -805,9 +813,11 @@ func (k *Keeper) SetAuctionParams(ctx sdk.Context, auctionParams auctiontypes.Au } func (k *Keeper) GetAuctionParams(ctx sdk.Context, AppId uint64) (asset auctiontypes.AuctionParams, found bool) { + key := auctiontypes.AuctionParamsKey(AppId) + var ( store = k.Store(ctx) - key = auctiontypes.AuctionParamsKey(AppId) + value = store.Get(key) ) diff --git a/x/auction/types/errors.go b/x/auction/types/errors.go index 7724f33bf..78dbc0b3c 100644 --- a/x/auction/types/errors.go +++ b/x/auction/types/errors.go @@ -43,6 +43,7 @@ var ( ErrorAppExtendedPairVaultData = sdkerrors.Register(ModuleName, 133, "extended pair vault data not found for app mapping id") ErrorDebtMaxBidFactor = sdkerrors.Register(ModuleName, 134, "bid should be less than bid amount by bid factor") ErrorLeaveChost = sdkerrors.Register(ModuleName, 135, "either bid all the amount or bid amount by leaving greater than chost") + ErrorInvalidAuctionParams = sdkerrors.Register(ModuleName, 136, "auction params not found for given app id") ) var (