From ae5371006e8323893bf966809d3af748a8e84156 Mon Sep 17 00:00:00 2001 From: Elijah Lopez Date: Mon, 7 Mar 2022 16:31:00 -0500 Subject: [PATCH] Make fiat price APIs more flexible --- main.qml | 55 +++++++++++++------------------ pages/settings/SettingsLayout.qml | 39 +++++++++++----------- 2 files changed, 42 insertions(+), 52 deletions(-) diff --git a/main.qml b/main.qml index 6e04cfe057..d58955b413 100644 --- a/main.qml +++ b/main.qml @@ -101,22 +101,18 @@ ApplicationWindow { // fiat price conversion property real fiatPrice: 0 - property var fiatPriceAPIs: { - return { - "kraken": { - "xmrusd": "https://api.kraken.com/0/public/Ticker?pair=XMRUSD", - "xmreur": "https://api.kraken.com/0/public/Ticker?pair=XMREUR" - }, - "coingecko": { - "xmrusd": "https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=usd", - "xmreur": "https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=eur" - }, - "cryptocompare": { - "xmrusd": "https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=USD", - "xmreur": "https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=EUR", - } - } - } + + // {provider name: {ticker: price_api_url}} + // API response schema depends on the provider + // fiat currencies also hard coded in SettingsLayout.qml + property var fiatPriceAPIs: ["usd", "eur"].reduce(function(obj, x) { + const key = `xmr${x}`; // e.g. xmrusd + const xUp = x.toUpperCase(); // e.g. usd -> USD + obj["kraken"][key] = `https://api.kraken.com/0/public/Ticker?pair=XMR${xUp}`; + obj["coingecko"][key] = `https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=${x}`; + obj["cryptocompare"][key] = `https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=${xUp}`; + return obj; + }, {"kraken": {}, "coingecko": {}, "cryptocompare": {}}) // true if wallet ever synchronized property bool walletInitialized : false @@ -1032,10 +1028,10 @@ ApplicationWindow { var isReserveProof = signature.indexOf("ReserveProofV") === 0; if (address.length > 0 && !isReserveProof) { result = currentWallet.checkTxProof(txid, address, message, signature); - } + } else if (isReserveProof) { result = currentWallet.checkReserveProof(address, message, signature); - } + } else { result = currentWallet.checkSpendProof(txid, message, signature); } @@ -1068,7 +1064,7 @@ ApplicationWindow { informationPopup.title = qsTr("Payment proof check") + translationManager.emptyString; informationPopup.icon = good ? StandardIcon.Information : StandardIcon.Critical; informationPopup.text = good ? qsTr("Good signature") : qsTr("Bad signature"); - } + } else if (isReserveProof && results[0] === "true") { var good = results[1] === "true"; informationPopup.title = qsTr("Reserve proof check") + translationManager.emptyString; @@ -1157,19 +1153,20 @@ ApplicationWindow { appWindow.fiatApiError("Kraken API has error(s)"); return; } - - var key = currency === "xmreur" ? "XXMRZEUR" : "XXMRZUSD"; + // currency is of the form xmr[a-Z]+. Replaces only starting XMR + var key = `${currency}`.replace("xmr", "xxmrz").toUpperCase(); var ticker = resp.result[key]["c"][0]; return ticker; } else if(url.startsWith("https://api.coingecko.com/api/v3/")){ - var key = currency === "xmreur" ? "eur" : "usd"; + // i.e. xmr[a-Z]+ -> [a-Z]+ + var key = currency.replace("xmr", ""); if(!resp.hasOwnProperty("monero") || !resp["monero"].hasOwnProperty(key)){ appWindow.fiatApiError("Coingecko API has error(s)"); return; } return resp["monero"][key]; } else if(url.startsWith("https://min-api.cryptocompare.com/data/")){ - var key = currency === "xmreur" ? "EUR" : "USD"; + var key = currency.replace("xmr", "").toUpperCase(); if(!resp.hasOwnProperty(key)){ appWindow.fiatApiError("cryptocompare API has error(s)"); return; @@ -1248,15 +1245,7 @@ ApplicationWindow { } function fiatApiCurrencySymbol() { - switch (persistentSettings.fiatPriceCurrency) { - case "xmrusd": - return "USD"; - case "xmreur": - return "EUR"; - default: - console.error("unsupported currency", persistentSettings.fiatPriceCurrency); - return "UNSUPPORTED"; - } + return persistentSettings.fiatPriceCurrency.replace("xmr", "").toUpperCase(); } function fiatApiConvertToFiat(amount) { @@ -2163,7 +2152,7 @@ ApplicationWindow { console.log("close accepted"); // Close wallet non async on exit daemonManager.exit(); - + closeWallet(Qt.quit); } diff --git a/pages/settings/SettingsLayout.qml b/pages/settings/SettingsLayout.qml index 0e54f018ee..cc4ee3b0ad 100644 --- a/pages/settings/SettingsLayout.qml +++ b/pages/settings/SettingsLayout.qml @@ -1,21 +1,21 @@ // Copyright (c) 2014-2018, The Monero Project -// +// // All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: -// +// // 1. Redistributions of source code must retain the above copyright notice, this list of // conditions and the following disclaimer. -// +// // 2. Redistributions in binary form must reproduce the above copyright notice, this list // of conditions and the following disclaimer in the documentation and/or other // materials provided with the distribution. -// +// // 3. Neither the name of the copyright holder nor the names of its contributors may be // used to endorse or promote products derived from this software without specific // prior written permission. -// +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL @@ -91,7 +91,7 @@ Rectangle { MoneroComponents.Style.blackTheme = !MoneroComponents.Style.blackTheme; } } - + MoneroComponents.CheckBox { checked: persistentSettings.askPasswordBeforeSending text: qsTr("Ask for password before sending a transaction") + translationManager.emptyString @@ -285,15 +285,17 @@ Rectangle { id: fiatPriceProvidersModel } + // fiat currencies also hard coded in main.qml ListModel { id: fiatPriceCurrencyModel - ListElement { - data: "xmrusd" - column1: "USD" - } - ListElement { - data: "xmreur" - column1: "EUR" + // from https://agateau.com/2018/working-around-listmodel-limitations/ + Component.onCompleted: { + ["usd", "eur"].forEach(el => { + append({ + data: `xmr${el}`, + column1: el.toUpperCase() + }); + }); } } @@ -303,13 +305,13 @@ Rectangle { fiatPriceProvidersModel.clear(); var i = 0; - for (var api in apis){ - if (!apis.hasOwnProperty(api)) + for (var apiProvider in apis){ + if (!apis.hasOwnProperty(apiProvider)) continue; - fiatPriceProvidersModel.append({"column1": Utils.capitalize(api), "data": api}); + fiatPriceProvidersModel.append({"column1": Utils.capitalize(apiProvider), "data": apiProvider}); - if(api === persistentSettings.fiatPriceProvider) + if(apiProvider === persistentSettings.fiatPriceProvider) fiatPriceProviderDropDown.currentIndex = i; i += 1; } @@ -317,4 +319,3 @@ Rectangle { console.log('SettingsLayout loaded'); } } -