Skip to content

Commit 9fac0e1

Browse files
Merge pull request #397 from Adamant-im/trello.com/c/H3H8SXeM
[trello.com/c/H3H8SXeM] Eth crash fix
2 parents 6541c95 + 0c26b20 commit 9fac0e1

File tree

4 files changed

+110
-76
lines changed

4 files changed

+110
-76
lines changed

Adamant.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
93C7944E2B077C1F00408826 /* DashSendRawTransactionDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93C7944D2B077C1F00408826 /* DashSendRawTransactionDTO.swift */; };
297297
93CC8DC7296F00D6003772BF /* ChatTransactionContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CC8DC6296F00D6003772BF /* ChatTransactionContainerView.swift */; };
298298
93CC8DC9296F01DE003772BF /* ChatTransactionContainerView+Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CC8DC8296F01DE003772BF /* ChatTransactionContainerView+Model.swift */; };
299+
93CC94C12B17EE73004842AC /* EthApiCore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CC94C02B17EE73004842AC /* EthApiCore.swift */; };
299300
93CCAE752B06CC3600EA5B94 /* LskNodeApiService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CCAE742B06CC3600EA5B94 /* LskNodeApiService.swift */; };
300301
93CCAE772B06D6CC00EA5B94 /* LskServiceApiService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CCAE762B06D6CC00EA5B94 /* LskServiceApiService.swift */; };
301302
93CCAE792B06D81D00EA5B94 /* DogeApiService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CCAE782B06D81D00EA5B94 /* DogeApiService.swift */; };
@@ -920,6 +921,7 @@
920921
93C7944D2B077C1F00408826 /* DashSendRawTransactionDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashSendRawTransactionDTO.swift; sourceTree = "<group>"; };
921922
93CC8DC6296F00D6003772BF /* ChatTransactionContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatTransactionContainerView.swift; sourceTree = "<group>"; };
922923
93CC8DC8296F01DE003772BF /* ChatTransactionContainerView+Model.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChatTransactionContainerView+Model.swift"; sourceTree = "<group>"; };
924+
93CC94C02B17EE73004842AC /* EthApiCore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthApiCore.swift; sourceTree = "<group>"; };
923925
93CCAE742B06CC3600EA5B94 /* LskNodeApiService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LskNodeApiService.swift; sourceTree = "<group>"; };
924926
93CCAE762B06D6CC00EA5B94 /* LskServiceApiService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LskServiceApiService.swift; sourceTree = "<group>"; };
925927
93CCAE782B06D81D00EA5B94 /* DogeApiService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DogeApiService.swift; sourceTree = "<group>"; };
@@ -2175,6 +2177,7 @@
21752177
E993302121354BC300CD5200 /* EthWalletFactory.swift */,
21762178
E940087A2114ED0600CD2D67 /* EthWalletService.swift */,
21772179
93FC169C2B019F440062B507 /* EthApiService.swift */,
2180+
93CC94C02B17EE73004842AC /* EthApiCore.swift */,
21782181
4186B333294200C5006594A3 /* EthWalletService+DynamicConstants.swift */,
21792182
E9AA8BF9212C166600F9249F /* EthWalletService+Send.swift */,
21802183
E9FEECA52143C300007DD7C8 /* EthWalletService+RichMessageProvider.swift */,
@@ -3025,6 +3028,7 @@
30253028
E9E7CD8B20026B0600DFC4DB /* AccountService.swift in Sources */,
30263029
41E3C9CC2A0E20F500AF0985 /* AdamantCoinTools.swift in Sources */,
30273030
93ADE0712ACA66AF008ED641 /* VibrationSelectionViewModel.swift in Sources */,
3031+
93CC94C12B17EE73004842AC /* EthApiCore.swift in Sources */,
30283032
93FC169D2B019F440062B507 /* EthApiService.swift in Sources */,
30293033
9371130F2996EDA900F64CF9 /* ChatRefreshMock.swift in Sources */,
30303034
93547BCA29E2262D00B0914B /* WelcomeViewController.swift in Sources */,
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// EthApiCore.swift
3+
// Adamant
4+
//
5+
// Created by Andrew G on 30.11.2023.
6+
// Copyright © 2023 Adamant. All rights reserved.
7+
//
8+
9+
import CommonKit
10+
import Foundation
11+
import web3swift
12+
import Web3Core
13+
14+
actor EthApiCore {
15+
let apiCore: APICoreProtocol
16+
private(set) var keystoreManager: KeystoreManager?
17+
private var web3Cache: [URL: Web3] = .init()
18+
19+
func performRequest<Success>(
20+
node: Node,
21+
_ body: @escaping @Sendable (_ web3: Web3) async throws -> Success
22+
) async -> WalletServiceResult<Success> {
23+
switch await getWeb3(node: node) {
24+
case let .success(web3):
25+
do {
26+
return .success(try await body(web3))
27+
} catch {
28+
return .failure(mapError(error))
29+
}
30+
case let .failure(error):
31+
return .failure(error)
32+
}
33+
}
34+
35+
func setKeystoreManager(_ keystoreManager: KeystoreManager) {
36+
self.keystoreManager = keystoreManager
37+
web3Cache = .init()
38+
}
39+
40+
init(apiCore: APICoreProtocol) {
41+
self.apiCore = apiCore
42+
}
43+
}
44+
45+
extension EthApiCore: BlockchainHealthCheckableService {
46+
func getStatusInfo(node: Node) async -> WalletServiceResult<NodeStatusInfo> {
47+
await performRequest(node: node) { web3 in
48+
let startTimestamp = Date.now.timeIntervalSince1970
49+
let height = try await web3.eth.blockNumber()
50+
let ping = Date.now.timeIntervalSince1970 - startTimestamp
51+
52+
return .init(
53+
ping: ping,
54+
height: Int(height.asDouble()),
55+
wsEnabled: false,
56+
wsPort: nil,
57+
version: nil
58+
)
59+
}
60+
}
61+
}
62+
63+
private extension EthApiCore {
64+
func getWeb3(node: Node) async -> WalletServiceResult<Web3> {
65+
guard let url = node.asURL() else {
66+
return .failure(.internalError(.endpointBuildFailed))
67+
}
68+
69+
if let web3 = web3Cache[url] {
70+
return .success(web3)
71+
}
72+
73+
do {
74+
let web3 = try await Web3.new(url)
75+
web3.addKeystoreManager(keystoreManager)
76+
web3Cache[url] = web3
77+
return .success(web3)
78+
} catch {
79+
return .failure(.internalError(
80+
message: error.localizedDescription,
81+
error: error
82+
))
83+
}
84+
}
85+
}
86+
87+
private func mapError(_ error: Error) -> WalletServiceError {
88+
if let error = error as? Web3Error {
89+
return error.asWalletServiceError()
90+
} else if let error = error as? ApiServiceError {
91+
return error.asWalletServiceError()
92+
} else if let error = error as? WalletServiceError {
93+
return error
94+
} else if let _ = error as? URLError {
95+
return .networkError
96+
} else {
97+
return .remoteServiceError(message: error.localizedDescription)
98+
}
99+
}

Adamant/Modules/Wallets/Ethereum/EthApiService.swift

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,6 @@ import Foundation
1111
import web3swift
1212
import Web3Core
1313

14-
@MainActor
15-
final class EthApiCore: BlockchainHealthCheckableService {
16-
let apiCore: APICoreProtocol
17-
private(set) var keystoreManager: KeystoreManager?
18-
19-
func makeWeb3(node: Node) async -> WalletServiceResult<Web3> {
20-
do {
21-
guard let url = node.asURL() else { throw InternalAPIError.endpointBuildFailed }
22-
let web3 = try await Web3.new(url)
23-
web3.addKeystoreManager(keystoreManager)
24-
return .success(web3)
25-
} catch {
26-
return .failure(.internalError(message: error.localizedDescription, error: error))
27-
}
28-
}
29-
30-
func performRequest<Success>(
31-
node: Node,
32-
_ body: @escaping @Sendable (_ web3: Web3) async throws -> Success
33-
) async -> WalletServiceResult<Success> {
34-
switch await makeWeb3(node: node) {
35-
case let .success(web3):
36-
do {
37-
return .success(try await body(web3))
38-
} catch {
39-
return .failure(mapError(error))
40-
}
41-
case let .failure(error):
42-
return .failure(error)
43-
}
44-
}
45-
46-
func getStatusInfo(node: Node) async -> WalletServiceResult<NodeStatusInfo> {
47-
await performRequest(node: node) { web3 in
48-
let startTimestamp = Date.now.timeIntervalSince1970
49-
let height = try await web3.eth.blockNumber()
50-
let ping = Date.now.timeIntervalSince1970 - startTimestamp
51-
52-
return .init(
53-
ping: ping,
54-
height: Int(height.asDouble()),
55-
wsEnabled: false,
56-
wsPort: nil,
57-
version: nil
58-
)
59-
}
60-
}
61-
62-
func setKeystoreManager(_ keystoreManager: KeystoreManager) {
63-
self.keystoreManager = keystoreManager
64-
}
65-
66-
nonisolated init(apiCore: APICoreProtocol) {
67-
self.apiCore = apiCore
68-
}
69-
}
70-
7114
class EthApiService: WalletApiService {
7215
let api: BlockchainHealthCheckWrapper<EthApiCore>
7316

@@ -113,17 +56,3 @@ class EthApiService: WalletApiService {
11356
await api.service.setKeystoreManager(keystoreManager)
11457
}
11558
}
116-
117-
private func mapError(_ error: Error) -> WalletServiceError {
118-
if let error = error as? Web3Error {
119-
return error.asWalletServiceError()
120-
} else if let error = error as? ApiServiceError {
121-
return error.asWalletServiceError()
122-
} else if let error = error as? WalletServiceError {
123-
return error
124-
} else if let _ = error as? URLError {
125-
return .networkError
126-
} else {
127-
return .remoteServiceError(message: error.localizedDescription)
128-
}
129-
}

Adamant/Modules/Wallets/Ethereum/EthWalletService+RichMessageProviderWithStatusCheck.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private extension EthWalletService {
7171
func getTransactionInfo(hash: String, web3: Web3) async throws -> EthTransactionInfo {
7272
try await withThrowingTaskGroup(
7373
of: EthTransactionInfoElement.self,
74-
returning: EthTransactionInfo.self
74+
returning: Atomic<EthTransactionInfo>.self
7575
) { group in
7676
group.addTask(priority: .userInitiated) {
7777
.details(try await web3.eth.transactionDetails(hash))
@@ -81,15 +81,17 @@ private extension EthWalletService {
8181
.receipt(try await web3.eth.transactionReceipt(hash))
8282
}
8383

84-
return try await group.reduce(into: .init()) { result, value in
84+
return try await group.reduce(
85+
into: .init(wrappedValue: .init())
86+
) { result, value in
8587
switch value {
8688
case let .receipt(receipt):
87-
result.receipt = receipt
89+
result.wrappedValue.receipt = receipt
8890
case let .details(details):
89-
result.details = details
91+
result.wrappedValue.details = details
9092
}
9193
}
92-
}
94+
}.wrappedValue
9395
}
9496

9597
func getStatus(

0 commit comments

Comments
 (0)