forked from CosmWasm/wasmvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcall.go
109 lines (92 loc) · 2.26 KB
/
subcall.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package types
import (
"encoding/json"
"fmt"
)
type replyOn int
const (
ReplyAlways replyOn = iota
ReplySuccess
ReplyError
ReplyNever
)
var fromReplyOn = map[replyOn]string{
ReplyAlways: "always",
ReplySuccess: "success",
ReplyError: "error",
ReplyNever: "never",
}
var toReplyOn = map[string]replyOn{
"always": ReplyAlways,
"success": ReplySuccess,
"error": ReplyError,
"never": ReplyNever,
}
func (r replyOn) String() string {
return fromReplyOn[r]
}
func (s replyOn) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *replyOn) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
voteOption, ok := toReplyOn[j]
if !ok {
return fmt.Errorf("invalid reply_on value '%v'", j)
}
*s = voteOption
return nil
}
// SubMsg wraps a CosmosMsg with some metadata for handling replies (ID) and optionally
// limiting the gas usage (GasLimit)
type SubMsg struct {
ID uint64 `json:"id"`
Msg CosmosMsg `json:"msg"`
GasLimit *uint64 `json:"gas_limit,omitempty"`
ReplyOn replyOn `json:"reply_on"`
}
type Reply struct {
ID uint64 `json:"id"`
Result SubcallResult `json:"result"`
}
// SubcallResult is the raw response we return from the sdk -> reply after executing a SubMsg.
// This is mirrors Rust's ContractResult<SubcallResponse>.
type SubcallResult struct {
Ok *SubcallResponse `json:"ok,omitempty"`
Err string `json:"error,omitempty"`
}
type SubcallResponse struct {
Events Events `json:"events"`
Data []byte `json:"data,omitempty"`
}
// Events must encode empty array as []
type Events []Event
// MarshalJSON ensures that we get [] for empty arrays
func (e Events) MarshalJSON() ([]byte, error) {
if len(e) == 0 {
return []byte("[]"), nil
}
var raw []Event = e
return json.Marshal(raw)
}
// UnmarshalJSON ensures that we get [] for empty arrays
func (e *Events) UnmarshalJSON(data []byte) error {
// make sure we deserialize [] back to null
if string(data) == "[]" || string(data) == "null" {
return nil
}
var raw []Event
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*e = raw
return nil
}
type Event struct {
Type string `json:"type"`
Attributes EventAttributes `json:"attributes"`
}