Skip to content

Commit

Permalink
Merge pull request #243 from comdex-official/dutch_tests_1
Browse files Browse the repository at this point in the history
fixed dust calculation and target required cmst calculation
  • Loading branch information
cgsingh33 authored Jun 10, 2022
2 parents 88f009b + 63d4b68 commit 12f9e2d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
57 changes: 43 additions & 14 deletions x/auction/keeper/auction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"time"

"github.com/comdex-official/comdex/x/auction/types"
Expand Down Expand Up @@ -89,6 +90,7 @@ func (k Keeper) AddAppExtendedPairVaultMapping(ctx sdk.Context, lockedVaultId ui
return nil
}

//Here inflow calculated properly as lot size is in ucmst and out flow calculated based on price ratio (so no usd involved here)
//In surplus we need to sell cmst and get harbour . we know amount of cmst(outflow token) to sell but we need to get how much harbor to collect from user .
func (k Keeper) getSurplusInflowTokenAmount(ctx sdk.Context, appId, AssetInId, AssetOutId uint64, lotSize sdk.Int) (status uint64, outflowToken, inflowToken sdk.Coin) {
emptyCoin := sdk.NewCoin("empty", sdk.NewIntFromUint64(1))
Expand Down Expand Up @@ -125,6 +127,7 @@ func (k Keeper) getSurplusInflowTokenAmount(ctx sdk.Context, appId, AssetInId, A
return 5, outflowToken, inflowToken
}

//Here inflow calculated properly as lot size is in ucmst and out flow calculated based on price ratio (so no usd involved here)
// In debt we know amount of how much cmst to collect (inflow token) , but we need to know how much harbour(outflow token) to MINT and give it to the user
func (k Keeper) getDebtOutflowTokenAmount(ctx sdk.Context, appId, AssetInId, AssetOutId uint64, lotSize sdk.Int) (status uint64, outflowToken, inflowToken sdk.Coin) {
emptyCoin := sdk.NewCoin("empty", sdk.NewIntFromUint64(1))
Expand Down Expand Up @@ -358,6 +361,7 @@ func (k Keeper) CreateNewDutchAuctions(ctx sdk.Context) error {
return auctiontypes.ErrorPrices
}
//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
outflowToken := sdk.NewCoin(assetIn.Denom, lockedVault.CollateralToBeAuctioned.Quo(sdk.NewDecFromInt(sdk.NewIntFromUint64(assetInPrice))).TruncateInt())
inflowToken := sdk.NewCoin(assetOut.Denom, sdk.ZeroInt())

Expand Down Expand Up @@ -476,6 +480,9 @@ func (k Keeper) RestartSurplusAuction(
return nil
}

//This function does two things
//updates outflowtoken current price and inflow token current price no matter whether auction duration completed or price below end price for outflow token
//if auction duration completed or price decreased , then update start time,end time , outFlowTokenInitialPrice, outFlowTokenEndPrice,
//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)
Expand Down Expand Up @@ -616,6 +623,7 @@ func (k Keeper) StartDebtAuction(
return nil
}

//here assetInId and assetOutId are reverse of vault's assetinID , assetOutID
func (k Keeper) StartDutchAuction(
ctx sdk.Context,
outFlowToken sdk.Coin,
Expand Down Expand Up @@ -678,6 +686,7 @@ func (k Keeper) StartDutchAuction(
}
inFlowTokenTargetAmount := k.getInflowTokenTargetAmount(outFlowToken.Amount, sdk.NewIntFromUint64(inFlowTokenPrice), sdk.NewIntFromUint64(outFlowTokenPrice))
inFlowTokenTarget := sdk.NewCoin(inFlowToken.Denom, inFlowTokenTargetAmount)
//These prices are in uusd
outFlowTokenInitialPrice := k.getOutflowTokenInitialPrice(sdk.NewIntFromUint64(outFlowTokenPrice), auctionParams.Buffer)
outFlowTokenEndPrice := k.getOutflowTokenEndPrice(outFlowTokenInitialPrice, auctionParams.Cusp)
vaultOwner, err := sdk.AccAddressFromBech32(lockedVaultOwner)
Expand Down Expand Up @@ -984,12 +993,16 @@ func (k Keeper) CloseDutchAuction(
burnToken.Amount = lockedVault.AmountOut
}
}

fmt.Println("before burn tokens in auction")
fmt.Println(k.GetModuleAccountBalance(ctx, auctiontypes.ModuleName, "ucmst"))
fmt.Println("BURN TOKEN 1 : ", burnToken)
//burn the burn tokens
err := k.BurnCoins(ctx, auctiontypes.ModuleName, burnToken)
if err != nil {
return err
}
fmt.Println("after burn tokens in auction")
fmt.Println(k.GetModuleAccountBalance(ctx, auctiontypes.ModuleName, "ucmst"))
ExtendedPairVault, found := k.GetPairsVault(ctx, lockedVault.ExtendedPairId)
if !found {
return auctiontypes.ErrorInvalidExtendedPairVault
Expand All @@ -999,7 +1012,9 @@ func (k Keeper) CloseDutchAuction(
if !found {
return sdkerrors.ErrNotFound
}

fmt.Println("BURN TOKEN 1 : ", burnToken)
fmt.Println("appExtendedPairVaultData : ", appExtendedPairVaultData)
fmt.Println("ExtendedPairVault")
k.UpdateTokenMintedAmountLockerMapping(ctx, appExtendedPairVaultData, ExtendedPairVault.Id, burnToken.Amount, false)

//send penalty
Expand Down Expand Up @@ -1146,7 +1161,7 @@ func (k Keeper) PlaceSurplusBid(ctx sdk.Context, appId, auctionMappingId, auctio
if err != nil {
return err
}
biddingId, err := k.CreateNewSurplusBid(ctx, auctionId, auctionMappingId, auctionId, bidder, bid)
biddingId, err := k.CreateNewSurplusBid(ctx, appId, auctionMappingId, auctionId, bidder, bid)
if err != nil {
return err
}
Expand Down Expand Up @@ -1260,25 +1275,28 @@ func (k Keeper) PlaceDutchBid(ctx sdk.Context, appId, auctionMappingId, auctionI
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "bid denom %s not found", bid.Denom)
}

//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()
inFlowTokenCurrentPrice := auction.InflowTokenCurrentPrice.Ceil().TruncateInt()
slice := sdk.MinInt(bid.Amount, auction.OutflowTokenCurrentAmount.Amount)
//amount in usd to be given to user
//owe is in usd * 10**-12
owe := slice.Mul(outFlowTokenCurrentPrice)
//required target cmst to raise in usd
tab := auction.InflowTokenTargetAmount.Amount.Mul(inFlowTokenCurrentPrice).Sub(auction.InflowTokenCurrentAmount.Amount)
//required target cmst to raise in usd * 10**-12
//here we are multiplying each ucmdx with uusd so cmdx tokens price will be calculated amount * 10**-12
tab := auction.InflowTokenTargetAmount.Amount.Mul(inFlowTokenCurrentPrice).Sub(auction.InflowTokenCurrentAmount.Amount.Mul(inFlowTokenCurrentPrice))

// here price ratio is taken so no problem whether price is usd or uusd
inFlowTokenAmount := slice.ToDec().Mul(outFlowTokenCurrentPrice.ToDec()).Quo(inFlowTokenCurrentPrice.ToDec()).Ceil().TruncateInt()
inFlowTokenCoin := sdk.NewCoin(auction.InflowTokenTargetAmount.Denom, inFlowTokenAmount)
//check if bid in usd is greater than required target cmst in usd

//here we are getting min dust for asset to be left in uusd(usd*10**-6)
lockedVault, found := k.GetLockedVault(ctx, auction.LockedVaultId)
if !found {
return auctiontypes.ErrorInvalidLockedVault
Expand All @@ -1287,18 +1305,29 @@ func (k Keeper) PlaceDutchBid(ctx sdk.Context, appId, auctionMappingId, auctionI
if !found {
return auctiontypes.ErrorInvalidExtendedPairVault
}
dust := sdk.NewIntFromUint64(ExtendedPairVault.MinUsdValueLeft)
amountLeft := auction.OutflowTokenCurrentAmount.Amount.ToDec().Sub(slice.ToDec()).Mul(outFlowTokenCurrentPrice.ToDec()).TruncateInt()

//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
amountLeftInPUSD := auction.OutflowTokenCurrentAmount.Amount.ToDec().Sub(slice.ToDec()).Mul(outFlowTokenCurrentPrice.ToDec())
//convert amountLeft to uusd from pusd(10**-12) so we can compare dust and amountLeft in UUSD . this happens by converting ucmdx to cmdx
amountLeft := amountLeftInPUSD.Quo(sdk.MustNewDecFromStr("1000000")).TruncateInt()

//check if bid in usd*10**-12 is greater than required target cmst in usd*10**-12
//if user wants to buy more than target cmst then user should be sold only required cmst amount
//so we need to divide tab by outflow token current price and we get outflowtoken amount to be sold to user
//if user is not buying more than required cmst then we check for dust
//here tab is divided by price again so price ration is only considered , we are not using owe again in this function so no problem
//As tab is the amount calculated from difference of target and current inflow token we will be using same as inflow token
if owe.GT(tab) && !auction.IsLockedVaultAmountInZero {
slice = tab.Quo(auction.OutflowTokenCurrentPrice.Ceil().TruncateInt())
inFlowTokenCoin.Amount = auction.InflowTokenTargetAmount.Amount.Sub(auction.InflowTokenCurrentAmount.Amount)
} else if amountLeft.LT(dust) && amountLeft.GT(sdk.ZeroInt()) {
//(outflowtokenavailableamount-slice) in usd < chost in usd
//see if user has balance to buy whole collateral
//(outflowtokenavailableamount-slice) in uusd < chost in uusd
//so ask user to increase his bid or decrease do that more than dust is left .
coll := auction.OutflowTokenCurrentAmount.Amount.Uint64()
dust := dust.Uint64() / 1000000
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "either bid all the amount %d or bid amount by leaving dust greater than %d usd", coll, dust)
dust := dust.Uint64()
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "either bid all the amount %d (UTOKEN) or bid amount by leaving dust greater than %d UUSD", coll, dust)
}

outFlowTokenCoin := sdk.NewCoin(auction.OutflowTokenInitAmount.Denom, slice)
Expand Down Expand Up @@ -1327,7 +1356,7 @@ func (k Keeper) PlaceDutchBid(ctx sdk.Context, appId, auctionMappingId, auctionI
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
//if inflow token current amount >= InflowTokenTargetAmount
if auction.InflowTokenCurrentAmount.IsGTE(auction.InflowTokenTargetAmount) && !auction.IsLockedVaultAmountInZero {
//send left overcollateral to vault owner as target cmst reached and also

Expand Down
1 change: 1 addition & 0 deletions x/auction/keeper/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Sub(a, b sdk.Dec) sdk.Dec {
return a.Sub(b)
}

//Here we are taking ratio of price so no problem with units of price
func (k Keeper) getInflowTokenTargetAmount(amount, inFlowTokenPrice, outFlowTokenPrice sdk.Int) sdk.Int {
result := amount.ToDec().Mul(outFlowTokenPrice.ToDec()).Quo(inFlowTokenPrice.ToDec()).Ceil().TruncateInt()
return result
Expand Down

0 comments on commit 12f9e2d

Please sign in to comment.