-
Notifications
You must be signed in to change notification settings - Fork 39
/
exec_msgs.go
143 lines (115 loc) · 3.59 KB
/
exec_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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package types
import (
"encoding/json"
"fmt"
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func NewRegistryRegisterMsg(req *ContractRegistrationRequest) RegistryRegisterMsg {
return RegistryRegisterMsg{
Register: &RegisterMsg{
GasLimit: req.GasLimit,
ContractAddress: req.ContractAddress,
GasPrice: req.GasPrice.String(),
IsExecutable: true,
},
}
}
type RegistryRegisterMsg struct {
Register *RegisterMsg `json:"register,omitempty"`
}
type RegisterMsg struct {
GasLimit uint64 `json:"gas_limit"`
ContractAddress string `json:"contract_address"`
GasPrice string `json:"gas_price"`
IsExecutable bool `json:"is_executable"`
}
type CWBeginBlockerExecMsg struct {
BeginBlockerMsg *BeginBlockerMsg `json:"begin_blocker,omitempty"`
}
type BeginBlockerMsg struct {
}
func BeginBlockerExecMsg() ([]byte, error) {
// Construct Exec message
beginBlocker := CWBeginBlockerExecMsg{BeginBlockerMsg: &BeginBlockerMsg{}}
//execMsg := []byte(`{"begin_blocker":{}}`)
execMsg, err := json.Marshal(beginBlocker)
if err != nil {
fmt.Println("Register marshal failed")
return nil, err
}
return execMsg, nil
}
type RegistryContractQueryMsg struct {
QueryContractsMsg *QueryContractsMsg `json:"get_contracts,omitempty"`
}
type QueryContractsMsg struct {
}
// NewRegistryContractQuery constructs the registyr Exec message
func NewRegistryContractQuery() ([]byte, error) {
contractQuery := RegistryContractQueryMsg{QueryContractsMsg: &QueryContractsMsg{}}
queryMsg, err := json.Marshal(contractQuery)
if err != nil {
fmt.Println("Register marshal failed")
return nil, err
}
return queryMsg, nil
}
type RegistryActiveContractQueryMsg struct {
QueryActiveContractsMsg *QueryActiveContractsMsg `json:"get_active_contracts,omitempty"`
}
type QueryActiveContractsMsg struct {
}
// NewRegistryActiveContractQuery constructs the registry active contracts query message
func NewRegistryActiveContractQuery() ([]byte, error) {
contractQuery := RegistryActiveContractQueryMsg{QueryActiveContractsMsg: &QueryActiveContractsMsg{}}
// queryData := []byte("{\"get_active_contracts\": {}}")
queryMsg, err := json.Marshal(contractQuery)
if err != nil {
fmt.Println("Register marshal failed")
return nil, err
}
return queryMsg, nil
}
type RawContractExecutionParams struct {
Address string `json:"address"`
GasLimit uint64 `json:"gas_limit"`
GasPrice string `json:"gas_price"`
IsExecutable bool `json:"is_executable"`
}
func (r *RawContractExecutionParams) ToContractExecutionParams() (p *ContractExecutionParams, err error) {
addr, err := sdk.AccAddressFromBech32(r.Address)
if err != nil {
return nil, err
}
gasPrice, ok := sdk.NewIntFromString(r.GasPrice)
if !ok {
return nil, ErrInvalidGasPrice
}
return &ContractExecutionParams{
Address: addr,
GasLimit: r.GasLimit,
GasPrice: gasPrice,
}, nil
}
type ContractExecutionParams struct {
Address sdk.AccAddress
GasLimit uint64
GasPrice sdk.Int
IsExecutable bool
}
// GetSortedContractExecutionParams returns the ContractExecutionParams sorted by descending order of gas price
func GetSortedContractExecutionParams(contractExecutionList []RawContractExecutionParams) ([]*ContractExecutionParams, error) {
paramList := make([]*ContractExecutionParams, len(contractExecutionList))
for idx, elem := range contractExecutionList {
if v, err := elem.ToContractExecutionParams(); err != nil {
return nil, err
} else {
paramList[idx] = v
}
}
sort.SliceStable(paramList, func(i, j int) bool {
return paramList[i].GasPrice.GT(paramList[j].GasPrice)
})
return paramList, nil
}