@@ -12,6 +12,10 @@ extension KyotoService {
1212 static var live : BDKSyncService = KyotoService ( )
1313}
1414
15+ extension Notification . Name {
16+ static let shouldUpdateWallet = Notification . Name ( " ShouldUpdateWallet " )
17+ }
18+
1519final class KyotoService: BDKSyncService {
1620
1721 static let shared : KyotoService = . init( )
@@ -23,7 +27,7 @@ final class KyotoService: BDKSyncService {
2327
2428 private var client : CbfClient ?
2529 private var node : CbfNode ?
26- private var connected = false
30+ private var isConnected = false
2731 private var isScanRunning = false
2832
2933 private var fullScanProgress : FullScanProgress ?
@@ -63,7 +67,7 @@ final class KyotoService: BDKSyncService {
6367 self . client = nodeComponents. client
6468 self . node = nodeComponents. node
6569 isScanRunning = true
66- try await startListen ( )
70+ try await startNode ( )
6771 }
6872
6973 func startFullScan( progress: @escaping FullScanProgress ) async throws {
@@ -80,7 +84,7 @@ final class KyotoService: BDKSyncService {
8084 self . client = nodeComponents. client
8185 self . node = nodeComponents. node
8286 isScanRunning = true
83- try await startListen ( )
87+ try await startNode ( )
8488 }
8589
8690 func send( address: String , amount: UInt64 , feeRate: UInt64 ) async throws {
@@ -118,17 +122,19 @@ final class KyotoService: BDKSyncService {
118122 . build ( wallet: wallet)
119123 }
120124
121- private func startListen ( ) async throws {
125+ private func startNode ( ) async throws {
122126 node? . run ( )
123127 printLogs ( )
124128 updateWarn ( )
125- try await startUpdating ( )
129+ try await updateWallet ( )
130+ startRealTimeWalletUpdate ( )
126131 }
127132
128133 @discardableResult
129- func startUpdating ( ) async throws -> Bool {
134+ private func updateWallet ( ) async throws -> Bool {
130135 guard let update = await self . client? . update ( ) else {
131136 isScanRunning = false
137+ print ( " Nothing to update " )
132138 return false
133139 }
134140 try self . wallet? . applyUpdate ( update: update)
@@ -137,7 +143,27 @@ final class KyotoService: BDKSyncService {
137143 isScanRunning = false
138144 return true
139145 }
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+
141167 private func printLogs( ) {
142168 Task {
143169 while true {
@@ -146,7 +172,7 @@ final class KyotoService: BDKSyncService {
146172 switch log {
147173 case . connectionsMet:
148174 print ( " ######### connected " )
149- self . connected = true
175+ self . isConnected = true
150176 case . progress( let progress) :
151177 if let fullScanProgress = self . fullScanProgress {
152178 let _progress = UInt64 ( progress * 100.0 )
@@ -167,7 +193,7 @@ final class KyotoService: BDKSyncService {
167193 switch warn {
168194 case . needConnections:
169195 print ( " ######### disconnected " )
170- self . connected = false
196+ self . isConnected = false
171197 default :
172198 #if DEBUG
173199 print ( warn)
0 commit comments