forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CurrencyPair.go
276 lines (245 loc) · 7.01 KB
/
CurrencyPair.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
package goex
import "strings"
type Currency struct {
Symbol string
Desc string
}
func (c Currency) String() string {
return c.Symbol
}
func (c Currency) Eq(c2 Currency) bool {
return c.Symbol == c2.Symbol
}
// A->B(A兑换为B)
type CurrencyPair struct {
CurrencyA Currency
CurrencyB Currency
}
var (
UNKNOWN = Currency{"UNKNOWN", ""}
CNY = Currency{"CNY", ""}
USD = Currency{"USD", ""}
USDT = Currency{"USDT", ""}
PAX = Currency{"PAX", "https://www.paxos.com/"}
USDC = Currency{"USDC", "https://www.centre.io/"}
EUR = Currency{"EUR", ""}
KRW = Currency{"KRW", ""}
JPY = Currency{"JPY", ""}
BTC = Currency{"BTC", "https://bitcoin.org/"}
XBT = Currency{"XBT", ""}
BCC = Currency{"BCC", ""}
BCH = Currency{"BCH", ""}
BCX = Currency{"BCX", ""}
LTC = Currency{"LTC", ""}
ETH = Currency{"ETH", ""}
ETC = Currency{"ETC", ""}
EOS = Currency{"EOS", ""}
BTS = Currency{"BTS", ""}
QTUM = Currency{"QTUM", ""}
SC = Currency{"SC", ""}
ANS = Currency{"ANS", ""}
ZEC = Currency{"ZEC", ""}
DCR = Currency{"DCR", ""}
XRP = Currency{"XRP", ""}
BTG = Currency{"BTG", ""}
BCD = Currency{"BCD", ""}
NEO = Currency{"NEO", ""}
HSR = Currency{"HSR", ""}
BSV = Currency{"BSV", ""}
OKB = Currency{"OKB", "OKB is a global utility token issued by OK Blockchain Foundation"}
HT = Currency{"HT", "HuoBi Token"}
BNB = Currency{"BNB", "BNB, or Binance Coin, is a cryptocurrency created by Binance."}
TRX = Currency{"TRX", ""}
//currency pair
BTC_CNY = CurrencyPair{BTC, CNY}
LTC_CNY = CurrencyPair{LTC, CNY}
BCC_CNY = CurrencyPair{BCC, CNY}
ETH_CNY = CurrencyPair{ETH, CNY}
ETC_CNY = CurrencyPair{ETC, CNY}
EOS_CNY = CurrencyPair{EOS, CNY}
BTS_CNY = CurrencyPair{BTS, CNY}
QTUM_CNY = CurrencyPair{QTUM, CNY}
SC_CNY = CurrencyPair{SC, CNY}
ANS_CNY = CurrencyPair{ANS, CNY}
ZEC_CNY = CurrencyPair{ZEC, CNY}
BTC_KRW = CurrencyPair{BTC, KRW}
ETH_KRW = CurrencyPair{ETH, KRW}
ETC_KRW = CurrencyPair{ETC, KRW}
LTC_KRW = CurrencyPair{LTC, KRW}
BCH_KRW = CurrencyPair{BCH, KRW}
BTC_USD = CurrencyPair{BTC, USD}
LTC_USD = CurrencyPair{LTC, USD}
ETH_USD = CurrencyPair{ETH, USD}
ETC_USD = CurrencyPair{ETC, USD}
BCH_USD = CurrencyPair{BCH, USD}
BCC_USD = CurrencyPair{BCC, USD}
XRP_USD = CurrencyPair{XRP, USD}
BCD_USD = CurrencyPair{BCD, USD}
EOS_USD = CurrencyPair{EOS, USD}
BTG_USD = CurrencyPair{BTG, USD}
BSV_USD = CurrencyPair{BSV, USD}
BTC_USDT = CurrencyPair{BTC, USDT}
LTC_USDT = CurrencyPair{LTC, USDT}
BCH_USDT = CurrencyPair{BCH, USDT}
BCC_USDT = CurrencyPair{BCC, USDT}
ETC_USDT = CurrencyPair{ETC, USDT}
ETH_USDT = CurrencyPair{ETH, USDT}
BCD_USDT = CurrencyPair{BCD, USDT}
NEO_USDT = CurrencyPair{NEO, USDT}
EOS_USDT = CurrencyPair{EOS, USDT}
XRP_USDT = CurrencyPair{XRP, USDT}
HSR_USDT = CurrencyPair{HSR, USDT}
BSV_USDT = CurrencyPair{BSV, USDT}
OKB_USDT = CurrencyPair{OKB, USDT}
HT_USDT = CurrencyPair{HT, USDT}
BNB_USDT = CurrencyPair{BNB, USDT}
PAX_USDT = CurrencyPair{PAX, USDT}
TRX_USDT = CurrencyPair{TRX, USDT}
XRP_EUR = CurrencyPair{XRP, EUR}
BTC_JPY = CurrencyPair{BTC, JPY}
LTC_JPY = CurrencyPair{LTC, JPY}
ETH_JPY = CurrencyPair{ETH, JPY}
ETC_JPY = CurrencyPair{ETC, JPY}
BCH_JPY = CurrencyPair{BCH, JPY}
LTC_BTC = CurrencyPair{LTC, BTC}
ETH_BTC = CurrencyPair{ETH, BTC}
ETC_BTC = CurrencyPair{ETC, BTC}
BCC_BTC = CurrencyPair{BCC, BTC}
BCH_BTC = CurrencyPair{BCH, BTC}
DCR_BTC = CurrencyPair{DCR, BTC}
XRP_BTC = CurrencyPair{XRP, BTC}
BTG_BTC = CurrencyPair{BTG, BTC}
BCD_BTC = CurrencyPair{BCD, BTC}
NEO_BTC = CurrencyPair{NEO, BTC}
EOS_BTC = CurrencyPair{EOS, BTC}
HSR_BTC = CurrencyPair{HSR, BTC}
BSV_BTC = CurrencyPair{BSV, BTC}
OKB_BTC = CurrencyPair{OKB, BTC}
HT_BTC = CurrencyPair{HT, BTC}
BNB_BTC = CurrencyPair{BNB, BTC}
TRX_BTC = CurrencyPair{TRX, BTC}
ETC_ETH = CurrencyPair{ETC, ETH}
EOS_ETH = CurrencyPair{EOS, ETH}
ZEC_ETH = CurrencyPair{ZEC, ETH}
NEO_ETH = CurrencyPair{NEO, ETH}
HSR_ETH = CurrencyPair{HSR, ETH}
LTC_ETH = CurrencyPair{LTC, ETH}
UNKNOWN_PAIR = CurrencyPair{UNKNOWN, UNKNOWN}
)
func (c CurrencyPair) String() string {
return c.ToSymbol("_")
}
func (c CurrencyPair) Eq(c2 CurrencyPair) bool {
return c.String() == c2.String()
}
func (c Currency) AdaptBchToBcc() Currency {
if c.Symbol == "BCH" || c.Symbol == "bch" {
return BCC
}
return c
}
func (c Currency) AdaptBccToBch() Currency {
if c.Symbol == "BCC" || c.Symbol == "bcc" {
return BCH
}
return c
}
func NewCurrency(symbol, desc string) Currency {
switch symbol {
case "cny", "CNY":
return CNY
case "usdt", "USDT":
return USDT
case "usd", "USD":
return USD
case "usdc", "USDC":
return USDC
case "pax", "PAX":
return PAX
case "jpy", "JPY":
return JPY
case "krw", "KRW":
return KRW
case "eur", "EUR":
return EUR
case "btc", "BTC":
return BTC
case "xbt", "XBT":
return XBT
case "bch", "BCH":
return BCH
case "bcc", "BCC":
return BCC
case "ltc", "LTC":
return LTC
case "sc", "SC":
return SC
case "ans", "ANS":
return ANS
case "neo", "NEO":
return NEO
case "okb", "OKB":
return OKB
case "ht", "HT":
return HT
case "bnb", "BNB":
return BNB
case "trx", "TRX":
return TRX
default:
return Currency{strings.ToUpper(symbol), desc}
}
}
func NewCurrencyPair(currencyA Currency, currencyB Currency) CurrencyPair {
return CurrencyPair{currencyA, currencyB}
}
func NewCurrencyPair2(currencyPairSymbol string) CurrencyPair {
currencys := strings.Split(currencyPairSymbol, "_")
if len(currencys) == 2 {
return CurrencyPair{NewCurrency(currencys[0], ""),
NewCurrency(currencys[1], "")}
}
return UNKNOWN_PAIR
}
func (pair CurrencyPair) ToSymbol(joinChar string) string {
return strings.Join([]string{pair.CurrencyA.Symbol, pair.CurrencyB.Symbol}, joinChar)
}
func (pair CurrencyPair) ToSymbol2(joinChar string) string {
return strings.Join([]string{pair.CurrencyB.Symbol, pair.CurrencyA.Symbol}, joinChar)
}
func (pair CurrencyPair) AdaptUsdtToUsd() CurrencyPair {
CurrencyB := pair.CurrencyB
if pair.CurrencyB.Eq(USDT) {
CurrencyB = USD
}
return CurrencyPair{pair.CurrencyA, CurrencyB}
}
func (pair CurrencyPair) AdaptUsdToUsdt() CurrencyPair {
CurrencyB := pair.CurrencyB
if pair.CurrencyB.Eq(USD) {
CurrencyB = USDT
}
return CurrencyPair{pair.CurrencyA, CurrencyB}
}
//It is currently applicable to binance and zb
func (pair CurrencyPair) AdaptBchToBcc() CurrencyPair {
CurrencyA := pair.CurrencyA
if pair.CurrencyA.Eq(BCH) {
CurrencyA = BCC
}
return CurrencyPair{CurrencyA, pair.CurrencyB}
}
func (pair CurrencyPair) AdaptBccToBch() CurrencyPair {
if pair.CurrencyA.Eq(BCC) {
return CurrencyPair{BCH, pair.CurrencyB}
}
return pair
}
//for to symbol lower , Not practical '==' operation method
func (pair CurrencyPair) ToLower() CurrencyPair {
return CurrencyPair{Currency{strings.ToLower(pair.CurrencyA.Symbol), pair.CurrencyA.Desc},
Currency{strings.ToLower(pair.CurrencyB.Symbol), pair.CurrencyB.Desc}}
}
func (pair CurrencyPair) Reverse() CurrencyPair {
return CurrencyPair{pair.CurrencyB, pair.CurrencyA}
}