main: add more fiat currency options, rework api logic
Co-authored-by: plowsof <[email protected]>
This commit is contained in:
3 files changed
+202
-137
No files matched your search
@@ -28,6 +28,7 @@
|
||||
|
||||
import QtQuick 2.9
|
||||
import QtQuick.Controls 2.2
|
||||
import QtQuick.Window 2.2
|
||||
import QtGraphicalEffects 1.0
|
||||
import FontAwesome 1.0
|
||||
import QtQuick.Layouts 1.1
|
||||
@@ -57,6 +58,7 @@ ColumnLayout {
|
||||
property int labelFontSize: 14
|
||||
property bool labelFontBold: false
|
||||
property int dropdownHeight: 39
|
||||
property int maxDropdownItems: 0
|
||||
property int fontSize: 14
|
||||
property int fontItemSize: 14
|
||||
property string colorBorder: MoneroComponents.Style.inputBorderColorInActive
|
||||
@@ -64,6 +66,20 @@ ColumnLayout {
|
||||
property bool headerBorder: true
|
||||
property bool headerFontBold: false
|
||||
|
||||
property real maxAvailableDropdownHeight: Number.POSITIVE_INFINITY
|
||||
|
||||
function updateMaxAvailableDropdownHeight() {
|
||||
var itemCap = dropdown.maxDropdownItems > 0 ? dropdown.maxDropdownItems * dropdown.dropdownHeight * 0.75 : Number.POSITIVE_INFINITY
|
||||
var win = dropdown.Window.window
|
||||
if (!win) {
|
||||
maxAvailableDropdownHeight = itemCap
|
||||
return
|
||||
}
|
||||
var headBottom = head.mapToItem(null, 0, head.height).y
|
||||
var spaceCap = Math.max(dropdown.dropdownHeight * 0.75, win.height - headBottom - 8)
|
||||
maxAvailableDropdownHeight = Math.min(itemCap, spaceCap)
|
||||
}
|
||||
|
||||
signal changed();
|
||||
|
||||
onExpandedChanged: if(expanded) appWindow.currentItem = dropdown
|
||||
@@ -135,7 +151,14 @@ ColumnLayout {
|
||||
MouseArea {
|
||||
id: dropArea
|
||||
anchors.fill: parent
|
||||
onClicked: dropdown.expanded ? popup.close() : popup.open()
|
||||
onClicked: {
|
||||
if (dropdown.expanded) {
|
||||
popup.close()
|
||||
} else {
|
||||
dropdown.updateMaxAvailableDropdownHeight()
|
||||
popup.open()
|
||||
}
|
||||
}
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.ArrowCursor
|
||||
}
|
||||
@@ -152,72 +175,80 @@ ColumnLayout {
|
||||
width: dropdown.width
|
||||
y: head.y + head.height
|
||||
clip: true
|
||||
height: dropdown.expanded ? columnid.height : 0
|
||||
height: dropdown.expanded ? Math.min(columnid.height, dropdown.maxAvailableDropdownHeight) : 0
|
||||
color: dropdown.pressedColor
|
||||
|
||||
Behavior on height {
|
||||
NumberAnimation { duration: 100; easing.type: Easing.InQuad }
|
||||
}
|
||||
|
||||
Column {
|
||||
id: columnid
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
anchors.top: parent.top
|
||||
property int currentIndex: 0
|
||||
Flickable {
|
||||
anchors.fill: parent
|
||||
contentHeight: columnid.height
|
||||
interactive: contentHeight > height
|
||||
boundsBehavior: Flickable.StopAtBounds
|
||||
clip: true
|
||||
ScrollBar.vertical: ScrollBar { }
|
||||
|
||||
Repeater {
|
||||
id: repeater
|
||||
Column {
|
||||
id: columnid
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
anchors.top: parent.top
|
||||
property int currentIndex: 0
|
||||
|
||||
// Workaround for translations in listElements. All translated strings needs to be listed in this file.
|
||||
property string stringAutomatic: qsTr("Automatic") + translationManager.emptyString
|
||||
property string stringSlow: qsTr("Slow (x0.2 fee)") + translationManager.emptyString
|
||||
property string stringNormal: qsTr("Normal (x1 fee)") + translationManager.emptyString
|
||||
property string stringFast: qsTr("Fast (x5 fee)") + translationManager.emptyString
|
||||
property string stringFastest: qsTr("Fastest (x200 fee)") + translationManager.emptyString
|
||||
Repeater {
|
||||
id: repeater
|
||||
|
||||
delegate: Rectangle {
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
height: (dropdown.dropdownHeight * 0.75)
|
||||
//radius: index === repeater.count - 1 ? 4 : 0
|
||||
color: itemArea.containsMouse || index === columnid.currentIndex || itemArea.containsMouse ? dropdown.releasedColor : dropdown.pressedColor
|
||||
property string stringAutomatic: qsTr("Automatic") + translationManager.emptyString
|
||||
property string stringSlow: qsTr("Slow (x0.2 fee)") + translationManager.emptyString
|
||||
property string stringNormal: qsTr("Normal (x1 fee)") + translationManager.emptyString
|
||||
property string stringFast: qsTr("Fast (x5 fee)") + translationManager.emptyString
|
||||
property string stringFastest: qsTr("Fastest (x200 fee)") + translationManager.emptyString
|
||||
|
||||
MoneroComponents.TextPlain {
|
||||
id: col1Text
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
delegate: Rectangle {
|
||||
anchors.left: parent.left
|
||||
anchors.right: col2Text.left
|
||||
anchors.leftMargin: 12
|
||||
anchors.rightMargin: 0
|
||||
font.family: MoneroComponents.Style.fontRegular.name
|
||||
font.bold: false
|
||||
font.pixelSize: fontItemSize
|
||||
color: itemArea.containsMouse || index === columnid.currentIndex || itemArea.containsMouse ? "#FA6800" : "#FFFFFF"
|
||||
text: qsTr(column1) + translationManager.emptyString
|
||||
}
|
||||
|
||||
MoneroComponents.TextPlain {
|
||||
id: col2Text
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 45
|
||||
font.family: MoneroComponents.Style.fontRegular.name
|
||||
font.pixelSize: 14
|
||||
color: "#FFFFFF"
|
||||
text: ""
|
||||
}
|
||||
height: (dropdown.dropdownHeight * 0.75)
|
||||
//radius: index === repeater.count - 1 ? 4 : 0
|
||||
color: itemArea.containsMouse || index === columnid.currentIndex || itemArea.containsMouse ? dropdown.releasedColor : dropdown.pressedColor
|
||||
|
||||
MouseArea {
|
||||
id: itemArea
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.ArrowCursor
|
||||
MoneroComponents.TextPlain {
|
||||
id: col1Text
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
anchors.left: parent.left
|
||||
anchors.right: col2Text.left
|
||||
anchors.leftMargin: 12
|
||||
anchors.rightMargin: 0
|
||||
font.family: MoneroComponents.Style.fontRegular.name
|
||||
font.bold: false
|
||||
font.pixelSize: fontItemSize
|
||||
color: itemArea.containsMouse || index === columnid.currentIndex || itemArea.containsMouse ? "#FA6800" : "#FFFFFF"
|
||||
text: qsTr(column1) + translationManager.emptyString
|
||||
}
|
||||
|
||||
onClicked: {
|
||||
popup.close()
|
||||
columnid.currentIndex = index
|
||||
changed();
|
||||
MoneroComponents.TextPlain {
|
||||
id: col2Text
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 45
|
||||
font.family: MoneroComponents.Style.fontRegular.name
|
||||
font.pixelSize: 14
|
||||
color: "#FFFFFF"
|
||||
text: ""
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: itemArea
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.ArrowCursor
|
||||
|
||||
onClicked: {
|
||||
popup.close()
|
||||
columnid.currentIndex = index
|
||||
changed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,20 +105,20 @@ ApplicationWindow {
|
||||
|
||||
// fiat price conversion
|
||||
property real fiatPrice: 0
|
||||
property real fiatPriceUSD: 0
|
||||
property var fiatRates: ({"USD": 1}) // units of each currency per 1 USD
|
||||
property real fiatRatesTimestamp: 0
|
||||
readonly property real fiatRatesMaxAge: 60 * 60 * 1000
|
||||
readonly property string fiatRatesAPI: "https://api.frankfurter.dev/v1/latest?base=USD"
|
||||
readonly property var fiatCurrencies: [
|
||||
"USD", "EUR", "GBP", "AUD", "BRL", "CAD", "CHF", "CNY", "CZK", "DKK",
|
||||
"HKD", "HUF", "IDR", "ILS", "INR", "ISK", "JPY", "KRW", "MXN", "MYR",
|
||||
"NOK", "NZD", "PHP", "PLN", "RON", "SEK", "SGD", "THB", "TRY", "ZAR"
|
||||
]
|
||||
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",
|
||||
}
|
||||
"kraken": "https://api.kraken.com/0/public/Ticker?pair=XMRUSD",
|
||||
"coinpaprika": "https://api.coinpaprika.com/v1/tickers/xmr-monero"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1226,55 +1226,37 @@ ApplicationWindow {
|
||||
|
||||
Timer {
|
||||
id: fiatPriceTimer
|
||||
interval: 1000 * 60;
|
||||
interval: 1000 * 60 * 5;
|
||||
running: persistentSettings.fiatPriceEnabled && currentWallet !== undefined
|
||||
repeat: true
|
||||
onTriggered: appWindow.fiatApiRefresh()
|
||||
triggeredOnStart: true
|
||||
}
|
||||
|
||||
function fiatApiParseTicker(url, resp, currency){
|
||||
// parse & validate incoming JSON
|
||||
function getKeyFromCurrency(currency) {
|
||||
if (currency.length !== 6 || currency.substring(0, 3) !== "xmr") return undefined;
|
||||
return currency.substring(3);
|
||||
}
|
||||
|
||||
function fiatApiParseTicker(url, resp){
|
||||
if(url.startsWith("https://api.kraken.com/0/")){
|
||||
if(resp.hasOwnProperty("error") && resp.error.length > 0 || !resp.hasOwnProperty("result")){
|
||||
appWindow.fiatApiError("Kraken API has error(s)");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = currency === "xmreur" ? "XXMRZEUR" : "XXMRZUSD";
|
||||
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";
|
||||
if(!resp.hasOwnProperty("monero") || !resp["monero"].hasOwnProperty(key)){
|
||||
appWindow.fiatApiError("Coingecko API has error(s)");
|
||||
var key = "XXMRZUSD";
|
||||
if(!resp.result.hasOwnProperty(key)){
|
||||
appWindow.fiatApiError("Kraken 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";
|
||||
if(!resp.hasOwnProperty(key)){
|
||||
appWindow.fiatApiError("cryptocompare API has error(s)");
|
||||
return parseFloat(resp.result[key]["c"][0]);
|
||||
} else if(url.startsWith("https://api.coinpaprika.com/v1/")){
|
||||
if(!resp.hasOwnProperty("quotes") || !resp.quotes.hasOwnProperty("USD") || !resp.quotes["USD"].hasOwnProperty("price")){
|
||||
appWindow.fiatApiError("CoinPaprika API has error(s)");
|
||||
return;
|
||||
}
|
||||
return resp[key];
|
||||
}
|
||||
}
|
||||
|
||||
function fiatApiGetCurrency(url) {
|
||||
var apis = appWindow.fiatPriceAPIs;
|
||||
for (var api in apis){
|
||||
if (!apis.hasOwnProperty(api))
|
||||
continue;
|
||||
|
||||
for (var cur in apis[api]){
|
||||
if(!apis[api].hasOwnProperty(cur))
|
||||
continue;
|
||||
|
||||
if (apis[api][cur] === url) {
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
return resp.quotes["USD"]["price"];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1291,20 +1273,52 @@ ApplicationWindow {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle incoming JSON, set ticker
|
||||
var currency = appWindow.fiatApiGetCurrency(url);
|
||||
if(typeof currency == "undefined"){
|
||||
appWindow.fiatApiError("could not get currency");
|
||||
return;
|
||||
}
|
||||
|
||||
var ticker = appWindow.fiatApiParseTicker(url, resp, currency);
|
||||
if(ticker <= 0){
|
||||
var ticker = appWindow.fiatApiParseTicker(url, resp);
|
||||
if(!(ticker > 0)){
|
||||
appWindow.fiatApiError("could not get ticker");
|
||||
return;
|
||||
}
|
||||
|
||||
appWindow.fiatPrice = ticker;
|
||||
appWindow.fiatPriceUSD = ticker;
|
||||
appWindow.fiatApiUpdatePrice();
|
||||
}
|
||||
|
||||
function fiatApiRatesReceived(url, resp, error) {
|
||||
if (error) {
|
||||
appWindow.fiatApiError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
resp = JSON.parse(resp);
|
||||
} catch (e) {
|
||||
appWindow.fiatApiError("bad JSON: " + e);
|
||||
return;
|
||||
}
|
||||
|
||||
if(resp.base !== "USD" || typeof resp.rates !== "object" || resp.rates === null){
|
||||
appWindow.fiatApiError("fiat rates API has error(s)");
|
||||
return;
|
||||
}
|
||||
|
||||
var rates = {"USD": 1};
|
||||
for (var code in resp.rates){
|
||||
if (!resp.rates.hasOwnProperty(code)) continue;
|
||||
|
||||
const rate = Number(resp.rates[code]);
|
||||
if (isFinite(rate) && rate > 0)
|
||||
rates[code] = rate;
|
||||
}
|
||||
|
||||
appWindow.fiatRates = rates;
|
||||
appWindow.fiatRatesTimestamp = Date.now();
|
||||
appWindow.fiatApiUpdatePrice();
|
||||
}
|
||||
|
||||
function fiatApiUpdatePrice() {
|
||||
const symbol = appWindow.fiatApiCurrencySymbol();
|
||||
const rate = appWindow.fiatRates.hasOwnProperty(symbol) ? appWindow.fiatRates[symbol] : 0;
|
||||
appWindow.fiatPrice = (appWindow.fiatPriceUSD > 0 && rate > 0) ? appWindow.fiatPriceUSD * rate : 0;
|
||||
|
||||
appWindow.updateBalance();
|
||||
}
|
||||
@@ -1320,26 +1334,26 @@ ApplicationWindow {
|
||||
return;
|
||||
}
|
||||
|
||||
var provider = appWindow.fiatPriceAPIs[userProvider];
|
||||
var userCurrency = persistentSettings.fiatPriceCurrency;
|
||||
if(!provider.hasOwnProperty(userCurrency)){
|
||||
appWindow.fiatApiError("currency \"" + userCurrency + "\" not implemented");
|
||||
}
|
||||
network.getJSON(appWindow.fiatPriceAPIs[userProvider], fiatApiJsonReceived);
|
||||
|
||||
var url = provider[userCurrency];
|
||||
network.getJSON(url, fiatApiJsonReceived);
|
||||
if(appWindow.fiatApiCurrencySymbol() !== "USD"){
|
||||
if(!persistentSettings.fiatRatesConsentGiven){
|
||||
appWindow.fiatApiError("exchange rate provider consent not given for non-USD currency");
|
||||
return;
|
||||
}
|
||||
|
||||
if(Date.now() - appWindow.fiatRatesTimestamp > appWindow.fiatRatesMaxAge)
|
||||
network.getJSON(appWindow.fiatRatesAPI, fiatApiRatesReceived);
|
||||
}
|
||||
}
|
||||
|
||||
function fiatApiCurrencySymbol() {
|
||||
switch (persistentSettings.fiatPriceCurrency) {
|
||||
case "xmrusd":
|
||||
return "USD";
|
||||
case "xmreur":
|
||||
return "EUR";
|
||||
default:
|
||||
console.error("unsupported currency", persistentSettings.fiatPriceCurrency);
|
||||
return "UNSUPPORTED";
|
||||
const key = getKeyFromCurrency(persistentSettings.fiatPriceCurrency);
|
||||
if (key === undefined) {
|
||||
console.error("unsupported currency", persistentSettings.fiatPriceCurrency);
|
||||
return "UNSUPPORTED";
|
||||
}
|
||||
return key.toUpperCase();
|
||||
}
|
||||
|
||||
function fiatApiConvertToFiat(amount) {
|
||||
@@ -1519,6 +1533,7 @@ ApplicationWindow {
|
||||
property bool fiatPriceToggle: false
|
||||
property string fiatPriceProvider: "kraken"
|
||||
property string fiatPriceCurrency: "xmrusd"
|
||||
property bool fiatRatesConsentGiven: false
|
||||
|
||||
property string proxyAddress: "127.0.0.1:9050"
|
||||
property bool proxyEnabled: isTails
|
||||
@@ -1548,6 +1563,12 @@ ApplicationWindow {
|
||||
|
||||
Component.onCompleted: {
|
||||
MoneroComponents.Style.blackTheme = persistentSettings.blackTheme
|
||||
|
||||
if (!appWindow.fiatPriceAPIs.hasOwnProperty(fiatPriceProvider)) {
|
||||
console.warn("unsupported fiat price provider \"" + fiatPriceProvider + "\", resetting");
|
||||
fiatPriceProvider = "kraken";
|
||||
fiatPriceEnabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -186,6 +186,8 @@ Rectangle {
|
||||
}
|
||||
|
||||
//! Manage pricing
|
||||
readonly property bool fiatRatesConsentNeeded: persistentSettings.fiatPriceCurrency !== "xmrusd" && !persistentSettings.fiatRatesConsentGiven
|
||||
|
||||
RowLayout {
|
||||
MoneroComponents.CheckBox {
|
||||
id: enableConvertCurrency
|
||||
@@ -227,14 +229,16 @@ Rectangle {
|
||||
Layout.maximumWidth: 100
|
||||
labelText: qsTr("Currency") + translationManager.emptyString
|
||||
labelFontSize: 14
|
||||
currentIndex: persistentSettings.fiatPriceCurrency === "xmrusd" ? 0 : 1
|
||||
maxDropdownItems: 8
|
||||
dataModel: fiatPriceCurrencyModel
|
||||
onChanged: {
|
||||
var obj = dataModel.get(currentIndex);
|
||||
persistentSettings.fiatPriceCurrency = obj.data;
|
||||
|
||||
if(persistentSettings.fiatPriceEnabled)
|
||||
if(persistentSettings.fiatPriceEnabled) {
|
||||
appWindow.fiatApiUpdatePrice();
|
||||
appWindow.fiatApiRefresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,24 +247,30 @@ Rectangle {
|
||||
|
||||
ColumnLayout {
|
||||
// Feature needs to be double enabled for security purposes (miss-clicks)
|
||||
visible: enableConvertCurrency.checked && !persistentSettings.fiatPriceEnabled
|
||||
visible: enableConvertCurrency.checked && (!persistentSettings.fiatPriceEnabled || settingsUI.fiatRatesConsentNeeded)
|
||||
spacing: 0
|
||||
Layout.topMargin: 5
|
||||
Layout.leftMargin: 36
|
||||
|
||||
MoneroComponents.WarningBox {
|
||||
text: qsTr("Enabling price conversion exposes your IP address to the selected price source.") + translationManager.emptyString;
|
||||
text: !persistentSettings.fiatPriceEnabled
|
||||
? qsTr("Enabling price conversion exposes your IP address to the selected price source, and for currencies other than USD, to the exchange rate provider (frankfurter.dev).") + translationManager.emptyString
|
||||
: qsTr("Using a non-USD currency exposes your IP address to the exchange rate provider (frankfurter.dev).") + translationManager.emptyString;
|
||||
}
|
||||
|
||||
MoneroComponents.StandardButton {
|
||||
Layout.topMargin: 10
|
||||
Layout.bottomMargin: 10
|
||||
small: true
|
||||
text: qsTr("Confirm and enable") + translationManager.emptyString
|
||||
text: !persistentSettings.fiatPriceEnabled
|
||||
? qsTr("Confirm and enable") + translationManager.emptyString
|
||||
: qsTr("Confirm") + translationManager.emptyString
|
||||
|
||||
onClicked: {
|
||||
console.log("Enabled price conversion");
|
||||
console.log(persistentSettings.fiatPriceEnabled ? "Confirmed exchange rate provider consent" : "Enabled price conversion");
|
||||
persistentSettings.fiatPriceEnabled = true;
|
||||
persistentSettings.fiatRatesConsentGiven = true;
|
||||
appWindow.fiatApiRefresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -314,14 +324,6 @@ Rectangle {
|
||||
|
||||
ListModel {
|
||||
id: fiatPriceCurrencyModel
|
||||
ListElement {
|
||||
data: "xmrusd"
|
||||
column1: "USD"
|
||||
}
|
||||
ListElement {
|
||||
data: "xmreur"
|
||||
column1: "EUR"
|
||||
}
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
@@ -341,6 +343,17 @@ Rectangle {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
var currencies = appWindow.fiatCurrencies;
|
||||
fiatPriceCurrencyModel.clear();
|
||||
|
||||
for (var j = 0; j < currencies.length; ++j) {
|
||||
const key = "xmr" + currencies[j].toLowerCase();
|
||||
fiatPriceCurrencyModel.append({"column1": currencies[j], "data": key});
|
||||
|
||||
if(key === persistentSettings.fiatPriceCurrency)
|
||||
fiatPriceCurrencyDropdown.currentIndex = j;
|
||||
}
|
||||
|
||||
console.log('SettingsLayout loaded');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user