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

changed burn mechanism in dutch auction and integrated auction params in auction module #216

Merged
merged 2 commits into from
Jun 8, 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
1 change: 1 addition & 0 deletions x/auction/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/auction/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
49 changes: 37 additions & 12 deletions x/auction/keeper/auction.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {

Expand All @@ -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 {
Expand Down
64 changes: 37 additions & 27 deletions x/auction/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -614,15 +619,18 @@ 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 (
store = k.Store(ctx)
key = auctiontypes.AuctionKey(appId, auctionType, auctionId)
value = store.Get(key)
)

if value == nil {
return auction, sdkerrors.ErrNotFound
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
)

Expand Down
1 change: 1 addition & 0 deletions x/auction/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down