fix(@wallet): estimate gas fail when not enough fund

fixes #5267
This commit is contained in:
Anthony Laibe 2022-04-05 15:38:45 +02:00 committed by Anthony Laibe
parent f18e09769a
commit b1775357c3
2 changed files with 36 additions and 34 deletions

View File

@ -110,7 +110,6 @@ QtObject:
proc estimateGas*(self: View, from_addr: string, to: string, assetAddress: string, value: string, data: string): string {.slot.} = proc estimateGas*(self: View, from_addr: string, to: string, assetAddress: string, value: string, data: string): string {.slot.} =
result = self.delegate.estimateGas(from_addr, to, assetAddress, value, data) result = self.delegate.estimateGas(from_addr, to, assetAddress, value, data)
result = self.delegate.estimateGas(from_addr, to, assetAddress, value, data)
proc transactionSent*(self: View, txResult: string) {.signal.} proc transactionSent*(self: View, txResult: string) {.signal.}

View File

@ -215,49 +215,52 @@ QtObject:
self.threadpool.start(arg) self.threadpool.start(arg)
proc estimateGas*( proc estimateGas*(
self: Service, self: Service,
from_addr: string, from_addr: string,
to: string, to: string,
assetAddress: string, assetAddress: string,
value: string, value: string,
data: string = "" data: string = ""
): string {.slot.} = ): string {.slot.} =
var response: RpcResponse[JsonNode] var response: RpcResponse[JsonNode]
# TODO make this async # TODO make this async
try: if assetAddress != ZERO_ADDRESS and not assetAddress.isEmptyOrWhitespace:
if assetAddress != ZERO_ADDRESS and not assetAddress.isEmptyOrWhitespace: var tx = buildTokenTransaction(
var tx = buildTokenTransaction( parseAddress(from_addr),
parseAddress(from_addr), parseAddress(assetAddress)
parseAddress(assetAddress) )
) let networkType = self.settingsService.getCurrentNetwork().toNetworkType()
let networkType = self.settingsService.getCurrentNetwork().toNetworkType() let network = self.networkService.getNetwork(networkType)
let network = self.networkService.getNetwork(networkType) let token = self.tokenService.findTokenByAddress(network, parseAddress(assetAddress))
let token = self.tokenService.findTokenByAddress(network, parseAddress(assetAddress))
if token == nil: if token == nil:
raise newException(ValueError, fmt"Could not find ERC-20 contract with address '{assetAddress}' for the current network") raise newException(ValueError, fmt"Could not find ERC-20 contract with address '{assetAddress}' for the current network")
let transfer = Transfer(to: parseAddress(to), value: conversion.eth2Wei(parseFloat(value), token.decimals)) let transfer = Transfer(to: parseAddress(to), value: conversion.eth2Wei(parseFloat(value), token.decimals))
let transferproc = ERC20_procS.toTable["transfer"] let transferproc = ERC20_procS.toTable["transfer"]
var success: bool var success: bool
try:
let gas = transferproc.estimateGas(tx, transfer, success) let gas = transferproc.estimateGas(tx, transfer, success)
let res = fromHex[int](gas) let res = fromHex[int](gas)
result = $(%* { "result": res, "success": success }) return $(%* { "result": res, "success": success })
else: except Exception as e:
var tx = ens_utils.buildTransaction( error "Error estimating gas", msg = e.msg
parseAddress(from_addr), return $(%* { "result": "-1", "success": false, "error": { "message": e.msg } })
eth2Wei(parseFloat(value), 18),
data = data
)
tx.to = parseAddress(to).some
response = eth.estimateGas(%*[%tx])
let res = fromHex[int](response.result.getStr) var tx = ens_utils.buildTransaction(
result = $(%* { "result": %res, "success": true }) parseAddress(from_addr),
eth2Wei(parseFloat(value), 18),
data = data
)
tx.to = parseAddress(to).some
try:
response = eth.estimateGas(%*[%tx])
let res = fromHex[int](response.result.getStr)
except Exception as e: except Exception as e:
error "Error estimating gas", msg = e.msg error "Error estimating gas", msg = e.msg
result = $(%* { "result": "-1", "success": false, "error": { "message": e.msg } }) return $(%* { "result": "-1", "success": false })
proc transferEth*( proc transferEth*(
self: Service, self: Service,