Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add More Fiat Currencies #3852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 30 additions & 32 deletions main.qml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,23 +102,24 @@ ApplicationWindow {


// fiat price conversion // fiat price conversion
property real fiatPrice: 0 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
property var fiatCurrencies: ["usd", "eur", "aed", "ars", "aud", "bdt", "bhd", "brl", "cad", "chf", "clp", "cny", "czk", "gbp", "hkd",
"huf", "idr", "ils", "inr", "jpy", "krw", "kwd", "lkr", "mmk", "mxn", "myr", "ngn", "nok", "nzd", "php",
"pkr", "pln", "rub", "sar", "sek", "sgd", "thb", "try", "twd", "uah", "vef", "vnd", "zar", "xau"];
property var fiatPriceAPIs: fiatCurrencies.reduce(function(obj, x) {
const key = `xmr${x}`; // e.g. xmrusd
if (x === "usd" || x === "eur") {
// Kraken only supports XMRUSD and XMREUR
obj["kraken"][key] = `https://api.kraken.com/0/public/Ticker?pair=XMR${x}`;
}
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=${x}`;
return obj;
}, {"kraken": {}, "coingecko": {}, "cryptocompare": {}})
// if the user is using Kraken, the following is used if the user wants non USD/EUR
property string fiatPriceBackupProvider: "coingecko"
// true if wallet ever synchronized // true if wallet ever synchronized
property bool walletInitialized : false property bool walletInitialized : false


Expand Down Expand Up @@ -1029,11 +1030,9 @@ ApplicationWindow {
var isReserveProof = signature.indexOf("ReserveProofV") === 0; var isReserveProof = signature.indexOf("ReserveProofV") === 0;
if (address.length > 0 && !isReserveProof) { if (address.length > 0 && !isReserveProof) {
result = currentWallet.checkTxProof(txid, address, message, signature); result = currentWallet.checkTxProof(txid, address, message, signature);
} } else if (isReserveProof) {
else if (isReserveProof) {
result = currentWallet.checkReserveProof(address, message, signature); result = currentWallet.checkReserveProof(address, message, signature);
} } else {
else {
result = currentWallet.checkSpendProof(txid, message, signature); result = currentWallet.checkSpendProof(txid, message, signature);
} }
var results = result.split("|"); var results = result.split("|");
Expand Down Expand Up @@ -1152,19 +1151,21 @@ ApplicationWindow {
appWindow.fiatApiError("Kraken API has error(s)"); appWindow.fiatApiError("Kraken API has error(s)");
return; return;
} }

// i.e. xmr[a-z]+ -> XXMRZ[A-Z]+
var key = currency === "xmreur" ? "XXMRZEUR" : "XXMRZUSD"; var key = `XXMRZ${currency.substring(3).toUpperCase()}`;
var ticker = resp.result[key]["c"][0]; var ticker = resp.result[key]["c"][0];
return ticker; return ticker;
} else if(url.startsWith("https://api.coingecko.com/api/v3/")){ } 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.substring(3);
if(!resp.hasOwnProperty("monero") || !resp["monero"].hasOwnProperty(key)){ if(!resp.hasOwnProperty("monero") || !resp["monero"].hasOwnProperty(key)){
appWindow.fiatApiError("Coingecko API has error(s)"); appWindow.fiatApiError("Coingecko API has error(s)");
return; return;
} }
return resp["monero"][key]; return resp["monero"][key];
} else if(url.startsWith("https://min-api.cryptocompare.com/data/")){ } else if(url.startsWith("https://min-api.cryptocompare.com/data/")){
var key = currency === "xmreur" ? "EUR" : "USD"; // i.e. xmr[a-z]+ -> [A-Z]+
var key = currency.substring(3).toUpperCase();
if(!resp.hasOwnProperty(key)){ if(!resp.hasOwnProperty(key)){
appWindow.fiatApiError("cryptocompare API has error(s)"); appWindow.fiatApiError("cryptocompare API has error(s)");
return; return;
Expand Down Expand Up @@ -1235,24 +1236,21 @@ ApplicationWindow {
var provider = appWindow.fiatPriceAPIs[userProvider]; var provider = appWindow.fiatPriceAPIs[userProvider];
var userCurrency = persistentSettings.fiatPriceCurrency; var userCurrency = persistentSettings.fiatPriceCurrency;
if(!provider.hasOwnProperty(userCurrency)){ if(!provider.hasOwnProperty(userCurrency)){
appWindow.fiatApiError("currency \"" + userCurrency + "\" not implemented"); appWindow.fiatApiError("currency \"" + userCurrency + "\"is not supported by provider \"" + userProvider + "\"");
} }


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


function fiatApiCurrencySymbol() { function fiatApiCurrencySymbol() {
switch (persistentSettings.fiatPriceCurrency) { let currency = persistentSettings.fiatPriceCurrency.substring(3);
case "xmrusd": if (fiatCurrencies.indexOf(currency) !== -1) {
return "USD"; return currency.toUpperCase();
case "xmreur": }
return "EUR";
default:
console.error("unsupported currency", persistentSettings.fiatPriceCurrency); console.error("unsupported currency", persistentSettings.fiatPriceCurrency);
elibroftw marked this conversation as resolved.
Show resolved Hide resolved
return "UNSUPPORTED"; return "UNSUPPORTED";
} }
}


function fiatApiConvertToFiat(amount) { function fiatApiConvertToFiat(amount) {
const ticker = appWindow.fiatPrice; const ticker = appWindow.fiatPrice;
Expand Down
56 changes: 35 additions & 21 deletions pages/settings/SettingsLayout.qml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -190,9 +190,21 @@ Rectangle {
labelFontSize: 14 labelFontSize: 14
dataModel: fiatPriceProvidersModel dataModel: fiatPriceProvidersModel
onChanged: { onChanged: {
var obj = dataModel.get(currentIndex); var newProvider = dataModel.get(currentIndex).data;
persistentSettings.fiatPriceProvider = obj.data; var providerCurrencies = appWindow.fiatPriceAPIs[newProvider];

// ONLY when the user changes the provider should the currency list also update
// This way, since Kraken is the default, users can see ALL supported currencies
fiatPriceCurrencyModel.clear();
appWindow.fiatCurrencies.forEach(el => {
if (`xmr${el}` in providerCurrencies) fiatPriceCurrencyModel.append({ data: `xmr${el}`, column1: el.toUpperCase()})
});
// if fiatPriceCurrency is not supported by the new provider, use first available currency
if (!(persistentSettings.fiatPriceCurrency in providerCurrencies)) {
persistentSettings.fiatPriceCurrency = Object.keys(providerCurrencies)[0];
fiatPriceCurrencyDropdown.currentIndex = 0;
}
persistentSettings.fiatPriceProvider = newProvider;
// refresh price after validating that the fiat currency is provided by the provider
if(persistentSettings.fiatPriceEnabled) if(persistentSettings.fiatPriceEnabled)
appWindow.fiatApiRefresh(); appWindow.fiatApiRefresh();
} }
Expand All @@ -203,12 +215,18 @@ Rectangle {
Layout.maximumWidth: 100 Layout.maximumWidth: 100
labelText: qsTr("Currency") + translationManager.emptyString labelText: qsTr("Currency") + translationManager.emptyString
labelFontSize: 14 labelFontSize: 14
currentIndex: persistentSettings.fiatPriceCurrency === "xmrusd" ? 0 : 1 currentIndex: appWindow.fiatCurrencies.indexOf(persistentSettings.fiatPriceCurrency.substring(3))
dataModel: fiatPriceCurrencyModel dataModel: fiatPriceCurrencyModel
onChanged: { onChanged: {
var obj = dataModel.get(currentIndex); var newCurrency = dataModel.get(currentIndex).data;
persistentSettings.fiatPriceCurrency = obj.data; if (!(newCurrency in appWindow.fiatPriceAPIs[persistentSettings.fiatPriceProvider])) {

// this occurs if a fiat currency other than EUR/USD is selected and provider is Kraken
// so use appWindow.fiatPriceBackupProvider instead
let backupIdx = Object.keys(appWindow.fiatPriceAPIs).indexOf(appWindow.fiatPriceBackupProvider);
fiatPriceProviderDropDown.currentIndex = backupIdx;
persistentSettings.fiatPriceProvider = appWindow.fiatPriceBackupProvider;
}
persistentSettings.fiatPriceCurrency = newCurrency;
if(persistentSettings.fiatPriceEnabled) if(persistentSettings.fiatPriceEnabled)
appWindow.fiatApiRefresh(); appWindow.fiatApiRefresh();
} }
Expand Down Expand Up @@ -290,34 +308,30 @@ Rectangle {


ListModel { ListModel {
id: fiatPriceCurrencyModel id: fiatPriceCurrencyModel
ListElement { Component.onCompleted: {
data: "xmrusd" // populate with fiat currencies
column1: "USD" appWindow.fiatCurrencies.forEach(el => {
} fiatPriceCurrencyModel.append({ data: `xmr${el}`, column1: el.toUpperCase()});
ListElement { });
data: "xmreur"
column1: "EUR"
} }
} }


Component.onCompleted: { Component.onCompleted: {
// Dynamically fill fiatPrice dropdown based on `appWindow.fiatPriceAPIs` // Dynamically fill fiatPrice dropdowns based on `appWindow.fiatPriceAPIs`
var apis = appWindow.fiatPriceAPIs; var apis = appWindow.fiatPriceAPIs;
fiatPriceProvidersModel.clear(); fiatPriceProvidersModel.clear();

var i = 0; var i = 0;
for (var api in apis){ for (var apiProvider in apis){
if (!apis.hasOwnProperty(api)) if (!apis.hasOwnProperty(apiProvider))
continue; continue;


fiatPriceProvidersModel.append({"column1": Utils.capitalize(api), "data": api}); fiatPriceProvidersModel.append({data: apiProvider, column1: Utils.capitalize(apiProvider)});


if(api === persistentSettings.fiatPriceProvider) if(apiProvider === persistentSettings.fiatPriceProvider)
fiatPriceProviderDropDown.currentIndex = i; fiatPriceProviderDropDown.currentIndex = i;
i += 1; i += 1;
} }


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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert all the unnecessary white space changes to reduce the diffs of this PR please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just let it be.

Loading