wallet settings modal; save default currency

wallet settings modal; save default currency

wallet settings modal; save default currency

save/load currency preference

fix default currency call

use chronicle instead of echo

fix issues
This commit is contained in:
Iuri Matias 2020-06-01 21:02:50 -04:00
parent ac37f0fdbb
commit f62d3aaca7
12 changed files with 226 additions and 29 deletions

View File

@ -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])

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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}
}
##^##*/

View File

@ -1,2 +1,3 @@
SendModalContent 1.0 SendModalContent.qml
SettingsModalContent 1.0 SettingsModalContent.qml
AddAccount 1.0 AddAccount.qml

View File

@ -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}
}
##^##*/

View File

@ -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
}
}
}
}

View File

@ -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 \