forked from InjectiveLabs/sdk-go
/
common_utils.go
348 lines (281 loc) · 8.79 KB
/
common_utils.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package types
import (
"bytes"
"math/big"
"reflect"
"regexp"
"strconv"
"strings"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)
func IsEqualDenoms(denoms1, denoms2 []string) bool {
denom1Map := make(map[string]struct{})
denom2Map := make(map[string]struct{})
for _, denom := range denoms1 {
denom1Map[denom] = struct{}{}
}
for _, denom := range denoms2 {
denom2Map[denom] = struct{}{}
}
return reflect.DeepEqual(denom1Map, denom2Map)
}
func IsPeggyToken(denom string) bool {
return strings.HasPrefix(denom, "peggy0x")
}
func IsIBCDenom(denom string) bool {
return strings.HasPrefix(denom, "ibc/")
}
type SpotLimitOrderDelta struct {
Order *SpotLimitOrder
FillQuantity sdk.Dec
}
type DerivativeLimitOrderDelta struct {
Order *DerivativeLimitOrder
FillQuantity sdk.Dec
CancelQuantity sdk.Dec
}
type DerivativeMarketOrderDelta struct {
Order *DerivativeMarketOrder
FillQuantity sdk.Dec
}
func (d *DerivativeMarketOrderDelta) UnfilledQuantity() sdk.Dec {
return d.Order.OrderInfo.Quantity.Sub(d.FillQuantity)
}
func (d *DerivativeLimitOrderDelta) IsBuy() bool {
return d.Order.IsBuy()
}
func (d *DerivativeLimitOrderDelta) SubaccountID() common.Hash {
return d.Order.SubaccountID()
}
func (d *DerivativeLimitOrderDelta) Price() sdk.Dec {
return d.Order.Price()
}
func (d *DerivativeLimitOrderDelta) FillableQuantity() sdk.Dec {
return d.Order.Fillable.Sub(d.CancelQuantity)
}
func (d *DerivativeLimitOrderDelta) OrderHash() common.Hash {
return d.Order.Hash()
}
var AuctionSubaccountID = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
var ZeroSubaccountID = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
// inj1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqe2hm49
var TempRewardsSenderAddress = sdk.AccAddress(common.HexToAddress(ZeroSubaccountID.Hex()).Bytes())
// inj1qqq3zyg3zyg3zyg3zyg3zyg3zyg3zyg3c9gg96
var AuctionFeesAddress = sdk.AccAddress(common.HexToAddress(AuctionSubaccountID.Hex()).Bytes())
func StringInSlice(a string, list *[]string) bool {
for _, b := range *list {
if b == a {
return true
}
}
return false
}
func IsDefaultSubaccountID(subaccountID common.Hash) bool {
// empty 12 bytes
emptyBytes := make([]byte, common.HashLength-common.AddressLength)
return bytes.Equal(subaccountID[common.AddressLength:], emptyBytes)
}
func IsNonceDerivedSubaccountID(subaccountID string) bool {
return len(subaccountID) <= MaxSubaccountNonceLength
}
// CheckValidSubaccountIDOrNonce checks that either 1) the subaccountId is well-formatted subaccount nonce or
// 2) the normal subaccountId is a valid subaccount ID and the sender is the owner of the subaccount
func CheckValidSubaccountIDOrNonce(sender sdk.AccAddress, subaccountId string) error {
if IsNonceDerivedSubaccountID(subaccountId) {
if _, err := GetNonceDerivedSubaccountID(sender, subaccountId); err != nil {
return errors.Wrap(ErrBadSubaccountID, subaccountId)
}
return nil
}
subaccountAddress, ok := IsValidSubaccountID(subaccountId)
if !ok {
return errors.Wrap(ErrBadSubaccountID, subaccountId)
}
if !bytes.Equal(subaccountAddress.Bytes(), sender.Bytes()) {
return errors.Wrap(ErrBadSubaccountID, subaccountId)
}
return nil
}
func IsValidSubaccountID(subaccountID string) (*common.Address, bool) {
if !IsHexHash(subaccountID) {
return nil, false
}
subaccountIdBytes := common.FromHex(subaccountID)
addressBytes := subaccountIdBytes[:common.AddressLength]
address := common.BytesToAddress(addressBytes)
return &address, true
}
func IsValidOrderHash(orderHash string) bool {
return IsHexHash(orderHash)
}
// IsHexHash verifies whether a string can represent a valid hex-encoded hash or not.
func IsHexHash(s string) bool {
if !isHexString(s) {
return false
}
if strings.HasPrefix(s, "0x") {
return len(s) == 2*common.HashLength+2
}
return len(s) == 2*common.HashLength
}
func isHexString(str string) bool {
isMatched, _ := regexp.MatchString("^(0x)?[0-9a-fA-F]+$", str)
return isMatched
}
func BreachesMinimumTickSize(value, minTickSize sdk.Dec) bool {
// obviously breached if the value less than the minTickSize
if value.LT(minTickSize) {
return true
}
// prevent mod by 0
if minTickSize.IsZero() {
return true
}
// is breaching when value % minTickSize != 0
residue := new(big.Int).Mod(value.BigInt(), minTickSize.BigInt())
return !bytes.Equal(residue.Bytes(), big.NewInt(0).Bytes())
}
func (s *Subaccount) GetSubaccountID() (*common.Hash, error) {
trader, err := sdk.AccAddressFromBech32(s.Trader)
if err != nil {
return nil, err
}
return SdkAddressWithNonceToSubaccountID(trader, s.SubaccountNonce)
}
type Account [20]byte
func SdkAccAddressToAccount(account sdk.AccAddress) Account {
var accAddr Account
copy(accAddr[:], account.Bytes())
return accAddr
}
func SubaccountIDToAccount(subaccountID common.Hash) Account {
var accAddr Account
copy(accAddr[:], subaccountID.Bytes()[:20])
return accAddr
}
func GetSubaccountNonce(subaccountIdStr string) (uint32, error) {
// maximum of 10^3 = 1000 numeric subaccounts allowed
isValidLength := len(subaccountIdStr) <= MaxSubaccountNonceLength
if !isValidLength {
return 0, ErrBadSubaccountNonce
}
if subaccountIdStr == "" || subaccountIdStr == "0" {
return 0, nil
}
num, err := strconv.Atoi(subaccountIdStr)
if err != nil {
return 0, err
}
if num < 0 || num > 999 {
return 0, ErrBadSubaccountNonce
}
return uint32(num), nil
}
func MustGetSubaccountIDOrDeriveFromNonce(sender sdk.AccAddress, subaccountId string) common.Hash {
subaccountID, err := GetSubaccountIDOrDeriveFromNonce(sender, subaccountId)
if err != nil {
panic(err)
}
return subaccountID
}
func GetNonceDerivedSubaccountID(sender sdk.AccAddress, subaccountId string) (common.Hash, error) {
nonce, err := GetSubaccountNonce(subaccountId)
if err != nil {
return common.Hash{}, err
}
subaccountID, err := SdkAddressWithNonceToSubaccountID(sender, nonce)
if err != nil {
return common.Hash{}, err
}
return *subaccountID, nil
}
func GetSubaccountIDOrDeriveFromNonce(sender sdk.AccAddress, subaccountId string) (common.Hash, error) {
if IsNonceDerivedSubaccountID(subaccountId) {
return GetNonceDerivedSubaccountID(sender, subaccountId)
}
if !IsHexHash(subaccountId) {
return common.Hash{}, errors.Wrap(ErrBadSubaccountID, subaccountId)
}
return common.HexToHash(subaccountId), nil
}
func SdkAddressWithNonceToSubaccountID(addr sdk.AccAddress, nonce uint32) (*common.Hash, error) {
if len(addr.Bytes()) > common.AddressLength {
return &AuctionSubaccountID, ErrBadSubaccountID
}
subaccountID := common.BytesToHash(append(addr.Bytes(), common.LeftPadBytes(big.NewInt(int64(nonce)).Bytes(), 12)...))
return &subaccountID, nil
}
func MustSdkAddressWithNonceToSubaccountID(addr sdk.AccAddress, nonce uint32) common.Hash {
if len(addr.Bytes()) > common.AddressLength {
panic(ErrBadSubaccountID)
}
subaccountID := common.BytesToHash(append(addr.Bytes(), common.LeftPadBytes(big.NewInt(int64(nonce)).Bytes(), 12)...))
return subaccountID
}
func SdkAddressToSubaccountID(addr sdk.AccAddress) common.Hash {
return common.BytesToHash(common.RightPadBytes(addr.Bytes(), 32))
}
func SdkAddressToEthAddress(addr sdk.AccAddress) common.Address {
return common.BytesToAddress(addr.Bytes())
}
func SubaccountIDToSdkAddress(subaccountID common.Hash) sdk.AccAddress {
return sdk.AccAddress(subaccountID[:common.AddressLength])
}
func SubaccountIDToEthAddress(subaccountID common.Hash) common.Address {
return common.BytesToAddress(subaccountID[:common.AddressLength])
}
func EthAddressToSubaccountID(addr common.Address) common.Hash {
return common.BytesToHash(common.RightPadBytes(addr.Bytes(), 32))
}
func DecToDecBytes(dec sdk.Dec) []byte {
return dec.BigInt().Bytes()
}
func DecBytesToDec(bz []byte) sdk.Dec {
dec := sdk.NewDecFromBigIntWithPrec(new(big.Int).SetBytes(bz), sdk.Precision)
if dec.IsNil() {
return sdk.ZeroDec()
}
return dec
}
func HasDuplicates(slice []string) bool {
seen := make(map[string]struct{})
for _, item := range slice {
if _, ok := seen[item]; ok {
return true
}
seen[item] = struct{}{}
}
return false
}
func HasDuplicatesHexHash(slice []string) bool {
seen := make(map[common.Hash]struct{})
for _, item := range slice {
if _, ok := seen[common.HexToHash(item)]; ok {
return true
}
seen[common.HexToHash(item)] = struct{}{}
}
return false
}
func HasDuplicatesCoin(slice []sdk.Coin) bool {
seen := make(map[string]struct{})
for _, item := range slice {
if _, ok := seen[item.Denom]; ok {
return true
}
seen[item.Denom] = struct{}{}
}
return false
}
func HasDuplicatesOrder(slice []*OrderData) bool {
seen := make(map[string]struct{})
for _, item := range slice {
if _, ok := seen[item.OrderHash]; ok {
return true
}
seen[item.OrderHash] = struct{}{}
}
return false
}