Skip to content

Commit

Permalink
Merge pull request #3361 from safe-global/GH-3360/nonces
Browse files Browse the repository at this point in the history
Gh 3360/nonces
  • Loading branch information
DmitryBespalov authored Nov 2, 2023
2 parents 8592ff7 + 3f9ef70 commit 9e6a204
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Multisig.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@
B0D61EC425A934B90D6A4288 /* WebConnectionRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0D6155D349D7F06F7D48818 /* WebConnectionRequest.swift */; };
B0D61F54F176906A7C5A65CB /* SendTransactionRequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0D61BDFEADB74805D9221DB /* SendTransactionRequestViewController.swift */; };
B0D61FFF1935AA2484BD940A /* WebConnectionRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0D617C60F3081A3A923005D /* WebConnectionRepository.swift */; };
B300E8CB2AF3A7A90073A908 /* SafeNoncesRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B300E8CA2AF3A7A90073A908 /* SafeNoncesRequest.swift */; };
B32620492A961E690003A2F0 /* AddSafeFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = B32620482A961E690003A2F0 /* AddSafeFlow.swift */; };
B343BEB32A77DDA1006BF46B /* DMSans-Bold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 0491067C28F99246005A4A99 /* DMSans-Bold.ttf */; };
B35BFD0F2A937DC000A9FB15 /* NavigationRouterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B35BFD0E2A937DC000A9FB15 /* NavigationRouterTests.swift */; };
Expand Down Expand Up @@ -2080,6 +2081,7 @@
B0D61DB08E699747DCEE7325 /* GnosisSafeWebPeerInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GnosisSafeWebPeerInfo.swift; sourceTree = "<group>"; };
B0D61EF933B18A7181F0F6E8 /* WebConnectionOpenRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebConnectionOpenRequest.swift; sourceTree = "<group>"; };
B0D61FB05FC8C6D45FAC1246 /* ActionPanelView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ActionPanelView.xib; sourceTree = "<group>"; };
B300E8CA2AF3A7A90073A908 /* SafeNoncesRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafeNoncesRequest.swift; sourceTree = "<group>"; };
B32620482A961E690003A2F0 /* AddSafeFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSafeFlow.swift; sourceTree = "<group>"; };
B35BFD0E2A937DC000A9FB15 /* NavigationRouterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationRouterTests.swift; sourceTree = "<group>"; };
B3710B3B2AD58492002E503B /* SecureConfig */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SecureConfig; path = Packages/SecureConfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2274,6 +2276,7 @@
0AB34D6524B71DE300A43988 /* CollectiblesRequest.swift */,
043ABD0A24EF0C33008826D5 /* TransactionDetailsRequest.swift */,
0439638427077E3C00018F74 /* TransactionEstimationRequest.swift */,
B300E8CA2AF3A7A90073A908 /* SafeNoncesRequest.swift */,
0A5307152543110300E8A270 /* ConfirmTransactionRequest.swift */,
04D1306B25DD525A0032FA03 /* ProposeTransactionRequest.swift */,
04FA1A2D2580FAE300E45671 /* QueuedTransactionsSummaryListRequest.swift */,
Expand Down Expand Up @@ -5832,6 +5835,7 @@
5562DD732693425E00B56722 /* NetworkIndicatorHeaderView.swift in Sources */,
0AB34D6624B71DE300A43988 /* CollectiblesRequest.swift in Sources */,
55A5853F25014AD7005E778B /* BIP39+WordLists.swift in Sources */,
B300E8CB2AF3A7A90073A908 /* SafeNoncesRequest.swift in Sources */,
0A6630EC28C0F22400F0FFAE /* AllocationBoxCell.swift in Sources */,
0428DA5A25CBFC25002CF4A9 /* HelpLinkTableViewCell.swift in Sources */,
9388410D2A3D74E20052EB87 /* GoogleWeb3AuthLoginModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,11 @@ extension SCGModels {
let recommendedNonce: UInt256String
let safeTxGas: String
}

struct Nonces: Decodable {
let currentNonce: UInt256String
let recommendedNonce: UInt256String
}

struct KeyDelegate: Codable {
var safe: AddressString?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// SafeNoncesRequest.swift
// Multisig
//
// Created by Dmitrii Bespalov on 02.11.23.
// Copyright © 2023 Gnosis Ltd. All rights reserved.
//

import Foundation

struct SafeNoncesRequest: JSONRequest {
var safeAddress: String
var chainId: String
var httpMethod: String { "GET" }

var urlPath: String {
"/v1/chains/\(chainId)/safes/\(safeAddress)/nonces"
}

typealias ResponseType = SCGModels.Nonces

}

extension SafeClientGatewayService {
func asyncSafeNonces(
chainId: String,
safeAddress: Address,
completion: @escaping (Result<SCGModels.Nonces, Error>) -> Void
) -> URLSessionTask? {
asyncExecute(
request: SafeNoncesRequest(safeAddress: safeAddress.checksummed,
chainId: chainId),
completion: completion
)
}
}
2 changes: 1 addition & 1 deletion Multisig/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>3.24.1</string>
<string>3.24.2</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@ class ReviewSafeTransactionViewController: UIViewController {
let safeAddress = tx.safe?.address
else { return }

if let version = self.safe.contractVersion.flatMap(Version.init),
version < Version(1, 3, 0) {
estimateTransaction(chainId, safeAddress, tx)
} else {
fetchNonces(chainId, safeAddress)
}
}

// Requires:
// safe.version < 1.3.0
fileprivate func estimateTransaction(_ chainId: String, _ safeAddress: Address, _ tx: Transaction) {
startLoading()
currentDataTask?.cancel()

currentDataTask = App.shared.clientGatewayService.asyncTransactionEstimation(
chainId: chainId,
safeAddress: safeAddress,
Expand All @@ -151,54 +162,76 @@ class ReviewSafeTransactionViewController: UIViewController {
guard let `self` = self else { return }
switch result {
case .failure(let error):
DispatchQueue.main.async { [weak self] in
guard let `self` = self else { return }
if (error as NSError).code == URLError.cancelled.rawValue &&
(error as NSError).domain == NSURLErrorDomain {
return
}
self.showError(GSError.error(description: "Failed to create transaction", error: error))
}
self.handleError(error)
case .success(let estimationResult):
self.minimalNonce = estimationResult.currentNonce
self.nonce = estimationResult.recommendedNonce

if let contractVersion = self.safe.contractVersion,
let version = Version(contractVersion),
version >= Version(1, 3, 0) {
self.safeTxGas = nil
} else if let estimatedSafeTxGas = UInt256(estimationResult.safeTxGas) {

if let estimatedSafeTxGas = UInt256(estimationResult.safeTxGas) {
self.safeTxGas = UInt256String(estimatedSafeTxGas)
}

self.handleEstimationSuccess()
}
}
}

// Requires:
// safe.version >= 1.3.0
fileprivate func fetchNonces(_ chainId: String, _ safeAddress: Address) {
startLoading()
currentDataTask?.cancel()

if self.shouldLoadTransactionPreview, let transaction = self.createTransaction() {
self.transactionPreview = nil

self.currentDataTask = App.shared.clientGatewayService.asyncPreviewTransaction(
transaction: transaction,
sender: AddressString(self.safe.addressValue),
chainId: self.safe.chain!.id!
) { result in
DispatchQueue.main.async {
switch result {
case .success(let response):
self.transactionPreview = response
self.onSuccess()
case .failure(let error):
if (error as NSError).code == URLError.cancelled.rawValue &&
(error as NSError).domain == NSURLErrorDomain {
return
}
self.showError(GSError.error(description: "Failed to create transaction", error: error))
}
}
}
} else {
DispatchQueue.main.async {
currentDataTask = App.shared.clientGatewayService.asyncSafeNonces(
chainId: chainId,
safeAddress: safeAddress
) { [weak self] result in
guard let `self` = self else { return }
switch result {
case .failure(let error):
self.handleError(error)
case .success(let nonces):
self.minimalNonce = nonces.currentNonce
self.nonce = nonces.recommendedNonce
self.safeTxGas = nil
self.handleEstimationSuccess()
}
}
}

fileprivate func handleError(_ error: Error) {
DispatchQueue.main.async { [weak self] in
guard let `self` = self else { return }
if (error as NSError).code == URLError.cancelled.rawValue &&
(error as NSError).domain == NSURLErrorDomain {
return
}
self.showError(GSError.error(description: "Failed to create transaction", error: error))
}
}

fileprivate func handleEstimationSuccess() {
if self.shouldLoadTransactionPreview, let transaction = self.createTransaction() {
self.transactionPreview = nil

self.currentDataTask = App.shared.clientGatewayService.asyncPreviewTransaction(
transaction: transaction,
sender: AddressString(self.safe.addressValue),
chainId: self.safe.chain!.id!
) { result in
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
switch result {
case .success(let response):
self.transactionPreview = response
self.onSuccess()
case .failure(let error):
self.handleError(error)
}
}
}
} else {
self.onSuccess()
}
}

Expand Down
2 changes: 1 addition & 1 deletion MultisigIntegrationTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>3.24.1</string>
<string>3.24.2</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion MultisigTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>3.24.1</string>
<string>3.24.2</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion NotificationServiceExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>3.24.1</string>
<string>3.24.2</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSExtension</key>
Expand Down

0 comments on commit 9e6a204

Please sign in to comment.