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

Test cases fix #435

Merged
merged 9 commits into from
Sep 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
6 changes: 3 additions & 3 deletions x/asset/keeper/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ func (s *KeeperTestSuite) TestAddApp() {
s.Require().Equal(res.App.GenesisToken[0].GenesisSupply, tc.msg.GenesisToken[0].GenesisSupply)
s.Require().Equal(res.App.GenesisToken[0].Recipient, tc.msg.GenesisToken[0].Recipient)
s.Require().Equal(res.App.GenesisToken[0].IsGovToken, tc.msg.GenesisToken[0].IsGovToken)
err = assetKeeper.UpdateGovTimeInApp(*ctx, assetTypes.AppAndGovTime{AppId: tc.appID, GovTimeInSeconds: 653})
err = assetKeeper.UpdateGovTimeInApp(*ctx, assetTypes.AppAndGovTime{AppId: tc.appID, GovTimeInSeconds: 653, MinGovDeposit: sdk.NewIntFromUint64(5000000)})
s.Require().NoError(err)
minGovDeposit, govTimeInSeconds, _, err := assetKeeper.GetAppWasmQuery(*ctx, tc.appID)
s.Require().NoError(err)
s.Require().Equal(minGovDeposit, tc.msg.MinGovDeposit.Int64())
s.Require().Equal(minGovDeposit, sdk.NewIntFromUint64(5000000).Int64())
s.Require().Equal(govTimeInSeconds, int64(653))
}

Expand Down Expand Up @@ -180,7 +180,7 @@ func (s *KeeperTestSuite) TestUpdateAssetRecords() {
{"Update Asset cmdx ucmdx",
assetTypes.Asset{
Id: 1,
Name: "CMDX",
Name: "CMRT",
Denom: "ucmrt",
Decimals: 100,
IsOnChain: false,
Expand Down
3 changes: 3 additions & 0 deletions x/asset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
liquidationKeeper "github.com/comdex-official/comdex/x/liquidation/keeper"
"github.com/comdex-official/comdex/x/liquidation/types"
marketKeeper "github.com/comdex-official/comdex/x/market/keeper"
rewardsKeeper "github.com/comdex-official/comdex/x/rewards/keeper"
vaultKeeper "github.com/comdex-official/comdex/x/vault/keeper"
vaultTypes "github.com/comdex-official/comdex/x/vault/types"
)
Expand All @@ -31,6 +32,7 @@ type KeeperTestSuite struct {
querier assetKeeper.QueryServer
vaultQuerier vaultKeeper.QueryServer
vaultMsgServer vaultTypes.MsgServer
rewardsKeeper rewardsKeeper.Keeper
}

func TestKeeperTestSuite(t *testing.T) {
Expand All @@ -47,6 +49,7 @@ func (s *KeeperTestSuite) SetupTest() {
s.vaultMsgServer = vaultKeeper.NewMsgServer(s.vaultKeeper)
s.vaultQuerier = vaultKeeper.QueryServer{Keeper: s.vaultKeeper}
s.marketKeeper = s.app.MarketKeeper
s.rewardsKeeper = s.app.Rewardskeeper
}

//
Expand Down
1 change: 0 additions & 1 deletion x/auction/keeper/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func (k Keeper) closeDebtAuction(
debtAuction auctiontypes.DebtAuction,
statusEsm bool,
) error { //If there are bids

if statusEsm && debtAuction.BiddingIds != nil {
bidding, err := k.GetDebtUserBidding(ctx, debtAuction.Bidder.String(), debtAuction.AppId, debtAuction.ActiveBiddingId)
if err != nil {
Expand Down
22 changes: 15 additions & 7 deletions x/auction/keeper/debt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/comdex-official/comdex/app/wasm/bindings"
auctionKeeper "github.com/comdex-official/comdex/x/auction/keeper"
auctionTypes "github.com/comdex-official/comdex/x/auction/types"
esmtypes "github.com/comdex-official/comdex/x/esm/types"
tokenmintKeeper1 "github.com/comdex-official/comdex/x/tokenmint/keeper"
tokenminttypes "github.com/comdex-official/comdex/x/tokenmint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -166,7 +167,6 @@ func (s *KeeperTestSuite) TestDebtActivatorBetweenThreshholdAndLotsize() {
s.AddAuctionParams()
s.WasmSetCollectorLookupTableAndAuctionControlForDebt()
s.WasmUpdateCollectorLookupTable(30000, 20500, 800, 501)
s.LiquidateVaults1()

k, ctx := &s.keeper, &s.ctx

Expand All @@ -189,7 +189,6 @@ func (s *KeeperTestSuite) TestDebtActivator() {
s.AddPairAndExtendedPairVault1()
s.AddAuctionParams()
s.WasmSetCollectorLookupTableAndAuctionControlForDebt()
s.LiquidateVaults1()

k, collectorKeeper, ctx := &s.keeper, &s.collectorKeeper, &s.ctx

Expand All @@ -199,15 +198,24 @@ func (s *KeeperTestSuite) TestDebtActivator() {
auctionMappingId := uint64(2)
auctionId := uint64(1)

debtAuction, err := k.GetDebtAuction(*ctx, appId, auctionMappingId, auctionId)
s.Require().NoError(err)

collectorLookUp, found := collectorKeeper.GetCollectorLookupTable(*ctx, 1, 3)
collectorLookUp, found := collectorKeeper.GetCollectorLookupTable(*ctx, 1, 2)
s.Require().True(found)

netFees, found := k.GetNetFeeCollectedData(*ctx, uint64(1), 3)
auctionMapData, auctionMappingFound := k.GetAuctionMappingForApp(*ctx, appId, collectorLookUp.CollectorAssetId)
s.Require().True(auctionMappingFound)
klswData := esmtypes.KillSwitchParams{
1,
false,
}
err1 := k.DebtActivator(*ctx, auctionMapData, klswData, false)
s.Require().NoError(err1)

netFees, found := k.GetNetFeeCollectedData(*ctx, uint64(1), 2)
s.Require().True(found)

debtAuction, err := k.GetDebtAuction(*ctx, appId, auctionMappingId, auctionId)
s.Require().NoError(err)

s.Require().Equal(debtAuction.AppId, appId)
s.Require().Equal(debtAuction.AuctionId, auctionId)
s.Require().Equal(debtAuction.AuctionMappingId, auctionMappingId)
Expand Down
70 changes: 41 additions & 29 deletions x/auction/keeper/dutch.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ func (k Keeper) StartDutchAuction(
if !found {
return auctiontypes.ErrorInvalidAuctionParams
}
err := k.SendCoinsFromModuleToModule(ctx, vaulttypes.ModuleName, auctiontypes.ModuleName, sdk.NewCoins(outFlowToken))
if err != nil {
return err
if outFlowToken.Amount.GT(sdk.ZeroInt()) {
err := k.SendCoinsFromModuleToModule(ctx, vaulttypes.ModuleName, auctiontypes.ModuleName, sdk.NewCoins(outFlowToken))
if err != nil {
return err
}
}
//calculate target amount of cmst to collect
outFlowTokenPrice, found = k.GetPriceForAsset(ctx, assetOutID)
Expand Down Expand Up @@ -223,13 +225,17 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appID, auctionMappingID, a

outFlowTokenCoin := sdk.NewCoin(auction.OutflowTokenInitAmount.Denom, slice)

err = k.SendCoinsFromAccountToModule(ctx, bidder, auctiontypes.ModuleName, sdk.NewCoins(inFlowTokenCoin))
if err != nil {
return err
if inFlowTokenCoin.Amount.GT(sdk.ZeroInt()) {
err = k.SendCoinsFromAccountToModule(ctx, bidder, auctiontypes.ModuleName, sdk.NewCoins(inFlowTokenCoin))
if err != nil {
return err
}
}
err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, bidder, sdk.NewCoins(outFlowTokenCoin))
if err != nil {
return err
if outFlowTokenCoin.Amount.GT(sdk.ZeroInt()) {
err = k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, bidder, sdk.NewCoins(outFlowTokenCoin))
if err != nil {
return err
}
}

biddingID, err := k.CreateNewDutchBid(ctx, appID, auctionMappingID, auctionID, bidder.String(), inFlowTokenCoin, outFlowTokenCoin)
Expand All @@ -252,9 +258,11 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appID, auctionMappingID, a
//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))
if err != nil {
return err
if total.Amount.GT(sdk.ZeroInt()) {
err := k.SendCoinsFromModuleToAccount(ctx, auctiontypes.ModuleName, sdk.AccAddress(lockedVault.Owner), sdk.NewCoins(total))
if err != nil {
return err
}
}

err = k.SetDutchAuction(ctx, auction)
Expand All @@ -269,7 +277,6 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appID, auctionMappingID, a
return err
}
} else if auction.OutflowTokenCurrentAmount.Amount.IsZero() && auction.InflowTokenCurrentAmount.IsLT(auction.InflowTokenTargetAmount) { //entire collateral sold out

requiredAmount := auction.InflowTokenTargetAmount.Sub(auction.InflowTokenCurrentAmount)
_, err := k.GetAmountFromCollector(ctx, auction.AppId, auction.AssetInId, requiredAmount.Amount)
if err != nil {
Expand All @@ -290,7 +297,6 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appID, auctionMappingID, a
return err
}
} else if auction.OutflowTokenCurrentAmount.Amount.IsZero() { //entire collateral sold out

err = k.SetDutchAuction(ctx, auction)
if err != nil {
return err
Expand All @@ -302,7 +308,6 @@ func (k Keeper) PlaceDutchAuctionBid(ctx sdk.Context, appID, auctionMappingID, a
return err
}
} else {

err = k.SetDutchAuction(ctx, auction)
if err != nil {
return err
Expand Down Expand Up @@ -370,18 +375,22 @@ func (k Keeper) CloseDutchAuction(
penaltyCoin.Amount = dutchAuction.InflowTokenTargetAmount.Amount.Sub(burnToken.Amount)

//burning
err := k.BurnCoins(ctx, auctiontypes.ModuleName, burnToken)
if err != nil {
return err
if burnToken.Amount.GT(sdk.ZeroInt()) {
err := k.BurnCoins(ctx, auctiontypes.ModuleName, burnToken)
if err != nil {
return err
}
}

// send penalty
err = k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(penaltyCoin))
if err != nil {
return err
if penaltyCoin.Amount.GT(sdk.ZeroInt()) {
err := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(penaltyCoin))
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, penaltyCoin.Amount)
if err != nil {
return err
}
Expand Down Expand Up @@ -491,18 +500,22 @@ func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appID uint64) error {
vaultID, userExists := k.GetUserAppExtendedPairMappingData(ctx, string(dutchAuction.VaultOwner), dutchAuction.AppId, lockedVault.ExtendedPairId)
if userExists {
vaultData, _ := k.GetVault(ctx, vaultID.VaultId)
err := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, vaulttypes.ModuleName, sdk.NewCoins(dutchAuction.OutflowTokenCurrentAmount))
if err != nil {
return err
if dutchAuction.OutflowTokenCurrentAmount.Amount.GT(sdk.ZeroInt()) {
err := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, vaulttypes.ModuleName, sdk.NewCoins(dutchAuction.OutflowTokenCurrentAmount))
if err != nil {
return err
}
}
// append to existing vault
vaultData.AmountIn = vaultData.AmountIn.Add(dutchAuction.OutflowTokenCurrentAmount.Amount)
vaultData.AmountOut = vaultData.AmountOut.Add(inflowLeft)
k.SetVault(ctx, vaultData)
} else {
err1 := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, vaulttypes.ModuleName, sdk.NewCoins(dutchAuction.OutflowTokenCurrentAmount))
if err1 != nil {
return err1
if dutchAuction.OutflowTokenCurrentAmount.Amount.GT(sdk.ZeroInt()) {
err1 := k.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, vaulttypes.ModuleName, sdk.NewCoins(dutchAuction.OutflowTokenCurrentAmount))
if err1 != nil {
return err1
}
}
// create new vault done
err := k.CreateNewVault(ctx, dutchAuction.VaultOwner.String(), lockedVault.AppId, lockedVault.ExtendedPairId, dutchAuction.OutflowTokenCurrentAmount.Amount, inflowLeft)
Expand Down Expand Up @@ -537,7 +550,6 @@ func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appID uint64) error {
return err
}
k.DeleteLockedVault(ctx, lockedVault.AppId, lockedVault.LockedVaultId)

} else {
OutFlowTokenCurrentPrice, found := k.GetPriceForAsset(ctx, dutchAuction.AssetOutId)
if !found {
Expand Down
19 changes: 15 additions & 4 deletions x/auction/keeper/dutch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (s *KeeperTestSuite) LiquidateVaults1() {
price, found := s.app.MarketKeeper.GetPriceForAsset(*ctx, uint64(1))
s.Require().True(found)
s.Require().Equal(lockedVault[0].CollateralToBeAuctioned, beforeVault.AmountIn.ToDec().Mul(sdk.NewIntFromUint64(price).ToDec()))
s.Require().Equal(lockedVault[0].CurrentCollaterlisationRatio, lockedVault[0].AmountIn.ToDec().Mul(s.GetAssetPrice(1)).Quo(lockedVault[0].UpdatedAmountOut.ToDec().Mul(s.GetAssetPrice(2))))
s.Require().Equal(lockedVault[0].CrAtLiquidation, lockedVault[0].AmountIn.ToDec().Mul(s.GetAssetPrice(1)).Quo(lockedVault[0].UpdatedAmountOut.ToDec().Mul(s.GetAssetPrice(2))))
}

func (s *KeeperTestSuite) AddAuctionParams() {
Expand Down Expand Up @@ -313,10 +313,12 @@ func (s *KeeperTestSuite) TestDutchActivator() {
appId := uint64(1)
auctionMappingId := uint64(3)
auctionId := uint64(1)
dutchAuction, err := k.GetDutchAuction(*ctx, appId, auctionMappingId, auctionId)
s.Require().NoError(err)
lockedVault, found := liquidationKeeper.GetLockedVault(*ctx, 1, 1)
s.Require().True(found)
err = k.DutchActivator(*ctx, lockedVault)
s.Require().NoError(err)
dutchAuction, err := k.GetDutchAuction(*ctx, appId, auctionMappingId, auctionId)
s.Require().NoError(err)

s.Require().Equal(dutchAuction.AppId, lockedVault.AppId)
s.Require().Equal(dutchAuction.AuctionId, auctionId)
Expand Down Expand Up @@ -528,11 +530,20 @@ func (s *KeeperTestSuite) TestCloseDutchAuction() {

func (s *KeeperTestSuite) TestCloseDutchAuctionWithProtocolLoss() {
userAddress1 := "cosmos1q7q90qsl9g0gl2zz0njxwv2a649yqrtyxtnv3v"
s.AddAppAsset()
s.AddPairAndExtendedPairVault1()
s.LiquidateVaults1()
s.AddAuctionParams()
s.TestDutchBid()
k, ctx := &s.keeper, &s.ctx
k, liquidationKeeper, ctx := &s.keeper, &s.liquidationKeeper, &s.ctx
//k, ctx := &s.keeper, &s.ctx
appId := uint64(1)
auctionMappingId := uint64(3)
auctionId := uint64(1)
lockedVault, found := liquidationKeeper.GetLockedVault(*ctx, 1, 1)
s.Require().True(found)
err := k.DutchActivator(*ctx, lockedVault)
s.Require().NoError(err)
server := auctionKeeper.NewMsgServiceServer(*k)
beforeAuction, err := k.GetDutchAuction(*ctx, appId, auctionMappingId, auctionId)
s.Require().NoError(err)
Expand Down
3 changes: 3 additions & 0 deletions x/auction/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ func (q QueryServer) QueryFilterDutchAuctions(c context.Context, req *types.Quer
items []types.DutchAuction
ctx = sdk.UnwrapSDKContext(c)
key []byte
count = 0
)
if req.History {
key = types.HistoryAuctionTypeKey(req.AppId, types.DutchString)
Expand All @@ -551,6 +552,7 @@ func (q QueryServer) QueryFilterDutchAuctions(c context.Context, req *types.Quer
for _, data := range req.Denom {
if item.OutflowTokenCurrentAmount.Denom == data || item.InflowTokenCurrentAmount.Denom == data {
check = true
count ++
break
}
}
Expand All @@ -566,6 +568,7 @@ func (q QueryServer) QueryFilterDutchAuctions(c context.Context, req *types.Quer
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
pagination.Total = uint64(count)

return &types.QueryFilterDutchAuctionsResponse{
Auctions: items,
Expand Down
1 change: 0 additions & 1 deletion x/auction/keeper/surplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func (k Keeper) closeSurplusAuction(
if err2 != nil {
return auctiontypes.ErrorUnableToSetNetFees
}

} else if !statusEsm && surplusAuction.Bidder != nil {
highestBidReceived := surplusAuction.Bid

Expand Down
2 changes: 0 additions & 2 deletions x/auction/keeper/surplus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (s *KeeperTestSuite) TestSurplusActivatorBetweenThreshholdAndLotsize() {
s.AddAuctionParams()
s.WasmSetCollectorLookupTableAndAuctionControlForSurplus()
s.WasmUpdateCollectorLookupTable(19500, 1000, 501, 300)
s.LiquidateVaults1()

k, ctx := &s.keeper, &s.ctx

Expand All @@ -57,7 +56,6 @@ func (s *KeeperTestSuite) TestSurplusActivator() {
s.AddAuctionParams()
s.WasmSetCollectorLookupTableAndAuctionControlForSurplus()
s.WasmUpdateCollectorLookupTable(1000, 19800, 100, 300)
s.LiquidateVaults1()

k, collectorKeeper, ctx := &s.keeper, &s.collectorKeeper, &s.ctx

Expand Down
2 changes: 1 addition & 1 deletion x/collector/keeper/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (k Keeper) GetCollectorDataForAppIDAssetID(ctx sdk.Context, appID uint64, a
k.cdc.MustUnmarshal(value, &appAssetCollectorData)
collectorData = appAssetCollectorData.Collector

return collectorData, false
return collectorData, true
}

// SetCollectorLookupTable updates the collector lookup store.
Expand Down
12 changes: 6 additions & 6 deletions x/collector/keeper/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,20 @@ func (s *KeeperTestSuite) TestGetAmountFromCollector() {
} {

s.Run(tc.name, func() {
err := auctionKeeper.FundModule(*ctx, "auction", tc.denom, tc.FundAmount)
err := auctionKeeper.FundModule(*ctx, "auctionV1", tc.denom, tc.FundAmount)
s.Require().NoError(err)
err = auctionKeeper.SendCoinsFromModuleToModule(*ctx, "auction", "collector", sdk.NewCoins(sdk.NewCoin(tc.denom, sdk.NewIntFromUint64(tc.FundAmount))))
err = auctionKeeper.SendCoinsFromModuleToModule(*ctx, "auctionV1", "collectorV1", sdk.NewCoins(sdk.NewCoin(tc.denom, sdk.NewIntFromUint64(tc.FundAmount))))
s.Require().NoError(err)
beforeCollectorBalance := auctionKeeper.GetModuleAccountBalance(*ctx, "collector", tc.denom)
beforeCollectorBalance := auctionKeeper.GetModuleAccountBalance(*ctx, "collectorV1", tc.denom)
returnAmount, err := collectorKeeper.GetAmountFromCollector(*ctx, tc.appID, tc.assetID, tc.GetAmount)
if tc.errorExpected {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().Equal(tc.GetAmount, returnAmount)
auctionBalance := auctionKeeper.GetModuleAccountBalance(*ctx, "auction", tc.denom)
auctionBalance := auctionKeeper.GetModuleAccountBalance(*ctx, "auctionV1", tc.denom)
s.Require().Equal(tc.GetAmount, auctionBalance)
currentCollectorBalance := auctionKeeper.GetModuleAccountBalance(*ctx, "collector", tc.denom)
currentCollectorBalance := auctionKeeper.GetModuleAccountBalance(*ctx, "collectorV1", tc.denom)
s.Require().Equal(currentCollectorBalance, beforeCollectorBalance.Sub(tc.GetAmount))
}
})
Expand Down Expand Up @@ -438,7 +438,7 @@ func (s *KeeperTestSuite) TestUpdateCollector() {
CollectedStabilityFee: sdk.NewIntFromUint64(100),
CollectedClosingFee: sdk.NewIntFromUint64(200),
CollectedOpeningFee: sdk.NewIntFromUint64(300),
LiquidationRewardsCollected: sdk.NewIntFromUint64(400),
LiquidationRewardsCollected: sdk.NewIntFromUint64(500),
},
errorExpected: false,
},
Expand Down
Loading