Skip to content

Commit 39d0107

Browse files
Merge pull request #629 from Adamant-im/dev/trello.com/c/ysg4LpCL
2 parents 57d16ac + c47e108 commit 39d0107

File tree

6 files changed

+39
-45
lines changed

6 files changed

+39
-45
lines changed

Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
266266
}
267267
}
268268

269+
@MainActor
269270
func update() async {
270271
guard let wallet = btcWallet else {
271272
return
@@ -282,13 +283,12 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
282283
setState(.updating)
283284

284285
if let balance = try? await getBalance() {
285-
markBalanceAsFresh()
286-
287286
if wallet.balance < balance, wallet.isBalanceInitialized {
288-
await vibroService.applyVibration(.success)
287+
vibroService.applyVibration(.success)
289288
}
290289

291290
wallet.balance = balance
291+
markBalanceAsFresh(wallet)
292292

293293
NotificationCenter.default.post(
294294
name: walletUpdatedNotification,
@@ -363,12 +363,12 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
363363
return output
364364
}
365365

366-
private func markBalanceAsFresh() {
367-
btcWallet?.isBalanceInitialized = true
366+
private func markBalanceAsFresh(_ wallet: BtcWallet) {
367+
wallet.isBalanceInitialized = true
368368

369369
balanceInvalidationSubscription = Task { [weak self] in
370370
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
371-
guard let self, let wallet = btcWallet else { return }
371+
guard let self else { return }
372372
wallet.isBalanceInitialized = false
373373

374374
NotificationCenter.default.post(
@@ -536,16 +536,11 @@ extension BtcWalletService {
536536
}
537537

538538
func getBalance(address: String) async throws -> Decimal {
539-
do {
540-
let response: BtcBalanceResponse = try await btcApiService.request(waitsForConnectivity: false) { api, origin in
541-
await api.sendRequestJsonResponse(origin: origin, path: BtcApiCommands.balance(for: address))
542-
}.get()
543-
544-
return response.value / BtcWalletService.multiplier
545-
} catch {
546-
print("--debug", error.localizedDescription)
547-
return 0
548-
}
539+
let response: BtcBalanceResponse = try await btcApiService.request(waitsForConnectivity: false) { api, origin in
540+
await api.sendRequestJsonResponse(origin: origin, path: BtcApiCommands.balance(for: address))
541+
}.get()
542+
543+
return response.value / BtcWalletService.multiplier
549544
}
550545

551546
func getFeeRate() async throws -> Decimal {

Adamant/Modules/Wallets/Dash/DashWalletService.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
239239
}
240240
}
241241

242+
@MainActor
242243
func update() async {
243244
guard let wallet = dashWallet else {
244245
return
@@ -255,13 +256,12 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
255256
setState(.updating)
256257

257258
if let balance = try? await getBalance() {
258-
markBalanceAsFresh()
259-
260259
if wallet.balance < balance, wallet.isBalanceInitialized {
261-
await vibroService.applyVibration(.success)
260+
vibroService.applyVibration(.success)
262261
}
263262

264263
wallet.balance = balance
264+
markBalanceAsFresh(wallet)
265265

266266
NotificationCenter.default.post(
267267
name: walletUpdatedNotification,
@@ -284,12 +284,12 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
284284
}
285285
}
286286

287-
private func markBalanceAsFresh() {
288-
dashWallet?.isBalanceInitialized = true
287+
private func markBalanceAsFresh(_ wallet: DashWallet) {
288+
wallet.isBalanceInitialized = true
289289

290290
balanceInvalidationSubscription = Task { [weak self] in
291291
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
292-
guard let self, let wallet = dashWallet else { return }
292+
guard let self else { return }
293293
wallet.isBalanceInitialized = false
294294

295295
NotificationCenter.default.post(

Adamant/Modules/Wallets/Doge/DogeWalletService.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
236236
}
237237
}
238238

239+
@MainActor
239240
func update() async {
240241
guard let wallet = dogeWallet else {
241242
return
@@ -252,13 +253,12 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
252253
setState(.updating)
253254

254255
if let balance = try? await getBalance() {
255-
markBalanceAsFresh()
256-
257256
if wallet.balance < balance, wallet.isBalanceInitialized {
258-
await vibroService.applyVibration(.success)
257+
vibroService.applyVibration(.success)
259258
}
260259

261260
wallet.balance = balance
261+
markBalanceAsFresh(wallet)
262262

263263
NotificationCenter.default.post(
264264
name: walletUpdatedNotification,
@@ -281,12 +281,12 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
281281
}
282282
}
283283

284-
private func markBalanceAsFresh() {
285-
dogeWallet?.isBalanceInitialized = true
284+
private func markBalanceAsFresh(_ wallet: DogeWallet) {
285+
wallet.isBalanceInitialized = true
286286

287287
balanceInvalidationSubscription = Task { [weak self] in
288288
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
289-
guard let self, let wallet = dogeWallet else { return }
289+
guard let self else { return }
290290
wallet.isBalanceInitialized = false
291291

292292
NotificationCenter.default.post(

Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
245245
}
246246
}
247247

248+
@MainActor
248249
func update() async {
249250
guard let wallet = ethWallet else {
250251
return
@@ -261,13 +262,12 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
261262
setState(.updating)
262263

263264
if let balance = try? await getBalance(forAddress: wallet.ethAddress) {
264-
markBalanceAsFresh()
265-
266265
if wallet.balance < balance, wallet.isBalanceInitialized {
267-
await vibroService.applyVibration(.success)
266+
vibroService.applyVibration(.success)
268267
}
269268

270269
wallet.balance = balance
270+
markBalanceAsFresh(wallet)
271271

272272
NotificationCenter.default.post(
273273
name: walletUpdatedNotification,
@@ -348,12 +348,12 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
348348
}.get()
349349
}
350350

351-
private func markBalanceAsFresh() {
352-
ethWallet?.isBalanceInitialized = true
351+
private func markBalanceAsFresh(_ wallet: EthWallet) {
352+
wallet.isBalanceInitialized = true
353353

354354
balanceInvalidationSubscription = Task { [weak self] in
355355
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
356-
guard let self, let wallet = ethWallet else { return }
356+
guard let self else { return }
357357
wallet.isBalanceInitialized = false
358358

359359
NotificationCenter.default.post(

Adamant/Modules/Wallets/Ethereum/EthWalletService.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,12 @@ final class EthWalletService: WalletCoreProtocol, @unchecked Sendable {
291291
setState(.updating)
292292

293293
if let balance = try? await getBalance(forAddress: wallet.ethAddress) {
294-
markBalanceAsFresh()
295-
296294
if wallet.balance < balance, wallet.isBalanceInitialized {
297295
vibroService.applyVibration(.success)
298296
}
299297

300298
wallet.balance = balance
299+
markBalanceAsFresh(wallet)
301300

302301
NotificationCenter.default.post(
303302
name: walletUpdatedNotification,
@@ -310,12 +309,12 @@ final class EthWalletService: WalletCoreProtocol, @unchecked Sendable {
310309
await calculateFee()
311310
}
312311

313-
private func markBalanceAsFresh() {
314-
ethWallet?.isBalanceInitialized = true
312+
private func markBalanceAsFresh(_ wallet: EthWallet) {
313+
wallet.isBalanceInitialized = true
315314

316315
balanceInvalidationSubscription = Task { [weak self] in
317316
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
318-
guard let self, let wallet = ethWallet else { return }
317+
guard let self else { return }
319318
wallet.isBalanceInitialized = false
320319

321320
NotificationCenter.default.post(

Adamant/Modules/Wallets/Klayr/WalletService/KlyWalletService.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ private extension KlyWalletService {
229229
.store(in: &subscriptions)
230230
}
231231

232+
@MainActor
232233
func update() async {
233234
guard let wallet = klyWallet else {
234235
return
@@ -245,13 +246,12 @@ private extension KlyWalletService {
245246
setState(.updating)
246247

247248
if let balance = try? await getBalance() {
248-
markBalanceAsFresh()
249-
250249
if wallet.balance < balance, wallet.isBalanceInitialized {
251-
await vibroService.applyVibration(.success)
250+
vibroService.applyVibration(.success)
252251
}
253252

254253
wallet.balance = balance
254+
markBalanceAsFresh(wallet)
255255

256256
NotificationCenter.default.post(
257257
name: walletUpdatedNotification,
@@ -275,12 +275,12 @@ private extension KlyWalletService {
275275
setState(.upToDate)
276276
}
277277

278-
func markBalanceAsFresh() {
279-
klyWallet?.isBalanceInitialized = true
278+
func markBalanceAsFresh(_ wallet: KlyWallet) {
279+
wallet.isBalanceInitialized = true
280280

281281
balanceInvalidationSubscription = Task { [weak self] in
282282
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
283-
guard let self, let wallet = klyWallet else { return }
283+
guard let self else { return }
284284
wallet.isBalanceInitialized = false
285285

286286
NotificationCenter.default.post(

0 commit comments

Comments
 (0)