fix(dapps) minor wallet connect fixes and improvements

Also

- don't report error in case of status-go returning nil instead
of an empty array.
- fix misleading alias to AmountsArithmetic
- minor improvements requested in previous discussions

Updates: #15707
This commit is contained in:
Stefan 2024-07-22 14:33:47 +03:00 committed by Stefan Dunca
parent 166a0376ff
commit dc43a40a76
3 changed files with 39 additions and 33 deletions

View File

@ -49,6 +49,7 @@ proc disconnectSession*(topic: string): bool =
error "wallet_disconnectWalletConnectSession failed: ", "msg", e.msg error "wallet_disconnectWalletConnectSession failed: ", "msg", e.msg
return false return false
# returns nil if error
proc getActiveSessions*(validAtTimestamp: int): JsonNode = proc getActiveSessions*(validAtTimestamp: int): JsonNode =
try: try:
let rpcRes = getWalletConnectActiveSessions(validAtTimestamp) let rpcRes = getWalletConnectActiveSessions(validAtTimestamp)
@ -57,7 +58,8 @@ proc getActiveSessions*(validAtTimestamp: int): JsonNode =
let jsonResultStr = rpcRes.result.getStr() let jsonResultStr = rpcRes.result.getStr()
if jsonResultStr == "null": if jsonResultStr == "null":
return nil # nil means error
return newJArray()
if rpcRes.result.kind != JArray: if rpcRes.result.kind != JArray:
error "Unexpected result kind: ", rpcRes.result.kind error "Unexpected result kind: ", rpcRes.result.kind

View File

@ -644,7 +644,7 @@ Item {
verify(eip155.hasOwnProperty("methods")) verify(eip155.hasOwnProperty("methods"))
verify(eip155.methods.length > 0) verify(eip155.methods.length > 0)
verify(eip155.hasOwnProperty("events")) verify(eip155.hasOwnProperty("events"))
verify(eip155.events.length > 0) compare(eip155.events.length, 2)
} }
} }

View File

@ -390,15 +390,15 @@ SQUtils.QObject {
tx = d.getTxObject(method, data) tx = d.getTxObject(method, data)
} }
let Math = SQUtils.AmountsArithmetic let BigOps = SQUtils.AmountsArithmetic
let gasLimit = Math.fromString("21000") let gasLimit = BigOps.fromString("21000")
let gasPrice, maxFeePerGas, maxPriorityFeePerGas let gasPrice, maxFeePerGas, maxPriorityFeePerGas
let l1GasFee = Math.fromNumber(0) let l1GasFee = BigOps.fromNumber(0)
// Beware, the tx values are standard blockchain hex big number values; the fees values are nim's float64 values, hence the complex conversions // Beware, the tx values are standard blockchain hex big number values; the fees values are nim's float64 values, hence the complex conversions
if (!!tx.maxFeePerGas && !!tx.maxPriorityFeePerGas) { if (!!tx.maxFeePerGas && !!tx.maxPriorityFeePerGas) {
let maxFeePerGasDec = root.store.hexToDec(tx.maxFeePerGas) let maxFeePerGasDec = root.store.hexToDec(tx.maxFeePerGas)
gasPrice = Math.fromString(maxFeePerGasDec) gasPrice = BigOps.fromString(maxFeePerGasDec)
// Source fees info from the incoming transaction for when we process it // Source fees info from the incoming transaction for when we process it
maxFeePerGas = maxFeePerGasDec maxFeePerGas = maxFeePerGasDec
let maxPriorityFeePerGasDec = root.store.hexToDec(tx.maxPriorityFeePerGas) let maxPriorityFeePerGasDec = root.store.hexToDec(tx.maxPriorityFeePerGas)
@ -408,11 +408,11 @@ SQUtils.QObject {
maxPriorityFeePerGas = fees.maxPriorityFeePerGas maxPriorityFeePerGas = fees.maxPriorityFeePerGas
if (fees.eip1559Enabled) { if (fees.eip1559Enabled) {
if (!!fees.maxFeePerGasM) { if (!!fees.maxFeePerGasM) {
gasPrice = Math.fromNumber(fees.maxFeePerGasM) gasPrice = BigOps.fromNumber(fees.maxFeePerGasM)
maxFeePerGas = fees.maxFeePerGasM maxFeePerGas = fees.maxFeePerGasM
} else if(!!tx.maxFeePerGas) { } else if(!!tx.maxFeePerGas) {
let maxFeePerGasDec = root.store.hexToDec(tx.maxFeePerGas) let maxFeePerGasDec = root.store.hexToDec(tx.maxFeePerGas)
gasPrice = Math.fromString(maxFeePerGasDec) gasPrice = BigOps.fromString(maxFeePerGasDec)
maxFeePerGas = maxFeePerGasDec maxFeePerGas = maxFeePerGasDec
} else { } else {
console.error("Error fetching maxFeePerGas from fees or tx objects") console.error("Error fetching maxFeePerGas from fees or tx objects")
@ -420,38 +420,38 @@ SQUtils.QObject {
} }
} else { } else {
if (!!fees.gasPrice) { if (!!fees.gasPrice) {
gasPrice = Math.fromNumber(fees.gasPrice) gasPrice = BigOps.fromNumber(fees.gasPrice)
} else { } else {
console.error("Error fetching suggested fees") console.error("Error fetching suggested fees")
return return
} }
} }
l1GasFee = Math.fromNumber(fees.l1GasFee) l1GasFee = BigOps.fromNumber(fees.l1GasFee)
} }
let maxFees = Math.times(gasLimit, gasPrice) let maxFees = BigOps.times(gasLimit, gasPrice)
return {maxFees, maxFeePerGas, maxPriorityFeePerGas, gasPrice, l1GasFee} return {maxFees, maxFeePerGas, maxPriorityFeePerGas, gasPrice, l1GasFee}
} }
// Returned values are Big numbers // Returned values are Big numbers
function getEstimatedFeesStatus(data, method, chainId, mainNetChainId) { function getEstimatedFeesStatus(data, method, chainId, mainNetChainId) {
let Math = SQUtils.AmountsArithmetic let BigOps = SQUtils.AmountsArithmetic
let feesInfo = getEstimatedMaxFees(data, method, chainId, mainNetChainId) let feesInfo = getEstimatedMaxFees(data, method, chainId, mainNetChainId)
let totalMaxFees = Math.sum(feesInfo.maxFees, feesInfo.l1GasFee) let totalMaxFees = BigOps.sum(feesInfo.maxFees, feesInfo.l1GasFee)
let maxFeesEth = Math.div(totalMaxFees, Math.fromNumber(1, 9)) let maxFeesEth = BigOps.div(totalMaxFees, BigOps.fromNumber(1, 9))
let maxFeesEthStr = maxFeesEth.toString() let maxFeesEthStr = maxFeesEth.toString()
let fiatMaxFeesStr = root.currenciesStore.getFiatValue(maxFeesEthStr, Constants.ethToken) let fiatMaxFeesStr = root.currenciesStore.getFiatValue(maxFeesEthStr, Constants.ethToken)
let fiatMaxFees = Math.fromString(fiatMaxFeesStr) let fiatMaxFees = BigOps.fromString(fiatMaxFeesStr)
let symbol = root.currenciesStore.currentCurrencySymbol let symbol = root.currenciesStore.currentCurrencySymbol
return {fiatMaxFees, maxFeesEth, symbol, feesInfo} return {fiatMaxFees, maxFeesEth, symbol, feesInfo}
} }
function getBalanceInEth(balances, address, chainId) { function getBalanceInEth(balances, address, chainId) {
const Math = SQUtils.AmountsArithmetic const BigOps = SQUtils.AmountsArithmetic
let accEth = SQUtils.ModelUtils.getFirstModelEntryIf(balances, (balance) => { let accEth = SQUtils.ModelUtils.getFirstModelEntryIf(balances, (balance) => {
return balance.account.toLowerCase() === address.toLowerCase() && balance.chainId === chainId return balance.account.toLowerCase() === address.toLowerCase() && balance.chainId === chainId
}) })
@ -459,13 +459,13 @@ SQUtils.QObject {
console.error("Error balance lookup for account ", address, " on chain ", chainId) console.error("Error balance lookup for account ", address, " on chain ", chainId)
return null return null
} }
let accountFundsWei = Math.fromString(accEth.balance) let accountFundsWei = BigOps.fromString(accEth.balance)
return Math.div(accountFundsWei, Math.fromNumber(1, 18)) return BigOps.div(accountFundsWei, BigOps.fromNumber(1, 18))
} }
// Returns {haveEnoughForFees, haveEnoughFunds} and true in case of error not to block request // Returns {haveEnoughForFees, haveEnoughFunds} and true in case of error not to block request
function checkFundsStatus(maxFees, l1GasFee, address, chainId, mainNetChainId, valueEth) { function checkFundsStatus(maxFees, l1GasFee, address, chainId, mainNetChainId, valueEth) {
let Math = SQUtils.AmountsArithmetic let BigOps = SQUtils.AmountsArithmetic
let haveEnoughForFees = true let haveEnoughForFees = true
let haveEnoughFunds = true let haveEnoughFunds = true
@ -481,25 +481,25 @@ SQUtils.QObject {
console.error("Error fetching chain balance") console.error("Error fetching chain balance")
return {haveEnoughForFees, haveEnoughFunds} return {haveEnoughForFees, haveEnoughFunds}
} }
haveEnoughFunds = Math.cmp(chainBalance, valueEth) >= 0 haveEnoughFunds = BigOps.cmp(chainBalance, valueEth) >= 0
if (haveEnoughFunds) { if (haveEnoughFunds) {
chainBalance = Math.sub(chainBalance, valueEth) chainBalance = BigOps.sub(chainBalance, valueEth)
if (chainId == mainNetChainId) { if (chainId == mainNetChainId) {
const finalFees = Math.sum(maxFees, l1GasFee) const finalFees = BigOps.sum(maxFees, l1GasFee)
let feesEth = Math.div(finalFees, Math.fromNumber(1, 9)) let feesEth = BigOps.div(finalFees, BigOps.fromNumber(1, 9))
haveEnoughForFees = Math.cmp(chainBalance, feesEth) >= 0 haveEnoughForFees = BigOps.cmp(chainBalance, feesEth) >= 0
} else { } else {
const feesChain = Math.div(maxFees, Math.fromNumber(1, 9)) const feesChain = BigOps.div(maxFees, BigOps.fromNumber(1, 9))
const haveEnoughOnChain = Math.cmp(chainBalance, feesChain) >= 0 const haveEnoughOnChain = BigOps.cmp(chainBalance, feesChain) >= 0
const mainBalance = getBalanceInEth(token.balances, address, mainNetChainId) const mainBalance = getBalanceInEth(token.balances, address, mainNetChainId)
if (!mainBalance) { if (!mainBalance) {
console.error("Error fetching mainnet balance") console.error("Error fetching mainnet balance")
return {haveEnoughForFees, haveEnoughFunds} return {haveEnoughForFees, haveEnoughFunds}
} }
const feesMain = Math.div(l1GasFee, Math.fromNumber(1, 9)) const feesMain = BigOps.div(l1GasFee, BigOps.fromNumber(1, 9))
const haveEnoughOnMain = Math.cmp(mainBalance, feesMain) >= 0 const haveEnoughOnMain = BigOps.cmp(mainBalance, feesMain) >= 0
haveEnoughForFees = haveEnoughOnChain && haveEnoughOnMain haveEnoughForFees = haveEnoughOnChain && haveEnoughOnMain
} }
@ -552,9 +552,13 @@ SQUtils.QObject {
payload = JSON.stringify(JSON.parse(stringPayload), null, 2) payload = JSON.stringify(JSON.parse(stringPayload), null, 2)
break break
} }
case SessionRequest.methods.signTransaction.name:
case SessionRequest.methods.sendTransaction.name:
// For transactions we process the data in a different way as follows
break
default: default:
// For transaction we process the data in a different way console.error("Unhandled method", method)
break; break
} }
let value = SQUtils.AmountsArithmetic.fromNumber(0) let value = SQUtils.AmountsArithmetic.fromNumber(0)
@ -600,12 +604,12 @@ SQUtils.QObject {
"gwei": 9, "gwei": 9,
"eth": 18 "eth": 18
} }
let Math = SQUtils.AmountsArithmetic let BigOps = SQUtils.AmountsArithmetic
let decValue = root.store.hexToDec(value) let decValue = root.store.hexToDec(value)
if (!!decValue) { if (!!decValue) {
return Math.div(Math.fromNumber(decValue), Math.fromNumber(1, unitMapping[ethUnit])) return BigOps.div(BigOps.fromNumber(decValue), BigOps.fromNumber(1, unitMapping[ethUnit]))
} }
return Math.fromNumber(0) return BigOps.fromNumber(0)
} }
} }