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

140 lines
5.0 KiB
Go
Raw Normal View History

package keeper_test
import (
"errors"
"testing"
"github.com/stretchr/testify/suite"
2020-04-30 14:23:41 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
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
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
2020-04-30 14:13:31 +00:00
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/cdp/keeper"
"github.com/0glabs/0g-chain/x/cdp/types"
)
type DepositTestSuite struct {
suite.Suite
keeper keeper.Keeper
app app.TestApp
ctx sdk.Context
addrs []sdk.AccAddress
}
func (suite *DepositTestSuite) SetupTest() {
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
cdc := tApp.AppCodec()
_, addrs := app.GeneratePrivKeyAddressPairs(10)
authGS := app.NewFundedGenStateWithCoins(
cdc,
[]sdk.Coins{
cs(c("xrp", 500000000), c("btc", 500000000)),
cs(c("xrp", 200000000)),
},
addrs[0:2],
)
tApp.InitializeFromGenesisStates(
authGS,
NewPricefeedGenStateMulti(cdc),
NewCDPGenStateMulti(cdc),
)
keeper := tApp.GetCDPKeeper()
suite.app = tApp
suite.keeper = keeper
suite.ctx = ctx
suite.addrs = addrs
err := suite.keeper.AddCdp(suite.ctx, addrs[0], c("xrp", 400000000), c("usdx", 10000000), "xrp-a")
suite.NoError(err)
}
func (suite *DepositTestSuite) TestGetSetDeposit() {
d, found := suite.keeper.GetDeposit(suite.ctx, uint64(1), suite.addrs[0])
suite.True(found)
td := types.NewDeposit(uint64(1), suite.addrs[0], c("xrp", 400000000))
suite.True(d.Equals(td))
ds := suite.keeper.GetDeposits(suite.ctx, uint64(1))
suite.Equal(1, len(ds))
suite.True(ds[0].Equals(td))
suite.keeper.DeleteDeposit(suite.ctx, uint64(1), suite.addrs[0])
_, found = suite.keeper.GetDeposit(suite.ctx, uint64(1), suite.addrs[0])
suite.False(found)
ds = suite.keeper.GetDeposits(suite.ctx, uint64(1))
suite.Equal(0, len(ds))
}
func (suite *DepositTestSuite) TestDepositCollateral() {
err := suite.keeper.DepositCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], c("xrp", 10000000), "xrp-a")
suite.NoError(err)
d, found := suite.keeper.GetDeposit(suite.ctx, uint64(1), suite.addrs[0])
suite.True(found)
td := types.NewDeposit(uint64(1), suite.addrs[0], c("xrp", 410000000))
suite.True(d.Equals(td))
ds := suite.keeper.GetDeposits(suite.ctx, uint64(1))
suite.Equal(1, len(ds))
suite.True(ds[0].Equals(td))
cd, _ := suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(1))
suite.Equal(c("xrp", 410000000), cd.Collateral)
ak := suite.app.GetAccountKeeper()
bk := suite.app.GetBankKeeper()
acc := ak.GetAccount(suite.ctx, suite.addrs[0])
suite.Equal(i(90000000), bk.GetBalance(suite.ctx, acc.GetAddress(), "xrp").Amount)
err = suite.keeper.DepositCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], c("btc", 1), "btc-a")
suite.Require().True(errors.Is(err, types.ErrCdpNotFound))
err = suite.keeper.DepositCollateral(suite.ctx, suite.addrs[1], suite.addrs[0], c("xrp", 1), "xrp-a")
suite.Require().True(errors.Is(err, types.ErrCdpNotFound))
err = suite.keeper.DepositCollateral(suite.ctx, suite.addrs[0], suite.addrs[1], c("xrp", 10000000), "xrp-a")
suite.NoError(err)
d, found = suite.keeper.GetDeposit(suite.ctx, uint64(1), suite.addrs[1])
suite.True(found)
td = types.NewDeposit(uint64(1), suite.addrs[1], c("xrp", 10000000))
suite.True(d.Equals(td))
ds = suite.keeper.GetDeposits(suite.ctx, uint64(1))
suite.Equal(2, len(ds))
suite.True(ds[1].Equals(td))
}
func (suite *DepositTestSuite) TestWithdrawCollateral() {
err := suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], c("xrp", 400000000), "xrp-a")
suite.Require().True(errors.Is(err, types.ErrInvalidCollateralRatio))
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], c("xrp", 321000000), "xrp-a")
suite.Require().True(errors.Is(err, types.ErrInvalidCollateralRatio))
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[1], suite.addrs[0], c("xrp", 10000000), "xrp-a")
suite.Require().True(errors.Is(err, types.ErrCdpNotFound))
cd, _ := suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(1))
cd.AccumulatedFees = c("usdx", 1)
err = suite.keeper.SetCDP(suite.ctx, cd)
suite.NoError(err)
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], c("xrp", 320000000), "xrp-a")
suite.Require().True(errors.Is(err, types.ErrInvalidCollateralRatio))
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], c("xrp", 10000000), "xrp-a")
suite.NoError(err)
dep, _ := suite.keeper.GetDeposit(suite.ctx, uint64(1), suite.addrs[0])
td := types.NewDeposit(uint64(1), suite.addrs[0], c("xrp", 390000000))
suite.True(dep.Equals(td))
ak := suite.app.GetAccountKeeper()
bk := suite.app.GetBankKeeper()
acc := ak.GetAccount(suite.ctx, suite.addrs[0])
suite.Equal(i(110000000), bk.GetBalance(suite.ctx, acc.GetAddress(), "xrp").Amount)
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[1], c("xrp", 10000000), "xrp-a")
suite.Require().True(errors.Is(err, types.ErrDepositNotFound))
}
func TestDepositTestSuite(t *testing.T) {
suite.Run(t, new(DepositTestSuite))
}