mirror of
https://github.com/0glabs/0g-chain.git
synced 2025-01-12 16:25:17 +00:00
address review comments
This commit is contained in:
parent
f425efc56c
commit
8f9aece875
@ -25,7 +25,10 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, gs GenesisState) {
|
||||
// Set the current price (if any) based on what's now in the store
|
||||
for _, market := range params.Markets {
|
||||
if market.Active {
|
||||
rps := keeper.GetRawPrices(ctx, market.MarketID)
|
||||
rps, err := keeper.GetRawPrices(ctx, market.MarketID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if len(rps) > 0 {
|
||||
err := keeper.SetCurrentPrices(ctx, market.MarketID)
|
||||
if err != nil {
|
||||
@ -44,7 +47,10 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
|
||||
|
||||
var postedPrices []PostedPrice
|
||||
for _, market := range keeper.GetMarkets(ctx) {
|
||||
pp := keeper.GetRawPrices(ctx, market.MarketID)
|
||||
pp, err := keeper.GetRawPrices(ctx, market.MarketID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
postedPrices = append(postedPrices, pp...)
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,10 @@ func (k Keeper) SetPrice(
|
||||
// If the expiry is less than or equal to the current blockheight, we consider the price valid
|
||||
if expiry.After(ctx.BlockTime()) {
|
||||
store := ctx.KVStore(k.key)
|
||||
prices := k.GetRawPrices(ctx, marketID)
|
||||
prices, err := k.GetRawPrices(ctx, marketID)
|
||||
if err != nil {
|
||||
return types.PostedPrice{}, err
|
||||
}
|
||||
var index int
|
||||
found := false
|
||||
for i := range prices {
|
||||
@ -96,7 +99,10 @@ func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) sdk.Error {
|
||||
validPrevPrice = false
|
||||
}
|
||||
|
||||
prices := k.GetRawPrices(ctx, marketID)
|
||||
prices, err := k.GetRawPrices(ctx, marketID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var notExpiredPrices types.CurrentPrices
|
||||
// filter out expired prices
|
||||
for _, v := range prices {
|
||||
@ -174,8 +180,10 @@ func (k Keeper) GetCurrentPrice(ctx sdk.Context, marketID string) (types.Current
|
||||
return types.CurrentPrice{}, types.ErrNoValidPrice(k.codespace)
|
||||
}
|
||||
var price types.CurrentPrice
|
||||
k.cdc.MustUnmarshalBinaryBare(bz, &price)
|
||||
|
||||
err := k.cdc.UnmarshalBinaryBare(bz, &price)
|
||||
if err != nil {
|
||||
return types.CurrentPrice{}, sdk.ErrInternal(sdk.AppendMsgToErr("failed to unmarshal result", err.Error()))
|
||||
}
|
||||
if price.Price.Equal(sdk.ZeroDec()) {
|
||||
return types.CurrentPrice{}, types.ErrNoValidPrice(k.codespace)
|
||||
}
|
||||
@ -183,12 +191,18 @@ func (k Keeper) GetCurrentPrice(ctx sdk.Context, marketID string) (types.Current
|
||||
}
|
||||
|
||||
// GetRawPrices fetches the set of all prices posted by oracles for an asset
|
||||
func (k Keeper) GetRawPrices(ctx sdk.Context, marketID string) types.PostedPrices {
|
||||
func (k Keeper) GetRawPrices(ctx sdk.Context, marketID string) (types.PostedPrices, sdk.Error) {
|
||||
store := ctx.KVStore(k.key)
|
||||
bz := store.Get(types.RawPriceKey(marketID))
|
||||
if bz == nil {
|
||||
return types.PostedPrices{}, nil
|
||||
}
|
||||
var prices types.PostedPrices
|
||||
k.cdc.MustUnmarshalBinaryBare(bz, &prices)
|
||||
return prices
|
||||
err := k.cdc.UnmarshalBinaryBare(bz, &prices)
|
||||
if err != nil {
|
||||
return types.PostedPrices{}, sdk.ErrInternal(sdk.AppendMsgToErr("failed to unmarshal result", err.Error()))
|
||||
}
|
||||
return prices, nil
|
||||
}
|
||||
|
||||
// Codespace return the codespace for the keeper
|
||||
|
@ -67,7 +67,8 @@ func TestKeeper_GetSetPrice(t *testing.T) {
|
||||
time.Now().Add(1*time.Hour))
|
||||
require.NoError(t, err)
|
||||
// Get raw prices
|
||||
rawPrices := keeper.GetRawPrices(ctx, "tstusd")
|
||||
rawPrices, err := keeper.GetRawPrices(ctx, "tstusd")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(rawPrices), 1)
|
||||
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.33")), true)
|
||||
// Set price by oracle 2
|
||||
@ -77,7 +78,8 @@ func TestKeeper_GetSetPrice(t *testing.T) {
|
||||
time.Now().Add(time.Hour*1))
|
||||
require.NoError(t, err)
|
||||
|
||||
rawPrices = keeper.GetRawPrices(ctx, "tstusd")
|
||||
rawPrices, err = keeper.GetRawPrices(ctx, "tstusd")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(rawPrices), 2)
|
||||
require.Equal(t, rawPrices[1].Price.Equal(sdk.MustNewDecFromStr("0.35")), true)
|
||||
|
||||
@ -87,7 +89,8 @@ func TestKeeper_GetSetPrice(t *testing.T) {
|
||||
sdk.MustNewDecFromStr("0.37"),
|
||||
time.Now().Add(time.Hour*1))
|
||||
require.NoError(t, err)
|
||||
rawPrices = keeper.GetRawPrices(ctx, "tstusd")
|
||||
rawPrices, err = keeper.GetRawPrices(ctx, "tstusd")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.37")), true)
|
||||
}
|
||||
|
||||
|
@ -56,18 +56,20 @@ func queryPrice(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []by
|
||||
|
||||
func queryRawPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) {
|
||||
var requestParams types.QueryWithMarketIDParams
|
||||
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
||||
if err != nil {
|
||||
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
||||
err2 := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
||||
if err2 != nil {
|
||||
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err2))
|
||||
}
|
||||
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
||||
if !found {
|
||||
return []byte{}, sdk.ErrUnknownRequest("asset not found")
|
||||
}
|
||||
rawPrices := keeper.GetRawPrices(ctx, requestParams.MarketID)
|
||||
|
||||
bz, err := codec.MarshalJSONIndent(keeper.cdc, rawPrices)
|
||||
rawPrices, err := keeper.GetRawPrices(ctx, requestParams.MarketID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, rawPrices)
|
||||
if err2 != nil {
|
||||
panic("could not marshal result to JSON")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user