Skip to content

Commit

Permalink
Merge pull request #1888 from gnosis/gh-1886/fix-tx-check
Browse files Browse the repository at this point in the history
gh-1886 using legacy tx data format
  • Loading branch information
Dmitry Bespalov authored Feb 4, 2022
2 parents 364eb46 + ea1c175 commit 9e36337
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 39 additions & 3 deletions Packages/Ethereum/Sources/Ethereum/EthRpc1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 9e36337

Please sign in to comment.