2020-01-12 15:35:34 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-01-28 17:47:08 +00:00
|
|
|
"time"
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-01-12 15:35:34 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/simulation"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-30 14:13:31 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-04-30 14:13:31 +00:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
|
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"
|
|
|
|
pfkeeper "github.com/0glabs/0g-chain/x/pricefeed/keeper"
|
|
|
|
pftypes "github.com/0glabs/0g-chain/x/pricefeed/types"
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
custom = "custom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QuerierTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
keeper keeper.Keeper
|
|
|
|
pricefeedKeeper pfkeeper.Keeper
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
app app.TestApp
|
|
|
|
cdps types.CDPs
|
|
|
|
augmentedCDPs types.AugmentedCDPs
|
|
|
|
ctx sdk.Context
|
|
|
|
querier sdk.Querier
|
2022-01-08 00:39:27 +00:00
|
|
|
cdc codec.Codec
|
|
|
|
legacyAmino codec.LegacyAmino
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) SetupTest() {
|
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
suite.cdc = tApp.AppCodec()
|
|
|
|
suite.legacyAmino = *tApp.LegacyAmino()
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
cdps := make(types.CDPs, 100)
|
2020-01-28 17:47:08 +00:00
|
|
|
augmentedCDPs := make(types.AugmentedCDPs, 100)
|
2020-01-12 15:35:34 +00:00
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(100)
|
2022-01-08 00:39:27 +00:00
|
|
|
coins := cs(c("btc", 10000000000), c("xrp", 10000000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
|
2020-01-12 15:35:34 +00:00
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
2022-01-08 00:39:27 +00:00
|
|
|
NewPricefeedGenStateMulti(suite.cdc),
|
|
|
|
NewCDPGenStateHighDebtLimit(suite.cdc),
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.app = tApp
|
|
|
|
suite.keeper = tApp.GetCDPKeeper()
|
2020-01-28 17:47:08 +00:00
|
|
|
suite.pricefeedKeeper = tApp.GetPriceFeedKeeper()
|
|
|
|
|
|
|
|
// Set up markets
|
|
|
|
oracle := addrs[9]
|
|
|
|
marketParams := pftypes.Params{
|
|
|
|
Markets: pftypes.Markets{
|
|
|
|
pftypes.Market{MarketID: "xrp-usd", BaseAsset: "xrp", QuoteAsset: "usd", Oracles: []sdk.AccAddress{oracle}, Active: true},
|
|
|
|
pftypes.Market{MarketID: "btc-usd", BaseAsset: "btc", QuoteAsset: "usd", Oracles: []sdk.AccAddress{oracle}, Active: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
suite.pricefeedKeeper.SetParams(ctx, marketParams)
|
|
|
|
|
|
|
|
// Set collateral prices for use in collateralization calculations
|
|
|
|
_, err := suite.pricefeedKeeper.SetPrice(
|
|
|
|
ctx, oracle, "xrp-usd",
|
|
|
|
sdk.MustNewDecFromStr("0.75"),
|
|
|
|
time.Now().Add(1*time.Hour))
|
|
|
|
suite.Nil(err)
|
|
|
|
|
|
|
|
_, err = suite.pricefeedKeeper.SetPrice(
|
|
|
|
ctx, oracle, "btc-usd",
|
|
|
|
sdk.MustNewDecFromStr("5000"),
|
|
|
|
time.Now().Add(1*time.Hour))
|
|
|
|
suite.Nil(err)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
collateral := "xrp"
|
|
|
|
amount := simulation.RandIntBetween(rand.New(rand.NewSource(int64(j))), 2500000000, 9000000000)
|
|
|
|
debt := simulation.RandIntBetween(rand.New(rand.NewSource(int64(j))), 50000000, 250000000)
|
|
|
|
if j%2 == 0 {
|
|
|
|
collateral = "btc"
|
|
|
|
amount = simulation.RandIntBetween(rand.New(rand.NewSource(int64(j))), 500000000, 5000000000)
|
|
|
|
debt = simulation.RandIntBetween(rand.New(rand.NewSource(int64(j))), 1000000000, 25000000000)
|
|
|
|
}
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddCdp(suite.ctx, addrs[j], c(collateral, int64(amount)), c("usdx", int64(debt)), collateral+"-a")
|
2020-04-15 17:50:14 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
c, f := suite.keeper.GetCDP(suite.ctx, collateral+"-a", uint64(j+1))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.True(f)
|
|
|
|
cdps[j] = c
|
2020-05-13 14:19:19 +00:00
|
|
|
aCDP := suite.keeper.LoadAugmentedCDP(suite.ctx, c)
|
2020-01-28 17:47:08 +00:00
|
|
|
augmentedCDPs[j] = aCDP
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
suite.cdps = cdps
|
2020-01-28 17:47:08 +00:00
|
|
|
suite.augmentedCDPs = augmentedCDPs
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.querier = keeper.NewQuerier(suite.keeper, tApp.LegacyAmino())
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.addrs = addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryCdp() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdp}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpParams(suite.cdps[0].Owner, suite.cdps[0].Type)),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetCdp}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
var c types.AugmentedCDP
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &c))
|
2020-01-28 17:47:08 +00:00
|
|
|
suite.Equal(suite.augmentedCDPs[0], c)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdp}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpParams(suite.cdps[0].Owner, "lol-a")),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
_, err = suite.querier(ctx, []string{types.QueryGetCdp}, query)
|
|
|
|
suite.Error(err)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, "nonsense"}, "/"),
|
|
|
|
Data: []byte("nonsense"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = suite.querier(ctx, []string{query.Path}, query)
|
|
|
|
suite.Error(err)
|
|
|
|
|
|
|
|
_, err = suite.querier(ctx, []string{types.QueryGetCdp}, query)
|
|
|
|
suite.Error(err)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdp}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpParams(suite.cdps[0].Owner, "xrp-a")),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
_, err = suite.querier(ctx, []string{types.QueryGetCdp}, query)
|
|
|
|
suite.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryCdpsByCollateralType() {
|
2020-01-12 15:35:34 +00:00
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
query := abci.RequestQuery{
|
2020-09-17 00:45:10 +00:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdpsByCollateralType}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpsByCollateralTypeParams(suite.cdps[0].Type)),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetCdpsByCollateralType}, query)
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
var c types.AugmentedCDPs
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &c))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(50, len(c))
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
2020-09-17 00:45:10 +00:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdpsByCollateralType}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpsByCollateralTypeParams("lol-a")),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
_, err = suite.querier(ctx, []string{types.QueryGetCdpsByCollateralType}, query)
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryCdpsByRatio() {
|
|
|
|
ratioCountBtc := 0
|
|
|
|
ratioCountXrp := 0
|
2020-01-28 17:47:08 +00:00
|
|
|
xrpRatio := d("2.0")
|
|
|
|
btcRatio := d("2500")
|
2020-01-12 15:35:34 +00:00
|
|
|
expectedXrpIds := []int{}
|
|
|
|
expectedBtcIds := []int{}
|
|
|
|
for _, cdp := range suite.cdps {
|
2020-08-21 19:42:46 +00:00
|
|
|
absoluteRatio := suite.keeper.CalculateCollateralToDebtRatio(suite.ctx, cdp.Collateral, cdp.Type, cdp.Principal)
|
|
|
|
collateralizationRatio, err := suite.keeper.CalculateCollateralizationRatioFromAbsoluteRatio(suite.ctx, cdp.Type, absoluteRatio, "liquidation")
|
2020-01-28 17:47:08 +00:00
|
|
|
suite.Nil(err)
|
2020-04-27 14:40:34 +00:00
|
|
|
if cdp.Collateral.Denom == "xrp" {
|
2020-01-28 17:47:08 +00:00
|
|
|
if collateralizationRatio.LT(xrpRatio) {
|
2020-01-12 15:35:34 +00:00
|
|
|
ratioCountXrp += 1
|
|
|
|
expectedXrpIds = append(expectedXrpIds, int(cdp.ID))
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-28 17:47:08 +00:00
|
|
|
if collateralizationRatio.LT(btcRatio) {
|
2020-01-12 15:35:34 +00:00
|
|
|
ratioCountBtc += 1
|
|
|
|
expectedBtcIds = append(expectedBtcIds, int(cdp.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdpsByCollateralization}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpsByRatioParams("xrp-a", xrpRatio)),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetCdpsByCollateralization}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
var c types.AugmentedCDPs
|
2020-01-12 15:35:34 +00:00
|
|
|
actualXrpIds := []int{}
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &c))
|
2020-01-12 15:35:34 +00:00
|
|
|
for _, k := range c {
|
|
|
|
actualXrpIds = append(actualXrpIds, int(k.ID))
|
|
|
|
}
|
|
|
|
sort.Ints(actualXrpIds)
|
|
|
|
suite.Equal(expectedXrpIds, actualXrpIds)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdpsByCollateralization}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpsByRatioParams("btc-a", btcRatio)),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
bz, err = suite.querier(ctx, []string{types.QueryGetCdpsByCollateralization}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
c = types.AugmentedCDPs{}
|
2020-01-12 15:35:34 +00:00
|
|
|
actualBtcIds := []int{}
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &c))
|
2020-01-12 15:35:34 +00:00
|
|
|
for _, k := range c {
|
|
|
|
actualBtcIds = append(actualBtcIds, int(k.ID))
|
|
|
|
}
|
|
|
|
sort.Ints(actualBtcIds)
|
|
|
|
suite.Equal(expectedBtcIds, actualBtcIds)
|
|
|
|
|
|
|
|
query = abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdpsByCollateralization}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpsByRatioParams("xrp-a", d("0.003"))),
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
bz, err = suite.querier(ctx, []string{types.QueryGetCdpsByCollateralization}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
2020-01-28 17:47:08 +00:00
|
|
|
c = types.AugmentedCDPs{}
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &c))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(0, len(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryParams() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetParams}, abci.RequestQuery{})
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.NoError(err)
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
var p types.Params
|
2022-01-08 00:39:27 +00:00
|
|
|
err = suite.legacyAmino.UnmarshalJSON(bz, &p)
|
|
|
|
suite.NoError(err)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
cdpGS := NewCDPGenStateHighDebtLimit(suite.app.AppCodec())
|
2020-01-12 15:35:34 +00:00
|
|
|
gs := types.GenesisState{}
|
2022-01-08 00:39:27 +00:00
|
|
|
err = suite.legacyAmino.UnmarshalJSON(cdpGS["cdp"], &gs)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(gs.Params, p)
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:08:17 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryDeposits() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdpDeposits}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCdpDeposits(suite.cdps[0].Owner, suite.cdps[0].Type)),
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetCdpDeposits}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
deposits := suite.keeper.GetDeposits(ctx, suite.cdps[0].ID)
|
|
|
|
|
|
|
|
var d types.Deposits
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &d))
|
2020-01-28 20:08:17 +00:00
|
|
|
suite.Equal(deposits, d)
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:30:10 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryAccounts() {
|
|
|
|
bz, err := suite.querier(suite.ctx, []string{types.QueryGetAccounts}, abci.RequestQuery{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(bz)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var accounts []authtypes.ModuleAccount
|
|
|
|
suite.Require().Nil(suite.legacyAmino.UnmarshalJSON(bz, &accounts))
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().Equal(2, len(accounts))
|
2020-06-19 19:30:10 +00:00
|
|
|
|
|
|
|
findByName := func(name string) bool {
|
|
|
|
for _, account := range accounts {
|
|
|
|
if account.GetName() == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.Require().True(findByName("cdp"))
|
|
|
|
suite.Require().True(findByName("liquidator"))
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestFindIntersection() {
|
|
|
|
a := types.CDPs{suite.cdps[0], suite.cdps[1], suite.cdps[2], suite.cdps[3], suite.cdps[4]}
|
|
|
|
b := types.CDPs{suite.cdps[3], suite.cdps[4], suite.cdps[5], suite.cdps[6], suite.cdps[7]}
|
|
|
|
expectedIntersection1 := types.CDPs{suite.cdps[3], suite.cdps[4]}
|
|
|
|
|
|
|
|
intersection1 := keeper.FindIntersection(a, b)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().Equal(expectedIntersection1, intersection1)
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
c := types.CDPs{suite.cdps[0], suite.cdps[1], suite.cdps[2], suite.cdps[3], suite.cdps[4]}
|
|
|
|
d := types.CDPs{suite.cdps[5], suite.cdps[6], suite.cdps[7], suite.cdps[8], suite.cdps[9]}
|
|
|
|
expectedIntersection2 := types.CDPs{}
|
|
|
|
|
|
|
|
intersection2 := keeper.FindIntersection(c, d)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().Equal(expectedIntersection2, intersection2)
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
e := types.CDPs{suite.cdps[0]}
|
|
|
|
f := types.CDPs{}
|
|
|
|
expectedIntersection3 := types.CDPs{}
|
|
|
|
|
|
|
|
intersection3 := keeper.FindIntersection(e, f)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().Equal(expectedIntersection3, intersection3)
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestFilterCDPs() {
|
|
|
|
paramsType := types.NewQueryCdpsParams(1, 100, "btc-a", sdk.AccAddress{}, 0, sdk.ZeroDec())
|
2022-01-08 00:39:27 +00:00
|
|
|
filteredCDPs1, err := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsType)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(50, len(filteredCDPs1))
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
paramsOwner := types.NewQueryCdpsParams(1, 100, "", suite.cdps[10].Owner, 0, sdk.ZeroDec())
|
2022-01-08 00:39:27 +00:00
|
|
|
filteredCDPs2, err := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsOwner)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(1, len(filteredCDPs2))
|
|
|
|
suite.Require().Equal(suite.cdps[10].Owner, filteredCDPs2[0].Owner)
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
paramsID := types.NewQueryCdpsParams(1, 100, "", sdk.AccAddress{}, 68, sdk.ZeroDec())
|
2022-01-08 00:39:27 +00:00
|
|
|
filteredCDPs3, err := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsID)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(1, len(filteredCDPs3))
|
|
|
|
suite.Require().Equal(suite.cdps[68-1].ID, filteredCDPs3[0].ID)
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
ratioCountBtc := 0
|
|
|
|
btcRatio := d("2500")
|
|
|
|
for _, cdp := range suite.cdps {
|
|
|
|
if cdp.Type == "btc-a" {
|
|
|
|
absoluteRatio := suite.keeper.CalculateCollateralToDebtRatio(suite.ctx, cdp.Collateral, cdp.Type, cdp.Principal)
|
|
|
|
collateralizationRatio, _ := suite.keeper.CalculateCollateralizationRatioFromAbsoluteRatio(suite.ctx, cdp.Type, absoluteRatio, "liquidation")
|
|
|
|
if collateralizationRatio.LT(btcRatio) {
|
|
|
|
ratioCountBtc += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paramsTypeAndRatio := types.NewQueryCdpsParams(1, 100, "btc-a", sdk.AccAddress{}, 0, sdk.NewDec(2500))
|
2022-01-08 00:39:27 +00:00
|
|
|
filteredCDPs4, err := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsTypeAndRatio)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(ratioCountBtc, len(filteredCDPs4))
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryCdps() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
params := types.NewQueryCdpsParams(1, 100, "btc-a", sdk.AccAddress{}, 0, sdk.ZeroDec())
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdps}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(params),
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetCdps}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
output := types.AugmentedCDPs{}
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &output))
|
2020-09-17 00:45:10 +00:00
|
|
|
suite.Equal(50, len(output))
|
|
|
|
}
|
|
|
|
|
2021-10-23 18:09:59 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryTotalPrincipal() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
params := types.NewQueryGetTotalPrincipalParams("btc-a")
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetTotalPrincipal}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(params),
|
2021-10-23 18:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalPrincipal}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var output types.TotalPrincipals
|
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &output))
|
2021-10-23 18:09:59 +00:00
|
|
|
suite.Equal(1, len(output))
|
|
|
|
suite.Equal("btc-a", output[0].CollateralType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryTotalPrincipalAll() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
params := types.NewQueryGetTotalPrincipalParams("")
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetTotalPrincipal}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(params),
|
2021-10-23 18:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalPrincipal}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var output types.TotalPrincipals
|
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &output))
|
2021-10-23 18:09:59 +00:00
|
|
|
|
|
|
|
var outputTypes []string
|
|
|
|
for _, c := range output {
|
|
|
|
outputTypes = append(outputTypes, c.CollateralType)
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.Greater(len(output), 0)
|
|
|
|
suite.Subset(outputTypes, []string{"btc-a", "xrp-a"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryTotalCollateral() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
params := types.NewQueryGetTotalCollateralParams("btc-a")
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetTotalCollateral}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(params),
|
2021-10-23 18:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalCollateral}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var output types.TotalCollaterals
|
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &output))
|
2021-10-23 18:09:59 +00:00
|
|
|
suite.Equal(1, len(output))
|
|
|
|
suite.Equal("btc-a", output[0].CollateralType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryTotalCollateralAll() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
params := types.NewQueryGetTotalCollateralParams("")
|
|
|
|
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetTotalCollateral}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(params),
|
2021-10-23 18:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalCollateral}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var output types.TotalCollaterals
|
|
|
|
suite.Nil(suite.legacyAmino.UnmarshalJSON(bz, &output))
|
2021-10-23 18:09:59 +00:00
|
|
|
|
|
|
|
var outputTypes []string
|
|
|
|
for _, c := range output {
|
|
|
|
outputTypes = append(outputTypes, c.CollateralType)
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.Greater(len(output), 0)
|
|
|
|
suite.Subset(outputTypes, []string{"btc-a", "xrp-a"})
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
func TestQuerierTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(QuerierTestSuite))
|
|
|
|
}
|