2020-01-15 14:19:33 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2020-01-15 14:19:33 +00:00
|
|
|
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"
|
2020-01-15 14:19:33 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
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"
|
2020-01-15 14:19:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AuctionTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
keeper keeper.Keeper
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
2020-06-04 12:58:39 +00:00
|
|
|
addrs []sdk.AccAddress
|
2020-01-15 14:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AuctionTestSuite) SetupTest() {
|
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
tApp := app.NewTestApp()
|
2020-06-04 12:58:39 +00:00
|
|
|
taddr := sdk.AccAddress(crypto.AddressHash([]byte("KavaTestUser1")))
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), cs(c("usdx", 21000000000)), []sdk.AccAddress{taddr})
|
2020-01-15 14:19:33 +00:00
|
|
|
tApp.InitializeFromGenesisStates(
|
2020-06-04 12:58:39 +00:00
|
|
|
authGS,
|
2022-01-08 00:39:27 +00:00
|
|
|
NewPricefeedGenStateMulti(tApp.AppCodec()),
|
|
|
|
NewCDPGenStateMulti(tApp.AppCodec()),
|
2020-01-15 14:19:33 +00:00
|
|
|
)
|
|
|
|
keeper := tApp.GetCDPKeeper()
|
|
|
|
suite.app = tApp
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.keeper = keeper
|
2020-06-04 12:58:39 +00:00
|
|
|
suite.addrs = []sdk.AccAddress{taddr}
|
2020-01-15 14:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AuctionTestSuite) TestNetDebtSurplus() {
|
2022-01-08 00:39:27 +00:00
|
|
|
bk := suite.app.GetBankKeeper()
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
|
|
|
|
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 100)))
|
2020-01-15 14:19:33 +00:00
|
|
|
suite.NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 10)))
|
2020-01-15 14:19:33 +00:00
|
|
|
suite.NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
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()))
|
2020-01-15 14:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:58:39 +00:00
|
|
|
func (suite *AuctionTestSuite) TestCollateralAuction() {
|
2022-01-08 00:39:27 +00:00
|
|
|
bk := suite.app.GetBankKeeper()
|
|
|
|
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 21000000000), c("bnb", 190000000000)))
|
2020-06-04 12:58:39 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
testDeposit := types.NewDeposit(1, suite.addrs[0], c("bnb", 190000000000))
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AuctionCollateral(suite.ctx, types.Deposits{testDeposit}, "bnb-a", i(21000000000), "usdx")
|
2020-06-04 12:58:39 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
func (suite *AuctionTestSuite) TestSurplusAuction() {
|
2022-01-08 00:39:27 +00:00
|
|
|
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)))
|
2020-01-15 14:19:33 +00:00
|
|
|
suite.NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
err = suite.keeper.RunSurplusAndDebtAuctions(suite.ctx)
|
2020-01-15 14:19:33 +00:00
|
|
|
suite.NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
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()))
|
2020-01-15 14:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AuctionTestSuite) TestDebtAuction() {
|
2022-01-08 00:39:27 +00:00
|
|
|
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)))
|
2020-01-15 14:19:33 +00:00
|
|
|
suite.NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
err = suite.keeper.RunSurplusAndDebtAuctions(suite.ctx)
|
2020-01-15 14:19:33 +00:00
|
|
|
suite.NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
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()))
|
2020-01-15 14:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:18:13 +00:00
|
|
|
func (suite *AuctionTestSuite) TestGetTotalSurplus() {
|
2022-01-08 00:39:27 +00:00
|
|
|
bk := suite.app.GetBankKeeper()
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// liquidator account has zero coins
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(0), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// mint some coins
|
2022-01-08 00:39:27 +00:00
|
|
|
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 100e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 200e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// liquidator account has 300e6 total usdx
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// mint some debt
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 500e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// liquidator account still has 300e6 total usdx -- debt balance is ignored
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// burn some usdx
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.BurnCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 50e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// liquidator usdx decreases
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(250e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AuctionTestSuite) TestGetTotalDebt() {
|
2022-01-08 00:39:27 +00:00
|
|
|
bk := suite.app.GetBankKeeper()
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// liquidator account has zero debt
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(0), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// mint some debt
|
2022-01-08 00:39:27 +00:00
|
|
|
err := bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 100e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 200e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// liquidator account has 300e6 total debt
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// mint some usdx
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 500e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// liquidator account still has 300e6 total debt -- usdx balance is ignored
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(300e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
|
|
|
|
// burn some debt
|
2022-01-08 00:39:27 +00:00
|
|
|
err = bk.BurnCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 50e6)))
|
2020-06-10 14:18:13 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// liquidator debt decreases
|
2023-04-05 23:21:59 +00:00
|
|
|
suite.Require().Equal(sdkmath.NewInt(250e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc))
|
2020-06-10 14:18:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 14:19:33 +00:00
|
|
|
func TestAuctionTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AuctionTestSuite))
|
|
|
|
}
|