Skip to content

Commit

Permalink
Add more fiat currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
elibroftw committed Mar 26, 2022
1 parent 562162f commit 74b1519
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
36 changes: 21 additions & 15 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,21 @@ ApplicationWindow {

// {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) {
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
const xUp = x.toUpperCase(); // e.g. usd -> USD
obj["kraken"][key] = `https://api.kraken.com/0/public/Ticker?pair=XMR${xUp}`;
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=${xUp}`;
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
property bool walletInitialized : false

Expand Down Expand Up @@ -1143,7 +1148,7 @@ ApplicationWindow {
if(persistentSettings.fiatPriceEnabled)
appWindow.fiatApiRefresh();
}
triggeredOnStart: false
triggeredOnStart: true
}

function fiatApiParseTicker(url, resp, currency){
Expand All @@ -1153,20 +1158,21 @@ ApplicationWindow {
appWindow.fiatApiError("Kraken API has error(s)");
return;
}
// currency is of the form xmr[a-Z]+. Replaces only starting XMR
var key = `${currency}`.replace("xmr", "xxmrz").toUpperCase();
// i.e. xmr[a-z]+ -> XXMRZ[A-Z]+
var key = `XXMRZ${currency.substring(3).toUpperCase()}`;
var ticker = resp.result[key]["c"][0];
return ticker;
} else if(url.startsWith("https://api.coingecko.com/api/v3/")){
// i.e. xmr[a-Z]+ -> [a-Z]+
var key = currency.replace("xmr", "");
// i.e. xmr[a-z]+ -> [a-z]+
var key = currency.substring(3);
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.replace("xmr", "").toUpperCase();
// i.e. xmr[a-z]+ -> [A-Z]+
var key = currency.substring(3).toUpperCase();
if(!resp.hasOwnProperty(key)){
appWindow.fiatApiError("cryptocompare API has error(s)");
return;
Expand Down Expand Up @@ -1237,15 +1243,15 @@ ApplicationWindow {
var provider = appWindow.fiatPriceAPIs[userProvider];
var userCurrency = persistentSettings.fiatPriceCurrency;
if(!provider.hasOwnProperty(userCurrency)){
appWindow.fiatApiError("currency \"" + userCurrency + "\" not implemented");
appWindow.fiatApiError("currency \"" + userCurrency + "\"is not supported by provider \"" + userProvider + "\"");
}

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

function fiatApiCurrencySymbol() {
return persistentSettings.fiatPriceCurrency.replace("xmr", "").toUpperCase();
return persistentSettings.fiatPriceCurrency.substring(3).toUpperCase();
}

function fiatApiConvertToFiat(amount) {
Expand Down Expand Up @@ -1339,7 +1345,7 @@ ApplicationWindow {
}

if(persistentSettings.fiatPriceEnabled){
appWindow.fiatApiRefresh();
// appWindow.fiatApiRefresh();
appWindow.fiatTimerStart();
}

Expand Down
45 changes: 28 additions & 17 deletions pages/settings/SettingsLayout.qml
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,20 @@ Rectangle {
labelFontSize: 14
dataModel: fiatPriceProvidersModel
onChanged: {
var obj = dataModel.get(currentIndex);
persistentSettings.fiatPriceProvider = obj.data;
var newProvider = dataModel.get(currentIndex).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;

if(persistentSettings.fiatPriceEnabled)
appWindow.fiatApiRefresh();
Expand All @@ -198,12 +210,18 @@ Rectangle {
Layout.maximumWidth: 100
labelText: qsTr("Currency") + translationManager.emptyString
labelFontSize: 14
currentIndex: persistentSettings.fiatPriceCurrency === "xmrusd" ? 0 : 1
currentIndex: appWindow.fiatCurrencies.indexOf(persistentSettings.fiatPriceCurrency.substring(3))
dataModel: fiatPriceCurrencyModel
onChanged: {
var obj = dataModel.get(currentIndex);
persistentSettings.fiatPriceCurrency = obj.data;

var newCurrency = dataModel.get(currentIndex).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)
appWindow.fiatApiRefresh();
}
Expand Down Expand Up @@ -285,24 +303,17 @@ Rectangle {
id: fiatPriceProvidersModel
}

// fiat currencies also hard coded in main.qml
ListModel {
id: fiatPriceCurrencyModel
// from https://agateau.com/2018/working-around-listmodel-limitations/
Component.onCompleted: {
["usd", "eur"].forEach(el => {
append({
data: `xmr${el}`,
column1: el.toUpperCase()
});
});
}
}

Component.onCompleted: {
// Dynamically fill fiatPrice dropdown based on `appWindow.fiatPriceAPIs`
// Dynamically fill fiatPrice dropdowns based on `appWindow.fiatPriceAPIs`
var apis = appWindow.fiatPriceAPIs;
fiatPriceProvidersModel.clear();
fiatPriceCurrencyModel.clear();
// populate fiat currency dropdown
appWindow.fiatCurrencies.forEach(el => fiatPriceCurrencyModel.append({ data: `xmr${el}`, column1: el.toUpperCase()}));

var i = 0;
for (var apiProvider in apis){
Expand Down

0 comments on commit 74b1519

Please sign in to comment.