0g-chain/x/pricefeed/keeper/keeper_test.go

141 lines
4.4 KiB
Go
Raw Normal View History

2019-12-06 23:53:20 +00:00
package keeper_test
2019-11-27 14:45:59 +00:00
import (
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
2019-12-06 23:53:20 +00:00
"github.com/kava-labs/kava/app"
2019-11-27 14:45:59 +00:00
"github.com/kava-labs/kava/x/pricefeed/types"
)
2019-12-04 16:32:08 +00:00
// TestKeeper_SetGetMarket tests adding markets to the pricefeed, getting markets from the store
func TestKeeper_SetGetMarket(t *testing.T) {
2019-12-06 23:53:20 +00:00
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, abci.Header{})
keeper := tApp.GetPriceFeedKeeper()
2019-11-27 14:45:59 +00:00
2019-12-04 16:32:08 +00:00
mp := types.Params{
Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
2019-11-27 14:45:59 +00:00
},
}
2019-12-06 23:53:20 +00:00
keeper.SetParams(ctx, mp)
markets := keeper.GetMarkets(ctx)
2019-12-04 16:32:08 +00:00
require.Equal(t, len(markets), 1)
require.Equal(t, markets[0].MarketID, "tstusd")
2019-11-27 14:45:59 +00:00
2019-12-06 23:53:20 +00:00
_, found := keeper.GetMarket(ctx, "tstusd")
2019-11-27 14:45:59 +00:00
require.Equal(t, found, true)
2019-12-04 16:32:08 +00:00
mp = types.Params{
Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
types.Market{MarketID: "tst2usd", BaseAsset: "tst2", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
2019-11-27 14:45:59 +00:00
},
}
2019-12-06 23:53:20 +00:00
keeper.SetParams(ctx, mp)
markets = keeper.GetMarkets(ctx)
2019-12-04 16:32:08 +00:00
require.Equal(t, len(markets), 2)
require.Equal(t, markets[0].MarketID, "tstusd")
require.Equal(t, markets[1].MarketID, "tst2usd")
2019-11-27 14:45:59 +00:00
2019-12-06 23:53:20 +00:00
_, found = keeper.GetMarket(ctx, "nan")
2019-11-27 14:45:59 +00:00
require.Equal(t, found, false)
}
// TestKeeper_GetSetPrice Test Posting the price by an oracle
func TestKeeper_GetSetPrice(t *testing.T) {
2019-12-06 23:53:20 +00:00
_, addrs := app.GeneratePrivKeyAddressPairs(2)
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, abci.Header{})
keeper := tApp.GetPriceFeedKeeper()
2019-12-04 16:32:08 +00:00
mp := types.Params{
Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
2019-11-27 14:45:59 +00:00
},
}
2019-12-06 23:53:20 +00:00
keeper.SetParams(ctx, mp)
2019-11-27 14:45:59 +00:00
// Set price by oracle 1
2019-12-06 23:53:20 +00:00
_, err := keeper.SetPrice(
ctx, addrs[0], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.33"),
2019-12-06 23:53:20 +00:00
time.Now().Add(1*time.Hour))
2019-11-27 14:45:59 +00:00
require.NoError(t, err)
// Get raw prices
2020-04-13 18:08:14 +00:00
rawPrices, err := keeper.GetRawPrices(ctx, "tstusd")
require.NoError(t, err)
2019-11-27 14:45:59 +00:00
require.Equal(t, len(rawPrices), 1)
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.33")), true)
// Set price by oracle 2
2019-12-06 23:53:20 +00:00
_, err = keeper.SetPrice(
ctx, addrs[1], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.35"),
2019-12-06 23:53:20 +00:00
time.Now().Add(time.Hour*1))
2019-11-27 14:45:59 +00:00
require.NoError(t, err)
2020-04-13 18:08:14 +00:00
rawPrices, err = keeper.GetRawPrices(ctx, "tstusd")
require.NoError(t, err)
2019-11-27 14:45:59 +00:00
require.Equal(t, len(rawPrices), 2)
require.Equal(t, rawPrices[1].Price.Equal(sdk.MustNewDecFromStr("0.35")), true)
// Update Price by Oracle 1
2019-12-06 23:53:20 +00:00
_, err = keeper.SetPrice(
ctx, addrs[0], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.37"),
2019-12-06 23:53:20 +00:00
time.Now().Add(time.Hour*1))
2019-11-27 14:45:59 +00:00
require.NoError(t, err)
2020-04-13 18:08:14 +00:00
rawPrices, err = keeper.GetRawPrices(ctx, "tstusd")
require.NoError(t, err)
2019-11-27 14:45:59 +00:00
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.37")), true)
}
// TestKeeper_GetSetCurrentPrice Test Setting the median price of an Asset
func TestKeeper_GetSetCurrentPrice(t *testing.T) {
2019-12-06 23:53:20 +00:00
_, addrs := app.GeneratePrivKeyAddressPairs(4)
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, abci.Header{})
keeper := tApp.GetPriceFeedKeeper()
2019-12-04 16:32:08 +00:00
mp := types.Params{
Markets: types.Markets{
types.Market{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
2019-11-27 14:45:59 +00:00
},
}
2019-12-06 23:53:20 +00:00
keeper.SetParams(ctx, mp)
keeper.SetPrice(
ctx, addrs[0], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.33"),
2019-12-06 23:53:20 +00:00
time.Now().Add(time.Hour*1))
keeper.SetPrice(
ctx, addrs[1], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.35"),
2019-12-06 23:53:20 +00:00
time.Now().Add(time.Hour*1))
keeper.SetPrice(
ctx, addrs[2], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.34"),
2019-12-06 23:53:20 +00:00
time.Now().Add(time.Hour*1))
2019-11-27 14:45:59 +00:00
// Set current price
2019-12-06 23:53:20 +00:00
err := keeper.SetCurrentPrices(ctx, "tstusd")
2019-11-27 14:45:59 +00:00
require.NoError(t, err)
// Get Current price
price, err := keeper.GetCurrentPrice(ctx, "tstusd")
require.Nil(t, err)
2019-11-27 14:45:59 +00:00
require.Equal(t, price.Price.Equal(sdk.MustNewDecFromStr("0.34")), true)
// Even number of oracles
2019-12-06 23:53:20 +00:00
keeper.SetPrice(
ctx, addrs[3], "tstusd",
2019-11-27 14:45:59 +00:00
sdk.MustNewDecFromStr("0.36"),
2019-12-06 23:53:20 +00:00
time.Now().Add(time.Hour*1))
err = keeper.SetCurrentPrices(ctx, "tstusd")
2019-11-27 14:45:59 +00:00
require.NoError(t, err)
price, err = keeper.GetCurrentPrice(ctx, "tstusd")
require.Nil(t, err)
2019-11-27 14:45:59 +00:00
require.Equal(t, price.Price.Equal(sdk.MustNewDecFromStr("0.345")), true)
}