Skip to content

Commit

Permalink
refactor: send
Browse files Browse the repository at this point in the history
  • Loading branch information
reez authored Aug 22, 2023
1 parent f5acb81 commit 32fe95d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
17 changes: 16 additions & 1 deletion BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,29 @@ class BDKService {
}

func send(address: String, amount: UInt64, feeRate: Float?) throws {
let txBuilder = try buildTransaction(address: address, amount: amount, feeRate: feeRate)
// showFee()
try signAndBroadcast(txBuilder: txBuilder)
}

private func buildTransaction(address: String, amount: UInt64, feeRate: Float?) throws -> TxBuilderResult {
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
guard let config = blockchainConfig else { throw WalletError.blockchainConfigNotFound }
let script = try Address(address: address)
.scriptPubkey()
let txBuilder = try TxBuilder()
.addRecipient(script: script, amount: amount)
.feeRate(satPerVbyte: feeRate ?? 1.0)
.finish(wallet: wallet)
return txBuilder
}

private func showFee() {
// TODO: let result = txBuilder.transactionDetails.fee
}

private func signAndBroadcast(txBuilder: TxBuilderResult) throws {
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
guard let config = blockchainConfig else { throw WalletError.blockchainConfigNotFound }
let _ = try wallet.sign(psbt: txBuilder.psbt, signOptions: nil)
let transaction = txBuilder.psbt.extractTx()
let blockchain = try Blockchain(config: config)
Expand Down
26 changes: 12 additions & 14 deletions BDKSwiftExampleWallet/View Model/WalletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class WalletViewModel {
func getPrices() async {
do {
let price = try await priceService.prices()
self.price = price.usd
self.time = price.time
self.price = price.usd
self.time = price.time
} catch {
print("getPrices error: \(error.localizedDescription)")
}
Expand Down Expand Up @@ -60,18 +60,16 @@ class WalletViewModel {
}

func sync() async {
self.walletSyncState = .syncing
Task {
do {
try await BDKService.shared.sync()
self.walletSyncState = .synced
self.lastSyncTime = Date()
self.getBalance()
self.getTransactions()
self.valueInUSD()
} catch {
self.walletSyncState = .error(error)
}
self.walletSyncState = .syncing
do {
try await BDKService.shared.sync()
self.walletSyncState = .synced
self.lastSyncTime = Date()
self.getBalance()
self.getTransactions()
self.valueInUSD()
} catch {
self.walletSyncState = .error(error)
}
}

Expand Down

0 comments on commit 32fe95d

Please sign in to comment.