forked from cosmos/relayer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg.go
78 lines (67 loc) · 1.95 KB
/
msg.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cosmos
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
chantypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
"github.com/defund-labs/relayer/v3/relayer/provider"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap/zapcore"
)
var _ provider.RelayerMessage = &CosmosMessage{}
type CosmosMessage struct {
Msg sdk.Msg
}
func NewCosmosMessage(msg sdk.Msg) provider.RelayerMessage {
return CosmosMessage{
Msg: msg,
}
}
func CosmosMsg(rm provider.RelayerMessage) sdk.Msg {
if val, ok := rm.(CosmosMessage); !ok {
fmt.Printf("got data of type %T but wanted provider.CosmosMessage \n", val)
return nil
} else {
return val.Msg
}
}
// typedCosmosMsg does not accept nil. IBC Message must be of the requested type.
func typedCosmosMsg[T *chantypes.MsgRecvPacket | *chantypes.MsgAcknowledgement](msg provider.RelayerMessage) T {
if msg == nil {
panic("msg is nil")
}
cosmosMsg := CosmosMsg(msg)
if cosmosMsg == nil {
panic("cosmosMsg is nil")
}
return cosmosMsg.(T)
}
func CosmosMsgs(rm ...provider.RelayerMessage) []sdk.Msg {
sdkMsgs := make([]sdk.Msg, 0)
for _, rMsg := range rm {
if val, ok := rMsg.(CosmosMessage); !ok {
fmt.Printf("got data of type %T but wanted provider.CosmosMessage \n", val)
return nil
} else {
sdkMsgs = append(sdkMsgs, val.Msg)
}
}
return sdkMsgs
}
func (cm CosmosMessage) Type() string {
return sdk.MsgTypeURL(cm.Msg)
}
func (cm CosmosMessage) MsgBytes() ([]byte, error) {
return proto.Marshal(cm.Msg)
}
// MarshalLogObject is used to encode cm to a zap logger with the zap.Object field type.
func (cm CosmosMessage) MarshalLogObject(enc zapcore.ObjectEncoder) error {
// Using plain json.Marshal or calling cm.Msg.String() both fail miserably here.
// There is probably a better way to encode the message than this.
j, err := codec.NewLegacyAmino().MarshalJSON(cm.Msg)
if err != nil {
return err
}
enc.AddByteString("msg_json", j)
return nil
}