refactor: separate slots for sending eth and tokens

This commit is contained in:
Richard Ramos 2021-06-29 09:25:05 -04:00 committed by Iuri Matias
parent fd976c770d
commit 5f7f899c3b
4 changed files with 106 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import algorithm, atomics, sequtils, strformat, strutils, sugar, sequtils, json, parseUtils, std/wrapnils, tables import algorithm, atomics, sequtils, strformat, strutils, sugar, sequtils, json, parseUtils, std/wrapnils, tables
import NimQml, json, sequtils, chronicles, strutils, strformat, json, stint import NimQml, json, sequtils, chronicles, strutils, strformat, json, stint
import web3/ethhexstrings
import import
../../../status/[status, settings, wallet, tokens], ../../../status/[status, settings, wallet, tokens],
@ -108,6 +109,41 @@ QtObject:
proc sendTransaction*(self: TransactionsView, from_addr: string, to: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string) {.slot.} = proc sendTransaction*(self: TransactionsView, from_addr: string, to: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string) {.slot.} =
self.sendTransaction("transactionSent", from_addr, to, assetAddress, value, gas, gasPrice, password, uuid) self.sendTransaction("transactionSent", from_addr, to, assetAddress, value, gas, gasPrice, password, uuid)
proc transferEth*(self: TransactionsView, from_addr: string, to_addr: string, value: string, gas: string, gasPrice: string, password: string, uuid: string): bool {.slot.} =
try:
if not validate(HexDataStr(from_addr)): return false
if from_addr.len != 42: return false
if not validate(HexDataStr(to_addr)): return false
if to_addr.len != 42: return false
if parseFloat(value) < 0: return false
if parseInt(gas) < 0: return false
if parseFloat(gasPrice) <= 0: return false
if uuid.isEmptyOrWhitespace(): return false
self.sendTransaction("transactionSent", from_addr, to_addr, ZERO_ADDRESS, value, gas, gasPrice, password, uuid)
except Exception as e:
error "Error sending eth transfer transaction", msg = e.msg
return false
return true
proc transferTokens*(self: TransactionsView, from_addr: string, to_addr: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string): bool {.slot.} =
try:
if not validate(HexDataStr(from_addr)): return false
if from_addr.len != 42: return false
if not validate(HexDataStr(to_addr)): return false
if to_addr.len != 42: return false
if not validate(HexDataStr(assetAddress)): return false
if assetAddress.len != 42: return false
if parseFloat(value) <= 0: return false
if parseInt(gas) <= 0: return false
if parseFloat(gasPrice) <= 0: return false
if uuid.isEmptyOrWhitespace(): return false
self.sendTransaction("transactionSent", from_addr, to_addr, assetAddress, value, gas, gasPrice, password, uuid)
except Exception as e:
error "Error sending token transfer transaction", msg = e.msg
return false
return true
proc checkRecentHistory*(self: TransactionsView) {.slot.} = proc checkRecentHistory*(self: TransactionsView) {.slot.} =
var addresses:seq[string] = @[] var addresses:seq[string] = @[]
for acc in self.status.wallet.accounts: for acc in self.status.wallet.accounts:

View File

@ -9,6 +9,7 @@ import chronicles
import nbaser import nbaser
import stew/byteutils import stew/byteutils
from base32 import nil from base32 import nil
import web3/ethhexstrings
const HTTPS_SCHEME = "https" const HTTPS_SCHEME = "https"
const IPFS_GATEWAY = ".infura.status.im" const IPFS_GATEWAY = ".infura.status.im"
@ -91,6 +92,19 @@ proc toAPIRequest(message: string): APIRequest =
hostname: data{"hostname"}.getStr() hostname: data{"hostname"}.getStr()
) )
proc validateInput(from_addr, to_addr, value, gas, gasPrice, data: string): bool =
if not validate(HexDataStr(from_addr)): return false
if from_addr.len != 42: return false
if to_addr != "":
if not validate(HexDataStr(to_addr)): return false
if to_addr.len != 42: return false
if parseFloat(value) < 0: return false
if parseInt(gas) <= 0: return false
if parseFloat(gasPrice) <= 0: return false
if data != "":
if not validate(HexDataStr(data)): return false
return true
proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string =
if AUTH_METHODS.contains(data.payload.rpcMethod) and not self.permissions.hasPermission(data.hostname, Permission.Web3): if AUTH_METHODS.contains(data.payload.rpcMethod) and not self.permissions.hasPermission(data.hostname, Permission.Web3):
return $ %* { return $ %* {
@ -118,16 +132,26 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string =
else: else:
"" ""
let validInput = validateInput(fromAddress, to, value, selectedGasLimit, selectedGasPrice, txData)
var success: bool var success: bool
# TODO make this async var errorMessage = ""
let response = wallet.sendTransaction(fromAddress, to, value, selectedGasLimit, selectedGasPrice, password, success, txData) var response = ""
let errorMessage = if not success:
if response == "": if validInput:
"web3-response-error" # TODO make this async
response = wallet.sendTransaction(fromAddress, to, value, selectedGasLimit, selectedGasPrice, password, success, txData)
errorMessage = if not success:
if response == "":
"web3-response-error"
else:
response
else: else:
response ""
else: else:
"" success = false
errorMessage = "Invalid input"
return $ %* { return $ %* {
"type": ResponseTypes.Web3SendAsyncCallback, "type": ResponseTypes.Web3SendAsyncCallback,

View File

@ -19,7 +19,19 @@ ModalPopup {
property alias transactionSigner: transactionSigner property alias transactionSigner: transactionSigner
property var sendTransaction: function(selectedGasLimit, selectedGasPrice, enteredPassword) { property var sendTransaction: function(selectedGasLimit, selectedGasPrice, enteredPassword) {
let responseStr = walletModel.transactionsView.sendTransaction(selectFromAccount.selectedAccount.address, let success = false
if(root.selectedAsset.address == Constants.zeroAddress){
success = walletModel.transactionsView.transferEth(
selectFromAccount.selectedAccount.address,
selectRecipient.selectedRecipient.address,
root.selectedAmount,
selectedGasLimit,
selectedGasPrice,
enteredPassword,
stack.uuid)
} else {
success = walletModel.transactionsView.transferTokens(
selectFromAccount.selectedAccount.address,
selectRecipient.selectedRecipient.address, selectRecipient.selectedRecipient.address,
root.selectedAsset.address, root.selectedAsset.address,
root.selectedAmount, root.selectedAmount,
@ -27,6 +39,13 @@ ModalPopup {
selectedGasPrice, selectedGasPrice,
enteredPassword, enteredPassword,
stack.uuid) stack.uuid)
}
if(!success){
sendingError.text = qsTr("Invalid transaction parameters")
return sendingError.open()
}
root.close() root.close()
} }

View File

@ -27,7 +27,19 @@ ModalPopup {
function sendTransaction() { function sendTransaction() {
stack.currentGroup.isPending = true stack.currentGroup.isPending = true
walletModel.transactionsView.sendTransaction(selectFromAccount.selectedAccount.address, let success = false
if(txtAmount.selectedAsset.address == ""){
success = walletModel.transactionsView.transferEth(
selectFromAccount.selectedAccount.address,
selectRecipient.selectedRecipient.address,
txtAmount.selectedAmount,
gasSelector.selectedGasLimit,
gasSelector.selectedGasPrice,
transactionSigner.enteredPassword,
stack.uuid)
} else {
success = walletModel.transactionsView.transferTokens(
selectFromAccount.selectedAccount.address,
selectRecipient.selectedRecipient.address, selectRecipient.selectedRecipient.address,
txtAmount.selectedAsset.address, txtAmount.selectedAsset.address,
txtAmount.selectedAmount, txtAmount.selectedAmount,
@ -35,6 +47,12 @@ ModalPopup {
gasSelector.selectedGasPrice, gasSelector.selectedGasPrice,
transactionSigner.enteredPassword, transactionSigner.enteredPassword,
stack.uuid) stack.uuid)
}
if(!success){
sendingError.text = qsTr("Invalid transaction parameters")
return sendingError.open()
}
} }
TransactionStackView { TransactionStackView {