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

Adding Liquidation unit tests #308

Merged
merged 6 commits into from
Jul 2, 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: 0 additions & 1 deletion x/collector/keeper/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (s *KeeperTestSuite) AddAuctionParams() {
s.auctionKeeper.SetAuctionParams(*ctx, auctionParams)
}


func (s *KeeperTestSuite) TestWasmUpdateCollectorLookupTable() {
collectorKeeper, ctx := &s.collectorKeeper, &s.ctx
s.TestWasmSetCollectorLookupTableAndAuctionControl()
Expand Down
5 changes: 2 additions & 3 deletions x/collector/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package keeper_test

import (
"fmt"
"time"

"github.com/stretchr/testify/suite"
"testing"

"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

Expand Down Expand Up @@ -82,7 +82,6 @@ func (s *KeeperTestSuite) fundAddr(addr string, amt sdk.Coin) {

func (s *KeeperTestSuite) advanceseconds(dur int64) {
s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Second * time.Duration(dur)))
fmt.Println(s.ctx.BlockTime())
}

// ParseCoins parses and returns sdk.Coins.
Expand Down
104 changes: 104 additions & 0 deletions x/liquidation/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package keeper_test

import (
"time"

"testing"

"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

chain "github.com/comdex-official/comdex/app"
assetKeeper "github.com/comdex-official/comdex/x/asset/keeper"
"github.com/comdex-official/comdex/x/liquidation/keeper"
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"
vaultKeeper "github.com/comdex-official/comdex/x/vault/keeper"
vaultTypes "github.com/comdex-official/comdex/x/vault/types"
)

type KeeperTestSuite struct {
suite.Suite

app *chain.App
ctx sdk.Context
vaultKeeper vaultKeeper.Keeper
assetKeeper assetKeeper.Keeper
liquidationKeeper liquidationKeeper.Keeper
marketKeeper marketKeeper.Keeper
querier liquidationKeeper.QueryServer
vaultQuerier vaultKeeper.QueryServer
msgServer types.MsgServer
vaultMsgServer vaultTypes.MsgServer
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (s *KeeperTestSuite) SetupTest() {
s.app = chain.Setup(false)
s.ctx = s.app.BaseApp.NewContext(false, tmproto.Header{})
s.vaultKeeper = s.app.VaultKeeper
s.liquidationKeeper = s.app.LiquidationKeeper
s.assetKeeper = s.app.AssetKeeper
s.querier = keeper.QueryServer{Keeper: s.liquidationKeeper}
s.msgServer = keeper.NewMsgServer(s.liquidationKeeper)
s.vaultMsgServer = vaultKeeper.NewMsgServer(s.vaultKeeper)
s.vaultQuerier = vaultKeeper.QueryServer{Keeper: s.vaultKeeper}
s.marketKeeper = s.app.MarketKeeper
}

//
//// Below are just shortcuts to frequently-used functions.
//func (s *KeeperTestSuite) getBalances(addr sdk.AccAddress) sdk.Coins {
// return s.app.bankKeeper.GetAllBalances(s.ctx, addr)
//}
//
//func (s *KeeperTestSuite) getBalance(addr sdk.AccAddress, denom string) sdk.Coin {
// return s.app.bankKeeper.GetBalance(s.ctx, addr, denom)
//}
//
//func (s *KeeperTestSuite) sendCoins(fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) {
// s.T().Helper()
// err := s.app.bankKeeper.SendCoins(s.ctx, fromAddr, toAddr, amt)
// s.Require().NoError(err)
//}
//
//func (s *KeeperTestSuite) nextBlock() {
// liquidity.EndBlocker(s.ctx, s.keeper)
// liquidity.BeginBlocker(s.ctx, s.keeper)
//}
//
//// Below are useful helpers to write test code easily.
//func (s *KeeperTestSuite) addr(addrNum int) sdk.AccAddress {
// addr := make(sdk.AccAddress, 20)
// binary.PutVarint(addr, int64(addrNum))
// return addr
//}

func (s *KeeperTestSuite) fundAddr(addr sdk.AccAddress, amt sdk.Coin) {
amt1 := sdk.NewCoins(amt)
s.T().Helper()
err := s.app.BankKeeper.MintCoins(s.ctx, types.ModuleName, amt1)
s.Require().NoError(err)
err = s.app.BankKeeper.SendCoinsFromModuleToAccount(s.ctx, types.ModuleName, addr, amt1)
s.Require().NoError(err)
}

func (s *KeeperTestSuite) advanceseconds(dur int64) {
s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Second * time.Duration(dur)))

}

// ParseCoins parses and returns sdk.Coins.
func ParseCoin(s string) sdk.Coin {
coins, err := sdk.ParseCoinNormalized(s)
if err != nil {
panic(err)
}
return coins
}
26 changes: 13 additions & 13 deletions x/liquidation/keeper/liquidate_vaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ func (k Keeper) CreateLockedVault(ctx sdk.Context, vault vaulttypes.Vault, colla

k.SetLockedVault(ctx, value)
k.SetLockedVaultID(ctx, value.LockedVaultId)

return nil
}

func (k Keeper) UpdateLockedVaultsAppMapping(ctx sdk.Context, lockedVault types.LockedVault) {
LockedVaultToApp, _ := k.GetLockedVaultByAppID(ctx, lockedVault.AppId)
LockedVaultToApp.LockedVault = append(LockedVaultToApp.LockedVault, &lockedVault)

newLockedVaultToApp := types.LockedVaultToAppMapping{
AppId: lockedVault.AppId,
LockedVault: LockedVaultToApp.LockedVault,
}
k.SetLockedVaultByAppID(ctx, newLockedVaultToApp)
}
//
//func (k Keeper) UpdateLockedVaultsAppMapping(ctx sdk.Context, lockedVault types.LockedVault) {
// LockedVaultToApp, _ := k.GetLockedVaultByAppID(ctx, lockedVault.AppMappingId)
// for index, vault := range LockedVaultToApp.LockedVault {
// if vault.OriginalVaultId == lockedVault.OriginalVaultId {
// LockedVaultToApp.LockedVault[index] = &lockedVault
// }
// }
//
// k.SetLockedVaultByAppID(ctx, LockedVaultToApp)
//}

func (k Keeper) SetLockedVaultByAppID(ctx sdk.Context, msg types.LockedVaultToAppMapping) {
var (
Expand Down Expand Up @@ -143,12 +143,12 @@ func (k Keeper) UpdateLockedVaults(ctx sdk.Context) error {
assetInPrice, _ := k.GetPriceForAsset(ctx, assetIn.Id)

totalIn := lockedVault.AmountIn.Mul(sdk.NewIntFromUint64(assetInPrice)).ToDec()

updatedLockedVault := lockedVault
updatedLockedVault.CurrentCollaterlisationRatio = collateralizationRatio
updatedLockedVault.CollateralToBeAuctioned = totalIn
k.SetLockedVault(ctx, updatedLockedVault)
k.UpdateLockedVaultsAppMapping(ctx, updatedLockedVault)
//k.UpdateLockedVaultsAppMapping(ctx, updatedLockedVault)

}
}
}
Expand Down
Loading