update pricefeed tests

This commit is contained in:
rhuairahrighairigh 2019-12-06 23:53:20 +00:00
parent 1295dfbffc
commit 83969f3702
3 changed files with 55 additions and 104 deletions

View File

@ -21,7 +21,10 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
// Set the current price (if any) based on what's now in the store // Set the current price (if any) based on what's now in the store
for _, a := range data.Params.Markets { for _, a := range data.Params.Markets {
if a.Active { if a.Active {
_ = keeper.SetCurrentPrices(ctx, a.MarketID) err := keeper.SetCurrentPrices(ctx, a.MarketID)
if err != nil {
panic(err)
}
} }
} }
} }

View File

@ -1,4 +1,4 @@
package keeper package keeper_test
import ( import (
"testing" "testing"
@ -7,31 +7,28 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/pricefeed/types" "github.com/kava-labs/kava/x/pricefeed/types"
) )
// TestKeeper_SetGetMarket tests adding markets to the pricefeed, getting markets from the store // TestKeeper_SetGetMarket tests adding markets to the pricefeed, getting markets from the store
func TestKeeper_SetGetMarket(t *testing.T) { func TestKeeper_SetGetMarket(t *testing.T) {
helper := getMockApp(t, 0, types.GenesisState{}, nil) tApp := app.NewTestApp()
header := abci.Header{ ctx := tApp.NewContext(true, abci.Header{})
Height: helper.mApp.LastBlockHeight() + 1, keeper := tApp.GetPriceFeedKeeper()
Time: tmtime.Now()}
helper.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
ctx := helper.mApp.BaseApp.NewContext(false, header)
mp := types.Params{ mp := types.Params{
Markets: types.Markets{ Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true}, types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true},
}, },
} }
helper.keeper.SetParams(ctx, mp) keeper.SetParams(ctx, mp)
markets := helper.keeper.GetMarketParams(ctx) markets := keeper.GetMarketParams(ctx)
require.Equal(t, len(markets), 1) require.Equal(t, len(markets), 1)
require.Equal(t, markets[0].MarketID, "tstusd") require.Equal(t, markets[0].MarketID, "tstusd")
_, found := helper.keeper.GetMarket(ctx, "tstusd") _, found := keeper.GetMarket(ctx, "tstusd")
require.Equal(t, found, true) require.Equal(t, found, true)
mp = types.Params{ mp = types.Params{
@ -40,102 +37,99 @@ func TestKeeper_SetGetMarket(t *testing.T) {
types.Market{MarketID: "tst2usd", BaseAsset: "tst2", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true}, types.Market{MarketID: "tst2usd", BaseAsset: "tst2", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true},
}, },
} }
helper.keeper.SetParams(ctx, mp) keeper.SetParams(ctx, mp)
markets = helper.keeper.GetMarketParams(ctx) markets = keeper.GetMarketParams(ctx)
require.Equal(t, len(markets), 2) require.Equal(t, len(markets), 2)
require.Equal(t, markets[0].MarketID, "tstusd") require.Equal(t, markets[0].MarketID, "tstusd")
require.Equal(t, markets[1].MarketID, "tst2usd") require.Equal(t, markets[1].MarketID, "tst2usd")
_, found = helper.keeper.GetMarket(ctx, "nan") _, found = keeper.GetMarket(ctx, "nan")
require.Equal(t, found, false) require.Equal(t, found, false)
} }
// TestKeeper_GetSetPrice Test Posting the price by an oracle // TestKeeper_GetSetPrice Test Posting the price by an oracle
func TestKeeper_GetSetPrice(t *testing.T) { func TestKeeper_GetSetPrice(t *testing.T) {
helper := getMockApp(t, 2, types.GenesisState{}, nil) _, addrs := app.GeneratePrivKeyAddressPairs(2)
header := abci.Header{ tApp := app.NewTestApp()
Height: helper.mApp.LastBlockHeight() + 1, ctx := tApp.NewContext(true, abci.Header{})
Time: tmtime.Now()} keeper := tApp.GetPriceFeedKeeper()
helper.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
ctx := helper.mApp.BaseApp.NewContext(false, header)
mp := types.Params{ mp := types.Params{
Markets: types.Markets{ Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true}, types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true},
}, },
} }
helper.keeper.SetParams(ctx, mp) keeper.SetParams(ctx, mp)
// Set price by oracle 1 // Set price by oracle 1
_, err := helper.keeper.SetPrice( _, err := keeper.SetPrice(
ctx, helper.addrs[0], "tstusd", ctx, addrs[0], "tstusd",
sdk.MustNewDecFromStr("0.33"), sdk.MustNewDecFromStr("0.33"),
header.Time.Add(1*time.Hour)) time.Now().Add(1*time.Hour))
require.NoError(t, err) require.NoError(t, err)
// Get raw prices // Get raw prices
rawPrices := helper.keeper.GetRawPrices(ctx, "tstusd") rawPrices := keeper.GetRawPrices(ctx, "tstusd")
require.Equal(t, len(rawPrices), 1) require.Equal(t, len(rawPrices), 1)
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.33")), true) require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.33")), true)
// Set price by oracle 2 // Set price by oracle 2
_, err = helper.keeper.SetPrice( _, err = keeper.SetPrice(
ctx, helper.addrs[1], "tstusd", ctx, addrs[1], "tstusd",
sdk.MustNewDecFromStr("0.35"), sdk.MustNewDecFromStr("0.35"),
header.Time.Add(time.Hour*1)) time.Now().Add(time.Hour*1))
require.NoError(t, err) require.NoError(t, err)
rawPrices = helper.keeper.GetRawPrices(ctx, "tstusd") rawPrices = keeper.GetRawPrices(ctx, "tstusd")
require.Equal(t, len(rawPrices), 2) require.Equal(t, len(rawPrices), 2)
require.Equal(t, rawPrices[1].Price.Equal(sdk.MustNewDecFromStr("0.35")), true) require.Equal(t, rawPrices[1].Price.Equal(sdk.MustNewDecFromStr("0.35")), true)
// Update Price by Oracle 1 // Update Price by Oracle 1
_, err = helper.keeper.SetPrice( _, err = keeper.SetPrice(
ctx, helper.addrs[0], "tstusd", ctx, addrs[0], "tstusd",
sdk.MustNewDecFromStr("0.37"), sdk.MustNewDecFromStr("0.37"),
header.Time.Add(time.Hour*1)) time.Now().Add(time.Hour*1))
require.NoError(t, err) require.NoError(t, err)
rawPrices = helper.keeper.GetRawPrices(ctx, "tstusd") rawPrices = keeper.GetRawPrices(ctx, "tstusd")
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.37")), true) require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.37")), true)
} }
// TestKeeper_GetSetCurrentPrice Test Setting the median price of an Asset // TestKeeper_GetSetCurrentPrice Test Setting the median price of an Asset
func TestKeeper_GetSetCurrentPrice(t *testing.T) { func TestKeeper_GetSetCurrentPrice(t *testing.T) {
helper := getMockApp(t, 4, types.GenesisState{}, nil) _, addrs := app.GeneratePrivKeyAddressPairs(4)
header := abci.Header{ tApp := app.NewTestApp()
Height: helper.mApp.LastBlockHeight() + 1, ctx := tApp.NewContext(true, abci.Header{})
Time: tmtime.Now()} keeper := tApp.GetPriceFeedKeeper()
helper.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
ctx := helper.mApp.BaseApp.NewContext(false, header)
mp := types.Params{ mp := types.Params{
Markets: types.Markets{ Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true}, types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: types.Oracles{}, Active: true},
}, },
} }
helper.keeper.SetParams(ctx, mp) keeper.SetParams(ctx, mp)
helper.keeper.SetPrice( keeper.SetPrice(
ctx, helper.addrs[0], "tstusd", ctx, addrs[0], "tstusd",
sdk.MustNewDecFromStr("0.33"), sdk.MustNewDecFromStr("0.33"),
header.Time.Add(time.Hour*1)) time.Now().Add(time.Hour*1))
helper.keeper.SetPrice( keeper.SetPrice(
ctx, helper.addrs[1], "tstusd", ctx, addrs[1], "tstusd",
sdk.MustNewDecFromStr("0.35"), sdk.MustNewDecFromStr("0.35"),
header.Time.Add(time.Hour*1)) time.Now().Add(time.Hour*1))
helper.keeper.SetPrice( keeper.SetPrice(
ctx, helper.addrs[2], "tstusd", ctx, addrs[2], "tstusd",
sdk.MustNewDecFromStr("0.34"), sdk.MustNewDecFromStr("0.34"),
header.Time.Add(time.Hour*1)) time.Now().Add(time.Hour*1))
// Set current price // Set current price
err := helper.keeper.SetCurrentPrices(ctx, "tstusd") err := keeper.SetCurrentPrices(ctx, "tstusd")
require.NoError(t, err) require.NoError(t, err)
// Get Current price // Get Current price
price := helper.keeper.GetCurrentPrice(ctx, "tstusd") price := keeper.GetCurrentPrice(ctx, "tstusd")
require.Equal(t, price.Price.Equal(sdk.MustNewDecFromStr("0.34")), true) require.Equal(t, price.Price.Equal(sdk.MustNewDecFromStr("0.34")), true)
// Even number of oracles // Even number of oracles
helper.keeper.SetPrice( keeper.SetPrice(
ctx, helper.addrs[3], "tstusd", ctx, addrs[3], "tstusd",
sdk.MustNewDecFromStr("0.36"), sdk.MustNewDecFromStr("0.36"),
header.Time.Add(time.Hour*1)) time.Now().Add(time.Hour*1))
err = helper.keeper.SetCurrentPrices(ctx, "tstusd") err = keeper.SetCurrentPrices(ctx, "tstusd")
require.NoError(t, err) require.NoError(t, err)
price = helper.keeper.GetCurrentPrice(ctx, "tstusd") price = keeper.GetCurrentPrice(ctx, "tstusd")
require.Equal(t, price.Price.Equal(sdk.MustNewDecFromStr("0.345")), true) require.Equal(t, price.Price.Equal(sdk.MustNewDecFromStr("0.345")), true)
} }

View File

@ -1,46 +0,0 @@
package keeper
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/mock"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"github.com/kava-labs/kava/x/pricefeed/types"
)
type testHelper struct {
mApp *mock.App
keeper Keeper
addrs []sdk.AccAddress
pubKeys []crypto.PubKey
privKeys []crypto.PrivKey
}
func getMockApp(t *testing.T, numGenAccs int, genState types.GenesisState, genAccs []authexported.Account) testHelper {
mApp := mock.NewApp()
types.RegisterCodec(mApp.Cdc)
keyPricefeed := sdk.NewKVStoreKey(types.StoreKey)
pk := mApp.ParamsKeeper
keeper := NewKeeper(mApp.Cdc, keyPricefeed, pk.Subspace(types.DefaultParamspace), types.DefaultCodespace)
require.NoError(t, mApp.CompleteSetup(keyPricefeed))
valTokens := sdk.TokensFromConsensusPower(42)
var (
addrs []sdk.AccAddress
pubKeys []crypto.PubKey
privKeys []crypto.PrivKey
)
if genAccs == nil || len(genAccs) == 0 {
genAccs, addrs, pubKeys, privKeys = mock.CreateGenAccounts(numGenAccs,
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, valTokens)))
}
mock.SetGenesis(mApp, genAccs)
return testHelper{mApp, keeper, addrs, pubKeys, privKeys}
}