refactor: use wallet model instead of lib status

This commit is contained in:
Jonathan Rainville 2020-09-11 16:01:37 -04:00 committed by Iuri Matias
parent f1e1462529
commit 22d9c3be5d
2 changed files with 14 additions and 49 deletions

View File

@ -280,17 +280,20 @@ QtObject:
proc transactionSent(self: WalletView, txResult: string) {.slot.} = proc transactionSent(self: WalletView, txResult: string) {.slot.} =
self.transactionWasSent(txResult) self.transactionWasSent(txResult)
proc sendTransaction*(self: WalletView, from_addr: string, to: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string) {.slot.} =
let tokens = self.status.wallet.tokens proc sendTransaction*(self: WalletView, from_addr: string, to: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string): string {.slot.} =
try: try:
spawnAndSend(self, "transactionSent") do: let wallet = self.status.wallet
$(%status_wallet.sendTransaction(tokens, from_addr, to, assetAddress, value, gas, gasPrice, password)) if assetAddress != ZERO_ADDRESS and not assetAddress.isEmptyOrWhitespace:
spawnAndSend(self, "transactionSent") do:
let response = wallet.sendTokenTransaction(from_addr, to, assetAddress, value, gas, gasPrice, password)
$(%* { "result": %response })
else:
spawnAndSend(self, "transactionSent") do:
let response = wallet.sendTransaction(from_addr, to, value, gas, gasPrice, password)
$(%* { "result": %response })
except RpcException as e: except RpcException as e:
self.transactionSent($(%*{ result = $(%* { "error": %* { "message": %e.msg }})
"error": %*{
"message": e.msg
}
}))
proc getDefaultAccount*(self: WalletView): string {.slot.} = proc getDefaultAccount*(self: WalletView): string {.slot.} =
self.currentAccount.address self.currentAccount.address

View File

@ -9,6 +9,7 @@ import ./eth/contracts as contractMethods
import eth/common/eth_types import eth/common/eth_types
import ./types import ./types
import ../signals/types as signal_types import ../signals/types as signal_types
import ../transactions
proc getWalletAccounts*(): seq[WalletAccount] = proc getWalletAccounts*(): seq[WalletAccount] =
try: try:
@ -68,46 +69,6 @@ proc getTransfersByAddress*(address: string): seq[types.Transaction] =
let msg = getCurrentExceptionMsg() let msg = getCurrentExceptionMsg()
error "Failed getting wallet account transactions", msg error "Failed getting wallet account transactions", msg
proc sendTransaction*(tx: EthSend, password: string): RpcResponse =
let responseStr = core.sendTransaction($(%tx), password)
result = Json.decode(responseStr, RpcResponse)
if not result.error.isNil:
raise newException(RpcException, "Error sending transaction: " & result.error.message)
trace "Transaction sent succesfully", hash=result
proc sendTransaction*(tokens: JsonNode, source, to, assetAddress, value, gas, gasPrice, password: string): RpcResponse =
var
weiValue = eth2Wei(parseFloat(value), 18) # ETH
data = ""
toAddr = eth_utils.parseAddress(to)
let gasPriceInWei = gwei2Wei(parseFloat(gasPrice))
# TODO: this code needs to be tested with testnet assets (to be implemented in
# a future PR
if assetAddress != constants.ZERO_ADDRESS and not assetAddress.isEmptyOrWhitespace:
let
token = tokens.first("address", assetAddress)
contract = contractMethods.getContract("snt")
transfer = coder.Transfer(to: toAddr, value: eth2Wei(parseFloat(value), token["decimals"].getInt))
weiValue = 0.u256
let transferThing = contract.methods["transfer"]
data = transferThing.encodeAbi(transfer)
toAddr = eth_utils.parseAddress(assetAddress)
let tx = EthSend(
source: eth_utils.parseAddress(source),
to: toAddr.some,
gas: (if gas.isEmptyOrWhitespace: Quantity.none else: Quantity(cast[uint64](parseFloat(gas).toUInt64)).some),
gasPrice: (if gasPrice.isEmptyOrWhitespace: int.none else: gwei2Wei(parseFloat(gasPrice)).truncate(int).some),
value: weiValue.some,
data: data
)
try:
result = sendTransaction(tx, password)
except RpcException as e:
raise
proc getBalance*(address: string): string = proc getBalance*(address: string): string =
let payload = %* [address, "latest"] let payload = %* [address, "latest"]
let response = parseJson(callPrivateRPC("eth_getBalance", payload)) let response = parseJson(callPrivateRPC("eth_getBalance", payload))
@ -149,6 +110,7 @@ proc getPendingOutboundTransactionsByAddress*(address: string): string =
let payload = %* [address] let payload = %* [address]
result = callPrivateRPC("wallet_getPendingOutboundTransactionsByAddress", payload) result = callPrivateRPC("wallet_getPendingOutboundTransactionsByAddress", payload)
proc deletePendingTransaction*(transactionHash: string) = proc deletePendingTransaction*(transactionHash: string) =
let payload = %* [transactionHash] let payload = %* [transactionHash]
discard callPrivateRPC("wallet_deletePendingTransaction", payload) discard callPrivateRPC("wallet_deletePendingTransaction", payload)