-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
52 lines (44 loc) · 1.39 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package pricefeed
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/lcnem/eurx/x/pricefeed/keeper"
"github.com/lcnem/eurx/x/pricefeed/types"
)
// NewHandler ...
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case *types.MsgPostPrice:
return HandleMsgPostPrice(ctx, k, msg)
// this line is used by starport scaffolding # 1
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
// HandleMsgPostPrice handles prices posted by oracles
func HandleMsgPostPrice(
ctx sdk.Context,
k keeper.Keeper,
msg *types.MsgPostPrice) (*sdk.Result, error) {
_, err := k.GetOracle(ctx, msg.MarketId, msg.From.AccAddress())
if err != nil {
return nil, err
}
_, err = k.SetPrice(ctx, msg.From.AccAddress(), msg.MarketId, msg.Price, msg.Expiry)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.AccAddress().String()),
),
)
return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil
}