Skip to content

Commit 74b1519

Browse files
committed
Add more fiat currencies
1 parent 562162f commit 74b1519

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

main.qml

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,21 @@ ApplicationWindow {
104104

105105
// {provider name: {ticker: price_api_url}}
106106
// API response schema depends on the provider
107-
// fiat currencies also hard coded in SettingsLayout.qml
108-
property var fiatPriceAPIs: ["usd", "eur"].reduce(function(obj, x) {
107+
property var fiatCurrencies: ["usd", "eur", "aed", "ars", "aud", "bdt", "bhd", "brl", "cad", "chf", "clp", "cny", "czk", "gbp", "hkd",
108+
"huf", "idr", "ils", "inr", "jpy", "krw", "kwd", "lkr", "mmk", "mxn", "myr", "ngn", "nok", "nzd", "php",
109+
"pkr", "pln", "rub", "sar", "sek", "sgd", "thb", "try", "twd", "uah", "vef", "vnd", "zar", "xau"];
110+
property var fiatPriceAPIs: fiatCurrencies.reduce(function(obj, x) {
109111
const key = `xmr${x}`; // e.g. xmrusd
110-
const xUp = x.toUpperCase(); // e.g. usd -> USD
111-
obj["kraken"][key] = `https://api.kraken.com/0/public/Ticker?pair=XMR${xUp}`;
112+
if (x === "usd" || x === "eur") {
113+
// Kraken only supports XMRUSD and XMREUR
114+
obj["kraken"][key] = `https://api.kraken.com/0/public/Ticker?pair=XMR${x}`;
115+
}
112116
obj["coingecko"][key] = `https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=${x}`;
113-
obj["cryptocompare"][key] = `https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=${xUp}`;
117+
obj["cryptocompare"][key] = `https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=${x}`;
114118
return obj;
115119
}, {"kraken": {}, "coingecko": {}, "cryptocompare": {}})
116-
120+
// if the user is using Kraken, the following is used if the user wants non USD/EUR
121+
property string fiatPriceBackupProvider: "coingecko"
117122
// true if wallet ever synchronized
118123
property bool walletInitialized : false
119124

@@ -1143,7 +1148,7 @@ ApplicationWindow {
11431148
if(persistentSettings.fiatPriceEnabled)
11441149
appWindow.fiatApiRefresh();
11451150
}
1146-
triggeredOnStart: false
1151+
triggeredOnStart: true
11471152
}
11481153

11491154
function fiatApiParseTicker(url, resp, currency){
@@ -1153,20 +1158,21 @@ ApplicationWindow {
11531158
appWindow.fiatApiError("Kraken API has error(s)");
11541159
return;
11551160
}
1156-
// currency is of the form xmr[a-Z]+. Replaces only starting XMR
1157-
var key = `${currency}`.replace("xmr", "xxmrz").toUpperCase();
1161+
// i.e. xmr[a-z]+ -> XXMRZ[A-Z]+
1162+
var key = `XXMRZ${currency.substring(3).toUpperCase()}`;
11581163
var ticker = resp.result[key]["c"][0];
11591164
return ticker;
11601165
} else if(url.startsWith("https://api.coingecko.com/api/v3/")){
1161-
// i.e. xmr[a-Z]+ -> [a-Z]+
1162-
var key = currency.replace("xmr", "");
1166+
// i.e. xmr[a-z]+ -> [a-z]+
1167+
var key = currency.substring(3);
11631168
if(!resp.hasOwnProperty("monero") || !resp["monero"].hasOwnProperty(key)){
11641169
appWindow.fiatApiError("Coingecko API has error(s)");
11651170
return;
11661171
}
11671172
return resp["monero"][key];
11681173
} else if(url.startsWith("https://min-api.cryptocompare.com/data/")){
1169-
var key = currency.replace("xmr", "").toUpperCase();
1174+
// i.e. xmr[a-z]+ -> [A-Z]+
1175+
var key = currency.substring(3).toUpperCase();
11701176
if(!resp.hasOwnProperty(key)){
11711177
appWindow.fiatApiError("cryptocompare API has error(s)");
11721178
return;
@@ -1237,15 +1243,15 @@ ApplicationWindow {
12371243
var provider = appWindow.fiatPriceAPIs[userProvider];
12381244
var userCurrency = persistentSettings.fiatPriceCurrency;
12391245
if(!provider.hasOwnProperty(userCurrency)){
1240-
appWindow.fiatApiError("currency \"" + userCurrency + "\" not implemented");
1246+
appWindow.fiatApiError("currency \"" + userCurrency + "\"is not supported by provider \"" + userProvider + "\"");
12411247
}
12421248

12431249
var url = provider[userCurrency];
12441250
network.getJSON(url, fiatApiJsonReceived);
12451251
}
12461252

12471253
function fiatApiCurrencySymbol() {
1248-
return persistentSettings.fiatPriceCurrency.replace("xmr", "").toUpperCase();
1254+
return persistentSettings.fiatPriceCurrency.substring(3).toUpperCase();
12491255
}
12501256

12511257
function fiatApiConvertToFiat(amount) {
@@ -1339,7 +1345,7 @@ ApplicationWindow {
13391345
}
13401346

13411347
if(persistentSettings.fiatPriceEnabled){
1342-
appWindow.fiatApiRefresh();
1348+
// appWindow.fiatApiRefresh();
13431349
appWindow.fiatTimerStart();
13441350
}
13451351

pages/settings/SettingsLayout.qml

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,20 @@ Rectangle {
185185
labelFontSize: 14
186186
dataModel: fiatPriceProvidersModel
187187
onChanged: {
188-
var obj = dataModel.get(currentIndex);
189-
persistentSettings.fiatPriceProvider = obj.data;
188+
var newProvider = dataModel.get(currentIndex).data;
189+
var providerCurrencies = appWindow.fiatPriceAPIs[newProvider];
190+
// ONLY when the user changes the provider should the currency list also update
191+
// This way, since Kraken is the default, users can see ALL supported currencies
192+
fiatPriceCurrencyModel.clear();
193+
appWindow.fiatCurrencies.forEach(el => {
194+
if (`xmr${el}` in providerCurrencies) fiatPriceCurrencyModel.append({ data: `xmr${el}`, column1: el.toUpperCase()})
195+
});
196+
// if fiatPriceCurrency is not supported by the new provider, use first available currency
197+
if (!(persistentSettings.fiatPriceCurrency in providerCurrencies)) {
198+
persistentSettings.fiatPriceCurrency = Object.keys(providerCurrencies)[0];
199+
fiatPriceCurrencyDropdown.currentIndex = 0;
200+
}
201+
persistentSettings.fiatPriceProvider = newProvider;
190202

191203
if(persistentSettings.fiatPriceEnabled)
192204
appWindow.fiatApiRefresh();
@@ -198,12 +210,18 @@ Rectangle {
198210
Layout.maximumWidth: 100
199211
labelText: qsTr("Currency") + translationManager.emptyString
200212
labelFontSize: 14
201-
currentIndex: persistentSettings.fiatPriceCurrency === "xmrusd" ? 0 : 1
213+
currentIndex: appWindow.fiatCurrencies.indexOf(persistentSettings.fiatPriceCurrency.substring(3))
202214
dataModel: fiatPriceCurrencyModel
203215
onChanged: {
204-
var obj = dataModel.get(currentIndex);
205-
persistentSettings.fiatPriceCurrency = obj.data;
206-
216+
var newCurrency = dataModel.get(currentIndex).data;
217+
if (!(newCurrency in appWindow.fiatPriceAPIs[persistentSettings.fiatPriceProvider])) {
218+
// this occurs if a fiat currency other than EUR/USD is selected and provider is Kraken
219+
// so use appWindow.fiatPriceBackupProvider instead
220+
let backupIdx = Object.keys(appWindow.fiatPriceAPIs).indexOf(appWindow.fiatPriceBackupProvider);
221+
fiatPriceProviderDropDown.currentIndex = backupIdx;
222+
persistentSettings.fiatPriceProvider = appWindow.fiatPriceBackupProvider;
223+
}
224+
persistentSettings.fiatPriceCurrency = newCurrency;
207225
if(persistentSettings.fiatPriceEnabled)
208226
appWindow.fiatApiRefresh();
209227
}
@@ -285,24 +303,17 @@ Rectangle {
285303
id: fiatPriceProvidersModel
286304
}
287305

288-
// fiat currencies also hard coded in main.qml
289306
ListModel {
290307
id: fiatPriceCurrencyModel
291-
// from https://agateau.com/2018/working-around-listmodel-limitations/
292-
Component.onCompleted: {
293-
["usd", "eur"].forEach(el => {
294-
append({
295-
data: `xmr${el}`,
296-
column1: el.toUpperCase()
297-
});
298-
});
299-
}
300308
}
301309

302310
Component.onCompleted: {
303-
// Dynamically fill fiatPrice dropdown based on `appWindow.fiatPriceAPIs`
311+
// Dynamically fill fiatPrice dropdowns based on `appWindow.fiatPriceAPIs`
304312
var apis = appWindow.fiatPriceAPIs;
305313
fiatPriceProvidersModel.clear();
314+
fiatPriceCurrencyModel.clear();
315+
// populate fiat currency dropdown
316+
appWindow.fiatCurrencies.forEach(el => fiatPriceCurrencyModel.append({ data: `xmr${el}`, column1: el.toUpperCase()}));
306317

307318
var i = 0;
308319
for (var apiProvider in apis){

0 commit comments

Comments
 (0)