0g-chain/x/cdp/keeper/auctions_test.go

169 lines
6.0 KiB
Go
Raw Normal View History

package keeper_test
import (
"testing"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-04-30 14:13:31 +00:00
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/app"
auctiontypes "github.com/0glabs/0g-chain/x/auction/types"
"github.com/0glabs/0g-chain/x/cdp/keeper"
"github.com/0glabs/0g-chain/x/cdp/types"
"github.com/stretchr/testify/suite"
2020-04-30 14:23:41 +00:00
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
"github.com/cometbft/cometbft/crypto"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
)
type AuctionTestSuite struct {
suite.Suite
keeper keeper.Keeper
app app.TestApp
ctx sdk.Context
addrs []sdk.AccAddress
}
func (suite *AuctionTestSuite) SetupTest() {
config := sdk.GetConfig()
app.SetBech32AddressPrefixes(config)
tApp := app.NewTestApp()
taddr := sdk.AccAddress(crypto.AddressHash([]byte("KavaTestUser1")))
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), cs(c("usdx", 21000000000)), []sdk.AccAddress{taddr})
tApp.InitializeFromGenesisStates(
authGS,
NewPricefeedGenStateMulti(tApp.AppCodec()),
NewCDPGenStateMulti(tApp.AppCodec()),
)
keeper := tApp.GetCDPKeeper()
suite.app = tApp
suite.ctx = ctx
suite.keeper = keeper
suite.addrs = []sdk.AccAddress{taddr}
}
func (suite *AuctionTestSuite) TestNetDebtSurplus() {
bk := suite.app.GetBankKeeper()
ak := suite.app.GetAccountKeeper()
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 100)))
suite.NoError(err)
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 10)))
suite.NoError(err)
suite.NotPanics(func() {
err := suite.keeper.NetSurplusAndDebt(suite.ctx)
suite.NoError(err)
})
acc := ak.GetModuleAccount(suite.ctx, types.LiquidatorMacc)
suite.Equal(cs(c("debt", 90)), bk.GetAllBalances(suite.ctx, acc.GetAddress()))
}
func (suite *AuctionTestSuite) TestCollateralAuction() {
bk := suite.app.GetBankKeeper()
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 21000000000), c("bnb", 190000000000)))
suite.Require().NoError(err)
testDeposit := types.NewDeposit(1, suite.addrs[0], c("bnb", 190000000000))
err = suite.keeper.AuctionCollateral(suite.ctx, types.Deposits{testDeposit}, "bnb-a", i(21000000000), "usdx")
suite.Require().NoError(err)
}
func (suite *AuctionTestSuite) TestSurplusAuction() {
bk := suite.app.GetBankKeeper()
ak := suite.app.GetAccountKeeper()
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 600000000000)))
suite.NoError(err)
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 100000000000)))
suite.NoError(err)
err = suite.keeper.RunSurplusAndDebtAuctions(suite.ctx)
suite.NoError(err)
acc := ak.GetModuleAccount(suite.ctx, auctiontypes.ModuleName)
suite.Equal(cs(c("usdx", 10000000000)), bk.GetAllBalances(suite.ctx, acc.GetAddress()))
acc = ak.GetModuleAccount(suite.ctx, types.LiquidatorMacc)
suite.Equal(cs(c("usdx", 490000000000)), bk.GetAllBalances(suite.ctx, acc.GetAddress()))
}
func (suite *AuctionTestSuite) TestDebtAuction() {
bk := suite.app.GetBankKeeper()
ak := suite.app.GetAccountKeeper()
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 100000000000)))
suite.NoError(err)
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 200000000000)))
suite.NoError(err)
err = suite.keeper.RunSurplusAndDebtAuctions(suite.ctx)
suite.NoError(err)
acc := ak.GetModuleAccount(suite.ctx, auctiontypes.ModuleName)
suite.Equal(cs(c("debt", 10000000000)), bk.GetAllBalances(suite.ctx, acc.GetAddress()))
acc = ak.GetModuleAccount(suite.ctx, types.LiquidatorMacc)
suite.Equal(cs(c("debt", 90000000000)), bk.GetAllBalances(suite.ctx, acc.GetAddress()))
}
func (suite *AuctionTestSuite) TestGetTotalSurplus() {
bk := suite.app.GetBankKeeper()
// liquidator account has zero coins
suite.Require().Equal(sdkmath.NewInt(0), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
// mint some coins
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 100e6)))
suite.Require().NoError(err)
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 200e6)))
suite.Require().NoError(err)
// liquidator account has 300e6 total usdx
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
// mint some debt
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 500e6)))
suite.Require().NoError(err)
// liquidator account still has 300e6 total usdx -- debt balance is ignored
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
// burn some usdx
err = bk.BurnCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 50e6)))
suite.Require().NoError(err)
// liquidator usdx decreases
suite.Require().Equal(sdkmath.NewInt(250e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
}
func (suite *AuctionTestSuite) TestGetTotalDebt() {
bk := suite.app.GetBankKeeper()
// liquidator account has zero debt
suite.Require().Equal(sdkmath.NewInt(0), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
// mint some debt
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 100e6)))
suite.Require().NoError(err)
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 200e6)))
suite.Require().NoError(err)
// liquidator account has 300e6 total debt
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc))
// mint some usdx
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 500e6)))
suite.Require().NoError(err)
// liquidator account still has 300e6 total debt -- usdx balance is ignored
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc))
// burn some debt
err = bk.BurnCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 50e6)))
suite.Require().NoError(err)
// liquidator debt decreases
suite.Require().Equal(sdkmath.NewInt(250e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc))
}
func TestAuctionTestSuite(t *testing.T) {
suite.Run(t, new(AuctionTestSuite))
}