fix: fill missing fees in tx object received from dApp

Fixes #16528
This commit is contained in:
Sale Djenic 2024-10-30 22:37:42 +01:00 committed by saledjenic
parent 623333ab8c
commit e3128587d8
8 changed files with 274 additions and 32 deletions

View File

@ -26,7 +26,6 @@ proc convertFeesInfoToHex*(feesInfoJson: string): string =
parsedJson = parseJson(feesInfoJson) parsedJson = parseJson(feesInfoJson)
maxFeePerGasFloat = getFloatFromJson(parsedJson, "maxFeePerGas") maxFeePerGasFloat = getFloatFromJson(parsedJson, "maxFeePerGas")
a = maxFeePerGasFloat * 1e9
maxFeePerGasWei = uint64(maxFeePerGasFloat * 1e9) maxFeePerGasWei = uint64(maxFeePerGasFloat * 1e9)
maxPriorityFeePerGasFloat = getFloatFromJson(parsedJson, "maxPriorityFeePerGas") maxPriorityFeePerGasFloat = getFloatFromJson(parsedJson, "maxPriorityFeePerGas")

View File

@ -74,6 +74,8 @@ DappsComboBox {
signal signRequestAccepted(string connectionId, string requestId) signal signRequestAccepted(string connectionId, string requestId)
signal signRequestRejected(string connectionId, string requestId) signal signRequestRejected(string connectionId, string requestId)
signal subscribeForFeeUpdates(string connectionId, string requestId)
/// Response to pairingValidationRequested /// Response to pairingValidationRequested
function pairingValidated(validationState) { function pairingValidated(validationState) {
if (pairWCLoader.item) { if (pairWCLoader.item) {
@ -327,11 +329,27 @@ DappsComboBox {
signingTransaction: !!request.method && (request.method === SessionRequest.methods.signTransaction.name signingTransaction: !!request.method && (request.method === SessionRequest.methods.signTransaction.name
|| request.method === SessionRequest.methods.sendTransaction.name) || request.method === SessionRequest.methods.sendTransaction.name)
requestPayload: request.preparedData requestPayload: {
try {
const data = JSON.parse(request.preparedData)
delete data.maxFeePerGas
delete data.maxPriorityFeePerGas
delete data.gasPrice
return JSON.stringify(data, null, 2)
} catch(_) {
return request.preparedData
}
}
expirationSeconds: request.expirationTimestamp ? request.expirationTimestamp - requestTimestamp.getTime() / 1000 expirationSeconds: request.expirationTimestamp ? request.expirationTimestamp - requestTimestamp.getTime() / 1000
: 0 : 0
hasExpiryDate: !!request.expirationTimestamp hasExpiryDate: !!request.expirationTimestamp
onOpened: {
root.subscribeForFeeUpdates(request.topic, request.id)
}
onClosed: { onClosed: {
Qt.callLater(rejectRequest) Qt.callLater(rejectRequest)
} }

View File

@ -165,6 +165,7 @@ Item {
onConnectionDeclined: (pairingId) => wcService.rejectPairSession(pairingId) onConnectionDeclined: (pairingId) => wcService.rejectPairSession(pairingId)
onSignRequestAccepted: (connectionId, requestId) => wcService.sign(connectionId, requestId) onSignRequestAccepted: (connectionId, requestId) => wcService.sign(connectionId, requestId)
onSignRequestRejected: (connectionId, requestId) => wcService.rejectSign(connectionId, requestId, false /*hasError*/) onSignRequestRejected: (connectionId, requestId) => wcService.rejectSign(connectionId, requestId, false /*hasError*/)
onSubscribeForFeeUpdates: (connectionId, requestId) => wcService.subscribeForFeeUpdates(connectionId, requestId)
Connections { Connections {
target: dappsWorkflow.wcService target: dappsWorkflow.wcService

View File

@ -24,11 +24,17 @@ SQUtils.QObject {
property alias requestsModel: requests property alias requestsModel: requests
function rejectSessionRequest(topic, id, hasError) { function rejectSessionRequest(topic, id, hasError) {
d.unsubscribeForFeeUpdates(topic, id)
sdk.rejectSessionRequest(topic, id, hasError) sdk.rejectSessionRequest(topic, id, hasError)
} }
function subscribeForFeeUpdates(topic, id) {
d.subscribeForFeeUpdates(topic, id)
}
/// Beware, it will fail if called multiple times before getting an answer /// Beware, it will fail if called multiple times before getting an answer
function authenticate(topic, id, address, payload) { function authenticate(topic, id, address, payload) {
d.unsubscribeForFeeUpdates(topic, id)
return store.authenticateUser(topic, id, address, payload) return store.authenticateUser(topic, id, address, payload)
} }
@ -158,6 +164,108 @@ SQUtils.QObject {
SQUtils.QObject { SQUtils.QObject {
id: d id: d
property int selectedFeesMode: Constants.FeesMode.Medium
function getFeesForFeesMode(feesObj) {
if (!(feesObj.hasOwnProperty("maxFeePerGasL") &&
feesObj.hasOwnProperty("maxFeePerGasM") &&
feesObj.hasOwnProperty("maxFeePerGasH"))) {
throw new Error("inappropriate fees object provided")
}
switch (d.selectedFeesMode) {
case Constants.FeesMode.Low:
return feesObj.maxFeePerGasL
case Constants.FeesMode.Medium:
return feesObj.maxFeePerGasM
case Constants.FeesMode.High:
return feesObj.maxFeePerGasH
default:
throw new Error("unknown selected mode")
}
}
property var feesSubscriptions: []
function findSubscriptionIndex(topic, id) {
for (let i = 0; i < d.feesSubscriptions.length; i++) {
const subscription = d.feesSubscriptions[i]
if (subscription.topic == topic && subscription.id == id) {
return i
}
}
return -1
}
function findChainIndex(chainId) {
for (let i = 0; i < feesSubscription.chainIds.length; i++) {
if (feesSubscription.chainIds[i] == chainId) {
return i
}
}
return -1
}
function subscribeForFeeUpdates(topic, id) {
const request = requests.findRequest(topic, id)
if (request === null) {
console.error("Error finding event for subscribing for fees for topic", topic, "id", id)
return
}
const index = d.findSubscriptionIndex(topic, id)
if (index >= 0) {
return
}
d.feesSubscriptions.push({
topic: topic,
id: id,
chainId: request.chainId
})
for (let i = 0; i < feesSubscription.chainIds.length; i++) {
if (feesSubscription.chainIds == request.chainId) {
return
}
}
feesSubscription.chainIds.push(request.chainId)
feesSubscription.restart()
}
function unsubscribeForFeeUpdates(topic, id) {
const index = d.findSubscriptionIndex(topic, id)
if (index == -1) {
return
}
const chainId = d.feesSubscriptions[index].chainId
d.feesSubscriptions.splice(index, 1)
const chainIndex = d.findChainIndex(chainId)
if (index == -1) {
return
}
let found = false
for (let i = 0; i < d.feesSubscriptions.length; i++) {
if (d.feesSubscriptions[i].chainId == chainId) {
found = true
break
}
}
if (found) {
return
}
feesSubscription.chainIds.splice(chainIndex, 1)
if (feesSubscription.chainIds.length == 0) {
feesSubscription.stop()
}
}
readonly property QtObject resolveAsyncResult: QtObject { readonly property QtObject resolveAsyncResult: QtObject {
readonly property int error: 0 readonly property int error: 0
readonly property int ok: 1 readonly property int ok: 1
@ -193,7 +301,7 @@ SQUtils.QObject {
return { obj: null, code: resolveAsyncResult.error } return { obj: null, code: resolveAsyncResult.error }
} }
const interpreted = d.prepareData(method, data) const interpreted = d.prepareData(method, data, chainId)
const enoughFunds = !d.isTransactionMethod(method) const enoughFunds = !d.isTransactionMethod(method)
const requestExpiry = event.params.request.expiryTimestamp const requestExpiry = event.params.request.expiryTimestamp
@ -232,27 +340,7 @@ SQUtils.QObject {
obj.resolveDappInfoFromSession(session) obj.resolveDappInfoFromSession(session)
root.sessionRequest(obj.id) root.sessionRequest(obj.id)
if (!d.isTransactionMethod(method)) { d.updateFeesParamsToPassedObj(obj)
return
}
obj.estimatedTimeCategory = getEstimatedTimeInterval(data, method, obj.chainId)
const mainNet = lookupMainnetNetwork()
let mainChainId = obj.chainId
if (!!mainNet) {
mainChainId = mainNet.chainId
} else {
console.error("Error finding mainnet network")
}
let st = getEstimatedFeesStatus(data, method, obj.chainId, mainChainId)
let fundsStatus = checkFundsStatus(st.feesInfo.maxFees, st.feesInfo.l1GasFee, obj.accountAddress, obj.chainId, mainNet.chainId, interpreted.value)
obj.fiatMaxFees = st.fiatMaxFees
obj.ethMaxFees = st.maxFeesEth
obj.haveEnoughFunds = fundsStatus.haveEnoughFunds
obj.haveEnoughFees = fundsStatus.haveEnoughForFees
obj.feesInfo = st.feesInfo
}) })
return { return {
@ -261,6 +349,32 @@ SQUtils.QObject {
} }
} }
function updateFeesParamsToPassedObj(obj) {
if (!d.isTransactionMethod(obj.method)) {
return
}
obj.estimatedTimeCategory = getEstimatedTimeInterval(obj.data, obj.method, obj.chainId)
const mainNet = lookupMainnetNetwork()
let mainChainId = obj.chainId
if (!!mainNet) {
mainChainId = mainNet.chainId
} else {
console.error("Error finding mainnet network")
}
const interpreted = d.prepareData(obj.method, obj.data, obj.chainId)
let st = getEstimatedFeesStatus(obj.data, obj.method, obj.chainId, mainChainId)
let fundsStatus = checkFundsStatus(st.feesInfo.maxFees, st.feesInfo.l1GasFee, obj.accountAddress, obj.chainId, mainNet.chainId, interpreted.value)
obj.fiatMaxFees = st.fiatMaxFees
obj.ethMaxFees = st.maxFeesEth
obj.haveEnoughFunds = fundsStatus.haveEnoughFunds
obj.haveEnoughFees = fundsStatus.haveEnoughForFees
obj.feesInfo = st.feesInfo
}
/// returns { /// returns {
/// accountAddress /// accountAddress
/// success /// success
@ -628,7 +742,7 @@ SQUtils.QObject {
// preparedData, // preparedData,
// value // null or ETH Big number // value // null or ETH Big number
// } // }
function prepareData(method, data) { function prepareData(method, data, chainId) {
let payload = null let payload = null
switch(method) { switch(method) {
case SessionRequest.methods.personalSign.name: { case SessionRequest.methods.personalSign.name: {
@ -662,22 +776,44 @@ SQUtils.QObject {
if (d.isTransactionMethod(method)) { if (d.isTransactionMethod(method)) {
let txObj = d.getTxObject(method, data) let txObj = d.getTxObject(method, data)
let tx = Object.assign({}, txObj) let tx = Object.assign({}, txObj)
let fees = root.store.getSuggestedFees(chainId)
if (tx.value) { if (tx.value) {
value = hexToEth(tx.value) value = hexToEth(tx.value)
tx.value = value.toString() tx.value = value.toString()
} }
if (tx.maxFeePerGas) {
tx.maxFeePerGas = hexToGwei(tx.maxFeePerGas).toString() if (tx.hasOwnProperty("maxFeePerGas")) {
if (tx.maxFeePerGas) {
tx.maxFeePerGas = hexToGwei(tx.maxFeePerGas).toString()
} else if (fees.eip1559Enabled) {
try {
tx.maxFeePerGas = d.getFeesForFeesMode(fees)
} catch (e) {
console.warn(e)
}
}
} }
if (tx.maxPriorityFeePerGas) {
tx.maxPriorityFeePerGas = hexToGwei(tx.maxPriorityFeePerGas).toString() if (tx.hasOwnProperty("maxPriorityFeePerGas")) {
if (tx.maxPriorityFeePerGas) {
tx.maxPriorityFeePerGas = hexToGwei(tx.maxPriorityFeePerGas).toString()
} else if (fees.eip1559Enabled) {
tx.maxPriorityFeePerGas = fees.maxPriorityFeePerGas
}
} }
if (tx.gasPrice) {
tx.gasPrice = hexToGwei(tx.gasPrice) if (tx.hasOwnProperty("gasPrice")) {
if (tx.gasPrice) {
tx.gasPrice = hexToGwei(tx.gasPrice)
} else if (!fees.eip1559Enabled) {
tx.gasPrice = fees.gasPrice
}
} }
if (tx.gasLimit) { if (tx.gasLimit) {
tx.gasLimit = parseInt(root.store.hexToDec(tx.gasLimit)) tx.gasLimit = parseInt(root.store.hexToDec(tx.gasLimit))
} }
if (tx.nonce) { if (tx.nonce) {
tx.nonce = parseInt(root.store.hexToDec(tx.nonce)) tx.nonce = parseInt(root.store.hexToDec(tx.nonce))
} }
@ -723,4 +859,30 @@ SQUtils.QObject {
sourceId: Constants.DAppConnectors.WalletConnect sourceId: Constants.DAppConnectors.WalletConnect
} }
} }
Timer {
id: feesSubscription
property var chainIds: []
interval: 5000
repeat: true
running: Qt.application.state === Qt.ApplicationActive
onTriggered: {
for (let i = 0; i < chainIds.length; i++) {
for (let j = 0; j < d.feesSubscriptions.length; j++) {
let subscription = d.feesSubscriptions[j]
if (subscription.chainId == chainIds[i]) {
let request = requests.findRequest(subscription.topic, subscription.id)
if (request === null) {
console.error("Error updating fees for topic", subscription.topic, "id", subscription.id)
continue
}
d.updateFeesParamsToPassedObj(request)
}
}
}
}
}
} }

View File

@ -63,6 +63,10 @@ QObject {
requestHandler.rejectSessionRequest(topic, id, hasError) requestHandler.rejectSessionRequest(topic, id, hasError)
} }
function subscribeForFeeUpdates(topic, id) {
requestHandler.subscribeForFeeUpdates(topic, id)
}
/// Validates the pairing URI /// Validates the pairing URI
function validatePairingUri(uri) { function validatePairingUri(uri) {
d.validatePairingUri(uri) d.validatePairingUri(uri)

View File

@ -11,6 +11,7 @@ import AppLayouts.Wallet.popups 1.0
import AppLayouts.Wallet.panels 1.0 import AppLayouts.Wallet.panels 1.0
import shared.popups.walletconnect.panels 1.0 import shared.popups.walletconnect.panels 1.0
import shared.panels 1.0
import utils 1.0 import utils 1.0
@ -93,6 +94,7 @@ SignTransactionModalBase {
font.pixelSize: Theme.additionalTextSize font.pixelSize: Theme.additionalTextSize
} }
StatusTextWithLoadingState { StatusTextWithLoadingState {
id: maxFees
Layout.fillWidth: true Layout.fillWidth: true
objectName: "footerFiatFeesText" objectName: "footerFiatFeesText"
text: formatBigNumber(root.fiatFees, root.currentCurrency) text: formatBigNumber(root.fiatFees, root.currentCurrency)
@ -103,6 +105,18 @@ SignTransactionModalBase {
when: !root.hasFees when: !root.hasFees
value: qsTr("No fees") value: qsTr("No fees")
} }
onTextChanged: {
if (text === "") {
return
}
maxFeesAnimation.restart()
}
AnimatedText {
id: maxFeesAnimation
target: maxFees
}
} }
} }
ColumnLayout { ColumnLayout {
@ -114,9 +128,22 @@ SignTransactionModalBase {
font.pixelSize: Theme.additionalTextSize font.pixelSize: Theme.additionalTextSize
} }
StatusTextWithLoadingState { StatusTextWithLoadingState {
id: estimatedTime
objectName: "footerEstimatedTime" objectName: "footerEstimatedTime"
text: root.estimatedTime text: root.estimatedTime
loading: root.feesLoading loading: root.feesLoading
onTextChanged: {
if (text === "") {
return
}
estimatedTimeAnimation.restart()
}
AnimatedText {
id: estimatedTimeAnimation
target: estimatedTime
}
} }
} }
} }
@ -169,6 +196,7 @@ SignTransactionModalBase {
ColumnLayout { ColumnLayout {
spacing: 2 spacing: 2
StatusTextWithLoadingState { StatusTextWithLoadingState {
id: fiatFees
objectName: "fiatFeesText" objectName: "fiatFeesText"
Layout.alignment: Qt.AlignRight Layout.alignment: Qt.AlignRight
text: formatBigNumber(root.fiatFees, root.currentCurrency) text: formatBigNumber(root.fiatFees, root.currentCurrency)
@ -176,8 +204,21 @@ SignTransactionModalBase {
font.pixelSize: Theme.additionalTextSize font.pixelSize: Theme.additionalTextSize
loading: root.feesLoading loading: root.feesLoading
customColor: root.enoughFundsForFees ? Theme.palette.directColor1 : Theme.palette.dangerColor1 customColor: root.enoughFundsForFees ? Theme.palette.directColor1 : Theme.palette.dangerColor1
onTextChanged: {
if (text === "") {
return
}
fiatFeesAnimation.restart()
}
AnimatedText {
id: fiatFeesAnimation
target: fiatFees
}
} }
StatusTextWithLoadingState { StatusTextWithLoadingState {
id: cryptoFees
objectName: "cryptoFeesText" objectName: "cryptoFeesText"
Layout.alignment: Qt.AlignRight Layout.alignment: Qt.AlignRight
text: formatBigNumber(root.cryptoFees, Constants.ethToken) text: formatBigNumber(root.cryptoFees, Constants.ethToken)
@ -185,6 +226,18 @@ SignTransactionModalBase {
font.pixelSize: Theme.additionalTextSize font.pixelSize: Theme.additionalTextSize
customColor: root.enoughFundsForFees ? Theme.palette.baseColor1 : Theme.palette.dangerColor1 customColor: root.enoughFundsForFees ? Theme.palette.baseColor1 : Theme.palette.dangerColor1
loading: root.feesLoading loading: root.feesLoading
onTextChanged: {
if (text === "") {
return
}
cryptoFeesAnimation.restart()
}
AnimatedText {
id: cryptoFeesAnimation
target: cryptoFees
}
} }
} }
] ]

View File

@ -33,7 +33,6 @@ Rectangle {
anchors.fill: parent anchors.fill: parent
anchors.margins: 20 anchors.margins: 20
text: root.payloadToDisplay
font.pixelSize: Theme.additionalTextSize font.pixelSize: Theme.additionalTextSize
lineHeightMode: Text.FixedHeight lineHeightMode: Text.FixedHeight
lineHeight: 18 lineHeight: 18

View File

@ -1098,6 +1098,12 @@ QtObject {
} }
} }
enum FeesMode {
Low,
Medium,
High
}
enum LoginType { enum LoginType {
Password, Password,
Biometrics, Biometrics,