refactor: use wallet model instead of lib status
This commit is contained in:
parent
f1e1462529
commit
22d9c3be5d
|
@ -280,17 +280,20 @@ QtObject:
|
|||
proc transactionSent(self: WalletView, txResult: string) {.slot.} =
|
||||
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:
|
||||
spawnAndSend(self, "transactionSent") do:
|
||||
$(%status_wallet.sendTransaction(tokens, from_addr, to, assetAddress, value, gas, gasPrice, password))
|
||||
let wallet = self.status.wallet
|
||||
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:
|
||||
self.transactionSent($(%*{
|
||||
"error": %*{
|
||||
"message": e.msg
|
||||
}
|
||||
}))
|
||||
result = $(%* { "error": %* { "message": %e.msg }})
|
||||
|
||||
proc getDefaultAccount*(self: WalletView): string {.slot.} =
|
||||
self.currentAccount.address
|
||||
|
|
|
@ -9,6 +9,7 @@ import ./eth/contracts as contractMethods
|
|||
import eth/common/eth_types
|
||||
import ./types
|
||||
import ../signals/types as signal_types
|
||||
import ../transactions
|
||||
|
||||
proc getWalletAccounts*(): seq[WalletAccount] =
|
||||
try:
|
||||
|
@ -68,46 +69,6 @@ proc getTransfersByAddress*(address: string): seq[types.Transaction] =
|
|||
let msg = getCurrentExceptionMsg()
|
||||
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 =
|
||||
let payload = %* [address, "latest"]
|
||||
let response = parseJson(callPrivateRPC("eth_getBalance", payload))
|
||||
|
@ -149,6 +110,7 @@ proc getPendingOutboundTransactionsByAddress*(address: string): string =
|
|||
let payload = %* [address]
|
||||
result = callPrivateRPC("wallet_getPendingOutboundTransactionsByAddress", payload)
|
||||
|
||||
|
||||
proc deletePendingTransaction*(transactionHash: string) =
|
||||
let payload = %* [transactionHash]
|
||||
discard callPrivateRPC("wallet_deletePendingTransaction", payload)
|
||||
|
|
Loading…
Reference in New Issue