-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.go
378 lines (324 loc) · 9.37 KB
/
common.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package interswap
import (
"errors"
"fmt"
"math"
"math/big"
"os"
"strconv"
"strings"
"github.com/go-resty/resty/v2"
"github.com/incognitochain/go-incognito-sdk-v2/incclient"
"github.com/incognitochain/incognito-web-based-backend/common"
beCommon "github.com/incognitochain/incognito-web-based-backend/common"
"github.com/mileusna/useragent"
)
var BEEndpoint = os.Getenv("BE_ENDPOINT")
var incClient *incclient.IncClient
func InitIncClient(network string, config common.Config) error {
var err error
switch network {
case "mainnet":
incClient, err = incclient.NewIncClient(config.FullnodeURL, incclient.MainNetETHHost, 2, network)
default:
incClient, err = incclient.NewIncClient(config.FullnodeURL, "", 2, network)
}
if err != nil {
return err
}
return nil
}
var restyClient = resty.New()
var SupportedMidTokens = []string{}
func InitSupportedMidTokens(network string) error {
switch network {
case "mainnet":
SupportedMidTokens = []string{
"076a4423fa20922526bd50b0d7b0dc1c593ce16e15ba141ede5fb5a28aa3f229", // USDT
// "3ee31eba6376fc16cadb52c8765f20b6ebff92c0b1c5ab5fc78c8c25703bb19e", // ETH
}
case "testnet":
SupportedMidTokens = []string{
"b35756452dc1fa1260513fa121c20c2b516a8645f8d496fa4235274dac0b1b52", // LINK (unified)
// "3a526c0fa9abfc3e3e37becc52c5c10abbb7897b0534ad17018e766fc6133590", // USDT
// "b366fa400c36e6bbcf24ac3e99c90406ddc64346ab0b7ba21e159b83d938812d", // ETH
}
default:
return errors.New("Invalid supported network")
}
return nil
}
func IsMidTokens(tokenID string) bool {
for _, item := range SupportedMidTokens {
if item == tokenID {
return true
}
}
return false
}
func IsExist(strArr []string, str string) bool {
for _, item := range strArr {
if item == str {
return true
}
}
return false
}
func strToFloat64(str string) (float64, error) {
if str == "" {
return 0, nil
}
return strconv.ParseFloat(str, 64)
}
func float64ToStr(f float64) string {
return fmt.Sprintf("%.9f", f)
}
func addStrs(str1, str2 string) (string, error) {
f1, err := strToFloat64(str1)
if err != nil {
return "", err
}
f2, err := strToFloat64(str2)
if err != nil {
return "", err
}
return float64ToStr(f1 + f2), nil
}
func subStrs(str1, str2 string) (string, error) {
f1, err := strToFloat64(str1)
if err != nil {
return "", err
}
f2, err := strToFloat64(str2)
if err != nil {
return "", err
}
if f1 < f2 {
return "", fmt.Errorf("%v is less than %v", str1, str2)
}
return float64ToStr(f1 - f2), nil
}
func divStrs(str1, str2 string) (string, error) {
f1, err := strToFloat64(str1)
if err != nil {
return "", err
}
f2, err := strToFloat64(str2)
if err != nil {
return "", err
}
return float64ToStr(f1 / f2), nil
}
func convertAmountDec(amt uint64, fromToken, toToken string, config common.Config) (uint64, error) {
tokenInfos, err := getTokensInfo([]string{fromToken, toToken}, config)
if err != nil {
return 0, err
}
fromTokenInfo := common.TokenInfo{}
toTokenInfo := common.TokenInfo{}
for _, info := range tokenInfos {
if info.TokenID == fromToken {
fromTokenInfo = info
} else if info.TokenID == toToken {
toTokenInfo = info
}
}
if fromTokenInfo.TokenID == "" || toTokenInfo.TokenID == "" {
return 0, errors.New("Can not get token info")
}
tmp := new(big.Int).Mul(new(big.Int).SetUint64(amt), new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(toTokenInfo.PDecimals)), nil))
tmp = new(big.Int).Div(tmp, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(fromTokenInfo.PDecimals)), nil))
return tmp.Uint64(), nil
}
func convertAmountUint64(amt uint64, fromToken, toToken string, config common.Config) (uint64, error) {
tokenInfos, err := getTokensInfo([]string{fromToken, toToken}, config)
if err != nil {
return 0, err
}
fromTokenInfo := common.TokenInfo{}
toTokenInfo := common.TokenInfo{}
for _, info := range tokenInfos {
if info.TokenID == fromToken {
fromTokenInfo = info
} else if info.TokenID == toToken {
toTokenInfo = info
}
}
if fromTokenInfo.TokenID == "" || toTokenInfo.TokenID == "" {
return 0, errors.New("Can not get token info")
}
amtInUSD := float64(amt) * fromTokenInfo.ExternalPriceUSD
amtTo := amtInUSD / toTokenInfo.ExternalPriceUSD
return uint64(amtTo), nil
}
func convertAmountFromToTokenInfo(amt uint64, fromTokenInfo, toTokenInfo beCommon.TokenInfo) (uint64, error) {
// tokenInfos, err := getTokensInfo([]string{fromToken, toToken}, config)
// if err != nil {
// return 0, err
// }
// fromTokenInfo := common.TokenInfo{}
// toTokenInfo := common.TokenInfo{}
// for _, info := range tokenInfos {
// if info.TokenID == fromToken {
// fromTokenInfo = info
// } else if info.TokenID == toToken {
// toTokenInfo = info
// }
// }
if fromTokenInfo.TokenID == "" || toTokenInfo.TokenID == "" {
return 0, errors.New("Invalid token info")
}
amtFloat64, err := ConvertAmountToWithoutDec(amt, uint64(fromTokenInfo.PDecimals))
if err != nil {
return 0, err
}
amtInUSD := amtFloat64 * fromTokenInfo.ExternalPriceUSD
amtToFloat64 := amtInUSD / toTokenInfo.ExternalPriceUSD
return convertToDecAmtWithTokenInfo(amtToFloat64, toTokenInfo)
}
func convertToWithoutDecStr(amt uint64, tokenID string, config common.Config) (string, error) {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return "", nil
}
tmp := float64(amt) / float64(math.Pow(10, float64(tokenInfo.PDecimals)))
return float64ToStr(tmp), nil
}
func convertToWithoutDecStrWithTokenInfo(amt uint64, tokenInfo *beCommon.TokenInfo) (string, error) {
tmp := float64(amt) / float64(math.Pow(10, float64(tokenInfo.PDecimals)))
return float64ToStr(tmp), nil
}
func convertToDecAmtStr(amt string, tokenID string, config common.Config) (string, error) {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return "", nil
}
amtTmp, err := strToFloat64(amt)
if err != nil {
return "", nil
}
tmp := uint64(float64(amtTmp) * float64(math.Pow(10, float64(tokenInfo.PDecimals))))
return fmt.Sprint(tmp), nil
}
func convertToDecAmtUint64(amt string, tokenID string, config common.Config) (uint64, error) {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return 0, nil
}
amtFloat64, err := strToFloat64(amt)
if err != nil {
return 0, nil
}
return convertToDecAmtWithTokenInfo(amtFloat64, *tokenInfo)
}
func convertToDecAmtWithTokenInfo(amt float64, tokenInfo beCommon.TokenInfo) (uint64, error) {
tmp := uint64(amt * float64(math.Pow(10, float64(tokenInfo.PDecimals))))
return tmp, nil
}
func ConvertUint64ToWithoutDecStr(amt uint64, tokenID string, config common.Config) (string, error) {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return "", nil
}
tmp := float64(amt) / float64(math.Pow(10, float64(tokenInfo.PDecimals)))
return float64ToStr(tmp), nil
}
func ConvertUint64ToWithoutDecStr2(amt uint64, pDecimal uint64) (string, error) {
tmp := float64(amt) / float64(math.Pow(10, float64(pDecimal)))
return float64ToStr(tmp), nil
}
func ConvertAmountToWithoutDec(amt uint64, pDecimal uint64) (float64, error) {
tmp := float64(amt) / float64(math.Pow(10, float64(pDecimal)))
return tmp, nil
}
func convertAmtExtDecToAmtPDec(amt *big.Int, tokenID string, config common.Config) (uint64, error) {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return 0, nil
}
tmp := new(big.Int).Mul(amt, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenInfo.PDecimals)), nil))
tmp = new(big.Int).Div(tmp, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenInfo.Decimals)), nil))
return tmp.Uint64(), nil
}
func convertAmtByDec(amt *big.Int, fromDec, toDec int) uint64 {
tmp := new(big.Int).Mul(amt, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(toDec)), nil))
tmp = new(big.Int).Div(tmp, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(fromDec)), nil))
return tmp.Uint64()
}
func IsUniqueSlices(s []string) bool {
m := map[string]bool{}
for _, i := range s {
if m[i] {
return false
}
m[i] = true
}
return true
}
func Has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
// Remove0xPrefix removes 0x prefix (if there) from string
func Remove0xPrefix(str string) string {
if Has0xPrefix(str) {
return str[2:]
}
return str
}
func ParseUserAgent(userAgent string) string {
ua := useragent.Parse(userAgent)
uaStr := ""
if ua.IsAndroid() {
uaStr = "mobile-android"
}
if ua.IsIOS() {
uaStr = "mobile-ios"
}
if ua.IsEdge() {
uaStr = "web-edge"
}
if ua.IsFirefox() {
uaStr = "web-firefox"
}
if ua.IsChrome() {
uaStr = "web-chrome"
}
if ua.IsOpera() {
uaStr = "web-opera"
}
if uaStr == "" {
if ua.Mobile || ua.Tablet {
uaStr = "mobile"
}
if ua.Bot {
uaStr = "bot"
}
if ua.Desktop {
uaStr = "web"
}
}
if uaStr == "" {
if strings.Contains(userAgent, "okhttp") {
uaStr = "mobile-android-ish"
}
if strings.Contains(userAgent, "CFNetwork") {
uaStr = "mobile-ios-ish"
}
}
if uaStr == "" {
uaStr = userAgent
}
return uaStr
}
func getTokenInfoWithCache(tokenID string, tokenInfoCaches map[string]*beCommon.TokenInfo, config beCommon.Config) (*beCommon.TokenInfo, map[string]*beCommon.TokenInfo, error) {
tmp := tokenInfoCaches[tokenID]
if tmp.TokenID == "" || tmp.TokenID != tokenID {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return nil, tokenInfoCaches, err
}
tokenInfoCaches[tokenID] = tokenInfo
return tokenInfo, tokenInfoCaches, nil
}
return tmp, tokenInfoCaches, nil
}