Skip to content

Commit 5061545

Browse files
[trello.com/c/XEj672SB] code improvements
1 parent 64439ba commit 5061545

File tree

5 files changed

+74
-87
lines changed

5 files changed

+74
-87
lines changed

Adamant/Modules/Wallets/Lisk/LskApiCore.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ class LskApiCore: BlockchainHealthCheckableService {
3333
}
3434
}
3535

36+
func request<Output>(
37+
node: CommonKit.Node,
38+
_ body: @Sendable @escaping (APIClient) async throws -> Output
39+
) async -> WalletServiceResult<Output> {
40+
let client = makeClient(node: node)
41+
42+
do {
43+
return .success(try await body(client))
44+
} catch {
45+
return .failure(mapError(error))
46+
}
47+
}
48+
3649
func getStatusInfo(node: CommonKit.Node) async -> WalletServiceResult<NodeStatusInfo> {
3750
let startTimestamp = Date.now.timeIntervalSince1970
3851

@@ -69,3 +82,11 @@ private func mapError(_ error: APIError) -> WalletServiceError {
6982
return .remoteServiceError(message: error.message, error: error)
7083
}
7184
}
85+
86+
private func mapError(_ error: Error) -> WalletServiceError {
87+
if let error = error as? APIError {
88+
return mapError(error)
89+
}
90+
91+
return .remoteServiceError(message: error.localizedDescription, error: error)
92+
}

Adamant/Modules/Wallets/Lisk/LskNodeApiService.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,18 @@ final class LskNodeApiService: WalletApiService {
3636
}
3737

3838
func requestTransactionsApi<Output>(
39-
body: @escaping @Sendable (
40-
_ api: Transactions,
41-
_ completion: @escaping @Sendable (LiskKit.Result<Output>) -> Void
42-
) -> Void
39+
_ request: @Sendable @escaping (Transactions) async throws -> Output
4340
) async -> WalletServiceResult<Output> {
44-
await requestClient { client, completion in
45-
body(.init(client: client), completion)
41+
await requestClient { client in
42+
try await request(Transactions(client: client))
4643
}
4744
}
4845

4946
func requestAccountsApi<Output>(
50-
body: @escaping @Sendable (
51-
_ api: Accounts,
52-
_ completion: @escaping @Sendable (LiskKit.Result<Output>) -> Void
53-
) -> Void
47+
_ request: @Sendable @escaping (Accounts) async throws -> Output
5448
) async -> WalletServiceResult<Output> {
55-
await requestClient { client, completion in
56-
body(.init(client: client), completion)
49+
await requestClient { client in
50+
try await request(Accounts(client: client))
5751
}
5852
}
5953

@@ -75,4 +69,12 @@ private extension LskNodeApiService {
7569
await core.request(node: node, body: body)
7670
}
7771
}
72+
73+
func requestClient<Output>(
74+
_ body: @Sendable @escaping (APIClient) async throws -> Output
75+
) async -> WalletServiceResult<Output> {
76+
await api.request { core, node in
77+
await core.request(node: node, body)
78+
}
79+
}
7880
}

Adamant/Modules/Wallets/Lisk/LskServiceApiService.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ final class LskServiceApiService: WalletApiService {
5555
body(.init(client: client, version: .v3), completion)
5656
}
5757
}
58+
59+
func requestServiceApi<Output>(
60+
_ request: @Sendable @escaping (LiskKit.Service) async throws -> Output
61+
) async -> WalletServiceResult<Output> {
62+
await requestClient { client in
63+
try await request(LiskKit.Service(client: client, version: .v3))
64+
}
65+
}
5866
}
5967

6068
private extension LskServiceApiService {
@@ -68,4 +76,12 @@ private extension LskServiceApiService {
6876
await core.request(node: node, body: body)
6977
}
7078
}
79+
80+
func requestClient<Output>(
81+
_ body: @Sendable @escaping (APIClient) async throws -> Output
82+
) async -> WalletServiceResult<Output> {
83+
await api.request { core, node in
84+
await core.request(node: node, body)
85+
}
86+
}
7187
}

Adamant/Modules/Wallets/Lisk/LskWalletService+Send.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,8 @@ extension LskWalletService: WalletServiceTwoStepSend {
5353
}
5454

5555
func sendTransaction(_ transaction: TransactionEntity) async throws {
56-
_ = try await lskNodeApiService.requestTransactionsApi { api, completion in
57-
Task {
58-
do {
59-
let id = try await api.submit(transaction: transaction)
60-
completion(.success(response: id))
61-
} catch let error as APIError {
62-
completion(.error(response: error))
63-
} catch {
64-
completion(.error(response: APIError.unknown(code: nil)))
65-
}
66-
}
56+
_ = try await lskNodeApiService.requestTransactionsApi { api in
57+
try await api.submit(transaction: transaction)
6758
}.get()
6859
}
6960
}

Adamant/Modules/Wallets/Lisk/LskWalletService.swift

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,8 @@ final class LskWalletService: WalletService {
273273
throw WalletServiceError.notLogged
274274
}
275275

276-
let minFeePerByte = try await lskNodeApiService.requestAccountsApi { api, completion in
277-
Task {
278-
do {
279-
let fee = try await api.getFees()
280-
completion(.success(response: fee.minFeePerByte))
281-
} catch {
282-
completion(.error(response: mapError(error)))
283-
}
284-
}
276+
let minFeePerByte = try await lskNodeApiService.requestAccountsApi { api in
277+
try await api.getFees().minFeePerByte
285278
}.get()
286279

287280
let tempTransaction = TransactionEntity().createTx(
@@ -298,15 +291,8 @@ final class LskWalletService: WalletService {
298291
let feeValue = tempTransaction.getFee(with: minFeePerByte)
299292
let fee = BigUInt(feeValue)
300293

301-
let lastBlock = try await lskNodeApiService.requestAccountsApi { api, completion in
302-
Task {
303-
do {
304-
let lastBlock = try await api.lastBlock()
305-
completion(.success(response: lastBlock))
306-
} catch {
307-
completion(.error(response: mapError(error)))
308-
}
309-
}
294+
let lastBlock = try await lskNodeApiService.requestAccountsApi { api in
295+
try await api.lastBlock()
310296
}.get()
311297

312298
let height = UInt64(lastBlock.header.height)
@@ -315,18 +301,18 @@ final class LskWalletService: WalletService {
315301
}
316302

317303
func isExist(address: String) async throws -> Bool {
318-
let result = try await lskServiceApiService.requestServiceApi { api, completion in
319-
api.exist(address: address) { result in
320-
switch result {
321-
case .success(let response):
322-
completion(.success(response: response.data.isExists))
323-
case .error(let error):
324-
completion(.error(response: mapError(error)))
304+
try await lskServiceApiService.requestServiceApi { api in
305+
try await withUnsafeThrowingContinuation { continuation in
306+
api.exist(address: address) { result in
307+
switch result {
308+
case .success(let response):
309+
continuation.resume(returning: response.data.isExists)
310+
case .error(let error):
311+
continuation.resume(throwing: error)
312+
}
325313
}
326314
}
327315
}.get()
328-
329-
return result
330316
}
331317
}
332318

@@ -486,16 +472,10 @@ extension LskWalletService {
486472
}
487473

488474
func getBalance(address: String) async throws -> Decimal {
489-
let result = await lskNodeApiService.requestAccountsApi { api, completion in
490-
Task {
491-
do {
492-
let balanceRaw = try await api.balance(address: address)
493-
let balance = BigUInt(balanceRaw?.availableBalance ?? "0") ?? .zero
494-
completion(.success(response: balance))
495-
} catch {
496-
completion(.error(response: mapError(error)))
497-
}
498-
}
475+
let result = await lskNodeApiService.requestAccountsApi { api in
476+
let balanceRaw = try await api.balance(address: address)
477+
let balance = BigUInt(balanceRaw?.availableBalance ?? "0") ?? .zero
478+
return balance
499479
}
500480

501481
switch result {
@@ -507,23 +487,11 @@ extension LskWalletService {
507487
}
508488

509489
func getNonce(address: String) async throws -> UInt64 {
510-
let result = await lskNodeApiService.requestAccountsApi { api, completion in
511-
Task {
512-
do {
513-
let nonce = try await api.nonce(address: address)
514-
completion(.success(response: UInt64(nonce) ?? .zero))
515-
} catch {
516-
completion(.error(response: mapError(error)))
517-
}
518-
}
519-
}
490+
let nonce = try await lskNodeApiService.requestAccountsApi { api in
491+
try await api.nonce(address: address)
492+
}.get()
520493

521-
switch result {
522-
case let .success(nonce):
523-
return nonce
524-
case let .failure(error):
525-
throw error
526-
}
494+
return UInt64(nonce) ?? .zero
527495
}
528496

529497
func handleAccountSuccess(with balance: String?, completion: @escaping (WalletServiceResult<Decimal>) -> Void) {
@@ -684,14 +652,3 @@ extension LskWalletService: PrivateKeyGenerator {
684652
return keypair.privateKeyString
685653
}
686654
}
687-
688-
689-
private func mapError(_ error: Error) -> APIError {
690-
if let error = error as? APIError {
691-
return error
692-
} else if let _ = error as? URLError {
693-
return APIError.noNetwork
694-
} else {
695-
return APIError.unknown(code: nil)
696-
}
697-
}

0 commit comments

Comments
 (0)