/
msgs.go
56 lines (48 loc) · 1.35 KB
/
msgs.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
package tx
import (
fmt "fmt"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
msgResponseInterfaceProtoName = "cosmos.tx.v1beta1.MsgResponse"
)
// MsgResponse is the interface all Msg server handlers' response types need to
// implement. It's the interface that's representing all Msg responses packed
// in Anys.
type MsgResponse interface{}
// SetMsgs takes a slice of sdk.Msg's and turn them into Any's.
func SetMsgs(msgs []sdk.Msg) ([]*types.Any, error) {
anys := make([]*types.Any, len(msgs))
for i, msg := range msgs {
var err error
anys[i], err = types.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
}
return anys, nil
}
// GetMsgs takes a slice of Any's and turn them into sdk.Msg's.
func GetMsgs(anys []*types.Any, name string) ([]sdk.Msg, error) {
msgs := make([]sdk.Msg, len(anys))
for i, any := range anys {
cached := any.GetCachedValue()
if cached == nil {
return nil, fmt.Errorf("any cached value is nil, %s messages must be correctly packed any values", name)
}
msgs[i] = cached.(sdk.Msg)
}
return msgs, nil
}
// UnpackInterfaces unpacks Any's to sdk.Msg's.
func UnpackInterfaces(unpacker types.AnyUnpacker, anys []*types.Any) error {
for _, any := range anys {
var msg sdk.Msg
err := unpacker.UnpackAny(any, &msg)
if err != nil {
return err
}
}
return nil
}