Skip to content

Commit

Permalink
Make fiat price APIs more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
elibroftw committed Mar 7, 2022
1 parent ea92f3f commit ae53710
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 52 deletions.
55 changes: 22 additions & 33 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -2163,7 +2152,7 @@ ApplicationWindow {
console.log("close accepted");
// Close wallet non async on exit
daemonManager.exit();

closeWallet(Qt.quit);
}

Expand Down
39 changes: 20 additions & 19 deletions pages/settings/SettingsLayout.qml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
});
});
}
}

Expand All @@ -303,18 +305,17 @@ 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;
}

console.log('SettingsLayout loaded');
}
}

0 comments on commit ae53710

Please sign in to comment.