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