Skip to content

Commit 927e606

Browse files
committed
[trello.com/c/dEf1yVQr] Update balance after reconnection to a node
1 parent 4c3bd38 commit 927e606

File tree

15 files changed

+85
-27
lines changed

15 files changed

+85
-27
lines changed

Adamant/Modules/Wallets/Adamant/AdmWalletService.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
126126
var hasEnabledNodePublisher: AnyObservable<Bool> {
127127
apiService.hasEnabledNodePublisher
128128
}
129+
130+
@MainActor
131+
var hasAllowedNodePublisher: AnyObservable<Bool> {
132+
apiService.hasAllowedNodePublisher
133+
}
129134

130135
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
131136
coinId: tokenUniqueID,
@@ -163,9 +168,10 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
163168
.store(in: &subscriptions)
164169

165170
NotificationCenter.default
166-
.notifications(named: .AdamantAccountService.walletUpdated, object: nil)
167-
.sink { [weak self] _ in
168-
self?.update()
171+
.publisher(for: .AdamantAccountService.isBalanceExpired)
172+
.compactMap { $0.object as? Bool }
173+
.sink { [weak self] isExpired in
174+
self?.resetBalanceAndUpdate()
169175
}
170176
.store(in: &subscriptions)
171177
}

Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
206206
var hasEnabledNodePublisher: AnyObservable<Bool> {
207207
btcApiService.hasEnabledNodePublisher
208208
}
209+
210+
@MainActor
211+
var hasAllowedNodePublisher: AnyObservable<Bool> {
212+
btcApiService.hasAllowedNodePublisher
213+
}
209214

210215
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
211216
coinId: tokenUniqueID,
@@ -264,7 +269,7 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
264269
.store(in: &subscriptions)
265270
}
266271

267-
func addTransactionObserver() {
272+
private func addTransactionObserver() {
268273
coinStorage.transactionsPublisher
269274
.sink { [weak self] transactions in
270275
self?.transactions = transactions

Adamant/Modules/Wallets/Dash/DashWalletService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
115115
var hasEnabledNodePublisher: AnyObservable<Bool> {
116116
dashApiService.hasEnabledNodePublisher
117117
}
118+
119+
@MainActor
120+
var hasAllowedNodePublisher: AnyObservable<Bool> {
121+
dashApiService.hasAllowedNodePublisher
122+
}
118123

119124
// MARK: - Notifications
120125
let serviceEnabledChanged = Notification.Name("adamant.dashWallet.enabledChanged")

Adamant/Modules/Wallets/Doge/DogeWalletService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
174174
var hasEnabledNodePublisher: AnyObservable<Bool> {
175175
dogeApiService.hasEnabledNodePublisher
176176
}
177+
178+
@MainActor
179+
var hasAllowedNodePublisher: AnyObservable<Bool> {
180+
dogeApiService.hasAllowedNodePublisher
181+
}
177182

178183
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
179184
coinId: tokenUniqueID,

Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ final class ERC20WalletService: WalletCoreProtocol, ERC20GasAlgorithmComputable,
204204
var hasEnabledNodePublisher: AnyObservable<Bool> {
205205
erc20ApiService.hasEnabledNodePublisher
206206
}
207+
208+
@MainActor
209+
var hasAllowedNodePublisher: AnyObservable<Bool> {
210+
erc20ApiService.hasAllowedNodePublisher
211+
}
207212

208213
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
209214
coinId: tokenUniqueID,

Adamant/Modules/Wallets/Ethereum/EthWalletService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ final class EthWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, ERC2
205205
var hasEnabledNodePublisher: AnyObservable<Bool> {
206206
ethApiService.hasEnabledNodePublisher
207207
}
208+
209+
@MainActor
210+
var hasAllowedNodePublisher: AnyObservable<Bool> {
211+
ethApiService.hasAllowedNodePublisher
212+
}
208213

209214
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
210215
coinId: tokenUniqueID,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ final class KlyWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
6969
.map { $0.0 && $0.1 }
7070
.eraseToAnyPublisher()
7171
}
72+
73+
@MainActor
74+
var hasAllowedNodePublisher: AnyObservable<Bool> {
75+
klyNodeApiService.hasAllowedNodePublisher
76+
}
7277

7378
@Atomic var transactionFeeRaw: BigUInt = BigUInt(integerLiteral: 141000)
7479

Adamant/Modules/Wallets/WalletViewControllerBase.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,21 @@ extension WalletViewControllerBase {
448448
.store(in: &subscriptions)
449449

450450
service.core.hasEnabledNodePublisher
451-
.sink { [weak self] _ in self?.updateWalletUI() }
451+
.sink { [weak self] _ in
452+
self?.updateWalletUI()
453+
}
454+
.store(in: &subscriptions)
455+
456+
service.core.hasAllowedNodePublisher
457+
.sink { [weak self] hasAllowedNode in
458+
if hasAllowedNode {
459+
guard self?.service?.core is AdmWalletService == false else {
460+
self?.accountService.update(resetBalanceAndUpdate: false, updateOnlyADM: true, updateOnlyVisible: false)
461+
return
462+
}
463+
self?.service?.core.update()
464+
}
465+
}
452466
.store(in: &subscriptions)
453467
}
454468

Adamant/Modules/Wallets/WalletsService/WalletCoreProtocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ protocol WalletCoreProtocol: AnyObject, Sendable {
292292

293293
@MainActor
294294
var hasEnabledNodePublisher: AnyObservable<Bool> { get }
295+
296+
// It is used to update the balance after the node is turned on
297+
@MainActor
298+
var hasAllowedNodePublisher: AnyObservable<Bool> { get }
295299

296300
@MainActor
297301
var walletUpdatePublisher: AnyObservable<Void> { get }

Adamant/ServiceProtocols/AccountService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension Notification.Name {
3636
/// Raised when wallets collection updated
3737
///
3838
/// Use only for communication between AdmanatAccountService and AdamantWalletService.
39-
static let walletUpdated = Notification.Name("adamant.accountService.walletUpdated")
39+
static let isBalanceExpired = Notification.Name("adamant.accountService.isBalanceExpired")
4040

4141
private init() {}
4242
}

Adamant/Services/AdamantAccountService.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ extension AdamantAccountService {
175175

176176
isBalanceExpired = true
177177
NotificationCenter.default.post(
178-
name: .AdamantAccountService.walletUpdated,
179-
object: self
178+
name: .AdamantAccountService.isBalanceExpired,
179+
object: isBalanceExpired
180180
)
181181
NotificationCenter.default.post(
182182
name: .AdamantAccountService.accountDataUpdated,
@@ -233,6 +233,9 @@ extension AdamantAccountService {
233233
// MARK: - AccountService
234234
extension AdamantAccountService {
235235
// MARK: Update logged account info
236+
/// Does not update UI balance, but the
237+
/// `func update(resetBalanceAndUpdate: Bool, updateOnlyADM: Bool, updateOnlyVisible: Bool)
238+
/// does, via `wallets.first(where: {$0.core is AdmWalletService})?.core.update()`in completion
236239
func update() {
237240
self.update(nil)
238241
}
@@ -306,14 +309,6 @@ extension AdamantAccountService {
306309
isBalanceExpired = true
307310
state = prevState
308311
}
309-
310-
// Holde case when t he expiring change or when the balance change
311-
// if (isBalanceExpiredPrev != isBalanceExpired && resetBalanceAndUpdate) || (account?.balance != balancePrev) {
312-
// NotificationCenter.default.post(
313-
// name: .AdamantAccountService.walletUpdated,
314-
// object: self
315-
// )
316-
// }
317312
}
318313
}
319314
}

Adamant/Services/WalletAutoUpdaterService.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ actor WalletAutoUpdateService {
109109
interval: getTimeIntervalFor(wallet: core),
110110
queue: .global(qos: .utility),
111111
callback: {
112-
print("qwe [\(Date())] " + "Update \(wallet.core.tokenName)" + "\n----------------------")
113112
guard !(wallet.core is AdmWalletService) else {
114113
self.accountService.update(resetBalanceAndUpdate: false, updateOnlyADM: true, updateOnlyVisible: false)
115114
return

CommonKit/Sources/CommonKit/Helpers/Nodes+Allowance.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
extension Collection where Element == Node {
1010
public func getAllowedNodes(sortedBySpeedDescending: Bool, needWS: Bool) -> [Node] {
1111
let allowedNodes = filter {
12-
$0.connectionStatus == .allowed
13-
&& $0.isEnabled
14-
&& (!needWS || $0.wsEnabled)
12+
$0.connectionStatus == .allowed && $0.isEnabled && (!needWS || $0.wsEnabled)
13+
}
14+
15+
guard sortedBySpeedDescending else {
16+
return allowedNodes.shuffled()
1517
}
1618

17-
return sortedBySpeedDescending
18-
? allowedNodes.sorted {
19-
$0.ping ?? .greatestFiniteMagnitude < $1.ping ?? .greatestFiniteMagnitude
20-
}
21-
: allowedNodes.shuffled()
19+
return allowedNodes.sorted {
20+
$0.ping ?? .greatestFiniteMagnitude < $1.ping ?? .greatestFiniteMagnitude
21+
}
2222
}
2323
}

CommonKit/Sources/CommonKit/Protocols/ApiServiceProtocol.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public protocol ApiServiceProtocol: Sendable {
1919
}
2020

2121
extension ApiServiceProtocol {
22+
23+
// It is used to update the balance after the node is turned on
24+
@MainActor
25+
public var hasAllowedNodePublisher: AnyObservable<Bool> {
26+
nodesInfoPublisher
27+
.map { $0.nodes.contains { $0.connectionStatus == .allowed } }
28+
.removeDuplicates()
29+
.eraseToAnyPublisher()
30+
}
31+
2232
@MainActor
2333
public var hasEnabledNodePublisher: AnyObservable<Bool> {
2434
nodesInfoPublisher

CommonKit/Sources/CommonKit/Services/HealthCheck/HealthCheckWrapper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
7979
var lastConnectionError: Error?
8080

8181
while true {
82-
let node = await nodesForRequest(waitsForConnectivity: waitsForConnectivity)
83-
.first { !usedNodesIds.contains($0.id) }
82+
let nodes = await nodesForRequest(waitsForConnectivity: waitsForConnectivity)
83+
let node = nodes.first { !usedNodesIds.contains($0.id) }
8484

8585
guard let node else { break }
8686
usedNodesIds.insert(node.id)

0 commit comments

Comments
 (0)