forked from InjectiveLabs/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authz_derivative.go
219 lines (202 loc) · 7.87 KB
/
authz_derivative.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
)
var (
_ authz.Authorization = &CreateDerivativeLimitOrderAuthz{}
_ authz.Authorization = &CreateDerivativeMarketOrderAuthz{}
_ authz.Authorization = &BatchCreateDerivativeLimitOrdersAuthz{}
_ authz.Authorization = &CancelDerivativeOrderAuthz{}
_ authz.Authorization = &BatchCancelDerivativeOrdersAuthz{}
)
// CreateDerivativeLimitOrderAuthz impl
func (a CreateDerivativeLimitOrderAuthz) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgCreateDerivativeLimitOrder{})
}
func (a CreateDerivativeLimitOrderAuthz) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
derivativeOrder, ok := msg.(*MsgCreateDerivativeLimitOrder)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
// check authorized subaccount
if derivativeOrder.Order.OrderInfo.SubaccountId != a.SubaccountId {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
}
// check authorized market
if !find(a.MarketIds, derivativeOrder.Order.MarketId) {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
}
func (a CreateDerivativeLimitOrderAuthz) ValidateBasic() error {
if !IsHexHash(a.SubaccountId) {
return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
}
if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
}
marketsSet := reduceToSet(a.MarketIds)
if len(a.MarketIds) != len(marketsSet) {
return sdkerrors.ErrLogic.Wrapf("cannot have duplicate markets")
}
for _, m := range a.MarketIds {
if !IsHexHash(m) {
return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
}
}
return nil
}
// CreateDerivativeMarketOrderAuthz impl
func (a CreateDerivativeMarketOrderAuthz) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgCreateDerivativeMarketOrder{})
}
func (a CreateDerivativeMarketOrderAuthz) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
derivativeOrder, ok := msg.(*MsgCreateDerivativeMarketOrder)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
// check authorized subaccount
if derivativeOrder.Order.OrderInfo.SubaccountId != a.SubaccountId {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
}
// check authorized market
if !find(a.MarketIds, derivativeOrder.Order.MarketId) {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
}
func (a CreateDerivativeMarketOrderAuthz) ValidateBasic() error {
if !IsHexHash(a.SubaccountId) {
return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
}
if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
}
marketsSet := reduceToSet(a.MarketIds)
if len(a.MarketIds) != len(marketsSet) {
return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
}
for _, m := range a.MarketIds {
if !IsHexHash(m) {
return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
}
}
return nil
}
// BatchCreateDerivativeLimitOrdersAuthz impl
func (a BatchCreateDerivativeLimitOrdersAuthz) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgBatchCreateDerivativeLimitOrders{})
}
func (a BatchCreateDerivativeLimitOrdersAuthz) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
derivativeOrders, ok := msg.(*MsgBatchCreateDerivativeLimitOrders)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
for _, o := range derivativeOrders.Orders {
// check authorized subaccount
if o.OrderInfo.SubaccountId != a.SubaccountId {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
}
// check authorized markets
if !find(a.MarketIds, o.MarketId) {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
}
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
}
func (a BatchCreateDerivativeLimitOrdersAuthz) ValidateBasic() error {
if !IsHexHash(a.SubaccountId) {
return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
}
if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
}
marketsSet := reduceToSet(a.MarketIds)
if len(a.MarketIds) != len(marketsSet) {
return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
}
for _, m := range a.MarketIds {
if !IsHexHash(m) {
return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
}
}
return nil
}
// CancelDerivativeOrderAuthz impl
func (a CancelDerivativeOrderAuthz) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgCancelDerivativeOrder{})
}
func (a CancelDerivativeOrderAuthz) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
orderToCancel, ok := msg.(*MsgCancelDerivativeOrder)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
// check authorized subaccount
if orderToCancel.SubaccountId != a.SubaccountId {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
}
// check authorized market
if !find(a.MarketIds, orderToCancel.MarketId) {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
}
func (a CancelDerivativeOrderAuthz) ValidateBasic() error {
if !IsHexHash(a.SubaccountId) {
return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
}
if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
}
marketsSet := reduceToSet(a.MarketIds)
if len(a.MarketIds) != len(marketsSet) {
return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
}
for _, m := range a.MarketIds {
if !IsHexHash(m) {
return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
}
}
return nil
}
// BatchCancelDerivativeOrdersAuthz impl
func (a BatchCancelDerivativeOrdersAuthz) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgBatchCancelDerivativeOrders{})
}
func (a BatchCancelDerivativeOrdersAuthz) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
ordersToCancel, ok := msg.(*MsgBatchCancelDerivativeOrders)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
for _, o := range ordersToCancel.Data {
// check authorized subaccount
if o.SubaccountId != a.SubaccountId {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
}
// check authorized markets
if !find(a.MarketIds, o.MarketId) {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
}
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
}
func (a BatchCancelDerivativeOrdersAuthz) ValidateBasic() error {
if !IsHexHash(a.SubaccountId) {
return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
}
if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
}
marketsSet := reduceToSet(a.MarketIds)
if len(a.MarketIds) != len(marketsSet) {
return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
}
for _, m := range a.MarketIds {
if !IsHexHash(m) {
return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
}
}
return nil
}