From ea1c175638dbe31398ebbb4b0e45df88c1ba1097 Mon Sep 17 00:00:00 2001 From: Dmitry Bespalov Date: Fri, 4 Feb 2022 13:45:09 +0100 Subject: [PATCH] gh-1886 using legacy tx data format --- .../TransactionEstimationController.swift | 9 ++-- .../TransactionExecutionController.swift | 8 ++-- .../Ethereum/Sources/Ethereum/EthRpc1.swift | 42 +++++++++++++++++-- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Multisig/UI/Transaction/Execution/TransactionEstimationController.swift b/Multisig/UI/Transaction/Execution/TransactionEstimationController.swift index bb583314b..cd5486802 100644 --- a/Multisig/UI/Transaction/Execution/TransactionEstimationController.swift +++ b/Multisig/UI/Transaction/Execution/TransactionEstimationController.swift @@ -61,7 +61,8 @@ class TransactionEstimationController { let getPrice = EthRpc1.eth_gasPrice() - let ethCall = EthRpc1.eth_call(transaction: EthRpc1.Transaction(tx), block: .tag(.pending)) + let ethCallNew = EthRpc1.eth_call(transaction: EthRpc1.Transaction(tx), block: .tag(.pending)) + let ethCallLegacy = EthRpc1.eth_callLegacyApi(transaction: EthRpc1.EstimateGasLegacyTransaction(tx), block: .tag(.pending)) let batch: JsonRpc2.BatchRequest let getEstimateRequest: JsonRpc2.Request @@ -74,7 +75,7 @@ class TransactionEstimationController { getEstimateRequest = try usingLegacyGasApi ? getEstimateLegacy.request(id: .int(1)) : getEstimateNew.request(id: .int(1)) getTransactionCountRequest = try getTransactionCount.request(id: .int(2)) getPriceRequest = try getPrice.request(id: .int(3)) - ethCallRequest = try ethCall.request(id: .int(4)) + ethCallRequest = try usingLegacyGasApi ? ethCallLegacy.request(id: .int(4)) : ethCallNew.request(id: .int(4)) batch = try JsonRpc2.BatchRequest(requests: [ getEstimateRequest, getTransactionCountRequest, getPriceRequest, ethCallRequest @@ -122,7 +123,9 @@ class TransactionEstimationController { : result(request: getEstimateRequest, method: getEstimateNew, responses: responses).map(\.storage) let txCountResult = result(request: getTransactionCountRequest, method: getTransactionCount, responses: responses).map(\.storage) let priceResult = fixedGasPrice.map { .success($0) } ?? result(request: getPriceRequest, method: getPrice, responses: responses).map(\.storage) - let callResult = result(request: ethCallRequest, method: ethCall, responses: responses).map(\.storage) + let callResult = usingLegacyGasApi ? + result(request: ethCallRequest, method: ethCallLegacy, responses: responses).map(\.storage) + : result(request: ethCallRequest, method: ethCallNew, responses: responses).map(\.storage) dispatchOnMainThread(completion(.success((gasResult, txCountResult, priceResult, callResult)))) } diff --git a/Multisig/UI/Transaction/Execution/TransactionExecutionController.swift b/Multisig/UI/Transaction/Execution/TransactionExecutionController.swift index 486d48881..27f3c2172 100644 --- a/Multisig/UI/Transaction/Execution/TransactionExecutionController.swift +++ b/Multisig/UI/Transaction/Execution/TransactionExecutionController.swift @@ -214,11 +214,9 @@ class TransactionExecutionController { gas = try partialResults.gas.get() let execTransactionSuccess = try partialResults.ethCall.get() - if !execTransactionSuccess.isEmpty { - let success = try Sol.Bool(execTransactionSuccess).storage - guard success else { - throw TransactionExecutionError(code: -7, message: "Internal Gnosis Safe transaction fails. Please double check whether this transaction is valid with the current state of the Safe.") - } + let success = try Sol.Bool(execTransactionSuccess).storage + guard success else { + throw TransactionExecutionError(code: -7, message: "Internal Gnosis Safe transaction fails. Please double check whether this transaction is valid with the current state of the Safe.") } } catch { errors.append(error) diff --git a/Packages/Ethereum/Sources/Ethereum/EthRpc1.swift b/Packages/Ethereum/Sources/Ethereum/EthRpc1.swift index ea3405d13..c87b00f3b 100644 --- a/Packages/Ethereum/Sources/Ethereum/EthRpc1.swift +++ b/Packages/Ethereum/Sources/Ethereum/EthRpc1.swift @@ -490,6 +490,21 @@ extension EthRpc1 { } } + public struct eth_callLegacyApi: JsonRpc2Method { + public static var name: String { "eth_call" } + + public var transaction: EstimateGasLegacyTransaction + + public var block: EthRpc1.BlockSpecifier + + public typealias Return = EthRpc1.Data + + public init(transaction: EstimateGasLegacyTransaction, block: EthRpc1.BlockSpecifier) { + self.transaction = transaction + self.block = block + } + } + /// Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. public struct eth_estimateGas: EthEstimateGasAbi, JsonRpc2Method, EthRpc1TransactionParams { /// Transaction. NOTE: `from` field MUST be present. @@ -626,6 +641,21 @@ extension EthRpc1.eth_call: Codable { } } +extension EthRpc1.eth_callLegacyApi: Codable { + public init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let transaction = try container.decode(EthRpc1.EstimateGasLegacyTransaction.self) + let block = try container.decode(EthRpc1.BlockSpecifier.self) + self.init(transaction: transaction, block: block) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.unkeyedContainer() + try container.encode(transaction) + try container.encode(block) + } +} + public protocol EthEstimateGasAbi {} extension EthEstimateGasAbi where Self: JsonRpc2Method, Self: EthRpc1TransactionParams { @@ -1373,14 +1403,20 @@ extension EthRpc1.Transaction { } extension EthRpc1.eth_estimateGasLegacyApi { + public init(_ tx: EthTransaction) { + self.init(transaction: EthRpc1.EstimateGasLegacyTransaction(tx)) + } +} + +extension EthRpc1.EstimateGasLegacyTransaction { public init(_ tx: EthTransaction) { switch tx { case let eip1559 as Eth.TransactionEip1559: - self.init(transaction: EthRpc1.EstimateGasLegacyTransaction(eip1559)) + self.init(eip1559) case let eip2930 as Eth.TransactionEip2930: - self.init(transaction: EthRpc1.EstimateGasLegacyTransaction(eip2930)) + self.init(eip2930) case let legacy as Eth.TransactionLegacy: - self.init(transaction: EthRpc1.EstimateGasLegacyTransaction(legacy)) + self.init(legacy) default: fatalError("Not implemented") }