mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 16:25:21 +00:00
075a3089ce
* 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
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package pricefeed
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
// NewHandler handles all pricefeed type messages
|
|
func NewHandler(k Keeper) sdk.Handler {
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
switch msg := msg.(type) {
|
|
case MsgPostPrice:
|
|
return HandleMsgPostPrice(ctx, k, msg)
|
|
default:
|
|
errMsg := fmt.Sprintf("unrecognized pricefeed message type: %T", msg)
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
|
}
|
|
}
|
|
}
|
|
|
|
// price feed questions:
|
|
// do proposers need to post the round in the message? If not, how do we determine the round?
|
|
|
|
// HandleMsgPostPrice handles prices posted by oracles
|
|
func HandleMsgPostPrice(
|
|
ctx sdk.Context,
|
|
k Keeper,
|
|
msg MsgPostPrice) sdk.Result {
|
|
|
|
_, err := k.GetOracle(ctx, msg.MarketID, msg.From)
|
|
if err != nil {
|
|
return err.Result()
|
|
}
|
|
_, err = k.SetPrice(ctx, msg.From, msg.MarketID, msg.Price, msg.Expiry)
|
|
if err != nil {
|
|
return err.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()}
|
|
}
|