Skip to content

Commit 3737185

Browse files
committed
fix connection and syncronization
1 parent db17c06 commit 3737185

File tree

6 files changed

+53
-15
lines changed

6 files changed

+53
-15
lines changed

BDKSwiftExampleWallet.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@
239239
7745E1922DD7D2E600D52A40 /* BDKSyncService */ = {
240240
isa = PBXGroup;
241241
children = (
242+
7745E1902DD7C47600D52A40 /* BDKSyncService.swift */,
242243
7745E1952DD7D8FD00D52A40 /* KyotoSyncService.swift */,
243244
7745E1932DD7D2F500D52A40 /* EsploraServerSyncService.swift */,
244-
7745E1902DD7C47600D52A40 /* BDKSyncService.swift */,
245245
);
246246
path = BDKSyncService;
247247
sourceTree = "<group>";

BDKSwiftExampleWallet/Extensions/BDK+Extensions/Connection+Extensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ extension Connection {
66
let documentsDirectoryURL = URL.defaultWalletDirectory
77
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent(URL.walletDirectoryName)
88

9+
if FileManager.default.fileExists(atPath: walletDataDirectoryURL.path) {
10+
try FileManager.default.removeItem(at: walletDataDirectoryURL)
11+
}
12+
913
try FileManager.default.ensureDirectoryExists(at: walletDataDirectoryURL)
1014
try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL)
1115
let connection = try Connection(path: URL.persistenceBackendPath)
1216
return connection
1317
}
18+
19+
static func loadConnection() throws -> Connection {
20+
let persistenceBackendPath = URL.persistenceBackendPath
21+
let connection = try Connection(path: persistenceBackendPath)
22+
return connection
23+
}
1424
}

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ protocol BDKSyncService {
2323

2424
func updateNetwork(network: Network)
2525
func updateEsploraURL(_ url: String)
26+
27+
func getTransactions() throws -> [CanonicalTx]
28+
func getBalance() throws -> Balance
2629
}
2730

2831
extension BDKSyncService {
2932
func buildWallet(params: String?) throws -> Wallet {
30-
guard let newConnection = self.connection == nil ?
31-
try Connection.createConnection() :
32-
self.connection else {
33+
guard let connection = self.connection else {
3334
throw WalletError.dbNotFound
3435
}
3536

@@ -45,7 +46,7 @@ extension BDKSyncService {
4546
descriptor: descriptor,
4647
changeDescriptor: changeDescriptor,
4748
network: network,
48-
connection: newConnection
49+
connection: connection
4950
)
5051

5152
return wallet
@@ -151,18 +152,16 @@ extension BDKSyncService {
151152
}
152153

153154
func loadWalleFromBackup() throws -> Wallet {
155+
guard let connection = self.connection else {
156+
throw WalletError.dbNotFound
157+
}
158+
154159
let backupInfo = try keyClient.getBackupInfo()
155160
let descriptor = try Descriptor(descriptor: backupInfo.descriptor, network: self.network)
156161
let changeDescriptor = try Descriptor(
157162
descriptor: backupInfo.changeDescriptor,
158163
network: self.network
159164
)
160-
161-
try FileManager.default.ensureDirectoryExists(at: URL.walletDataDirectoryURL)
162-
try FileManager.default.removeOldFlatFileIfNeeded(at: URL.defaultWalletDirectory)
163-
let persistenceBackendPath = URL.persistenceBackendPath
164-
let connection = try Connection(path: persistenceBackendPath)
165-
166165
let wallet = try Wallet.load(
167166
descriptor: descriptor,
168167
changeDescriptor: changeDescriptor,

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraServerSyncService.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ final class EsploraServerSyncService: BDKSyncService {
3434
}
3535

3636
func createWallet(params: String?) throws {
37+
self.connection = try Connection.createConnection()
3738
self.wallet = try buildWallet(params: params)
39+
needsFullScan = true
3840
}
3941

4042
func loadWallet() throws {
43+
self.connection = try Connection.loadConnection()
4144
let wallet = try loadWalleFromBackup()
4245
self.wallet = wallet
4346
}
@@ -92,4 +95,21 @@ final class EsploraServerSyncService: BDKSyncService {
9295
}
9396
let _ = try wallet.persist(connection: connection)
9497
}
98+
99+
func getTransactions() throws -> [CanonicalTx] {
100+
guard let wallet = self.wallet else {
101+
throw WalletError.walletNotFound
102+
}
103+
let transactions = wallet.transactions()
104+
let sortedTransactions = transactions.sorted { (tx1, tx2) in
105+
return tx1.chainPosition.isBefore(tx2.chainPosition)
106+
}
107+
return sortedTransactions
108+
}
109+
110+
func getBalance() throws -> Balance {
111+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
112+
let balance = wallet.balance()
113+
return balance
114+
}
95115
}

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoSyncService.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ final class KyotoSyncService: BDKSyncService {
5252
func startFullScan(progress: FullScanScriptInspector) async throws {
5353

5454
}
55+
56+
func getTransactions() throws -> [CanonicalTx] {
57+
[]
58+
}
59+
60+
func getBalance() throws -> Balance {
61+
fatalError("Missing implementation")
62+
}
5563
}

BDKSwiftExampleWallet/View Model/WalletViewModel.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import SwiftUI
1313
@MainActor
1414
@Observable
1515
class WalletViewModel {
16+
private let bdkSyncService: BDKSyncService
1617
let bdkClient: BDKClient
1718
let keyClient: KeyClient
1819
let priceClient: PriceClient
@@ -60,8 +61,6 @@ class WalletViewModel {
6061
}
6162
}
6263
}
63-
64-
private let bdkSyncService: BDKSyncService
6564

6665
init(
6766
bdkClient: BDKClient = .live,
@@ -104,7 +103,8 @@ class WalletViewModel {
104103

105104
func getBalance() {
106105
do {
107-
let balance = try bdkClient.getBalance()
106+
// let balance = try bdkClient.getBalance()
107+
let balance = try bdkSyncService.getBalance()
108108
self.balanceTotal = balance.total.toSat()
109109
} catch let error as WalletError {
110110
self.walletViewError = .generic(message: error.localizedDescription)
@@ -128,7 +128,8 @@ class WalletViewModel {
128128

129129
func getTransactions() {
130130
do {
131-
let transactionDetails = try bdkClient.transactions()
131+
let transactionDetails = try bdkSyncService.getTransactions()
132+
// let transactionDetails = try bdkClient.transactions()
132133
self.transactions = transactionDetails
133134
} catch let error as WalletError {
134135
self.walletViewError = .generic(message: error.localizedDescription)

0 commit comments

Comments
 (0)