diff options
| author | dsc <xmrdsc@protonmail.com> | 2019-05-01 04:05:16 +0200 |
|---|---|---|
| committer | xmrdsc <xmrdsc@protonmail.com> | 2019-05-06 04:36:23 +0200 |
| commit | d4be7634cbc80cd40c2ff8456bcd962b21b2dcb6 (patch) | |
| tree | 7fe6aa54f2935adc94d829c875b2300db272810c /main.qml | |
| parent | 3c1fe1da8b83738cb76a1a1242514bbe4ba515e6 (diff) | |
| download | monzero-gui-d4be7634cbc80cd40c2ff8456bcd962b21b2dcb6.tar.gz monzero-gui-d4be7634cbc80cd40c2ff8456bcd962b21b2dcb6.tar.xz monzero-gui-d4be7634cbc80cd40c2ff8456bcd962b21b2dcb6.zip | |
Fiat API
Co-Authored-By: selsta <selsta@sent.at>
Co-Authored-By: Gene Peters <gene@telligent-data.com>
Diffstat (limited to 'main.qml')
| -rw-r--r-- | main.qml | 174 |
1 files changed, 172 insertions, 2 deletions
@@ -91,6 +91,26 @@ ApplicationWindow { property int appEpoch: Math.floor((new Date).getTime() / 1000) property bool themeTransition: false + // fiat price conversion + property int fiatPriceXMRUSD: 0 + property int fiatPriceXMREUR: 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", + } + } + } + property string remoteNodeService: { // support user-defined remote node aggregators if(persistentSettings.remoteNodeService){ @@ -394,6 +414,10 @@ ApplicationWindow { middlePanel.balanceText = balance; leftPanel.balanceText = balance; + if (persistentSettings.fiatPriceEnabled) { + appWindow.fiatApiUpdateBalance(balance, balance_unlocked); + } + var accountLabel = currentWallet.getSubaddressLabel(currentWallet.currentSubaddressAccount, 0); leftPanel.balanceLabelText = qsTr("Balance (#%1%2)").arg(currentWallet.currentSubaddressAccount).arg(accountLabel === "" ? "" : (" – " + accountLabel)); } @@ -1102,6 +1126,7 @@ ApplicationWindow { rootItem.state = "wizard" // reset balance leftPanel.balanceText = leftPanel.unlockedBalanceText = walletManager.displayAmount(0); + fiatApiUpdateBalance(0, 0); // disable timers userInActivityTimer.running = false; simpleModeConnectionTimer.running = false; @@ -1126,6 +1151,143 @@ ApplicationWindow { flags: persistentSettings.customDecorations ? Windows.flagsCustomDecorations : Windows.flags onWidthChanged: x -= 0 + Timer { + id: fiatPriceTimer + interval: 1000 * 60; + running: persistentSettings.fiatPriceEnabled; + repeat: true + onTriggered: { + if(persistentSettings.fiatPriceEnabled) + appWindow.fiatApiRefresh(); + } + triggeredOnStart: false + } + + function fiatApiParseTicker(resp, currency){ + // parse & validate incoming JSON + if(resp._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]["o"]; + return ticker; + } else if(resp._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)"); + return; + } + return resp["monero"][key]; + } else if(resp._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; + } + return resp[key]; + } + } + + function fiatApiGetCurrency(resp){ + // map response to `appWindow.fiatPriceAPIs` object + if (!resp.hasOwnProperty('_url')){ + appWindow.fiatApiError("invalid JSON"); + return; + } + + 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; + + var url = apis[api][cur]; + if(url === resp._url){ + return cur; + } + } + } + } + + function fiatApiJsonReceived(resp){ + // handle incoming JSON, set ticker + var currency = appWindow.fiatApiGetCurrency(resp); + if(typeof currency == "undefined"){ + appWindow.fiatApiError("could not get currency"); + return; + } + + var ticker = appWindow.fiatApiParseTicker(resp, currency); + if(ticker <= 0){ + appWindow.fiatApiError("could not get ticker"); + return; + } + + if(persistentSettings.fiatPriceCurrency === "xmrusd") + appWindow.fiatPriceXMRUSD = ticker; + else if(persistentSettings.fiatPriceCurrency === "xmreur") + appWindow.fiatPriceXMREUR = ticker; + + appWindow.updateBalance(); + } + + function fiatApiRefresh(){ + // trigger API call + if(!persistentSettings.fiatPriceEnabled) + return; + + var userProvider = persistentSettings.fiatPriceProvider; + if(!appWindow.fiatPriceAPIs.hasOwnProperty(userProvider)){ + appWindow.fiatApiError("provider \"" + userProvider + "\" not implemented"); + return; + } + + var provider = appWindow.fiatPriceAPIs[userProvider]; + var userCurrency = persistentSettings.fiatPriceCurrency; + if(!provider.hasOwnProperty(userCurrency)){ + appWindow.fiatApiError("currency \"" + userCurrency + "\" not implemented"); + } + + var url = provider[userCurrency]; + Prices.getJSON(url); + } + + function fiatApiUpdateBalance(balance, unlocked_balance){ + // update balance card + var ticker = persistentSettings.fiatPriceCurrency === "xmrusd" ? appWindow.fiatPriceXMRUSD : appWindow.fiatPriceXMREUR; + var symbol = persistentSettings.fiatPriceCurrency === "xmrusd" ? "$" : "€" + if(ticker <= 0){ + console.log(fiatApiError("Could not update balance card; invalid ticker value")); + leftPanel.unlockedBalanceTextFiat = "N/A"; + leftPanel.balanceTextFiat = "N/A"; + return; + } + + var uFiat = Utils.formatMoney(unlocked_balance * ticker); + var bFiat = Utils.formatMoney(balance * ticker); + + leftPanel.unlockedBalanceTextFiat = symbol + uFiat; + leftPanel.balanceTextFiat = symbol + bFiat; + } + + function fiatTimerStart(){ + fiatPriceTimer.start(); + } + + function fiatTimerStop(){ + fiatPriceTimer.stop(); + } + + function fiatApiError(msg){ + console.log("fiatPriceError: " + msg); + } + Component.onCompleted: { x = (Screen.width - width) / 2 y = (Screen.height - maxWindowHeight) / 2 @@ -1137,6 +1299,7 @@ ApplicationWindow { walletManager.checkUpdatesComplete.connect(onWalletCheckUpdatesComplete); walletManager.walletPassphraseNeeded.connect(onWalletPassphraseNeeded); IPC.uriHandler.connect(onUriHandler); + Prices.priceJsonReceived.connect(appWindow.fiatApiJsonReceived); if(typeof daemonManager != "undefined") { daemonManager.daemonStarted.connect(onDaemonStarted); @@ -1144,8 +1307,6 @@ ApplicationWindow { daemonManager.daemonStopped.connect(onDaemonStopped); } - - // Connect app exit to qml window exit handling mainApp.closing.connect(appWindow.close); @@ -1177,6 +1338,11 @@ ApplicationWindow { } checkUpdates(); + + if(persistentSettings.fiatPriceEnabled){ + appWindow.fiatApiRefresh(); + appWindow.fiatTimerStart(); + } } Settings { @@ -1223,6 +1389,10 @@ ApplicationWindow { property bool showPid: false property bool blackTheme: true + property bool fiatPriceEnabled: false + property string fiatPriceProvider: "kraken" + property string fiatPriceCurrency: "xmrusd" + Component.onCompleted: { MoneroComponents.Style.blackTheme = persistentSettings.blackTheme } |
