2022-01-08 00:39:27 +00:00
|
|
|
package v0_16
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
app "github.com/kava-labs/kava/app"
|
|
|
|
v015cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_15"
|
|
|
|
v016cdp "github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type migrateTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
addresses []sdk.AccAddress
|
|
|
|
v15genstate v015cdp.GenesisState
|
|
|
|
cdc codec.Codec
|
|
|
|
legacyCdc *codec.LegacyAmino
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *migrateTestSuite) SetupTest() {
|
|
|
|
app.SetSDKConfig()
|
|
|
|
|
|
|
|
s.v15genstate = v015cdp.GenesisState{
|
|
|
|
Params: v015cdp.Params{},
|
|
|
|
CDPs: v015cdp.CDPs{},
|
|
|
|
Deposits: v015cdp.Deposits{},
|
|
|
|
StartingCdpID: 1,
|
|
|
|
DebtDenom: "usdx",
|
|
|
|
GovDenom: "ukava",
|
|
|
|
PreviousAccumulationTimes: v015cdp.GenesisAccumulationTimes{},
|
|
|
|
TotalPrincipals: v015cdp.GenesisTotalPrincipals{},
|
|
|
|
}
|
|
|
|
|
|
|
|
config := app.MakeEncodingConfig()
|
|
|
|
s.cdc = config.Marshaler
|
|
|
|
|
|
|
|
legacyCodec := codec.NewLegacyAmino()
|
|
|
|
s.legacyCdc = legacyCodec
|
|
|
|
|
|
|
|
_, accAddresses := app.GeneratePrivKeyAddressPairs(10)
|
|
|
|
s.addresses = accAddresses
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *migrateTestSuite) TestMigrate_JSON() {
|
|
|
|
file := filepath.Join("testdata", "v15-cdp.json")
|
|
|
|
data, err := ioutil.ReadFile(file)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
err = s.legacyCdc.UnmarshalJSON(data, &s.v15genstate)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
genstate := Migrate(s.v15genstate)
|
|
|
|
actual := s.cdc.MustMarshalJSON(genstate)
|
|
|
|
|
|
|
|
file = filepath.Join("testdata", "v16-cdp.json")
|
|
|
|
expected, err := ioutil.ReadFile(file)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().JSONEq(string(expected), string(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *migrateTestSuite) TestMigrate_GenState() {
|
|
|
|
s.v15genstate = v015cdp.GenesisState{
|
|
|
|
StartingCdpID: 2,
|
|
|
|
DebtDenom: "usdx",
|
|
|
|
GovDenom: "ukava",
|
|
|
|
Params: v015cdp.Params{
|
|
|
|
CollateralParams: v015cdp.CollateralParams{
|
|
|
|
{
|
|
|
|
Denom: "xrp",
|
|
|
|
Type: "xrp-a",
|
|
|
|
LiquidationRatio: sdk.MustNewDecFromStr("2.0"),
|
|
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 500000000000),
|
|
|
|
StabilityFee: sdk.MustNewDecFromStr("1.012"),
|
|
|
|
LiquidationPenalty: sdk.MustNewDecFromStr("0.05"),
|
2023-04-05 23:21:59 +00:00
|
|
|
AuctionSize: sdkmath.NewInt(70),
|
2022-01-08 00:39:27 +00:00
|
|
|
SpotMarketID: "xrp:usd",
|
|
|
|
LiquidationMarketID: "xrp:usd",
|
|
|
|
KeeperRewardPercentage: sdk.MustNewDecFromStr("0.01"),
|
2023-04-05 23:21:59 +00:00
|
|
|
CheckCollateralizationIndexCount: sdkmath.NewInt(10),
|
|
|
|
ConversionFactor: sdkmath.NewInt(6),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
DebtParam: v015cdp.DebtParam{
|
|
|
|
Denom: "usdx",
|
|
|
|
ReferenceAsset: "usd",
|
2023-04-05 23:21:59 +00:00
|
|
|
ConversionFactor: sdkmath.NewInt(6),
|
|
|
|
DebtFloor: sdkmath.NewInt(100),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 1000000000000),
|
2023-04-05 23:21:59 +00:00
|
|
|
SurplusAuctionThreshold: sdkmath.NewInt(6),
|
|
|
|
SurplusAuctionLot: sdkmath.NewInt(7),
|
|
|
|
DebtAuctionThreshold: sdkmath.NewInt(8),
|
|
|
|
DebtAuctionLot: sdkmath.NewInt(9),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
CDPs: v015cdp.CDPs{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
Owner: s.addresses[0],
|
|
|
|
Type: "xrp-a",
|
2023-04-05 23:21:59 +00:00
|
|
|
Collateral: sdk.NewCoin("xrp", sdkmath.NewInt(2123)),
|
|
|
|
Principal: sdk.NewCoin("usdx", sdkmath.NewInt(100)),
|
2022-01-08 00:39:27 +00:00
|
|
|
AccumulatedFees: sdk.NewCoin("usdx", sdk.ZeroInt()),
|
|
|
|
FeesUpdated: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
InterestFactor: sdk.NewDec(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Deposits: v015cdp.Deposits{
|
|
|
|
{
|
|
|
|
CdpID: 1,
|
|
|
|
Depositor: s.addresses[0],
|
2023-04-05 23:21:59 +00:00
|
|
|
Amount: sdk.NewCoin("usdx", sdkmath.NewInt(100)),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CdpID: 2,
|
|
|
|
Depositor: s.addresses[1],
|
2023-04-05 23:21:59 +00:00
|
|
|
Amount: sdk.NewCoin("ukava", sdkmath.NewInt(1200)),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
PreviousAccumulationTimes: v015cdp.GenesisAccumulationTimes{
|
|
|
|
{
|
|
|
|
CollateralType: "usdx",
|
|
|
|
PreviousAccumulationTime: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
InterestFactor: sdk.MustNewDecFromStr("0.02"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TotalPrincipals: v015cdp.GenesisTotalPrincipals{
|
|
|
|
{
|
|
|
|
CollateralType: "usdx",
|
2023-04-05 23:21:59 +00:00
|
|
|
TotalPrincipal: sdkmath.NewInt(1200),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expected := v016cdp.GenesisState{
|
|
|
|
StartingCdpID: 2,
|
|
|
|
DebtDenom: "usdx",
|
|
|
|
GovDenom: "ukava",
|
|
|
|
Params: v016cdp.Params{
|
|
|
|
CollateralParams: v016cdp.CollateralParams{
|
|
|
|
{
|
|
|
|
Denom: "xrp",
|
|
|
|
Type: "xrp-a",
|
|
|
|
LiquidationRatio: sdk.MustNewDecFromStr("2.0"),
|
|
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 500000000000),
|
|
|
|
StabilityFee: sdk.MustNewDecFromStr("1.012"),
|
|
|
|
LiquidationPenalty: sdk.MustNewDecFromStr("0.05"),
|
2023-04-05 23:21:59 +00:00
|
|
|
AuctionSize: sdkmath.NewInt(70),
|
2022-01-08 00:39:27 +00:00
|
|
|
SpotMarketID: "xrp:usd",
|
|
|
|
LiquidationMarketID: "xrp:usd",
|
|
|
|
KeeperRewardPercentage: sdk.MustNewDecFromStr("0.01"),
|
2023-04-05 23:21:59 +00:00
|
|
|
CheckCollateralizationIndexCount: sdkmath.NewInt(10),
|
|
|
|
ConversionFactor: sdkmath.NewInt(6),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
DebtParam: v016cdp.DebtParam{
|
|
|
|
Denom: "usdx",
|
|
|
|
ReferenceAsset: "usd",
|
2023-04-05 23:21:59 +00:00
|
|
|
ConversionFactor: sdkmath.NewInt(6),
|
|
|
|
DebtFloor: sdkmath.NewInt(100),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 1000000000000),
|
2023-04-05 23:21:59 +00:00
|
|
|
SurplusAuctionThreshold: sdkmath.NewInt(6),
|
|
|
|
SurplusAuctionLot: sdkmath.NewInt(7),
|
|
|
|
DebtAuctionThreshold: sdkmath.NewInt(8),
|
|
|
|
DebtAuctionLot: sdkmath.NewInt(9),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
CDPs: v016cdp.CDPs{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
Owner: s.addresses[0],
|
|
|
|
Type: "xrp-a",
|
2023-04-05 23:21:59 +00:00
|
|
|
Collateral: sdk.NewCoin("xrp", sdkmath.NewInt(2123)),
|
|
|
|
Principal: sdk.NewCoin("usdx", sdkmath.NewInt(100)),
|
2022-01-08 00:39:27 +00:00
|
|
|
AccumulatedFees: sdk.NewCoin("usdx", sdk.ZeroInt()),
|
|
|
|
FeesUpdated: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
InterestFactor: sdk.NewDec(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Deposits: v016cdp.Deposits{
|
|
|
|
{
|
|
|
|
CdpID: 1,
|
|
|
|
Depositor: s.addresses[0],
|
2023-04-05 23:21:59 +00:00
|
|
|
Amount: sdk.NewCoin("usdx", sdkmath.NewInt(100)),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CdpID: 2,
|
|
|
|
Depositor: s.addresses[1],
|
2023-04-05 23:21:59 +00:00
|
|
|
Amount: sdk.NewCoin("ukava", sdkmath.NewInt(1200)),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
PreviousAccumulationTimes: v016cdp.GenesisAccumulationTimes{
|
|
|
|
{
|
|
|
|
CollateralType: "usdx",
|
|
|
|
PreviousAccumulationTime: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
InterestFactor: sdk.MustNewDecFromStr("0.02"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TotalPrincipals: v016cdp.GenesisTotalPrincipals{
|
|
|
|
{
|
|
|
|
CollateralType: "usdx",
|
2023-04-05 23:21:59 +00:00
|
|
|
TotalPrincipal: sdkmath.NewInt(1200),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
genState := Migrate(s.v15genstate)
|
|
|
|
s.Require().Equal(expected, *genState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCdpMigrateTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(migrateTestSuite))
|
|
|
|
}
|