2019-12-06 23:53:20 +00:00
|
|
|
package keeper_test
|
2019-11-27 14:45:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
tmprototypes "github.com/cometbft/cometbft/proto/tendermint/types"
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/x/pricefeed/types"
|
2019-11-27 14:45:59 +00:00
|
|
|
)
|
|
|
|
|
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()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmprototypes.Header{})
|
2019-12-06 23:53:20 +00:00
|
|
|
keeper := tApp.GetPriceFeedKeeper()
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
mp := types.Params{
|
2022-01-08 00:39:27 +00:00
|
|
|
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)
|
2020-01-17 12:29:19 +00:00
|
|
|
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")
|
2022-01-08 00:39:27 +00:00
|
|
|
require.True(t, found, "market should be found")
|
|
|
|
|
|
|
|
_, found = keeper.GetMarket(ctx, "invalidmarket")
|
|
|
|
require.False(t, found, "invalidmarket should not be found")
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
mp = types.Params{
|
2022-01-08 00:39:27 +00:00
|
|
|
Markets: []types.Market{
|
|
|
|
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
|
|
{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)
|
2020-01-17 12:29:19 +00:00
|
|
|
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()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmprototypes.Header{})
|
|
|
|
keeper := tApp.GetPriceFeedKeeper()
|
|
|
|
|
|
|
|
mp := types.Params{
|
|
|
|
Markets: []types.Market{
|
|
|
|
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
keeper.SetParams(ctx, mp)
|
|
|
|
|
|
|
|
prices := []struct {
|
|
|
|
oracle sdk.AccAddress
|
|
|
|
marketID string
|
|
|
|
price sdk.Dec
|
|
|
|
total int
|
|
|
|
}{
|
|
|
|
{addrs[0], "tstusd", sdk.MustNewDecFromStr("0.33"), 1},
|
|
|
|
{addrs[1], "tstusd", sdk.MustNewDecFromStr("0.35"), 2},
|
|
|
|
{addrs[0], "tstusd", sdk.MustNewDecFromStr("0.37"), 2},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range prices {
|
|
|
|
// Set price by oracle 1
|
|
|
|
pp, err := keeper.SetPrice(
|
|
|
|
ctx,
|
|
|
|
p.oracle,
|
|
|
|
p.marketID,
|
|
|
|
p.price,
|
|
|
|
time.Now().UTC().Add(1*time.Hour),
|
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Get raw prices
|
|
|
|
rawPrices := keeper.GetRawPrices(ctx, "tstusd")
|
|
|
|
|
|
|
|
require.Equal(t, p.total, len(rawPrices))
|
|
|
|
require.Contains(t, rawPrices, pp)
|
|
|
|
|
|
|
|
// Find the oracle and require price to be same
|
|
|
|
for _, rp := range rawPrices {
|
|
|
|
if p.oracle.Equals(rp.OracleAddress) {
|
|
|
|
require.Equal(t, p.price, rp.Price)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestKeeper_GetSetCurrentPrice Test Setting the median price of an Asset
|
|
|
|
func TestKeeper_GetSetCurrentPrice(t *testing.T) {
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(5)
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, tmprototypes.Header{}).
|
|
|
|
WithBlockTime(time.Now().UTC())
|
2019-12-06 23:53:20 +00:00
|
|
|
keeper := tApp.GetPriceFeedKeeper()
|
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
mp := types.Params{
|
2022-01-08 00:39:27 +00:00
|
|
|
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)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
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"),
|
2022-01-08 00:39:27 +00:00
|
|
|
time.Now().Add(time.Hour*1))
|
2020-04-13 18:08:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
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)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err = keeper.SetPrice(
|
|
|
|
ctx, addrs[2], "tstusd",
|
|
|
|
sdk.MustNewDecFromStr("0.34"),
|
|
|
|
time.Now().Add(time.Hour*1))
|
2020-04-13 18:08:14 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// Add an expired one which should fail
|
2019-12-06 23:53:20 +00:00
|
|
|
_, err = keeper.SetPrice(
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx, addrs[3], "tstusd",
|
|
|
|
sdk.MustNewDecFromStr("0.9"),
|
|
|
|
ctx.BlockTime().Add(-time.Hour*1))
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Add a non-expired price, but will not be counted when BlockTime is changed
|
|
|
|
_, err = keeper.SetPrice(
|
|
|
|
ctx, addrs[3], "tstusd",
|
|
|
|
sdk.MustNewDecFromStr("0.9"),
|
|
|
|
time.Now().Add(time.Minute*30))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Update block time such that first 3 prices valid but last one is expired
|
|
|
|
ctx = ctx.WithBlockTime(time.Now().Add(time.Minute * 45))
|
|
|
|
|
|
|
|
// Set current price
|
|
|
|
err = keeper.SetCurrentPrices(ctx, "tstusd")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Get current price
|
|
|
|
price, err := keeper.GetCurrentPrice(ctx, "tstusd")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
expCurPrice := sdk.MustNewDecFromStr("0.34")
|
|
|
|
require.Truef(
|
|
|
|
t,
|
|
|
|
price.Price.Equal(expCurPrice),
|
|
|
|
"expected current price to equal %v, actual %v",
|
|
|
|
expCurPrice, price.Price,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Even number of oracles
|
|
|
|
_, err = keeper.SetPrice(
|
|
|
|
ctx, addrs[4], "tstusd",
|
|
|
|
sdk.MustNewDecFromStr("0.36"),
|
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)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
err = keeper.SetCurrentPrices(ctx, "tstusd")
|
2020-04-13 18:08:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
price, err = keeper.GetCurrentPrice(ctx, "tstusd")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
exp := sdk.MustNewDecFromStr("0.345")
|
|
|
|
require.Truef(t, price.Price.Equal(exp),
|
|
|
|
"current price %s should be %s",
|
|
|
|
price.Price.String(),
|
|
|
|
exp.String(),
|
|
|
|
)
|
|
|
|
|
|
|
|
prices := keeper.GetCurrentPrices(ctx)
|
|
|
|
require.Equal(t, 1, len(prices))
|
|
|
|
require.Equal(t, price, prices[0])
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func TestKeeper_ExpiredSetCurrentPrices(t *testing.T) {
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(5)
|
2019-12-06 23:53:20 +00:00
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmprototypes.Header{}).
|
|
|
|
WithBlockTime(time.Now().UTC())
|
2019-12-06 23:53:20 +00:00
|
|
|
keeper := tApp.GetPriceFeedKeeper()
|
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
mp := types.Params{
|
2022-01-08 00:39:27 +00:00
|
|
|
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)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
_, err := keeper.SetPrice(
|
2019-12-06 23:53:20 +00:00
|
|
|
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))
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = keeper.SetPrice(
|
2019-12-06 23:53:20 +00:00
|
|
|
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))
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = keeper.SetPrice(
|
2019-12-06 23:53:20 +00:00
|
|
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// Update block time such that all prices expire
|
|
|
|
ctx = ctx.WithBlockTime(time.Now().UTC().Add(time.Hour * 2))
|
|
|
|
|
2019-12-06 23:53:20 +00:00
|
|
|
err = keeper.SetCurrentPrices(ctx, "tstusd")
|
2022-01-08 00:39:27 +00:00
|
|
|
require.ErrorIs(t, types.ErrNoValidPrice, err, "there should be no valid prices to be set")
|
|
|
|
|
|
|
|
_, err = keeper.GetCurrentPrice(ctx, "tstusd")
|
|
|
|
require.ErrorIs(t, types.ErrNoValidPrice, err, "current prices should be invalid")
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|