@@ -12,6 +12,10 @@ extension KyotoService {
12
12
static var live : BDKSyncService = KyotoService ( )
13
13
}
14
14
15
+ extension Notification . Name {
16
+ static let shouldUpdateWallet = Notification . Name ( " ShouldUpdateWallet " )
17
+ }
18
+
15
19
final class KyotoService: BDKSyncService {
16
20
17
21
static let shared : KyotoService = . init( )
@@ -23,7 +27,7 @@ final class KyotoService: BDKSyncService {
23
27
24
28
private var client : CbfClient ?
25
29
private var node : CbfNode ?
26
- private var connected = false
30
+ private var isConnected = false
27
31
private var isScanRunning = false
28
32
29
33
private var fullScanProgress : FullScanProgress ?
@@ -63,7 +67,7 @@ final class KyotoService: BDKSyncService {
63
67
self . client = nodeComponents. client
64
68
self . node = nodeComponents. node
65
69
isScanRunning = true
66
- try await startListen ( )
70
+ try await startNode ( )
67
71
}
68
72
69
73
func startFullScan( progress: @escaping FullScanProgress ) async throws {
@@ -80,7 +84,7 @@ final class KyotoService: BDKSyncService {
80
84
self . client = nodeComponents. client
81
85
self . node = nodeComponents. node
82
86
isScanRunning = true
83
- try await startListen ( )
87
+ try await startNode ( )
84
88
}
85
89
86
90
func send( address: String , amount: UInt64 , feeRate: UInt64 ) async throws {
@@ -118,17 +122,19 @@ final class KyotoService: BDKSyncService {
118
122
. build ( wallet: wallet)
119
123
}
120
124
121
- private func startListen ( ) async throws {
125
+ private func startNode ( ) async throws {
122
126
node? . run ( )
123
127
printLogs ( )
124
128
updateWarn ( )
125
- try await startUpdating ( )
129
+ try await updateWallet ( )
130
+ startRealTimeWalletUpdate ( )
126
131
}
127
132
128
133
@discardableResult
129
- func startUpdating ( ) async throws -> Bool {
134
+ private func updateWallet ( ) async throws -> Bool {
130
135
guard let update = await self . client? . update ( ) else {
131
136
isScanRunning = false
137
+ print ( " Nothing to update " )
132
138
return false
133
139
}
134
140
try self . wallet? . applyUpdate ( update: update)
@@ -137,7 +143,27 @@ final class KyotoService: BDKSyncService {
137
143
isScanRunning = false
138
144
return true
139
145
}
140
-
146
+
147
+ private func startRealTimeWalletUpdate( ) {
148
+ print ( #function)
149
+ Task {
150
+ while true {
151
+ print ( " Updating: \( Date ( ) ) " )
152
+ if let update = await client? . update ( ) {
153
+ do {
154
+ try wallet? . applyUpdate ( update: update)
155
+ NotificationCenter . default. post ( name: . shouldUpdateWallet, object: nil )
156
+ print ( " Updated wallet " )
157
+ } catch {
158
+ print ( error)
159
+ }
160
+ } else {
161
+ print ( " Nothing to update " )
162
+ }
163
+ }
164
+ }
165
+ }
166
+
141
167
private func printLogs( ) {
142
168
Task {
143
169
while true {
@@ -146,7 +172,7 @@ final class KyotoService: BDKSyncService {
146
172
switch log {
147
173
case . connectionsMet:
148
174
print ( " ######### connected " )
149
- self . connected = true
175
+ self . isConnected = true
150
176
case . progress( let progress) :
151
177
if let fullScanProgress = self . fullScanProgress {
152
178
let _progress = UInt64 ( progress * 100.0 )
@@ -167,7 +193,7 @@ final class KyotoService: BDKSyncService {
167
193
switch warn {
168
194
case . needConnections:
169
195
print ( " ######### disconnected " )
170
- self . connected = false
196
+ self . isConnected = false
171
197
default :
172
198
#if DEBUG
173
199
print ( warn)
0 commit comments