mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
R4R: add events to pricefeed module (#343)
* added events, attributes to pricefeed msgs and core functionality * removed added space * conditionally emit market_price_updated if price has changed * updated expiry to Unix format
This commit is contained in:
parent
99be9dd8ab
commit
075a3089ce
@ -17,7 +17,14 @@ const (
|
||||
CodeInvalidPrice = types.CodeInvalidPrice
|
||||
CodeInvalidAsset = types.CodeInvalidAsset
|
||||
CodeInvalidOracle = types.CodeInvalidOracle
|
||||
EventTypeMarketPriceUpdated = types.EventTypeMarketPriceUpdated
|
||||
EventTypeOracleUpdatedPrice = types.EventTypeOracleUpdatedPrice
|
||||
EventTypeNoValidPrices = types.EventTypeNoValidPrices
|
||||
AttributeValueCategory = types.AttributeValueCategory
|
||||
AttributeMarketID = types.AttributeMarketID
|
||||
AttributeMarketPrice = types.AttributeMarketPrice
|
||||
AttributeOracle = types.AttributeOracle
|
||||
AttributeExpiry = types.AttributeExpiry
|
||||
AttributeKeyPriceUpdateFailed = types.AttributeKeyPriceUpdateFailed
|
||||
ModuleName = types.ModuleName
|
||||
StoreKey = types.StoreKey
|
||||
|
@ -36,5 +36,14 @@ func HandleMsgPostPrice(
|
||||
if err != nil {
|
||||
return err.Result()
|
||||
}
|
||||
return sdk.Result{}
|
||||
|
||||
ctx.EventManager().EmitEvent(
|
||||
sdk.NewEvent(
|
||||
sdk.EventTypeMessage,
|
||||
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
|
||||
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.String()),
|
||||
),
|
||||
)
|
||||
|
||||
return sdk.Result{Events: ctx.EventManager().Events()}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
@ -67,6 +68,16 @@ func (k Keeper) SetPrice(
|
||||
index = len(prices) - 1
|
||||
}
|
||||
|
||||
// Emit an event containing the oracle's new price
|
||||
ctx.EventManager().EmitEvent(
|
||||
sdk.NewEvent(
|
||||
types.EventTypeOracleUpdatedPrice,
|
||||
sdk.NewAttribute(types.AttributeMarketID, marketID),
|
||||
sdk.NewAttribute(types.AttributeOracle, oracle.String()),
|
||||
sdk.NewAttribute(types.AttributeMarketPrice, price.String()),
|
||||
sdk.NewAttribute(types.AttributeExpiry, fmt.Sprintf("%d", expiry.Unix())),
|
||||
),
|
||||
)
|
||||
store.Set(
|
||||
[]byte(types.RawPriceFeedPrefix+marketID), k.cdc.MustMarshalBinaryBare(prices),
|
||||
)
|
||||
@ -76,12 +87,19 @@ func (k Keeper) SetPrice(
|
||||
|
||||
}
|
||||
|
||||
// SetCurrentPrices updates the price of an asset to the meadian of all valid oracle inputs
|
||||
// SetCurrentPrices updates the price of an asset to the median of all valid oracle inputs
|
||||
func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) sdk.Error {
|
||||
_, ok := k.GetMarket(ctx, marketID)
|
||||
if !ok {
|
||||
return types.ErrInvalidMarket(k.codespace, marketID)
|
||||
}
|
||||
// store current price
|
||||
validPrevPrice := true
|
||||
prevPrice, err := k.GetCurrentPrice(ctx, marketID)
|
||||
if err != nil {
|
||||
validPrevPrice = false
|
||||
}
|
||||
|
||||
prices := k.GetRawPrices(ctx, marketID)
|
||||
var notExpiredPrices []types.CurrentPrice
|
||||
// filter out expired prices
|
||||
@ -102,11 +120,26 @@ func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) sdk.Error {
|
||||
}
|
||||
medianPrice := k.CalculateMedianPrice(ctx, notExpiredPrices)
|
||||
|
||||
// check case that market price was not set in genesis
|
||||
if validPrevPrice {
|
||||
// only emit event if price has changed
|
||||
if !medianPrice.Equal(prevPrice.Price) {
|
||||
ctx.EventManager().EmitEvent(
|
||||
sdk.NewEvent(
|
||||
types.EventTypeMarketPriceUpdated,
|
||||
sdk.NewAttribute(types.AttributeMarketID, fmt.Sprintf("%s", marketID)),
|
||||
sdk.NewAttribute(types.AttributeMarketPrice, fmt.Sprintf("%s", medianPrice.String())),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
store := ctx.KVStore(k.key)
|
||||
currentPrice := types.CurrentPrice{
|
||||
MarketID: marketID,
|
||||
Price: medianPrice,
|
||||
}
|
||||
|
||||
store.Set(
|
||||
[]byte(types.CurrentPricePrefix+marketID), k.cdc.MustMarshalBinaryBare(currentPrice),
|
||||
)
|
||||
|
@ -2,7 +2,14 @@ package types
|
||||
|
||||
// Pricefeed module event types
|
||||
const (
|
||||
EventTypeNoValidPrices = "no_valid_prices"
|
||||
EventTypeMarketPriceUpdated = "market_price_updated"
|
||||
EventTypeOracleUpdatedPrice = "oracle_updated_price"
|
||||
EventTypeNoValidPrices = "no_valid_prices"
|
||||
|
||||
AttributeValueCategory = ModuleName
|
||||
AttributeMarketID = "market_id"
|
||||
AttributeMarketPrice = "market_price"
|
||||
AttributeOracle = "oracle"
|
||||
AttributeExpiry = "expiry"
|
||||
AttributeKeyPriceUpdateFailed = "price_update_failed"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user