Skip to content

Commit ae53710

Browse files
committed
Make fiat price APIs more flexible
1 parent ea92f3f commit ae53710

File tree

2 files changed

+42
-52
lines changed

2 files changed

+42
-52
lines changed

main.qml

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,18 @@ ApplicationWindow {
101101

102102
// fiat price conversion
103103
property real fiatPrice: 0
104-
property var fiatPriceAPIs: {
105-
return {
106-
"kraken": {
107-
"xmrusd": "https://api.kraken.com/0/public/Ticker?pair=XMRUSD",
108-
"xmreur": "https://api.kraken.com/0/public/Ticker?pair=XMREUR"
109-
},
110-
"coingecko": {
111-
"xmrusd": "https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=usd",
112-
"xmreur": "https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=eur"
113-
},
114-
"cryptocompare": {
115-
"xmrusd": "https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=USD",
116-
"xmreur": "https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=EUR",
117-
}
118-
}
119-
}
104+
105+
// {provider name: {ticker: price_api_url}}
106+
// 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) {
109+
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+
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}`;
114+
return obj;
115+
}, {"kraken": {}, "coingecko": {}, "cryptocompare": {}})
120116

121117
// true if wallet ever synchronized
122118
property bool walletInitialized : false
@@ -1032,10 +1028,10 @@ ApplicationWindow {
10321028
var isReserveProof = signature.indexOf("ReserveProofV") === 0;
10331029
if (address.length > 0 && !isReserveProof) {
10341030
result = currentWallet.checkTxProof(txid, address, message, signature);
1035-
}
1031+
}
10361032
else if (isReserveProof) {
10371033
result = currentWallet.checkReserveProof(address, message, signature);
1038-
}
1034+
}
10391035
else {
10401036
result = currentWallet.checkSpendProof(txid, message, signature);
10411037
}
@@ -1068,7 +1064,7 @@ ApplicationWindow {
10681064
informationPopup.title = qsTr("Payment proof check") + translationManager.emptyString;
10691065
informationPopup.icon = good ? StandardIcon.Information : StandardIcon.Critical;
10701066
informationPopup.text = good ? qsTr("Good signature") : qsTr("Bad signature");
1071-
}
1067+
}
10721068
else if (isReserveProof && results[0] === "true") {
10731069
var good = results[1] === "true";
10741070
informationPopup.title = qsTr("Reserve proof check") + translationManager.emptyString;
@@ -1157,19 +1153,20 @@ ApplicationWindow {
11571153
appWindow.fiatApiError("Kraken API has error(s)");
11581154
return;
11591155
}
1160-
1161-
var key = currency === "xmreur" ? "XXMRZEUR" : "XXMRZUSD";
1156+
// currency is of the form xmr[a-Z]+. Replaces only starting XMR
1157+
var key = `${currency}`.replace("xmr", "xxmrz").toUpperCase();
11621158
var ticker = resp.result[key]["c"][0];
11631159
return ticker;
11641160
} else if(url.startsWith("https://api.coingecko.com/api/v3/")){
1165-
var key = currency === "xmreur" ? "eur" : "usd";
1161+
// i.e. xmr[a-Z]+ -> [a-Z]+
1162+
var key = currency.replace("xmr", "");
11661163
if(!resp.hasOwnProperty("monero") || !resp["monero"].hasOwnProperty(key)){
11671164
appWindow.fiatApiError("Coingecko API has error(s)");
11681165
return;
11691166
}
11701167
return resp["monero"][key];
11711168
} else if(url.startsWith("https://min-api.cryptocompare.com/data/")){
1172-
var key = currency === "xmreur" ? "EUR" : "USD";
1169+
var key = currency.replace("xmr", "").toUpperCase();
11731170
if(!resp.hasOwnProperty(key)){
11741171
appWindow.fiatApiError("cryptocompare API has error(s)");
11751172
return;
@@ -1248,15 +1245,7 @@ ApplicationWindow {
12481245
}
12491246

12501247
function fiatApiCurrencySymbol() {
1251-
switch (persistentSettings.fiatPriceCurrency) {
1252-
case "xmrusd":
1253-
return "USD";
1254-
case "xmreur":
1255-
return "EUR";
1256-
default:
1257-
console.error("unsupported currency", persistentSettings.fiatPriceCurrency);
1258-
return "UNSUPPORTED";
1259-
}
1248+
return persistentSettings.fiatPriceCurrency.replace("xmr", "").toUpperCase();
12601249
}
12611250

12621251
function fiatApiConvertToFiat(amount) {
@@ -2163,7 +2152,7 @@ ApplicationWindow {
21632152
console.log("close accepted");
21642153
// Close wallet non async on exit
21652154
daemonManager.exit();
2166-
2155+
21672156
closeWallet(Qt.quit);
21682157
}
21692158

pages/settings/SettingsLayout.qml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Copyright (c) 2014-2018, The Monero Project
2-
//
2+
//
33
// All rights reserved.
4-
//
4+
//
55
// Redistribution and use in source and binary forms, with or without modification, are
66
// permitted provided that the following conditions are met:
7-
//
7+
//
88
// 1. Redistributions of source code must retain the above copyright notice, this list of
99
// conditions and the following disclaimer.
10-
//
10+
//
1111
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
1212
// of conditions and the following disclaimer in the documentation and/or other
1313
// materials provided with the distribution.
14-
//
14+
//
1515
// 3. Neither the name of the copyright holder nor the names of its contributors may be
1616
// used to endorse or promote products derived from this software without specific
1717
// prior written permission.
18-
//
18+
//
1919
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
2020
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2121
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
@@ -91,7 +91,7 @@ Rectangle {
9191
MoneroComponents.Style.blackTheme = !MoneroComponents.Style.blackTheme;
9292
}
9393
}
94-
94+
9595
MoneroComponents.CheckBox {
9696
checked: persistentSettings.askPasswordBeforeSending
9797
text: qsTr("Ask for password before sending a transaction") + translationManager.emptyString
@@ -285,15 +285,17 @@ Rectangle {
285285
id: fiatPriceProvidersModel
286286
}
287287

288+
// fiat currencies also hard coded in main.qml
288289
ListModel {
289290
id: fiatPriceCurrencyModel
290-
ListElement {
291-
data: "xmrusd"
292-
column1: "USD"
293-
}
294-
ListElement {
295-
data: "xmreur"
296-
column1: "EUR"
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+
});
297299
}
298300
}
299301

@@ -303,18 +305,17 @@ Rectangle {
303305
fiatPriceProvidersModel.clear();
304306

305307
var i = 0;
306-
for (var api in apis){
307-
if (!apis.hasOwnProperty(api))
308+
for (var apiProvider in apis){
309+
if (!apis.hasOwnProperty(apiProvider))
308310
continue;
309311

310-
fiatPriceProvidersModel.append({"column1": Utils.capitalize(api), "data": api});
312+
fiatPriceProvidersModel.append({"column1": Utils.capitalize(apiProvider), "data": apiProvider});
311313

312-
if(api === persistentSettings.fiatPriceProvider)
314+
if(apiProvider === persistentSettings.fiatPriceProvider)
313315
fiatPriceProviderDropDown.currentIndex = i;
314316
i += 1;
315317
}
316318

317319
console.log('SettingsLayout loaded');
318320
}
319321
}
320-

0 commit comments

Comments
 (0)