-
Notifications
You must be signed in to change notification settings - Fork 4
/
requests.go
703 lines (599 loc) · 20.1 KB
/
requests.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package interswap
import (
"errors"
"fmt"
"log"
"strconv"
"github.com/incognitochain/go-incognito-sdk-v2/incclient"
"github.com/incognitochain/go-incognito-sdk-v2/metadata"
"github.com/incognitochain/go-incognito-sdk-v2/privacy"
beCommon "github.com/incognitochain/incognito-web-based-backend/common"
)
type EstimateSwapParam struct {
// FeeAddress string
// MultiTrades bool
// MinSplit int
Network string
Amount string
Slippage string
FromToken string // IncTokenID
ToToken string // IncTokenID
ShardID string
}
type PappNetworkFee struct {
TokenID string `json:"tokenid"`
Amount uint64 `json:"amount"`
AmountInBuyToken string `json:"amountInBuyToken"`
PrivacyFee uint64 `json:"privacyFee"`
FeeInUSD float64 `json:"feeInUSD"`
}
type QuoteData struct {
AppName string
PAppNetwork string
CallContract string
AmountIn string
AmountInRaw string
AmountOut string
AmountOutRaw string
AmountOutPreSlippage string
RedepositReward string
Rate string
Fee []PappNetworkFee
FeeAddress string
FeeAddressShardID int
Paths interface{}
PathsContract interface{}
PoolPairs []string
Calldata string
ImpactAmount string
RouteDebug interface{}
}
type EstimateSwapResult struct {
Networks map[string][]QuoteData
NetworksError map[string]interface{}
}
type EstimateSwapResponse struct {
Result EstimateSwapResult
Error interface{}
}
type SubmitpAppSwapTxRequest struct {
TxRaw string
TxHash string
FeeRefundOTA string
// FeeRefundAddress string // NOTE: don't use this field
}
type SubmitpAppSwapTxResponse struct {
Result map[string]interface{}
}
type TxStatusRespond struct {
TxHash string
Status string
Error string
}
type TransactionDetail struct {
BlockHash string `json:"BlockHash"`
BlockHeight uint64 `json:"BlockHeight"`
TxSize uint64 `json:"TxSize"`
Index uint64 `json:"Index"`
ShardID byte `json:"ShardID"`
Hash string `json:"Hash"`
Version int8 `json:"Version"`
Type string `json:"Type"` // Transaction type
LockTime string `json:"LockTime"`
RawLockTime int64 `json:"RawLockTime,omitempty"`
Fee uint64 `json:"Fee"` // Fee applies: always consant
Image string `json:"Image"`
IsPrivacy bool `json:"IsPrivacy"`
// Proof privacy.Proof `json:"Proof"`
// ProofDetail interface{} `json:"ProofDetail"`
InputCoinPubKey string `json:"InputCoinPubKey"`
SigPubKey string `json:"SigPubKey,omitempty"` // 64 bytes
RawSigPubKey []byte `json:"RawSigPubKey,omitempty"`
Sig string `json:"Sig,omitempty"` // 64 bytes
Metadata string `json:"Metadata"`
CustomTokenData string `json:"CustomTokenData"`
PrivacyCustomTokenID string `json:"PrivacyCustomTokenID"`
PrivacyCustomTokenName string `json:"PrivacyCustomTokenName"`
PrivacyCustomTokenSymbol string `json:"PrivacyCustomTokenSymbol"`
PrivacyCustomTokenData string `json:"PrivacyCustomTokenData"`
PrivacyCustomTokenProofDetail interface{} `json:"PrivacyCustomTokenProofDetail"`
PrivacyCustomTokenIsPrivacy bool `json:"PrivacyCustomTokenIsPrivacy"`
PrivacyCustomTokenFee uint64 `json:"PrivacyCustomTokenFee"`
IsInMempool bool `json:"IsInMempool"`
IsInBlock bool `json:"IsInBlock"`
Info string `json:"Info"`
}
// CallEstimateSwap call request to estimate swap
// for both pdex and papp
func CallEstimateSwap(params *EstimateSwapParam, config beCommon.Config, endpoint string) (*EstimateSwapResult, error) {
req := struct {
Network string
Amount string // without decimal
FromToken string // IncTokenID
ToToken string // IncTokenID
Slippage string
IsFromInterswap bool
}{
Network: params.Network,
Amount: params.Amount,
FromToken: params.FromToken,
ToToken: params.ToToken,
Slippage: params.Slippage,
IsFromInterswap: true,
}
if endpoint == "" {
endpoint = "http://localhost:" + strconv.Itoa(config.Port)
}
estSwapResponse := EstimateSwapResponse{}
response, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").SetBody(req).
SetResult(&estSwapResponse).
Post(endpoint + "/papps/estimateswapfee")
if err != nil {
err := fmt.Errorf("[ERR] Call API /papps/estimateswapfee request error: %v", err)
log.Println(err)
return nil, err
}
if response.StatusCode() != 200 {
err := fmt.Errorf("[ERR] Call API /papps/estimateswapfee status code error: %v", response.StatusCode())
log.Println(err)
return nil, err
}
if estSwapResponse.Error != nil {
err := fmt.Errorf("[ERR] Call API /papps/estimateswapfee response error: %v", err)
log.Println(err)
return nil, err
}
return &estSwapResponse.Result, nil
}
// CallSubmitPappSwapTx calls request to submit tx papp
func CallSubmitPappSwapTx(txRaw, txHash, feeRefundOTA string, config beCommon.Config, endpoint string) (map[string]interface{}, error) {
log.Printf("CallSubmitPappSwapTx txHash: %v\n", txHash)
req := SubmitpAppSwapTxRequest{
TxRaw: txRaw,
TxHash: txHash,
FeeRefundOTA: feeRefundOTA,
}
estSwapResponse := SubmitpAppSwapTxResponse{}
if endpoint == "" {
endpoint = "http://localhost:" + strconv.Itoa(config.Port)
}
response, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").SetBody(req).
SetResult(&estSwapResponse).
Post(endpoint + "/papps/submitswaptx")
if err != nil {
err := fmt.Errorf("[ERR] Call API /papps/submitswaptx request error: %v", err)
log.Println(err)
return nil, err
}
if response.StatusCode() != 200 {
err := fmt.Errorf("[ERR] Call API /papps/submitswaptx status code error: %v", response.StatusCode())
log.Println(err)
log.Printf("response: %+v\n", response)
return nil, err
}
return estSwapResponse.Result, nil
}
type PappStatus struct {
BurnStatus string
SwapStatus string
IsRedeposit bool
IsRedeposited bool
BuyAmount string
Reward string
}
// CallSubmitPappSwapTx calls request to submit tx papp
// func CallGetPappSwapTxStatus(txID string, config beCommon.Config) (*PappStatus, error) {
// req := struct {
// TxList []string
// }{
// TxList: []string{txID},
// }
// estSwapResponse := SubmitpAppSwapTxResponse{}
// response, err := restyClient.R().
// EnableTrace().
// SetHeader("Content-Type", "application/json").SetBody(req).
// SetResult(&estSwapResponse).
// Post("http://localhost:" + strconv.Itoa(config.Port) + "/papps/swapstatus")
// if err != nil {
// err := fmt.Errorf("[ERR] Call API /papps/swapstatus request error: %v", err)
// log.Println(err)
// return nil, err
// }
// if response.StatusCode() != 200 {
// err := fmt.Errorf("[ERR] Call API /papps/swapstatus status code error: %v", response.StatusCode())
// log.Println(err)
// return nil, err
// }
// m := estSwapResponse.Result[txID].(map[string]interface{})
// if len(m) == 0 {
// return nil, errors.New("[ERR] Call API /papps/swapstatus status not found")
// }
// if m["error"] != "" {
// return nil, fmt.Errorf("[ERR] Call API /papps/swapstatus status error %v", m["error"])
// }
// if m["swap_err"] != "" {
// return nil, fmt.Errorf("[ERR] Call API /papps/swapstatus swap error %v", m["swap_err"])
// }
// burnStatus := ""
// swapStatus := ""
// isRedeposit := false
// isRedeposited := false
// buyAmount := ""
// reward := ""
// if m["inc_request_tx_status"] != "" {
// burnStatus = m["inc_request_tx_status"].(string)
// }
// if m["swap_outcome"] != "" {
// swapStatus = m["swap_outcome"].(string)
// }
// if m["is_redeposit"] == true {
// isRedeposit = true
// }
// if m["redeposit_status"] == "success" {
// isRedeposited = true
// }
// if tmp, ok := m["swap_detail"].(map[string]interface{}); ok {
// if buyAmtTmp, ok := tmp["out_amount"].(string); ok {
// buyAmount = buyAmtTmp
// }
// if rewardTmp, ok := tmp["reward"].(string); ok {
// reward = rewardTmp
// }
// }
// pappStatus := PappStatus{
// BurnStatus: burnStatus,
// SwapStatus: swapStatus,
// IsRedeposit: isRedeposit,
// IsRedeposited: isRedeposited,
// BuyAmount: buyAmount,
// Reward: reward,
// }
// // "inc_request_tx_status":
// return &pappStatus, nil
// }
func genRPCBody(method string, params []interface{}) interface{} {
type RPC struct {
ID int `json:"id"`
JsonRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
req := RPC{
ID: 1,
JsonRPC: "1.0",
Method: method,
Params: params,
}
return req
}
func CallGetTxDetails(txhash string, config beCommon.Config) (*TransactionDetail, error) {
reqRPC := genRPCBody("gettransactionbyhash", []interface{}{
txhash,
})
type TxDetailRespond struct {
Result TransactionDetail
Error *string
}
var responseBodyData TxDetailRespond
_, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").
SetResult(&responseBodyData).SetBody(reqRPC).
Post(config.FullnodeURL)
if err != nil {
return nil, err
}
if responseBodyData.Error != nil {
return nil, fmt.Errorf("%v", responseBodyData.Error)
}
return &responseBodyData.Result, nil
}
func CallGetPdexSwapTxStatus(txhash string, config beCommon.Config) (bool, *beCommon.TradeDataRespond, error) {
type APIRespond struct {
Result []beCommon.TradeDataRespond
Error *string
}
var responseBodyData APIRespond
_, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").
SetResult(&responseBodyData).
Get(config.CoinserviceURL + "/pdex/v3/tradedetail?txhash=" + txhash)
if err != nil {
log.Println("CallGetPdexSwapTxStatus", err)
return false, nil, err
}
if responseBodyData.Error != nil {
log.Println("CallGetPdexSwapTxStatus", errors.New(*responseBodyData.Error))
return false, nil, errors.New(*responseBodyData.Error)
}
if len(responseBodyData.Result) == 0 {
return false, nil, errors.New("not found")
}
swapResult := responseBodyData.Result[0]
return true, &swapResult, nil
}
type ReceivedTransactionV2 struct {
TxDetail struct {
BlockHash string `json:"BlockHash"`
BlockHeight uint64 `json:"BlockHeight"`
TxSize uint64 `json:"TxSize"`
Index uint64 `json:"Index"`
ShardID byte `json:"ShardID"`
Hash string `json:"Hash"`
Version int8 `json:"Version"`
Type string `json:"Type"` // Transaction type
LockTime string `json:"LockTime"`
Fee uint64 `json:"Fee"` // Fee applies: always consant
Image string `json:"Image"`
IsPrivacy bool `json:"IsPrivacy"`
Proof privacy.Proof `json:"Proof"`
// ProofDetail jsonresult.ProofDetail `json:"ProofDetail"`
InputCoinPubKey string `json:"InputCoinPubKey"`
OutputCoinPubKey []string `json:"OutputCoinPubKey"`
OutputCoinSND []string `json:"OutputCoinSND"`
TokenInputCoinPubKey string `json:"TokenInputCoinPubKey"`
TokenOutputCoinPubKey []string `json:"TokenOutputCoinPubKey"`
TokenOutputCoinSND []string `json:"TokenOutputCoinSND"`
SigPubKey string `json:"SigPubKey,omitempty"` // 64 bytes
Sig string `json:"Sig,omitempty"` // 64 bytes
Metatype int `json:"Metatype"`
Metadata string `json:"Metadata"`
CustomTokenData string `json:"CustomTokenData"`
PrivacyCustomTokenID string `json:"PrivacyCustomTokenID"`
PrivacyCustomTokenName string `json:"PrivacyCustomTokenName"`
PrivacyCustomTokenSymbol string `json:"PrivacyCustomTokenSymbol"`
PrivacyCustomTokenData string `json:"PrivacyCustomTokenData"`
// PrivacyCustomTokenProofDetail jsonresult.ProofDetail `json:"PrivacyCustomTokenProofDetail"`
PrivacyCustomTokenIsPrivacy bool `json:"PrivacyCustomTokenIsPrivacy"`
PrivacyCustomTokenFee uint64 `json:"PrivacyCustomTokenFee"`
IsInMempool bool `json:"IsInMempool"`
IsInBlock bool `json:"IsInBlock"`
Info string `json:"Info"`
}
FromShardID byte
}
// call through coin service
func CallGetTxsByCoinPubKeys(coinPubKeys []string, config beCommon.Config) ([]ReceivedTransactionV2, error) {
type APIRespond struct {
Result []ReceivedTransactionV2
Error *string
}
var responseBodyData APIRespond
reqBody := struct {
Pubkeys []string
Base58 bool
}{
Pubkeys: coinPubKeys,
Base58: true,
}
_, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").
SetBody(reqBody).
SetResult(&responseBodyData).
Post(config.CoinserviceURL + "/gettxsbypubkey")
if err != nil {
log.Println("CallGetTxsByCoinPubKeys", err)
return nil, err
}
if responseBodyData.Error != nil {
log.Println("CallGetTxsByCoinPubKeys", errors.New(*responseBodyData.Error))
return nil, errors.New(*responseBodyData.Error)
}
if len(responseBodyData.Result) == 0 {
return nil, errors.New("CallGetTxsByCoinPubKeys result empty")
}
return responseBodyData.Result, nil
}
func CallGetTxsByCoinPubKeys2(coinPubKeys []string, config beCommon.Config, incClient *incclient.IncClient) (map[string]map[string]metadata.Transaction, error) {
// response := map[string]map[string]metadata.Transaction{}
resMap, err := incClient.GetTransactionsByPublicKeys(coinPubKeys)
if err != nil || resMap == nil {
log.Println("CallGetTxsByCoinPubKeys", err)
return nil, err
}
return resMap, nil
// for pubKey, txs := range resMap {
// for txID, tx := range txs {
// txDetail, err := incClient.GetTxDetail(tx)
// if err != nil || txDetail == nil {
// continue
// }
// response[pubKey] = append(response[pubKey], txDetail)
// }
// }
// return response, nil
}
// isBetterQuoteData returns true if d1 is better than d2
func isBetterQuoteData(d1 QuoteData, d2 QuoteData) (bool, error) {
// calculate actual received amount 1
if d1.Fee == nil || len(d1.Fee) == 0 {
return false, errors.New("Invalid format Fee is empty")
}
amt1, err1 := strToFloat64(d1.AmountOutPreSlippage)
fee1, err2 := strToFloat64(d1.Fee[0].AmountInBuyToken)
if err1 != nil || err2 != nil {
return false, errors.New("Invalid format AmountOutPreSlippage or feeAmountInBuyToken")
}
actualAmt1 := amt1 - fee1
// calculate actual received amount 2
if d2.Fee == nil || len(d2.Fee) == 0 {
return false, errors.New("Invalid format Fee is empty")
}
amt2, err1 := strToFloat64(d2.AmountOutPreSlippage)
fee2, err2 := strToFloat64(d2.Fee[0].AmountInBuyToken)
if err1 != nil || err2 != nil {
return false, errors.New("Invalid format AmountOutPreSlippage or feeAmountInBuyToken")
}
actualAmt2 := amt2 - fee2
return actualAmt1 > actualAmt2, nil
}
// isBetterQuoteData returns true if d1 is better than d2
func isBetterInterSwapPath(path1 InterSwapPath, path2 InterSwapPath) (bool, error) {
if len(path1.Paths) != 2 || len(path2.Paths) != 2 {
return false, errors.New("Invalid format interswap path")
}
// calculate actual received amount 1
amt1, err1 := strToFloat64(path1.Paths[1].AmountOutPreSlippage)
fee1, err2 := strToFloat64(path1.TotalFee.AmountInBuyToken)
if err1 != nil || err2 != nil {
return false, errors.New("Invalid format AmountOutPreSlippage or feeAmountInBuyToken")
}
actualAmt1 := amt1 - fee1
// calculate actual received amount 2
amt2, err1 := strToFloat64(path2.Paths[1].AmountOutPreSlippage)
fee2, err2 := strToFloat64(path2.TotalFee.AmountInBuyToken)
if err1 != nil || err2 != nil {
return false, errors.New("Invalid format AmountOutPreSlippage or feeAmountInBuyToken")
}
actualAmt2 := amt2 - fee2
return actualAmt1 > actualAmt2, nil
}
// GetBestRoute return the best one for each network (pdex & papps), and the best one for all pApps
func GetBestRoute(paths map[string][]QuoteData) map[string]*QuoteData {
res := map[string]*QuoteData{}
bestPAppPath := QuoteData{}
for network, datas := range paths {
// find the best one for each network
tmpBestPath := QuoteData{}
for _, d := range datas {
if tmpBestPath.AppName == "" {
tmpBestPath = d
} else if isBetter, err := isBetterQuoteData(d, tmpBestPath); err == nil && isBetter {
tmpBestPath = d
}
}
res[network] = &tmpBestPath
// find the best one for all papp
if network != IncNetworkStr {
tmpBestPath.PAppNetwork = network
if bestPAppPath.AppName == "" {
bestPAppPath = tmpBestPath
} else if isBetter, err := isBetterQuoteData(tmpBestPath, bestPAppPath); err == nil && isBetter {
bestPAppPath = tmpBestPath
}
}
}
if bestPAppPath.AppName != "" {
res[PAppStr] = &bestPAppPath
}
return res
}
func calTotalFee(interswapPath InterSwapPath, config beCommon.Config, tokenInfos map[string]*beCommon.TokenInfo) (*PappNetworkFee, map[string]*beCommon.TokenInfo, error) {
path := interswapPath.Paths
if len(path) != 2 || len(path[0].Fee) == 0 || len(path[1].Fee) == 0 {
return nil, tokenInfos, errors.New("Invalid path to calculate total fee")
}
fee1 := path[0].Fee[0]
fee2 := path[1].Fee[0]
// total fee paid in the token fee of the first swap info
feeToken := fee1.TokenID
feeTokenInfo, tokenInfos, err := getTokenInfoWithCache(feeToken, tokenInfos, config)
if err != nil {
return nil, tokenInfos, err
}
feeTokenInfo2, tokenInfos, err := getTokenInfoWithCache(fee2.TokenID, tokenInfos, config)
if err != nil {
return nil, tokenInfos, err
}
toTokenInfo, tokenInfos, err := getTokenInfoWithCache(interswapPath.ToToken, tokenInfos, config)
if err != nil {
return nil, tokenInfos, err
}
// feeAmt2, err := convertAmountUint64(fee2.Amount, fee2.TokenID, feeToken, config)
feeAmt2, err := convertAmountFromToTokenInfo(fee2.Amount, *feeTokenInfo2, *feeTokenInfo)
if err != nil {
return nil, tokenInfos, err
}
amount := fee1.Amount + feeAmt2
// amountInBuyToken, err := convertAmountUint64(amount, feeToken, interswapPath.ToToken, config)
amountInBuyToken, err := convertAmountFromToTokenInfo(amount, *feeTokenInfo, *toTokenInfo)
if err != nil {
return nil, tokenInfos, err
}
amountInBuyTokenStr, err := convertToWithoutDecStrWithTokenInfo(amountInBuyToken, toTokenInfo)
if err != nil {
return nil, tokenInfos, err
}
res := &PappNetworkFee{
TokenID: feeToken,
Amount: amount,
AmountInBuyToken: amountInBuyTokenStr,
// PrivacyFee: ,
// FeeInUSD:
}
fmt.Printf("Calculate total fee : %+v\n", res)
return res, tokenInfos, nil
}
func getTokenInfo(pUTokenID string, config beCommon.Config) (*beCommon.TokenInfo, error) {
type APIRespond struct {
Result []beCommon.TokenInfo
Error *string
}
reqBody := struct {
TokenIDs []string
}{
TokenIDs: []string{pUTokenID},
}
var responseBodyData APIRespond
_, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").
SetResult(&responseBodyData).SetBody(reqBody).
Post(config.CoinserviceURL + "/coins/tokeninfo")
if err != nil {
return nil, err
}
if len(responseBodyData.Result) == 1 {
return &responseBodyData.Result[0], nil
}
return nil, errors.New(fmt.Sprintf("token not found"))
}
func getTokensInfo(pUTokenID []string, config beCommon.Config) ([]beCommon.TokenInfo, error) {
type APIRespond struct {
Result []beCommon.TokenInfo
Error *string
}
reqBody := struct {
TokenIDs []string
}{
TokenIDs: pUTokenID,
}
var responseBodyData APIRespond
_, err := restyClient.R().
EnableTrace().
SetHeader("Content-Type", "application/json").
SetResult(&responseBodyData).SetBody(reqBody).
Post(config.CoinserviceURL + "/coins/tokeninfo")
if err != nil {
return nil, err
}
if len(responseBodyData.Result) == 0 {
return nil, errors.New(fmt.Sprintf("tokens not found"))
}
return responseBodyData.Result, nil
}
// GetChildTokenUnified returns child token of unified token
// if token is not unified, return itself
func GetChildTokenUnified(tokenID string, networkID int, config beCommon.Config) (string, error) {
tokenInfo, err := getTokenInfo(tokenID, config)
if err != nil {
return "", err
}
listUnified := tokenInfo.ListUnifiedToken
if len(listUnified) == 0 {
fmt.Printf("%v is not unified token\n", tokenID)
return tokenID, nil
}
for _, token := range listUnified {
if token.NetworkID == networkID {
return token.TokenID, nil
}
}
return "", errors.New("Invalid networkID")
}