trustwallet / watchmarket
1
package watchmarket
2

3
import (
4
	"math"
5
	"time"
6
)
7

8
type (
9
	Rate struct {
10
		Currency         string  `json:"currency"`
11
		PercentChange24h float64 `json:"percent_change_24h,omitempty"`
12
		Provider         string  `json:"provider,omitempty"`
13
		Rate             float64 `json:"rate"`
14
		Timestamp        int64   `json:"timestamp"`
15
		ShowOption       int     `json:"-"`
16
	}
17

18
	Rates []Rate
19

20
	CoinType string
21

22
	Ticker struct {
23
		Coin              uint      `json:"coin"`
24
		CoinName          string    `json:"coin_name"`
25
		TokenId           string    `json:"token_id,omitempty"`
26
		CoinType          CoinType  `json:"type,omitempty"`
27
		Price             Price     `json:"price,omitempty"`
28
		LastUpdate        time.Time `json:"last_update"`
29
		Error             string    `json:"error,omitempty"`
30
		Volume            float64   `json:"volume"`
31
		MarketCap         float64   `json:"market_cap"`
32
		CirculatingSupply float64   `json:"-"`
33
		TotalSupply       float64   `json:"-"`
34
		ShowOption        int       `json:"-"`
35
	}
36

37
	Price struct {
38
		Change24h float64 `json:"change_24h"`
39
		Currency  string  `json:"currency"`
40
		Provider  string  `json:"provider,omitempty"`
41
		Value     float64 `json:"value"`
42
	}
43

44
	Tickers []Ticker
45

46
	Chart struct {
47
		Provider string       `json:"provider,omitempty"`
48
		Prices   []ChartPrice `json:"prices,omitempty"`
49
		Error    string       `json:"error,omitempty"`
50
	}
51

52
	ChartPrice struct {
53
		Price float64 `json:"price"`
54
		Date  int64   `json:"date"`
55
	}
56

57
	CoinDetails struct {
58
		Provider    string `json:"provider,omitempty"`
59
		ProviderURL string `json:"provider_url,omitempty"`
60
		Info        *Info  `json:"info,omitempty"`
61
	}
62

63
	Info struct {
64
		Name             string       `json:"name,omitempty"`
65
		Website          string       `json:"website,omitempty"`
66
		SourceCode       string       `json:"source_code,omitempty"`
67
		WhitePaper       string       `json:"white_paper,omitempty"`
68
		Description      string       `json:"description,omitempty"`
69
		ShortDescription string       `json:"short_description,omitempty"`
70
		Research         string       `json:"research,omitempty"`
71
		Explorer         string       `json:"explorer,omitempty"`
72
		Socials          []SocialLink `json:"socials,omitempty"`
73
	}
74

75
	SocialLink struct {
76
		Name   string `json:"name"`
77
		Url    string `json:"url"`
78
		Handle string `json:"handle"`
79
	}
80
)
81

82
const (
83
	UnknownCoinID                 = 111111
84
	DefaultPrecision              = 10
85
	Coin                 CoinType = "coin"
86
	Token                CoinType = "token"
87
	DefaultCurrency               = "USD"
88
	DefaultMaxChartItems          = 64
89

90
	ErrNotFound   = "not found"
91
	ErrBadRequest = "bad request"
92
	ErrInternal   = "internal"
93
)
94

95
func (d Chart) IsEmpty() bool {
96 1
	return len(d.Prices) == 0
97
}
98

99
func (i CoinDetails) IsEmpty() bool {
100 1
	if i.Info == nil {
101 1
		return true
102
	}
103 1
	return i.Info.Name == ""
104
}
105

106
func round(num float64) int {
107 1
	return int(num + math.Copysign(0.5, num))
108
}
109

110
func TruncateWithPrecision(num float64, precision int) float64 {
111 1
	output := math.Pow(10, float64(precision))
112 1
	return float64(round(num*output)) / output
113
}
114

115
func UnixToDuration(unixTime uint) time.Duration {
116 1
	return time.Duration(unixTime * 1000000000)
117
}
118

119
func DurationToUnix(duration time.Duration) uint {
120 1
	return uint(duration.Seconds())
121
}
122

123
func IsRespectableValue(value, respValue float64) bool {
124 1
	return value >= respValue
125
}
126

127
func IsSuitableUpdateTime(LastUpdate time.Time, maxDuration time.Duration) bool {
128 1
	now := time.Now().Unix()
129 1
	last := LastUpdate.Unix()
130 1
	if now < last {
131 0
		return true
132
	}
133 1
	diff := now - last
134 1
	if diff < 0 {
135 0
		return true
136
	}
137 1
	respectableTime := DurationToUnix(maxDuration)
138 1
	return uint(diff) <= respectableTime
139
}
140

141
func IsFiatRate(currency string) bool {
142 1
	switch currency {
143 1
	case "AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BYR", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMK", "ZMW", "ZWL":
144 1
		return true
145 1
	default:
146
	}
147 1
	return false
148
}

Read our documentation on viewing source code .

Loading