2020-01-12 15:35:34 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2021-10-23 18:09:59 +00:00
|
|
|
"fmt"
|
2020-01-12 15:35:34 +00:00
|
|
|
"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"
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2020-06-19 19:30:10 +00:00
|
|
|
supply "github.com/cosmos/cosmos-sdk/x/supply"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-30 14:13:31 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
2020-01-28 17:47:08 +00:00
|
|
|
pfkeeper "github.com/kava-labs/kava/x/pricefeed/keeper"
|
|
|
|
pftypes "github.com/kava-labs/kava/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
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) SetupTest() {
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
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)
|
|
|
|
coins := []sdk.Coins{}
|
|
|
|
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
coins = append(coins, cs(c("btc", 10000000000), c("xrp", 10000000000)))
|
|
|
|
}
|
|
|
|
|
|
|
|
authGS := app.NewAuthGenState(
|
|
|
|
addrs, coins)
|
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
|
|
|
NewPricefeedGenStateMulti(),
|
2020-04-15 17:50:14 +00:00
|
|
|
NewCDPGenStateHighDebtLimit(),
|
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
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.querier = keeper.NewQuerier(suite.keeper)
|
|
|
|
suite.addrs = addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryCdp() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetCdp}, "/"),
|
2020-09-17 00:45:10 +00:00
|
|
|
Data: types.ModuleCdc.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
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Nil(types.ModuleCdc.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}, "/"),
|
2020-08-21 19:42:46 +00:00
|
|
|
Data: types.ModuleCdc.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}, "/"),
|
2020-08-21 19:42:46 +00:00
|
|
|
Data: types.ModuleCdc.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}, "/"),
|
|
|
|
Data: types.ModuleCdc.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
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &c))
|
|
|
|
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}, "/"),
|
|
|
|
Data: types.ModuleCdc.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}, "/"),
|
2020-08-21 19:42:46 +00:00
|
|
|
Data: types.ModuleCdc.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{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &c))
|
|
|
|
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}, "/"),
|
2020-08-21 19:42:46 +00:00
|
|
|
Data: types.ModuleCdc.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{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &c))
|
|
|
|
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}, "/"),
|
2020-08-21 19:42:46 +00:00
|
|
|
Data: types.ModuleCdc.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{}
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &c))
|
|
|
|
suite.Equal(0, len(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryParams() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetParams}, abci.RequestQuery{})
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
var p types.Params
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &p))
|
|
|
|
|
2020-04-15 17:50:14 +00:00
|
|
|
cdpGS := NewCDPGenStateHighDebtLimit()
|
2020-01-12 15:35:34 +00:00
|
|
|
gs := types.GenesisState{}
|
|
|
|
types.ModuleCdc.UnmarshalJSON(cdpGS["cdp"], &gs)
|
|
|
|
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}, "/"),
|
2020-09-17 00:45:10 +00:00
|
|
|
Data: types.ModuleCdc.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
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &d))
|
|
|
|
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)
|
|
|
|
|
|
|
|
var accounts []supply.ModuleAccount
|
|
|
|
suite.Require().Nil(supply.ModuleCdc.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)
|
|
|
|
suite.Require().Equal(intersection1, expectedIntersection1)
|
|
|
|
|
|
|
|
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)
|
|
|
|
suite.Require().Equal(intersection2, expectedIntersection2)
|
|
|
|
|
|
|
|
e := types.CDPs{suite.cdps[0]}
|
|
|
|
f := types.CDPs{}
|
|
|
|
expectedIntersection3 := types.CDPs{}
|
|
|
|
|
|
|
|
intersection3 := keeper.FindIntersection(e, f)
|
|
|
|
suite.Require().Equal(intersection3, expectedIntersection3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestFilterCDPs() {
|
|
|
|
paramsType := types.NewQueryCdpsParams(1, 100, "btc-a", sdk.AccAddress{}, 0, sdk.ZeroDec())
|
|
|
|
filteredCDPs1 := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsType)
|
|
|
|
suite.Require().Equal(len(filteredCDPs1), 50)
|
|
|
|
|
|
|
|
paramsOwner := types.NewQueryCdpsParams(1, 100, "", suite.cdps[10].Owner, 0, sdk.ZeroDec())
|
|
|
|
filteredCDPs2 := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsOwner)
|
|
|
|
suite.Require().Equal(len(filteredCDPs2), 1)
|
|
|
|
suite.Require().Equal(filteredCDPs2[0].Owner, suite.cdps[10].Owner)
|
|
|
|
|
|
|
|
paramsID := types.NewQueryCdpsParams(1, 100, "", sdk.AccAddress{}, 68, sdk.ZeroDec())
|
|
|
|
filteredCDPs3 := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsID)
|
|
|
|
suite.Require().Equal(len(filteredCDPs3), 1)
|
|
|
|
suite.Require().Equal(filteredCDPs3[0].ID, suite.cdps[68-1].ID)
|
|
|
|
|
|
|
|
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))
|
|
|
|
filteredCDPs4 := keeper.FilterCDPs(suite.ctx, suite.keeper, paramsTypeAndRatio)
|
|
|
|
suite.Require().Equal(len(filteredCDPs4), ratioCountBtc)
|
|
|
|
}
|
|
|
|
|
|
|
|
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}, "/"),
|
|
|
|
Data: types.ModuleCdc.MustMarshalJSON(params),
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetCdps}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
output := types.AugmentedCDPs{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &output))
|
|
|
|
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}, "/"),
|
|
|
|
Data: types.ModuleCdc.MustMarshalJSON(params),
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalPrincipal}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
output := []types.TotalCDPPrincipal{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &output))
|
|
|
|
fmt.Printf("%s", output)
|
|
|
|
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}, "/"),
|
|
|
|
Data: types.ModuleCdc.MustMarshalJSON(params),
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalPrincipal}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
output := []types.TotalCDPPrincipal{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &output))
|
|
|
|
|
|
|
|
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}, "/"),
|
|
|
|
Data: types.ModuleCdc.MustMarshalJSON(params),
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalCollateral}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
output := []types.TotalCDPCollateral{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &output))
|
|
|
|
fmt.Printf("%s", output)
|
|
|
|
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}, "/"),
|
|
|
|
Data: types.ModuleCdc.MustMarshalJSON(params),
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetTotalCollateral}, query)
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
output := []types.TotalCDPCollateral{}
|
|
|
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &output))
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|