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

Testdev auction #251

Merged
merged 2 commits into from
Jun 16, 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
8 changes: 4 additions & 4 deletions proto/comdex/auction/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ message MsgPlaceDutchBidRequest {
uint64 auction_id = 1;
string bidder = 2;
cosmos.base.v1beta1.Coin amount = 3 [(gogoproto.nullable) = false];
// string max= 4 [
// (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
// (gogoproto.nullable) = false,
// (gogoproto.moretags) = "yaml:\"max\""];
string max= 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"max\""];
uint64 app_id =5;
uint64 auction_mapping_id =6;
}
Expand Down
10 changes: 6 additions & 4 deletions x/auction/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func txPlaceDebtBid() *cobra.Command {

func txPlaceDutchBid() *cobra.Command {
cmd := &cobra.Command{
Use: "bid-dutch [auction-id] [amount] [app-id] [auction-mapping-id]",
Use: "bid-dutch [auction-id] [amount] [maxamountpercollateraltoken] [app-id] [auction-mapping-id]",
Short: "Place a Dutch bid on an auction",
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -131,12 +131,14 @@ func txPlaceDutchBid() *cobra.Command {
return fmt.Errorf("auction-id '%s' not a valid uint", args[0])
}

auctionMappingId, err := strconv.ParseUint(args[3], 10, 64)
max := sdk.MustNewDecFromStr(args[3])

auctionMappingId, err := strconv.ParseUint(args[4], 10, 64)
if err != nil {
return fmt.Errorf("auction-id '%s' not a valid uint", args[0])
}

msg := types.NewMsgPlaceDutchBid(clientCtx.GetFromAddress(), auctionId, amt, appId, auctionMappingId)
msg := types.NewMsgPlaceDutchBid(clientCtx.GetFromAddress(), auctionId, amt, max, appId, auctionMappingId)
err = msg.ValidateBasic()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion x/auction/keeper/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (k Keeper) checkStatusOfNetFeesCollectedAndStartDebtAuction(ctx sdk.Context
assetInId := collector.CollectorAssetId
assetOutId := collector.SecondaryAssetId
//net = 200 debtThreshhold = 500 , lotsize = 100
amount := sdk.NewIntFromUint64(collector.DebtThreshold).Sub(AssetIdToFeeCollected.NetFeesCollected)
amount := sdk.NewIntFromUint64(collector.LotSize)

status, outflowToken, inflowToken := k.getDebtSellTokenAmount(ctx, appId, assetInId, assetOutId, amount)
if status == auctiontypes.NoAuction {
Expand Down
75 changes: 44 additions & 31 deletions x/auction/keeper/dutch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"fmt"
"time"

vaulttypes "github.com/comdex-official/comdex/x/vault/types"
Expand Down Expand Up @@ -185,7 +184,7 @@ func (k Keeper) StartDutchAuction(
return nil
}

func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, auctionId uint64, bidder sdk.AccAddress, bid sdk.Coin) error {
func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, auctionId uint64, bidder sdk.AccAddress, bid sdk.Coin, max sdk.Dec) error {
auction, err := k.GetDutchAuction(ctx, appId, auctionMappingId, auctionId)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "auction id %d not found", auctionId)
Expand All @@ -197,6 +196,13 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "bid denom can't be greater than collateral available")
}

max = k.GetUUSDFromUSD(ctx, max)

//Here OutflowToken current price is in uusd and max is in uusd
if max.LT(auction.OutflowTokenCurrentPrice.Ceil()) {
return auctiontypes.ErrorInvalidDutchPrice
}

// slice tells amount of collateral user should be given
//using ceil as we need extract more from users
outFlowTokenCurrentPrice := auction.OutflowTokenCurrentPrice.Ceil().TruncateInt() //cmdx
Expand All @@ -208,12 +214,15 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a
b:= auction.InflowTokenCurrentAmount.Amount
tab := a.Sub(b)
// extra will be send to collector
extraCoin := sdk.NewCoin(auction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
// extraCoin := sdk.NewCoin(auction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
//owe is $cmdx to be given to user
owe := slice.Mul(outFlowTokenCurrentPrice)
inFlowTokenAmount := owe.Quo(inFlowTokenCurrentPrice)
if inFlowTokenAmount.GT(tab) {
extraCoin.Amount = inFlowTokenAmount.Sub(tab)
// extraCoin.Amount = inFlowTokenAmount.Sub(tab)
inFlowTokenAmount = tab
owe = inFlowTokenAmount.Mul(inFlowTokenCurrentPrice)
slice = owe.Quo(outFlowTokenCurrentPrice)
}
inFlowTokenCoin := sdk.NewCoin(auction.InflowTokenTargetAmount.Denom, inFlowTokenAmount)

Expand Down Expand Up @@ -270,14 +279,14 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a
if err != nil {
return err
}
err = k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(extraCoin))
if err != nil {
return err
}
err = k.SetNetFeeCollectedData(ctx, auction.AppId, auction.AssetOutId, extraCoin.Amount)
if err != nil {
return err
}
// err = k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(extraCoin))
// if err != nil {
// return err
// }
// err = k.SetNetFeeCollectedData(ctx, auction.AppId, auction.AssetOutId, extraCoin.Amount)
// if err != nil {
// return err
// }
//create user bidding
biddingId, err := k.CreateNewDutchBid(ctx, appId, auctionMappingId, auctionId, bidder, inFlowTokenCoin, outFlowTokenCoin)
if err != nil {
Expand All @@ -291,7 +300,7 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a

//calculate inflow amount and outflow amount if user transaction successfull
auction.OutflowTokenCurrentAmount = auction.OutflowTokenCurrentAmount.Sub(outFlowTokenCoin)
auction.InflowTokenCurrentAmount = auction.InflowTokenCurrentAmount.Add(inFlowTokenCoin.Sub(extraCoin))
auction.InflowTokenCurrentAmount = auction.InflowTokenCurrentAmount.Add(inFlowTokenCoin)

//collateral not over but target cmst reached then send remaining collateral to owner
//if inflow token current amount >= InflowTokenTargetAmount
Expand All @@ -307,7 +316,7 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a
// if err != nil {
// return err
// }
auction.ToBurnAmount = auction.ToBurnAmount.Add(inFlowTokenCoin.Sub(extraCoin))
auction.ToBurnAmount = auction.ToBurnAmount.Add(inFlowTokenCoin)

err = k.SetDutchAuction(ctx, auction)
if err != nil {
Expand All @@ -322,7 +331,7 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a
}
} else if auction.OutflowTokenCurrentAmount.Amount.IsZero() { //entire collateral sold out

auction.ToBurnAmount = auction.ToBurnAmount.Add(inFlowTokenCoin.Sub(extraCoin))
auction.ToBurnAmount = auction.ToBurnAmount.Add(inFlowTokenCoin)

err = k.SetDutchAuction(ctx, auction)
if err != nil {
Expand All @@ -336,7 +345,7 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appId, auctionMappingId, a
return err
}
} else {
auction.ToBurnAmount = auction.ToBurnAmount.Add(inFlowTokenCoin.Sub(extraCoin))
auction.ToBurnAmount = auction.ToBurnAmount.Add(inFlowTokenCoin)


err = k.SetDutchAuction(ctx, auction)
Expand Down Expand Up @@ -402,14 +411,19 @@ func (k Keeper) CloseDutchAuction(
}

//calculate penalty
penaltyCoin := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
// penaltyCoin := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
// penaltyCoin.Amount = dutchAuction.InflowTokenCurrentAmount.Amount.Mul(dutchAuction.LiquidationPenalty.TruncateInt())
// burn and send target CMST to collector
burnToken := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
// burnToken := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())

// penaltyCoin.Amount = (burnToken.Amount.Mul(dutchAuction.LiquidationPenalty.TruncateInt())).Quo(dutchAuction.LiquidationPenalty.TruncateInt().Add(sdk.NewIntFromUint64(1)))
// burnToken.Amount = dutchAuction.ToBurnAmount.Amount.Sub(penaltyCoin.Amount)

newcmstRecover := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
cmstRecovered := lockedVault.CollateralToBeAuctioned.Quo(dutchAuction.LiquidationPenalty.Add(sdk.MustNewDecFromStr("1")))
newcmstRecover.Amount = cmstRecovered.Quo(sdk.NewDec(1000000)).TruncateInt()
sendtoCollector := dutchAuction.ToBurnAmount.Amount.ToDec().Sub(sdk.Dec(newcmstRecover.Amount))

penaltyCoin.Amount = (burnToken.Amount.Mul(dutchAuction.LiquidationPenalty.TruncateInt())).Quo(dutchAuction.LiquidationPenalty.TruncateInt().Add(sdk.NewIntFromUint64(1)))
burnToken.Amount = dutchAuction.ToBurnAmount.Amount.Sub(penaltyCoin.Amount)
fmt.Print("burnToken amount", burnToken.Amount)
//doing burn amount = inflowtokencurrentamount / (1 + liq_penalty)
// burnToken.Amount = burnToken.Amount.Add(k.getBurnAmount(dutchAuction.InflowTokenCurrentAmount.Amount, dutchAuction.LiquidationPenalty))
// burnToken.Amount = dutchAuction.InflowTokenCurrentAmount.Amount.Sub(penaltyAmount)
Expand Down Expand Up @@ -440,27 +454,26 @@ func (k Keeper) CloseDutchAuction(
}

//burning
err1 := k.BurnCoins(ctx, auctiontypes.ModuleName, burnToken.Sub(penaltyCoin))
err1 := k.BurnCoins(ctx, auctiontypes.ModuleName, newcmstRecover)
if err1 != nil {
return err1
}

//send penalty
err := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(sdk.NewCoin(penaltyCoin.Denom, penaltyCoin.Amount)))
err := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(sdk.NewCoin(newcmstRecover.Denom, sendtoCollector.TruncateInt())))
if err != nil {
return err
}
//call increase function in collector
err = k.SetNetFeeCollectedData(ctx, dutchAuction.AppId, dutchAuction.AssetInId, penaltyCoin.Amount)
err = k.SetNetFeeCollectedData(ctx, dutchAuction.AppId, dutchAuction.AssetInId, sendtoCollector.TruncateInt())
if err != nil {
return err
}
lockedVault.AmountIn = lockedVault.AmountIn.Sub(dutchAuction.OutflowTokenInitAmount.Amount.Sub(dutchAuction.OutflowTokenCurrentAmount.Amount))

lockedVault.AmountOut = lockedVault.AmountOut.Sub(burnToken.Amount)
lockedVault.UpdatedAmountOut = lockedVault.UpdatedAmountOut.Sub(burnToken.Amount)
fmt.Println("lockedVault.AmountIn",lockedVault.AmountIn)
fmt.Println("lockedVault.AmountOut",lockedVault.AmountOut)
lockedVault.AmountOut = lockedVault.AmountOut.Sub(newcmstRecover.Amount)
lockedVault.UpdatedAmountOut = lockedVault.UpdatedAmountOut.Sub(newcmstRecover.Amount)


//set sell of history in locked vault
outFlowToken := dutchAuction.OutflowTokenInitAmount.Sub(dutchAuction.OutflowTokenCurrentAmount)
Expand All @@ -471,7 +484,7 @@ func (k Keeper) CloseDutchAuction(

dutchAuction.AuctionStatus = auctiontypes.AuctionEnded

err = k.UpdateProtocolData(ctx, dutchAuction, burnToken)
err = k.UpdateProtocolData(ctx, dutchAuction, newcmstRecover)
if err != nil {
return err
}
Expand Down Expand Up @@ -566,7 +579,7 @@ func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appId uint64) error {
return nil
}

func (k Keeper) UpdateProtocolData(ctx sdk.Context, auction auctiontypes.DutchAuction, burnToken sdk.Coin ) error {
func (k Keeper) UpdateProtocolData(ctx sdk.Context, auction auctiontypes.DutchAuction, newcmstRecover sdk.Coin ) error {
lockedVault, found1 := k.GetLockedVault(ctx, auction.LockedVaultId)
if !found1 {
return auctiontypes.ErrorVaultNotFound
Expand All @@ -582,7 +595,7 @@ func (k Keeper) UpdateProtocolData(ctx sdk.Context, auction auctiontypes.DutchAu
return sdkerrors.ErrNotFound
}

k.UpdateTokenMintedAmountLockerMapping(ctx, appExtendedPairVaultData, ExtendedPairVault.Id, burnToken.Amount, false)
k.UpdateTokenMintedAmountLockerMapping(ctx, appExtendedPairVaultData, ExtendedPairVault.Id, newcmstRecover.Amount, false)
k.UpdateCollateralLockedAmountLockerMapping(ctx, appExtendedPairVaultData, ExtendedPairVault.Id, auction.OutflowTokenInitAmount.Amount.Sub(auction.OutflowTokenCurrentAmount.Amount), false)
return nil
}
3 changes: 2 additions & 1 deletion x/auction/keeper/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (k Keeper) getOutflowTokenInitialPrice(price sdk.Int, buffer sdk.Dec) sdk.D
}

func (k Keeper) getOutflowTokenEndPrice(price, cusp , liquidationPenalty sdk.Dec) sdk.Dec {
result := price.Sub(Multiply(price, liquidationPenalty))
denomination := liquidationPenalty.Add(sdk.MustNewDecFromStr("1"))
result := price.Quo(denomination)
return result
}

Expand Down
2 changes: 1 addition & 1 deletion x/auction/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (k msgServer) MsgPlaceDutchBid(goCtx context.Context, msg *types.MsgPlaceDu
if err != nil {
return nil, err
}
err = k.PlaceDutchAuctionBid(ctx, msg.AppId, msg.AuctionMappingId, msg.AuctionId, bidder, msg.Amount)
err = k.PlaceDutchAuctionBid(ctx, msg.AppId, msg.AuctionMappingId, msg.AuctionId, bidder, msg.Amount, msg.Max)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/auction/keeper/surplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (k Keeper) checkStatusOfNetFeesCollectedAndStartSurplusAuction(ctx sdk.Cont
assetSellId := collector.CollectorAssetId

//net = 900 surplusThreshhold = 500 , lotsize = 100
amount := AssetIdToFeeCollected.NetFeesCollected.Sub(sdk.NewIntFromUint64(collector.SurplusThreshold))
amount := sdk.NewIntFromUint64(collector.LotSize)

status, sellToken, buyToken := k.getSurplusBuyTokenAmount(ctx, appId, assetBuyId, assetSellId, amount)

Expand Down
4 changes: 2 additions & 2 deletions x/auction/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func (m *MsgPlaceDebtBidRequest) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{from}
}

func NewMsgPlaceDutchBid(from sdk.AccAddress, auctionID uint64, amt sdk.Coin, appId, auctionMappingId uint64) *MsgPlaceDutchBidRequest {
func NewMsgPlaceDutchBid(from sdk.AccAddress, auctionID uint64, amt sdk.Coin, max sdk.Dec, appId, auctionMappingId uint64) *MsgPlaceDutchBidRequest {
return &MsgPlaceDutchBidRequest{
Bidder: from.String(),
AuctionId: auctionID,
Amount: amt,
// Max: max,
Max: max,
AppId: appId,
AuctionMappingId: auctionMappingId,
}
Expand Down
Loading