-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccount_helper.go
118 lines (96 loc) · 2.1 KB
/
account_helper.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
package main
import (
"encoding/json"
"fmt"
"github.com/incognitochain/go-incognito-sdk-v2/common"
"github.com/urfave/cli/v2"
"io/ioutil"
"net/http"
)
var listTokenInfo map[string]TokenInfo
type TokenInfo struct {
TokenID string `json:"TokenID"`
Name string `json:"Name"`
Symbol string `json:"Symbol"`
PSymbol string `json:"PSymbol"`
IsBridge bool `json:"IsBridge"`
Verified bool `json:"Verified"`
PDecimals int `json:"PDecimals"`
Network string `json:"Network"`
}
func initForFinancialReport(c *cli.Context) error {
err := initNetWork()
if err != nil {
return err
}
if network != "mainnet" {
return nil
}
listTokenInfo, err = getAllTokenInfo()
if err != nil {
listTokenInfo = nil
}
return nil
}
func getAllTokenInfo() (map[string]TokenInfo, error) {
url := "https://api-coinservice.incognito.org/coins/tokenlist?all=true"
resp, err := http.Get(url)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
type TmpRes struct {
Result []TokenInfo `json:"Result"`
Error string `json:"Error"`
}
var tmpRes TmpRes
err = json.Unmarshal(body, &tmpRes)
if err != nil {
return nil, err
}
if tmpRes.Error != "" {
return nil, fmt.Errorf("retrieving token info encountered an error: %v", tmpRes.Error)
}
res := make(map[string]TokenInfo)
for _, tokenInfo := range tmpRes.Result {
res[tokenInfo.TokenID] = tokenInfo
}
return res, nil
}
func getTokenName(tokenID string) string {
if tokenID == common.PRVIDStr {
return "PRV"
}
if listTokenInfo == nil {
return tokenID
}
if tokenInfo, ok := listTokenInfo[tokenID]; ok {
if !tokenInfo.Verified {
return tokenID
}
res := tokenInfo.Symbol
if tokenInfo.Network != "" {
res = fmt.Sprintf("%v (%v)", res, tokenInfo.Network)
}
return res
}
return tokenID
}
func getTokenDecimals(tokenID string) int {
if tokenID == common.PRVIDStr {
return 9
}
if listTokenInfo == nil {
return 0
}
if tokenInfo, ok := listTokenInfo[tokenID]; ok {
if !tokenInfo.Verified {
return 0
}
return tokenInfo.PDecimals
}
return 0
}