@@ -2,104 +2,20 @@ package appmodule
22
33import (
44 "context"
5- "fmt"
5+
6+ transaction "cosmossdk.io/core/transaction"
67)
78
89type (
910 // PreMsgHandler is a handler that is executed before Handler. If it errors the execution reverts.
10- PreMsgHandler = func (ctx context.Context , msg Message ) error
11+ PreMsgHandler = func (ctx context.Context , msg transaction. Msg ) error
1112 // Handler handles the state transition of the provided message.
12- Handler = func (ctx context.Context , msg Message ) (msgResp Message , err error )
13+ Handler = func (ctx context.Context , msg transaction. Msg ) (msgResp transaction. Msg , err error )
1314 // PostMsgHandler runs after Handler, only if Handler does not error. If PostMsgHandler errors
1415 // then the execution is reverted.
15- PostMsgHandler = func (ctx context.Context , msg , msgResp Message ) error
16+ PostMsgHandler = func (ctx context.Context , msg , msgResp transaction. Msg ) error
1617)
1718
18- // RegisterHandler is a helper function that modules can use to not lose type safety when registering handlers to the
19- // QueryRouter or MsgRouter. Example usage:
20- // ```go
21- //
22- // func (k Keeper) QueryBalance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
23- // ... query logic ...
24- // }
25- //
26- // func (m Module) RegisterQueryHandlers(router appmodule.QueryRouter) {
27- // appmodule.RegisterHandler(router, keeper.QueryBalance)
28- // }
29- //
30- // ```
31- func RegisterHandler [R interface { Register (string , Handler ) }, Req , Resp Message ](
32- router R ,
33- handler func (ctx context.Context , msg Req ) (msgResp Resp , err error ),
34- ) {
35- untypedHandler := func (ctx context.Context , m Message ) (Message , error ) {
36- typed , ok := m .(Req )
37- if ! ok {
38- return nil , fmt .Errorf ("unexpected type %T, wanted: %T" , m , * new (Req ))
39- }
40- return handler (ctx , typed )
41- }
42- router .Register (messageName [Req ](), untypedHandler )
43- }
44-
45- // RegisterPreHandler is a helper function that modules can use to not lose type safety when registering PreMsgHandler to the
46- // PreMsgRouter. Example usage:
47- // ```go
48- //
49- // func (k Keeper) BeforeSend(ctx context.Context, req *types.MsgSend) (*types.QueryBalanceResponse, error) {
50- // ... before send logic ...
51- // }
52- //
53- // func (m Module) RegisterPreMsgHandlers(router appmodule.PreMsgRouter) {
54- // appmodule.RegisterPreHandler(router, keeper.BeforeSend)
55- // }
56- //
57- // ```
58- func RegisterPreHandler [Req Message ](
59- router PreMsgRouter ,
60- handler func (ctx context.Context , msg Req ) error ,
61- ) {
62- untypedHandler := func (ctx context.Context , m Message ) error {
63- typed , ok := m .(Req )
64- if ! ok {
65- return fmt .Errorf ("unexpected type %T, wanted: %T" , m , * new (Req ))
66- }
67- return handler (ctx , typed )
68- }
69- router .Register (messageName [Req ](), untypedHandler )
70- }
71-
72- // RegisterPostHandler is a helper function that modules can use to not lose type safety when registering handlers to the
73- // PostMsgRouter. Example usage:
74- // ```go
75- //
76- // func (k Keeper) AfterSend(ctx context.Context, req *types.MsgSend, resp *types.MsgSendResponse) error {
77- // ... query logic ...
78- // }
79- //
80- // func (m Module) RegisterPostMsgHandlers(router appmodule.PostMsgRouter) {
81- // appmodule.RegisterPostHandler(router, keeper.AfterSend)
82- // }
83- //
84- // ```
85- func RegisterPostHandler [Req , Resp Message ](
86- router PostMsgRouter ,
87- handler func (ctx context.Context , msg Req , msgResp Resp ) error ,
88- ) {
89- untypedHandler := func (ctx context.Context , m , mResp Message ) error {
90- typed , ok := m .(Req )
91- if ! ok {
92- return fmt .Errorf ("unexpected type %T, wanted: %T" , m , * new (Req ))
93- }
94- typedResp , ok := mResp .(Resp )
95- if ! ok {
96- return fmt .Errorf ("unexpected type %T, wanted: %T" , m , * new (Resp ))
97- }
98- return handler (ctx , typed , typedResp )
99- }
100- router .Register (messageName [Req ](), untypedHandler )
101- }
102-
10319// msg handler
10420
10521type PreMsgRouter interface {
0 commit comments