feat: enable sending contract calls from the browser
This commit is contained in:
parent
0c1156b33c
commit
70177b803a
|
@ -113,15 +113,21 @@ QtObject:
|
||||||
let request = data.request.parseJson
|
let request = data.request.parseJson
|
||||||
let fromAddress = request["params"][0]["from"].getStr()
|
let fromAddress = request["params"][0]["from"].getStr()
|
||||||
let to = request["params"][0]["to"].getStr()
|
let to = request["params"][0]["to"].getStr()
|
||||||
let value = request["params"][0]["value"].getStr()
|
let value = if (request["params"][0]["value"] != nil):
|
||||||
|
request["params"][0]["value"].getStr()
|
||||||
|
else:
|
||||||
|
"0"
|
||||||
let password = request["password"].getStr()
|
let password = request["password"].getStr()
|
||||||
let selectedGasLimit = request["selectedGasLimit"].getStr()
|
let selectedGasLimit = request["selectedGasLimit"].getStr()
|
||||||
let selectedGasPrice = request["selectedGasPrice"].getStr()
|
let selectedGasPrice = request["selectedGasPrice"].getStr()
|
||||||
|
let txData = if (request["params"][0]["data"] != nil):
|
||||||
|
request["params"][0]["data"].getStr()
|
||||||
|
else:
|
||||||
|
""
|
||||||
|
|
||||||
var success: bool
|
var success: bool
|
||||||
# TODO make this async
|
# TODO make this async
|
||||||
let response = status.wallet.sendTransaction(fromAddress, to, value, selectedGasLimit, selectedGasPrice, password, success)
|
let response = status.wallet.sendTransaction(fromAddress, to, value, selectedGasLimit, selectedGasPrice, password, success, txData)
|
||||||
debug "Response", response, success
|
|
||||||
let errorMessage = if not success:
|
let errorMessage = if not success:
|
||||||
if response == "":
|
if response == "":
|
||||||
"web3-response-error"
|
"web3-response-error"
|
||||||
|
@ -134,7 +140,11 @@ QtObject:
|
||||||
"type": ResponseTypes.Web3SendAsyncCallback,
|
"type": ResponseTypes.Web3SendAsyncCallback,
|
||||||
"messageId": data.messageId,
|
"messageId": data.messageId,
|
||||||
"error": errorMessage,
|
"error": errorMessage,
|
||||||
"result": if (success): response else: ""
|
"result": {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": data.payload.id,
|
||||||
|
"result": if (success): response else: ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error "Error sending the transaction", msg = e.msg
|
error "Error sending the transaction", msg = e.msg
|
||||||
|
|
|
@ -8,12 +8,13 @@ import
|
||||||
libstatus/types
|
libstatus/types
|
||||||
from libstatus/utils as status_utils import toUInt64, gwei2Wei, parseAddress
|
from libstatus/utils as status_utils import toUInt64, gwei2Wei, parseAddress
|
||||||
|
|
||||||
proc buildTransaction*(source: Address, value: Uint256, gas = "", gasPrice = ""): EthSend =
|
proc buildTransaction*(source: Address, value: Uint256, gas = "", gasPrice = "", data = ""): EthSend =
|
||||||
result = EthSend(
|
result = EthSend(
|
||||||
source: source,
|
source: source,
|
||||||
value: value.some,
|
value: value.some,
|
||||||
gas: (if gas.isEmptyOrWhitespace: Quantity.none else: Quantity(cast[uint64](parseFloat(gas).toUInt64)).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)
|
gasPrice: (if gasPrice.isEmptyOrWhitespace: int.none else: gwei2Wei(parseFloat(gasPrice)).truncate(int).some),
|
||||||
|
data: data
|
||||||
)
|
)
|
||||||
|
|
||||||
proc buildTokenTransaction*(source, contractAddress: Address, gas = "", gasPrice = ""): EthSend =
|
proc buildTokenTransaction*(source, contractAddress: Address, gas = "", gasPrice = ""): EthSend =
|
||||||
|
|
|
@ -118,10 +118,10 @@ proc estimateTokenGas*(self: WalletModel, source, to, assetAddress, value: strin
|
||||||
let response = contract.methods["transfer"].estimateGas(tx, transfer, success)
|
let response = contract.methods["transfer"].estimateGas(tx, transfer, success)
|
||||||
result = fromHex[int](response)
|
result = fromHex[int](response)
|
||||||
|
|
||||||
proc sendTransaction*(self: WalletModel, source, to, value, gas, gasPrice, password: string, success: var bool): string =
|
proc sendTransaction*(self: WalletModel, source, to, value, gas, gasPrice, password: string, success: var bool, data = ""): string =
|
||||||
var tx = transactions.buildTransaction(
|
var tx = transactions.buildTransaction(
|
||||||
parseAddress(source),
|
parseAddress(source),
|
||||||
eth2Wei(parseFloat(value), 18), gas, gasPrice
|
eth2Wei(parseFloat(value), 18), gas, gasPrice, data
|
||||||
)
|
)
|
||||||
tx.to = parseAddress(to).some
|
tx.to = parseAddress(to).some
|
||||||
|
|
||||||
|
|
|
@ -193,7 +193,6 @@ Item {
|
||||||
|
|
||||||
// TODO change sendTransaction function to the postMessage one
|
// TODO change sendTransaction function to the postMessage one
|
||||||
sendDialog.sendTransaction = function (selectedGasLimit, selectedGasPrice, enteredPassword) {
|
sendDialog.sendTransaction = function (selectedGasLimit, selectedGasPrice, enteredPassword) {
|
||||||
console.log('OK', selectedGasLimit, selectedGasPrice, enteredPassword)
|
|
||||||
request.payload.selectedGasLimit = selectedGasLimit
|
request.payload.selectedGasLimit = selectedGasLimit
|
||||||
request.payload.selectedGasPrice = selectedGasPrice
|
request.payload.selectedGasPrice = selectedGasPrice
|
||||||
request.payload.password = enteredPassword
|
request.payload.password = enteredPassword
|
||||||
|
@ -215,7 +214,7 @@ Item {
|
||||||
toastMessage.source = "../../img/loading.svg"
|
toastMessage.source = "../../img/loading.svg"
|
||||||
toastMessage.iconColor = Style.current.primary
|
toastMessage.iconColor = Style.current.primary
|
||||||
toastMessage.iconRotates = true
|
toastMessage.iconRotates = true
|
||||||
toastMessage.link = `${walletModel.etherscanLink}/${responseOnj.result}`
|
toastMessage.link = `${walletModel.etherscanLink}/${responseObj.result}`
|
||||||
toastMessage.open()
|
toastMessage.open()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.message.includes("could not decrypt key with given password")){
|
if (e.message.includes("could not decrypt key with given password")){
|
||||||
|
|
Loading…
Reference in New Issue