diff --git a/x/pricefeed/alias.go b/x/pricefeed/alias.go index e3fa62ac..1c434163 100644 --- a/x/pricefeed/alias.go +++ b/x/pricefeed/alias.go @@ -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 diff --git a/x/pricefeed/handler.go b/x/pricefeed/handler.go index cfcc34e0..628f8951 100644 --- a/x/pricefeed/handler.go +++ b/x/pricefeed/handler.go @@ -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()} } diff --git a/x/pricefeed/keeper/keeper.go b/x/pricefeed/keeper/keeper.go index 10c8b410..0e4fc8e9 100644 --- a/x/pricefeed/keeper/keeper.go +++ b/x/pricefeed/keeper/keeper.go @@ -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), ) diff --git a/x/pricefeed/types/events.go b/x/pricefeed/types/events.go index 0bf5fa51..07a0007d 100644 --- a/x/pricefeed/types/events.go +++ b/x/pricefeed/types/events.go @@ -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" )