0g-chain/x/pricefeed/legacy/v0_16/migrate_test.go

152 lines
3.7 KiB
Go
Raw Normal View History

package v0_16
import (
"testing"
"time"
"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"
v015pricefeed "github.com/kava-labs/kava/x/pricefeed/legacy/v0_15"
v016pricefeed "github.com/kava-labs/kava/x/pricefeed/types"
)
type migrateTestSuite struct {
suite.Suite
addresses []sdk.AccAddress
v15genstate v015pricefeed.GenesisState
cdc codec.Codec
legacyCdc *codec.LegacyAmino
}
func (s *migrateTestSuite) SetupTest() {
app.SetSDKConfig()
s.v15genstate = v015pricefeed.GenesisState{
Params: v015pricefeed.Params{},
PostedPrices: v015pricefeed.PostedPrices{},
}
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() {
// Migrate v15 pricefeed to v16
data := `{
"params": {
"markets": [
{
"active": true,
"base_asset": "bnb",
"market_id": "bnb:usd",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "bnb",
"market_id": "bnb:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
}
]
},
"posted_prices": [
{
"expiry": "2022-07-20T00:00:00Z",
"market_id": "bnb:usd",
"oracle_address": "kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em",
"price": "215.962650000000001782"
},
{
"expiry": "2022-07-20T00:00:00Z",
"market_id": "bnb:usd:30",
"oracle_address": "kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em",
"price": "217.962650000000001782"
}
]
}`
err := s.legacyCdc.UnmarshalJSON([]byte(data), &s.v15genstate)
s.Require().NoError(err)
genstate := Migrate(s.v15genstate)
// v16 pricefeed json should be the same as v15
actual := s.cdc.MustMarshalJSON(genstate)
s.Require().NoError(err)
s.Require().JSONEq(data, string(actual))
}
func (s *migrateTestSuite) TestMigrate_Params() {
s.v15genstate.Params = v015pricefeed.Params{
Markets: v015pricefeed.Markets{
{
MarketID: "market-1",
BaseAsset: "kava",
QuoteAsset: "usd",
Oracles: s.addresses,
Active: true,
},
},
}
expectedParams := v016pricefeed.Params{
Markets: v016pricefeed.Markets{
{
MarketID: "market-1",
BaseAsset: "kava",
QuoteAsset: "usd",
Oracles: s.addresses,
Active: true,
},
},
}
genState := Migrate(s.v15genstate)
s.Require().Equal(expectedParams, genState.Params)
}
func (s *migrateTestSuite) TestMigrate_PostedPrices() {
s.v15genstate.PostedPrices = v015pricefeed.PostedPrices{
{
MarketID: "market-1",
OracleAddress: s.addresses[0],
Price: sdk.MustNewDecFromStr("1.2"),
Expiry: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
},
{
MarketID: "market-2",
OracleAddress: s.addresses[1],
Price: sdk.MustNewDecFromStr("1.899"),
Expiry: time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC),
},
}
expected := v016pricefeed.PostedPrices{
{
MarketID: "market-1",
OracleAddress: s.addresses[0],
Price: sdk.MustNewDecFromStr("1.2"),
Expiry: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
},
{
MarketID: "market-2",
OracleAddress: s.addresses[1],
Price: sdk.MustNewDecFromStr("1.899"),
Expiry: time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC),
},
}
genState := Migrate(s.v15genstate)
s.Require().Equal(expected, genState.PostedPrices)
}
func TestPriceFeedMigrateTestSuite(t *testing.T) {
suite.Run(t, new(migrateTestSuite))
}