/
CurrencyUtil.java
494 lines (426 loc) · 21 KB
/
CurrencyUtil.java
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
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.locale;
import bisq.core.app.BisqEnvironment;
import bisq.core.btc.BaseCurrencyNetwork;
import bisq.core.dao.governance.asset.AssetService;
import bisq.core.filter.FilterManager;
import bisq.asset.Asset;
import bisq.asset.AssetRegistry;
import bisq.asset.Coin;
import bisq.asset.Token;
import bisq.asset.coins.BSQ;
import bisq.common.app.DevEnv;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import static com.google.common.base.Preconditions.checkArgument;
@Slf4j
public class CurrencyUtil {
public static void setup() {
setBaseCurrencyCode(BisqEnvironment.getBaseCurrencyNetwork().getCurrencyCode());
}
private static final AssetRegistry assetRegistry = new AssetRegistry();
private static String baseCurrencyCode = "BTC";
private static List<FiatCurrency> allSortedFiatCurrencies;
private static List<CryptoCurrency> allSortedCryptoCurrencies;
public static void setBaseCurrencyCode(String baseCurrencyCode) {
CurrencyUtil.baseCurrencyCode = baseCurrencyCode;
}
public static List<FiatCurrency> getAllSortedFiatCurrencies() {
if (Objects.isNull(allSortedFiatCurrencies))
allSortedFiatCurrencies = createAllSortedFiatCurrenciesList();
return allSortedFiatCurrencies;
}
private static List<FiatCurrency> createAllSortedFiatCurrenciesList() {
return CountryUtil.getAllCountries().stream()
.map(country -> getCurrencyByCountryCode(country.code))
.distinct()
.sorted(TradeCurrency::compareTo)
.collect(Collectors.toList());
}
public static List<FiatCurrency> getMainFiatCurrencies() {
TradeCurrency defaultTradeCurrency = getDefaultTradeCurrency();
List<FiatCurrency> list = new ArrayList<>();
// Top traded currencies
list.add(new FiatCurrency("USD"));
list.add(new FiatCurrency("EUR"));
list.add(new FiatCurrency("GBP"));
list.add(new FiatCurrency("CAD"));
list.add(new FiatCurrency("AUD"));
list.add(new FiatCurrency("RUB"));
list.add(new FiatCurrency("INR"));
list.sort(TradeCurrency::compareTo);
FiatCurrency defaultFiatCurrency =
defaultTradeCurrency instanceof FiatCurrency ? (FiatCurrency) defaultTradeCurrency : null;
if (defaultFiatCurrency != null && list.contains(defaultFiatCurrency)) {
//noinspection SuspiciousMethodCalls
list.remove(defaultTradeCurrency);
list.add(0, defaultFiatCurrency);
}
return list;
}
public static List<CryptoCurrency> getAllSortedCryptoCurrencies() {
if (allSortedCryptoCurrencies == null)
allSortedCryptoCurrencies = createAllSortedCryptoCurrenciesList();
return allSortedCryptoCurrencies;
}
private static List<CryptoCurrency> createAllSortedCryptoCurrenciesList() {
return getSortedAssetStream()
.map(CurrencyUtil::assetToCryptoCurrency)
.collect(Collectors.toList());
}
public static Stream<Asset> getSortedAssetStream() {
return assetRegistry.stream()
.filter(CurrencyUtil::assetIsNotBaseCurrency)
.filter(asset -> isNotBsqOrBsqTradingActivated(asset, BisqEnvironment.getBaseCurrencyNetwork(), DevEnv.isDaoTradingActivated()))
.filter(asset -> assetMatchesNetworkIfMainnet(asset, BisqEnvironment.getBaseCurrencyNetwork()))
.sorted(Comparator.comparing(Asset::getName));
}
public static List<CryptoCurrency> getMainCryptoCurrencies() {
final List<CryptoCurrency> result = new ArrayList<>();
result.add(new CryptoCurrency("XRC", "Bitcoin Rhodium"));
if (DevEnv.isDaoTradingActivated())
result.add(new CryptoCurrency("BSQ", "BSQ"));
result.add(new CryptoCurrency("BEAM", "Beam"));
result.add(new CryptoCurrency("DASH", "Dash"));
result.add(new CryptoCurrency("DCR", "Decred"));
result.add(new CryptoCurrency("ETH", "Ether"));
result.add(new CryptoCurrency("GRIN", "Grin"));
result.add(new CryptoCurrency("LTC", "Litecoin"));
result.add(new CryptoCurrency("XMR", "Monero"));
result.add(new CryptoCurrency("NMC", "Namecoin"));
result.add(new CryptoCurrency("SF", "Siafund"));
result.add(new CryptoCurrency("ZEC", "Zcash"));
result.sort(TradeCurrency::compareTo);
return result;
}
public static List<CryptoCurrency> getRemovedCryptoCurrencies() {
final List<CryptoCurrency> currencies = new ArrayList<>();
currencies.add(new CryptoCurrency("BCH", "Bitcoin Cash"));
currencies.add(new CryptoCurrency("BCHC", "Bitcoin Clashic"));
currencies.add(new CryptoCurrency("ACH", "AchieveCoin"));
currencies.add(new CryptoCurrency("SC", "SpaceCash"));
currencies.add(new CryptoCurrency("PPI", "PiedPiper Coin"));
currencies.add(new CryptoCurrency("PEPECASH", "Pepe Cash"));
currencies.add(new CryptoCurrency("GRC", "Gridcoin"));
currencies.add(new CryptoCurrency("LTZ", "LitecoinZ"));
currencies.add(new CryptoCurrency("ZOC", "01coin"));
return currencies;
}
public static List<TradeCurrency> getAllAdvancedCashCurrencies() {
ArrayList<TradeCurrency> currencies = new ArrayList<>(Arrays.asList(
new FiatCurrency("USD"),
new FiatCurrency("EUR"),
new FiatCurrency("GBP"),
new FiatCurrency("RUB"),
new FiatCurrency("UAH"),
new FiatCurrency("KZT"),
new FiatCurrency("BRL")
));
currencies.sort(Comparator.comparing(TradeCurrency::getCode));
return currencies;
}
public static List<TradeCurrency> getAllMoneyGramCurrencies() {
ArrayList<TradeCurrency> currencies = new ArrayList<>(Arrays.asList(
new FiatCurrency("AED"),
new FiatCurrency("AUD"),
new FiatCurrency("BND"),
new FiatCurrency("CAD"),
new FiatCurrency("CHF"),
new FiatCurrency("CZK"),
new FiatCurrency("DKK"),
new FiatCurrency("EUR"),
new FiatCurrency("FJD"),
new FiatCurrency("GBP"),
new FiatCurrency("HKD"),
new FiatCurrency("HUF"),
new FiatCurrency("IDR"),
new FiatCurrency("ILS"),
new FiatCurrency("INR"),
new FiatCurrency("JPY"),
new FiatCurrency("KRW"),
new FiatCurrency("KWD"),
new FiatCurrency("LKR"),
new FiatCurrency("MAD"),
new FiatCurrency("MGA"),
new FiatCurrency("MXN"),
new FiatCurrency("MYR"),
new FiatCurrency("NOK"),
new FiatCurrency("NZD"),
new FiatCurrency("OMR"),
new FiatCurrency("PEN"),
new FiatCurrency("PGK"),
new FiatCurrency("PHP"),
new FiatCurrency("PKR"),
new FiatCurrency("PLN"),
new FiatCurrency("SAR"),
new FiatCurrency("SBD"),
new FiatCurrency("SCR"),
new FiatCurrency("SEK"),
new FiatCurrency("SGD"),
new FiatCurrency("THB"),
new FiatCurrency("TOP"),
new FiatCurrency("TRY"),
new FiatCurrency("TWD"),
new FiatCurrency("USD"),
new FiatCurrency("VND"),
new FiatCurrency("VUV"),
new FiatCurrency("WST"),
new FiatCurrency("XOF"),
new FiatCurrency("XPF"),
new FiatCurrency("ZAR")
));
currencies.sort(Comparator.comparing(TradeCurrency::getCode));
return currencies;
}
// https://support.uphold.com/hc/en-us/articles/202473803-Supported-currencies
public static List<TradeCurrency> getAllUpholdCurrencies() {
ArrayList<TradeCurrency> currencies = new ArrayList<>(Arrays.asList(
new FiatCurrency("USD"),
new FiatCurrency("EUR"),
new FiatCurrency("GBP"),
new FiatCurrency("CNY"),
new FiatCurrency("JPY"),
new FiatCurrency("CHF"),
new FiatCurrency("INR"),
new FiatCurrency("MXN"),
new FiatCurrency("AUD"),
new FiatCurrency("CAD"),
new FiatCurrency("HKD"),
new FiatCurrency("NZD"),
new FiatCurrency("SGD"),
new FiatCurrency("KES"),
new FiatCurrency("ILS"),
new FiatCurrency("DKK"),
new FiatCurrency("NOK"),
new FiatCurrency("SEK"),
new FiatCurrency("PLN"),
new FiatCurrency("ARS"),
new FiatCurrency("BRL"),
new FiatCurrency("AED"),
new FiatCurrency("PHP")
));
currencies.sort(Comparator.comparing(TradeCurrency::getCode));
return currencies;
}
//https://www.revolut.com/pa/faq#can-i-hold-multiple-currencies
public static List<TradeCurrency> getAllRevolutCurrencies() {
ArrayList<TradeCurrency> currencies = new ArrayList<>(Arrays.asList(
new FiatCurrency("USD"),
new FiatCurrency("GBP"),
new FiatCurrency("EUR"),
new FiatCurrency("PLN"),
new FiatCurrency("CHF"),
new FiatCurrency("DKK"),
new FiatCurrency("NOK"),
new FiatCurrency("SEK"),
new FiatCurrency("RON"),
new FiatCurrency("SGD"),
new FiatCurrency("HKD"),
new FiatCurrency("AUD"),
new FiatCurrency("NZD"),
new FiatCurrency("TRY"),
new FiatCurrency("ILS"),
new FiatCurrency("AED"),
new FiatCurrency("CAD"),
new FiatCurrency("HUF"),
new FiatCurrency("INR"),
new FiatCurrency("JPY"),
new FiatCurrency("MAD"),
new FiatCurrency("QAR"),
new FiatCurrency("THB"),
new FiatCurrency("ZAR")
));
currencies.sort(Comparator.comparing(TradeCurrency::getCode));
return currencies;
}
public static boolean isFiatCurrency(String currencyCode) {
try {
return currencyCode != null
&& !currencyCode.isEmpty()
&& !isCryptoCurrency(currencyCode)
&& Currency.getInstance(currencyCode) != null;
} catch (Throwable t) {
return false;
}
}
public static Optional<FiatCurrency> getFiatCurrency(String currencyCode) {
return getAllSortedFiatCurrencies().stream().filter(e -> e.getCode().equals(currencyCode)).findAny();
}
@SuppressWarnings("WeakerAccess")
/**
* We return true if it is BTC or any of our currencies available in the assetRegistry.
* For removed assets it would fail as they are not found but we don't want to conclude that they are fiat then.
* As the caller might not deal with the case that a currency can be neither a cryptoCurrency nor Fiat if not found
* we return true as well in case we have no fiat currency for the code.
*
* As we use a boolean result for isCryptoCurrency and isFiatCurrency we do not treat missing currencies correctly.
* To throw an exception might be an option but that will require quite a lot of code change, so we don't do that
* for the moment, but could be considered for the future. Another maybe better option is to introduce a enum which
* contains 3 entries (CryptoCurrency, Fiat, Undefined).
*/
public static boolean isCryptoCurrency(String currencyCode) {
// Some tests call that method with null values. Should be fixed in the tests but to not break them return false.
if (currencyCode == null)
return false;
// BTC is not part of our assetRegistry so treat it extra here. Other old base currencies (LTC, DOGE, DASH)
// are not supported anymore so we can ignore that case.
if (currencyCode.equals("BTC"))
return true;
// If we find the code in our assetRegistry we return true.
// It might be that an asset was removed from the assetsRegistry, we deal with such cases below by checking if
// it is a fiat currency
if (getCryptoCurrency(currencyCode).isPresent())
return true;
// In case the code is from a removed asset we cross check if there exist a fiat currency with that code,
// if we don't find a fiat currency we treat it as a crypto currency.
if (!getFiatCurrency(currencyCode).isPresent())
return true;
// If we would have found a fiat currency we return false
return false;
}
public static Optional<CryptoCurrency> getCryptoCurrency(String currencyCode) {
return getAllSortedCryptoCurrencies().stream().filter(e -> e.getCode().equals(currencyCode)).findAny();
}
public static Optional<TradeCurrency> getTradeCurrency(String currencyCode) {
Optional<FiatCurrency> fiatCurrencyOptional = getFiatCurrency(currencyCode);
if (isFiatCurrency(currencyCode) && fiatCurrencyOptional.isPresent())
return Optional.of(fiatCurrencyOptional.get());
Optional<CryptoCurrency> cryptoCurrencyOptional = getCryptoCurrency(currencyCode);
if (isCryptoCurrency(currencyCode) && cryptoCurrencyOptional.isPresent())
return Optional.of(cryptoCurrencyOptional.get());
return Optional.empty();
}
public static FiatCurrency getCurrencyByCountryCode(String countryCode) {
if (countryCode.equals("XK"))
return new FiatCurrency("EUR");
Currency currency = Currency.getInstance(new Locale(LanguageUtil.getDefaultLanguage(), countryCode));
return new FiatCurrency(currency.getCurrencyCode());
}
public static String getNameByCode(String currencyCode) {
if (isCryptoCurrency(currencyCode)) {
// We might not find the name in case we have a call for a removed asset.
// If BTC is the code (used in tests) we also want return Bitcoin as name.
final Optional<CryptoCurrency> removedCryptoCurrency = getRemovedCryptoCurrencies().stream()
.filter(cryptoCurrency -> cryptoCurrency.getCode().equals(currencyCode))
.findAny();
String btcOrRemovedAsset = "BTC".equals(currencyCode) ? "Bitcoin" :
removedCryptoCurrency.isPresent() ? removedCryptoCurrency.get().getName() : Res.get("shared.na");
return getCryptoCurrency(currencyCode).map(TradeCurrency::getName).orElse(btcOrRemovedAsset);
}
try {
return Currency.getInstance(currencyCode).getDisplayName();
} catch (Throwable t) {
log.debug("No currency name available " + t.getMessage());
return currencyCode;
}
}
public static Optional<CryptoCurrency> findCryptoCurrencyByName(String currencyName) {
return getAllSortedCryptoCurrencies().stream()
.filter(e -> e.getName().equals(currencyName))
.findAny();
}
public static String getNameAndCode(String currencyCode) {
return getNameByCode(currencyCode) + " (" + currencyCode + ")";
}
public static TradeCurrency getDefaultTradeCurrency() {
return GlobalSettings.getDefaultTradeCurrency();
}
private static boolean assetIsNotBaseCurrency(Asset asset) {
return !assetMatchesCurrencyCode(asset, baseCurrencyCode);
}
// TODO We handle assets of other types (Token, ERC20) as matching the network which is not correct.
// We should add support for network property in those tokens as well.
public static boolean assetMatchesNetwork(Asset asset, BaseCurrencyNetwork baseCurrencyNetwork) {
return !(asset instanceof Coin) ||
((Coin) asset).getNetwork().name().equals(baseCurrencyNetwork.getNetwork());
}
// We only check for coins not other types of assets (TODO network check should be supported for all assets)
public static boolean assetMatchesNetworkIfMainnet(Asset asset, BaseCurrencyNetwork baseCurrencyNetwork) {
return !(asset instanceof Coin) ||
coinMatchesNetworkIfMainnet((Coin) asset, baseCurrencyNetwork);
}
// We want all coins available also in testnet or regtest for testing purpose
public static boolean coinMatchesNetworkIfMainnet(Coin coin, BaseCurrencyNetwork baseCurrencyNetwork) {
boolean matchesNetwork = assetMatchesNetwork(coin, baseCurrencyNetwork);
return !baseCurrencyNetwork.isMainnet() ||
matchesNetwork;
}
private static CryptoCurrency assetToCryptoCurrency(Asset asset) {
return new CryptoCurrency(asset.getTickerSymbol(), asset.getName(), asset instanceof Token);
}
private static boolean isNotBsqOrBsqTradingActivated(Asset asset, BaseCurrencyNetwork baseCurrencyNetwork, boolean daoTradingActivated) {
return !(asset instanceof BSQ) ||
daoTradingActivated && assetMatchesNetwork(asset, baseCurrencyNetwork);
}
public static boolean assetMatchesCurrencyCode(Asset asset, String currencyCode) {
return currencyCode.equals(asset.getTickerSymbol());
}
public static Optional<Asset> findAsset(AssetRegistry assetRegistry, String currencyCode,
BaseCurrencyNetwork baseCurrencyNetwork, boolean daoTradingActivated) {
List<Asset> assets = assetRegistry.stream()
.filter(asset -> assetMatchesCurrencyCode(asset, currencyCode)).collect(Collectors.toList());
// If we don't have the ticker symbol we throw an exception
if (!assets.stream().findFirst().isPresent())
return Optional.empty();
if (currencyCode.equals("BSQ") && baseCurrencyNetwork.isMainnet() && !daoTradingActivated)
return Optional.empty();
// We check for exact match with network, e.g. BTC$TESTNET
Optional<Asset> optionalAssetMatchesNetwork = assets.stream()
.filter(asset -> assetMatchesNetwork(asset, baseCurrencyNetwork))
.findFirst();
if (optionalAssetMatchesNetwork.isPresent())
return optionalAssetMatchesNetwork;
// In testnet or regtest we want to show all coins as well. Most coins have only Mainnet defined so we deliver
// that if no exact match was found in previous step
if (!baseCurrencyNetwork.isMainnet()) {
Optional<Asset> optionalAsset = assets.stream().findFirst();
checkArgument(optionalAsset.isPresent(), "optionalAsset must be present as we checked for " +
"not matching ticker symbols already above");
return optionalAsset;
}
// If we are in mainnet we need have a mainet asset defined.
throw new IllegalArgumentException("We are on mainnet and we could not find an asset with network type mainnet");
}
public static Optional<Asset> findAsset(String tickerSymbol) {
return assetRegistry.stream()
.filter(asset -> asset.getTickerSymbol().equals(tickerSymbol))
.findAny();
}
public static Optional<Asset> findAsset(String tickerSymbol, BaseCurrencyNetwork baseCurrencyNetwork) {
return assetRegistry.stream()
.filter(asset -> asset.getTickerSymbol().equals(tickerSymbol))
.filter(asset -> assetMatchesNetwork(asset, baseCurrencyNetwork))
.findAny();
}
// Excludes all assets which got removed by DAO voting
public static List<CryptoCurrency> getActiveSortedCryptoCurrencies(AssetService assetService, FilterManager filterManager) {
return getAllSortedCryptoCurrencies().stream()
.filter(e -> e.getCode().equals("BSQ") || assetService.isActive(e.getCode()))
.filter(e -> !filterManager.isCurrencyBanned(e.getCode()))
.collect(Collectors.toList());
}
}