diff --git a/src/app/wallet/core.nim b/src/app/wallet/core.nim index d73accc8b3..59f2e7b674 100644 --- a/src/app/wallet/core.nim +++ b/src/app/wallet/core.nim @@ -35,10 +35,11 @@ proc init*(self: WalletController) = var totalAccountBalance: float = 0 const symbol = "ETH" + let defaultCurrency = self.status.wallet.getDefaultCurrency() for address in accounts: let eth_balance = self.status.wallet.getEthBalance(address) # TODO get all user assets and add them to balance - let usd_balance = self.status.wallet.getFiatValue(eth_balance, symbol, "USD") + let usd_balance = self.status.wallet.getFiatValue(eth_balance, symbol, defaultCurrency) totalAccountBalance = totalAccountBalance + usd_balance @@ -46,7 +47,7 @@ proc init*(self: WalletController) = let asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"{eth_balance:.6}", fiatValue: "$" & fmt"{usd_balance:.2f}", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg") assetList.addAssetToList(asset) - let account = Account(name: "Status Account", address: address, iconColor: "", balance: fmt"{totalAccountBalance:.2f} USD", assetList: assetList) + let account = Account(name: "Status Account", address: address, iconColor: "", balance: fmt"{totalAccountBalance:.2f} {defaultCurrency}", assetList: assetList) self.view.addAccountToList(account) self.view.setDefaultAccount(accounts[0]) diff --git a/src/app/wallet/view.nim b/src/app/wallet/view.nim index 38df780921..d60072a934 100644 --- a/src/app/wallet/view.nim +++ b/src/app/wallet/view.nim @@ -62,3 +62,17 @@ QtObject: proc getDefaultAccount*(self: WalletView): string {.slot.} = return self.defaultAccount + + proc defaultCurrency*(self: WalletView): string {.slot.} = + self.status.wallet.getDefaultCurrency() + + proc defaultCurrencyChanged*(self: WalletView) {.signal.} + + proc setDefaultCurrency*(self: WalletView, currency: string) {.slot.} = + self.status.wallet.setDefaultCurrency(currency) + self.defaultCurrencyChanged() + + QtProperty[string] defaultCurrency: + read = defaultCurrency + write = setDefaultCurrency + notify = defaultCurrencyChanged diff --git a/src/status/libstatus/core.nim b/src/status/libstatus/core.nim index f569460eaf..f25c0c4050 100644 --- a/src/status/libstatus/core.nim +++ b/src/status/libstatus/core.nim @@ -2,6 +2,10 @@ import json import libstatus import nimcrypto import utils +import chronicles + +logScope: + topics = "rpc" proc callRPC*(inputJSON: string): string = return $libstatus.callRPC(inputJSON) @@ -16,10 +20,13 @@ proc callPrivateRPC*(methodName: string, payload = %* []): string = "method": methodName, "params": %payload } - result = $libstatus.callPrivateRPC($inputJSON) + debug "calling json", request = $inputJSON + let response = libstatus.callPrivateRPC($inputJSON) + result = $response + if parseJSON(result).hasKey("error"): + error "rpc response error", result = result except: - echo "error doing rpc request" - echo methodName + error "error doing rpc request", methodName = methodName proc sendTransaction*(inputJSON: string, password: string): string = var hashed_password = "0x" & $keccak_256.digest(password) diff --git a/src/status/libstatus/settings.nim b/src/status/libstatus/settings.nim new file mode 100644 index 0000000000..95116cea78 --- /dev/null +++ b/src/status/libstatus/settings.nim @@ -0,0 +1,11 @@ +import core +import json + +proc saveSettings*(key: string, value: string): string = + callPrivateRPC("settings_saveSetting", %* [ + key, $value + ]) + +proc getSettings*(): string = + callPrivateRPC("settings_getSettings") +# TODO: return an Table/Object instead of string diff --git a/src/status/libstatus/wallet.nim b/src/status/libstatus/wallet.nim index 665aa7a803..efcaca54c2 100644 --- a/src/status/libstatus/wallet.nim +++ b/src/status/libstatus/wallet.nim @@ -28,8 +28,11 @@ proc getPrice*(crypto: string, fiat: string): string = let client = newHttpClient() client.headers = newHttpHeaders({ "Content-Type": "application/json" }) - let response = client.request(url) - $parseJson(response.body)["USD"] + try: + let response = client.request(url) + result = $parseJson(response.body)[fiat.toUpper] + except: + echo "error getting price" proc getBalance*(address: string): string = let payload = %* [address, "latest"] diff --git a/src/status/settings.nim b/src/status/settings.nim deleted file mode 100644 index e9099e2f39..0000000000 --- a/src/status/settings.nim +++ /dev/null @@ -1,11 +0,0 @@ -import core -import json - -proc saveSettings*(key: string, value = JsonNode) = - discard callPrivateRPC("settings_saveSetting", %* [ - [key, value] - ]) - - -proc getSettings*(): string = callPrivateRPC("settings_getSettings") -# TODO: return an Table/Object instead of string diff --git a/src/status/wallet.nim b/src/status/wallet.nim index 6254268097..2912effe82 100644 --- a/src/status/wallet.nim +++ b/src/status/wallet.nim @@ -1,7 +1,12 @@ import eventemitter +import json import strformat import strutils import libstatus/wallet as status_wallet +import libstatus/settings as status_settings + +type CurrencyArgs* = ref object of Args + currency*: string type Asset* = ref object name*, symbol*, value*, fiatValue*, image*: string @@ -28,12 +33,19 @@ proc getEthBalance*(self: WalletModel, address: string): string = echo(fmt"balance in eth: {eth_value}") eth_value +proc getDefaultCurrency*(self: WalletModel): string = + status_settings.getSettings().parseJSON()["result"]["currency"].getStr + +proc setDefaultCurrency*(self: WalletModel, currency: string) = + discard status_settings.saveSettings("currency", currency) + self.events.emit("currencyChanged", CurrencyArgs(currency: currency)) + proc getFiatValue*(self: WalletModel, eth_balance: string, symbol: string, fiat_symbol: string): float = # 3. get usd price of 1 eth - var usd_eth_price = status_wallet.getPrice("ETH", "USD") - echo(fmt"usd_price: {usd_eth_price}") + var fiat_eth_price = status_wallet.getPrice("ETH", fiat_symbol) + echo(fmt"fiat_price: {fiat_eth_price}") # 4. convert balance to usd - var usd_balance = parseFloat(eth_balance) * parseFloat(usd_eth_price) - echo(fmt"balance in usd: {usd_balance}") - usd_balance + var fiat_balance = parseFloat(eth_balance) * parseFloat(fiat_eth_price) + echo(fmt"balance in usd: {fiat_balance}") + fiat_balance diff --git a/ui/app/AppLayouts/Wallet/Components/SettingsModalContent.qml b/ui/app/AppLayouts/Wallet/Components/SettingsModalContent.qml new file mode 100644 index 0000000000..a101b2481e --- /dev/null +++ b/ui/app/AppLayouts/Wallet/Components/SettingsModalContent.qml @@ -0,0 +1,95 @@ +import QtQuick 2.3 +import QtQuick.Controls 2.3 +import QtQuick.Layouts 1.3 +import Qt.labs.platform 1.1 +import "../../../../imports" +import "../../../../shared" + +Item { + id: element + property string currency: "USD" + + Text { + id: modalDialogTitle + text: "Settings" + anchors.top: parent.top + anchors.left: parent.left + font.bold: true + font.pixelSize: 17 + anchors.leftMargin: 16 + anchors.topMargin: 16 + } + + Image { + id: closeModalImg + anchors.top: parent.top + anchors.right: parent.right + anchors.rightMargin: 16 + anchors.topMargin: 16 + source: "../../../img/close.svg" + MouseArea { + id: closeModalMouseArea + cursorShape: Qt.PointingHandCursor + anchors.fill: parent + onClicked: { + popup.close() + } + } + } + + Separator { + id: headerSeparator + anchors.top: modalDialogTitle.bottom + } + + Item { + id: modalBody + anchors.right: parent.right + anchors.rightMargin: 32 + anchors.top: headerSeparator.bottom + anchors.topMargin: Theme.padding + anchors.bottom: footerSeparator.top + anchors.bottomMargin: 16 + anchors.left: parent.left + anchors.leftMargin: 32 + + Input { + id: txtCurrency + label: "Currency" + anchors.top: parent.top + anchors.topMargin: 0 + anchors.right: parent.right + anchors.rightMargin: 0 + anchors.left: parent.left + anchors.leftMargin: 0 + placeholderText: qsTr("USD") + text: currency + } + + } + + Separator { + id: footerSeparator + anchors.bottom: parent.bottom + anchors.bottomMargin: 76 + } + + StyledButton { + anchors.right: parent.right + anchors.rightMargin: Theme.padding + label: "Save" + anchors.bottom: parent.bottom + anchors.bottomMargin: Theme.padding + onClicked: { + console.log(txtCurrency.textField.text) + assetsModel.setDefaultCurrency(txtCurrency.textField.text) + popup.close() + } + } +} + +/*##^## +Designer { + D{i:0;autoSize:true;height:480;width:640} +} +##^##*/ diff --git a/ui/app/AppLayouts/Wallet/Components/qmldir b/ui/app/AppLayouts/Wallet/Components/qmldir index b67e5e942d..bfd97c0c21 100644 --- a/ui/app/AppLayouts/Wallet/Components/qmldir +++ b/ui/app/AppLayouts/Wallet/Components/qmldir @@ -1,2 +1,3 @@ SendModalContent 1.0 SendModalContent.qml +SettingsModalContent 1.0 SettingsModalContent.qml AddAccount 1.0 AddAccount.qml diff --git a/ui/app/AppLayouts/Wallet/SettingsModal.qml b/ui/app/AppLayouts/Wallet/SettingsModal.qml new file mode 100644 index 0000000000..0455b791ac --- /dev/null +++ b/ui/app/AppLayouts/Wallet/SettingsModal.qml @@ -0,0 +1,46 @@ +import QtQuick 2.3 +import QtQuick.Controls 2.3 +import QtQuick.Layouts 1.3 +import Qt.labs.platform 1.1 +import "../../../imports" +import "../../../shared" +import "./Components" + +Item { + function open() { + popup.open() + settingsModalContent.currency = walletModel.defaultCurrency + } + + function close() { + popup.close() + } + + Popup { + id: popup + modal: true + closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside + Overlay.modal: Rectangle { + color: "#60000000" + } + parent: Overlay.overlay + x: Math.round((parent.width - width) / 2) + y: Math.round((parent.height - height) / 2) + width: 480 + height: 510 + background: Rectangle { + color: Theme.white + radius: Theme.radius + } + padding: 0 + contentItem: SettingsModalContent { + id: settingsModalContent + } + } +} + +/*##^## +Designer { + D{i:0;autoSize:true;height:480;width:640} +} +##^##*/ diff --git a/ui/app/AppLayouts/Wallet/WalletHeader.qml b/ui/app/AppLayouts/Wallet/WalletHeader.qml index 55ad1b7290..c363dcec38 100644 --- a/ui/app/AppLayouts/Wallet/WalletHeader.qml +++ b/ui/app/AppLayouts/Wallet/WalletHeader.qml @@ -4,7 +4,6 @@ import QtQuick.Layouts 1.3 import "../../../imports" import "../../../shared" - Item { id: walletHeader height: walletAddress.y + walletAddress.height @@ -67,6 +66,10 @@ Item { id: sendModal } + SettingsModal{ + id: settingsModal + } + Item { property int btnMargin: 8 property int btnOuterMargin: 32 @@ -133,14 +136,27 @@ Item { color: Theme.blue } } - Image { + Item { id: settingsBtn anchors.left: receiveBtn.right anchors.leftMargin: walletMenu.btnOuterMargin - width: 18 - height: 18 - fillMode: Image.PreserveAspectFit - source: "../../img/settings.svg" + width: settingsImg.width + Image { + id: settingsImg + width: 18 + height: 18 + fillMode: Image.PreserveAspectFit + source: "../../img/settings.svg" + } + MouseArea { + anchors.rightMargin: -Theme.smallPadding + anchors.leftMargin: -Theme.smallPadding + anchors.bottomMargin: -Theme.smallPadding + anchors.topMargin: -Theme.smallPadding + anchors.fill: parent + onClicked: settingsModal.open() + cursorShape: Qt.PointingHandCursor + } } } } diff --git a/ui/nim-status-client.pro b/ui/nim-status-client.pro index 63b5e255b8..8482c7b252 100644 --- a/ui/nim-status-client.pro +++ b/ui/nim-status-client.pro @@ -81,6 +81,7 @@ DISTFILES += \ app/AppLayouts/Wallet/CollectiblesTab.qml \ app/AppLayouts/Wallet/Components/AddAccount.qml \ app/AppLayouts/Wallet/Components/SendModalContent.qml \ + app/AppLayouts/Wallet/Components/SettingsModalContent.qml \ app/AppLayouts/Wallet/Components/qmldir \ app/AppLayouts/Wallet/HistoryTab.qml \ app/AppLayouts/Profile/Sections/AboutContainer.qml \ @@ -97,6 +98,7 @@ DISTFILES += \ app/AppLayouts/Profile/qmldir \ app/AppLayouts/Wallet/LeftTab.qml \ app/AppLayouts/Wallet/SendModal.qml \ + app/AppLayouts/Wallet/SettingsModal.qml \ app/AppLayouts/Wallet/WalletHeader.qml \ app/AppLayouts/Wallet/WalletLayout.qml \ app/AppLayouts/Wallet/qmldir \