fix: address more comments about the chat commands

This commit is contained in:
Jonathan Rainville 2020-09-09 14:15:14 -04:00 committed by Iuri Matias
parent 88e76f33dd
commit f3f27a5e59
9 changed files with 49 additions and 66 deletions

View File

@ -4,7 +4,7 @@ import ../../status/wallet/collectibles as status_collectibles
import ../../status/libstatus/wallet as status_wallet
import ../../status/libstatus/tokens
import ../../status/libstatus/types
import ../../status/libstatus/utils
import ../../status/libstatus/utils as status_utils
import views/[asset_list, account_list, account_item, token_list, transaction_list, collectibles_list]
QtObject:
@ -493,3 +493,10 @@ QtObject:
if address == self.currentAccount.address:
self.setCurrentTransactions(transactions)
self.loadingTrxHistory(false)
proc eth2Wei*(self: WalletView, eth: string, decimals: int): string {.slot.} =
return $status_utils.eth2Wei(parseFloat(eth), decimals)
proc wei2Token*(self: WalletView, wei: string, decimals: int): string {.slot.} =
return status_utils.wei2Token(wei, decimals)

View File

@ -1,4 +1,4 @@
import json, random, strutils, strformat, tables
import json, random, strutils, strformat, tables, chronicles
import stint, nim_status
from times import getTime, toUnix, nanosecond
import accounts/signing_phrases
@ -60,6 +60,22 @@ proc wei2Eth*(input: Stuint[256]): string =
fmt"{eth}.{leading_zeros}{remainder}"
proc wei2Token*(input: string, decimals: int): string =
try:
var value = input.parse(Stuint[256])
var p = u256(10).pow(decimals)
var i = value.div(p)
var r = value.mod(p)
var leading_zeros = "0".repeat(decimals - ($r).len)
var d = fmt"{leading_zeros}{$r}"
result = $i
if(r > 0): result = fmt"{result}.{d}"
result. trimZeros()
except Exception as e:
error "Error parsing this wei value", input
result = "0"
proc first*(jArray: JsonNode, fieldName, id: string): JsonNode =
if jArray == nil:
return nil

View File

@ -201,6 +201,7 @@ proc toMessage*(jsonMsg: JsonNode): Message =
.join(" ")
if message.contentType == ContentType.Transaction:
# TODO find a way to use json_seralization for this. When I try, I get an error
message.commandParameters = CommandParameters(
id: jsonMsg["commandParameters"]["id"].getStr,
fromAddress: jsonMsg["commandParameters"]["from"].getStr,

View File

@ -16,11 +16,7 @@ ModalPopup {
title: root.commandTitle
height: 504
property var selectedRecipient
onSelectedRecipientChanged: {
selectRecipient.selectedRecipient = this.selectedRecipient
selectRecipient.readOnly = !!this.selectedRecipient && !!this.selectedRecipient.address
}
property alias selectedRecipient: selectRecipient.selectedRecipient
onClosed: {
stack.reset()
@ -63,7 +59,6 @@ ModalPopup {
contacts: profileModel.addedContacts
label: qsTr("Recipient")
readOnly: true
selectedRecipient: root.selectedRecipient
anchors.top: separator.bottom
anchors.topMargin: 10
width: stack.width

View File

@ -27,7 +27,7 @@ Popup {
}
function requestAddressForTransaction(address, amount, tokenAddress, tokenDecimals = 18) {
amount = Utils.multiplyByDecimals(amount, tokenDecimals)
amount = walletModel.eth2Wei(amount.toString(), tokenDecimals)
chatsModel.requestAddressForTransaction(chatsModel.activeChannel.id,
address,
amount,
@ -35,7 +35,7 @@ Popup {
chatCommandModal.close()
}
function requestTransaction(address, amount, tokenAddress, tokenDecimals = 18) {
amount = Utils.multiplyByDecimals(amount, tokenDecimals)
amount = walletModel.eth2Wei(amount.toString(), tokenDecimals)
chatsModel.requestTransaction(chatsModel.activeChannel.id,
address,
amount,

View File

@ -90,6 +90,25 @@ ModalPopup {
slowestGasPrice = Qt.binding(function(){ return parseFloat(walletModel.safeLowGasPrice) })
fastestGasPrice = Qt.binding(function(){ return parseFloat(walletModel.fastestGasPrice) })
}
function estimateGas() {
if (!(root.selectedAccount && root.selectedAccount.address &&
root.selectedRecipient && root.selectedRecipient.address &&
root.selectedAsset && root.selectedAsset.address &&
root.selectedAmount)) return
let gasEstimate = JSON.parse(walletModel.estimateGas(
root.selectedAccount.address,
root.selectedRecipient.address,
root.selectedAsset.address,
root.selectedAmount))
if (gasEstimate.error) {
console.warn(qsTr("Error estimating gas: %1").arg(gasEstimate.error.message))
return
}
selectedGasLimit = gasEstimate.result
}
}
GasValidator {
id: gasValidator

View File

@ -49,7 +49,7 @@ Item {
return "0"
}
try {
return Utils.divideByDecimals(commandParametersObject.value, token.decimals)
return walletModel.wei2Token(commandParametersObject.value.toString(), token.decimals)
} catch (e) {
console.error("Error getting the ETH value of:", commandParametersObject.value)
console.error("Error:", e.message)

View File

@ -14,12 +14,6 @@ ModalPopup {
title: qsTrId("command-button-send")
height: 504
property var selectedRecipient
onSelectedRecipientChanged: {
selectRecipient.selectedRecipient = this.selectedRecipient
selectRecipient.readOnly = !!this.selectedRecipient && !!this.selectedRecipient.address
}
property MessageDialog sendingError: MessageDialog {
id: sendingError
title: qsTr("Error sending the transaction")
@ -100,8 +94,6 @@ ModalPopup {
accounts: walletModel.accounts
contacts: profileModel.addedContacts
label: qsTr("Recipient")
readOnly: !!root.selectedRecipient && !!root.selectedRecipient.address
selectedRecipient: root.selectedRecipient
anchors.top: separator.bottom
anchors.topMargin: 10
width: stack.width

View File

@ -4,53 +4,6 @@ import QtQuick 2.13
import "../shared/xss.js" as XSS
QtObject {
// Use this to multiply an amount by 10^decimals
function multiplyByDecimals(amount, decimals) {
amount = amount.toString()
let dotIndex = -1
for (let i = 0; i < decimals; i++) {
dotIndex = amount.indexOf('.')
if (dotIndex > -1) {
if (dotIndex === amount.length - 1) {
// The dot is at the end, we can get rid of it and add a 0
amount = amount.substring(0, dotIndex) + "0"
continue
}
// Move the dot one space to the right
amount = amount.substring(0, dotIndex) + amount.substring(dotIndex + 1, dotIndex + 2) + "." + amount.substring(dotIndex + 2)
continue
}
// Just add a new 0
amount += "0"
}
return stripStartingZeros(amount)
}
// Use this to divide an amount by 10^decimals
function divideByDecimals(amount, decimals) {
amount = amount.toString()
let dotIndex = amount.indexOf('.')
if (dotIndex === -1) {
amount = amount + "."
}
for (let i = 0; i < decimals; i++) {
dotIndex = amount.indexOf('.')
if (dotIndex === 0) {
// The dot is at the start, we need to add a zero in front before moving it
dotIndex++
amount = "0" + amount
}
// Move the dot one position left
amount = amount.substring(0, dotIndex - 1) + "." + amount.substring(dotIndex - 1, dotIndex) + amount.substring(dotIndex + 1)
}
if (amount.startsWith(".")) {
amount = "0" + amount
}
return stripTrailingZeros(amount)
}
function isHex(value) {
return /^(-0x|0x)?[0-9a-fA-F]*$/i.test(value)
}