Skip to content

Commit 881414d

Browse files
committed
use txdetails (instead of sentandreceivedvalues and manual calculation)
1 parent 48a0ec8 commit 881414d

File tree

6 files changed

+62
-100
lines changed

6 files changed

+62
-100
lines changed

BDKSwiftExampleWallet/Extensions/Int+Extensions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,16 @@ extension UInt64 {
7676
return Date(timeIntervalSince1970: TimeInterval(self))
7777
}
7878
}
79+
80+
extension Int64 {
81+
private static var numberFormatter: NumberFormatter = {
82+
let numberFormatter = NumberFormatter()
83+
numberFormatter.numberStyle = .decimal
84+
85+
return numberFormatter
86+
}()
87+
88+
var delimiter: String {
89+
return Int64.numberFormatter.string(from: NSNumber(value: self)) ?? ""
90+
}
91+
}

BDKSwiftExampleWallet/View Model/Activity/TransactionDetailViewModel.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import Observation
1414
class TransactionDetailViewModel {
1515
let bdkClient: BDKClient
1616

17-
var calculateFee: String?
18-
var calculateFeeError: CalculateFeeError?
1917
var esploraError: EsploraError?
2018
var esploraURL: String?
2119
var network: String?
@@ -28,18 +26,6 @@ class TransactionDetailViewModel {
2826
self.bdkClient = bdkClient
2927
}
3028

31-
func getCalulateFee(tx: BitcoinDevKit.Transaction) {
32-
do {
33-
let calculateFee = try bdkClient.calculateFee(tx)
34-
let feeString = String(calculateFee.toSat())
35-
self.calculateFee = feeString
36-
} catch let error as CalculateFeeError {
37-
DispatchQueue.main.async {
38-
self.calculateFeeError = error
39-
}
40-
} catch {}
41-
}
42-
4329
func getEsploraUrl() {
4430
let savedEsploraURL = bdkClient.getEsploraURL()
4531

@@ -65,16 +51,4 @@ class TransactionDetailViewModel {
6551
self.network = bdkClient.getNetwork().description
6652
}
6753

68-
func getSentAndReceived(tx: BitcoinDevKit.Transaction) -> SentAndReceivedValues? {
69-
do {
70-
let sentAndReceived = try bdkClient.sentAndReceived(tx)
71-
return sentAndReceived
72-
} catch {
73-
DispatchQueue.main.async {
74-
self.transactionDetailsError = .generic(message: error.localizedDescription)
75-
}
76-
return nil
77-
}
78-
}
79-
8054
}

BDKSwiftExampleWallet/View Model/Activity/TransactionListViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class TransactionListViewModel {
2222
self.bdkClient = bdkClient
2323
}
2424

25-
func getSentAndReceived(tx: BitcoinDevKit.Transaction) -> SentAndReceivedValues? {
25+
func getTxDetails(txid: Txid) -> TxDetails? {
2626
do {
27-
let sentAndReceived = try bdkClient.sentAndReceived(tx)
28-
return sentAndReceived
27+
let txDetails = try bdkClient.txDetails(txid)
28+
return txDetails
2929
} catch {
3030
self.walletTransactionsViewError = .generic(
3131
message: error.localizedDescription

BDKSwiftExampleWallet/View/Activity/TransactionDetailView.swift

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,33 @@ struct TransactionDetailView: View {
1313
@Bindable var viewModel: TransactionDetailViewModel
1414
@State private var isCopied = false
1515
@State private var showCheckmark = false
16-
let amount: UInt64
17-
let canonicalTx: CanonicalTx
16+
let txDetails: TxDetails
1817

1918
var body: some View {
2019

2120
VStack {
2221

2322
VStack(spacing: 8) {
2423
HStack(spacing: 3) {
25-
let sentAndReceivedValues = viewModel.getSentAndReceived(
26-
tx: canonicalTx.transaction
27-
)
28-
if let value = sentAndReceivedValues {
29-
let sent = value.sent
30-
let received = value.received
31-
if sent.toSat() == 0 && received.toSat() > 0 {
32-
VStack {
33-
Image("bitcoinsign.arrow.down")
34-
.symbolRenderingMode(.hierarchical)
35-
.font(.title)
36-
Text("Receive")
37-
}
38-
} else if sent.toSat() > 0 && received.toSat() >= 0 {
39-
VStack {
40-
Image("bitcoinsign.arrow.up")
41-
.symbolRenderingMode(.hierarchical)
42-
.font(.title)
43-
Text("Send")
44-
}
45-
} else {
46-
Text("?")
24+
if txDetails.balanceDelta >= 0 {
25+
VStack {
26+
Image("bitcoinsign.arrow.down")
27+
.symbolRenderingMode(.hierarchical)
28+
.font(.title)
29+
Text("Receive")
30+
}
31+
} else {
32+
VStack {
33+
Image("bitcoinsign.arrow.up")
34+
.symbolRenderingMode(.hierarchical)
35+
.font(.title)
36+
Text("Send")
4737
}
4838
}
4939
}
5040
.fontWeight(.semibold)
5141

52-
switch canonicalTx.chainPosition {
42+
switch txDetails.chainPosition {
5343
case .confirmed(let confirmationBlockTime, _):
5444
Text("Block \(confirmationBlockTime.blockId.height.delimiter)")
5545
.foregroundStyle(.secondary)
@@ -65,7 +55,7 @@ struct TransactionDetailView: View {
6555

6656
VStack(spacing: 8) {
6757
HStack {
68-
Text(amount.delimiter)
58+
Text(abs(txDetails.balanceDelta).delimiter)
6959
Text("sats")
7060
}
7161
.lineLimit(1)
@@ -75,7 +65,7 @@ struct TransactionDetailView: View {
7565
.fontWeight(.bold)
7666
.fontDesign(.rounded)
7767
VStack(spacing: 4) {
78-
switch canonicalTx.chainPosition {
68+
switch txDetails.chainPosition {
7969
case .confirmed(let confirmationBlockTime, _):
8070
Text(
8171
confirmationBlockTime.confirmationTime.toDate().formatted(
@@ -95,8 +85,8 @@ struct TransactionDetailView: View {
9585
Text("Pending")
9686
}
9787
}
98-
if let fee = viewModel.calculateFee {
99-
Text("\(fee.formattedWithSeparator) sats fee")
88+
if let fee = txDetails.fee {
89+
Text("\(fee.toSat().delimiter) sats fee")
10090
}
10191
}
10292
.foregroundStyle(.secondary)
@@ -110,7 +100,7 @@ struct TransactionDetailView: View {
110100
Button {
111101
if let esploraURL = viewModel.esploraURL {
112102
let urlString =
113-
"\(esploraURL)/tx/\(canonicalTx.transaction.computeTxid())"
103+
"\(esploraURL)/tx/\(txDetails.txid)"
114104
.replacingOccurrences(of: "/api", with: "")
115105
if let url = URL(string: urlString) {
116106
UIApplication.shared.open(url)
@@ -124,7 +114,7 @@ struct TransactionDetailView: View {
124114
Spacer()
125115
}
126116
Button {
127-
UIPasteboard.general.string = "\(canonicalTx.transaction.computeTxid())"
117+
UIPasteboard.general.string = "\(txDetails.txid)"
128118
isCopied = true
129119
showCheckmark = true
130120
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
@@ -133,7 +123,7 @@ struct TransactionDetailView: View {
133123
}
134124
} label: {
135125
HStack {
136-
Text("\(canonicalTx.transaction.computeTxid())")
126+
Text("\(txDetails.txid)")
137127
.lineLimit(1)
138128
.truncationMode(.middle)
139129
withAnimation {
@@ -154,7 +144,6 @@ struct TransactionDetailView: View {
154144
.task {
155145
viewModel.getNetwork()
156146
viewModel.getEsploraUrl()
157-
viewModel.getCalulateFee(tx: canonicalTx.transaction)
158147
}
159148

160149
}
@@ -179,8 +168,7 @@ struct TransactionDetailView: View {
179168
viewModel: .init(
180169
bdkClient: .mock
181170
),
182-
amount: UInt64(1_000_000),
183-
canonicalTx: .mock
171+
txDetails: .mock
184172
)
185173
}
186174
#endif

BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import SwiftUI
1111

1212
struct TransactionItemView: View {
1313
@Environment(\.dynamicTypeSize) var dynamicTypeSize
14-
let canonicalTx: CanonicalTx
14+
let txDetails: TxDetails
1515
let isRedacted: Bool
16-
let sentAndReceivedValues: SentAndReceivedValues
1716

1817
var body: some View {
1918

@@ -36,14 +35,13 @@ struct TransactionItemView: View {
3635
.foregroundStyle(Color.gray.opacity(0.25))
3736
Image(
3837
systemName:
39-
sentAndReceivedValues.sent.toSat() == 0
40-
&& sentAndReceivedValues.received.toSat() > 0
38+
txDetails.balanceDelta >= 0
4139
? "arrow.down" : "arrow.up"
4240
)
4341
.font(.callout)
4442
.foregroundStyle(
4543
{
46-
switch canonicalTx.chainPosition {
44+
switch txDetails.chainPosition {
4745
case .confirmed(_, _):
4846
Color.bitcoinOrange
4947
case .unconfirmed(_):
@@ -55,14 +53,14 @@ struct TransactionItemView: View {
5553
}
5654

5755
VStack(alignment: .leading, spacing: 5) {
58-
Text(canonicalTx.transaction.computeTxid().description)
56+
Text(txDetails.txid.description)
5957
.truncationMode(.middle)
6058
.lineLimit(1)
6159
.fontDesign(.monospaced)
6260
.fontWeight(.semibold)
6361
.font(.title)
6462
.foregroundStyle(.primary)
65-
switch canonicalTx.chainPosition {
63+
switch txDetails.chainPosition {
6664
case .confirmed(let confirmationBlockTime, _):
6765
Text(
6866
confirmationBlockTime.confirmationTime.toDate().formatted(
@@ -99,17 +97,16 @@ struct TransactionItemView: View {
9997

10098
Spacer()
10199

102-
Text(
103-
sentAndReceivedValues.sent.toSat() == 0
104-
&& sentAndReceivedValues.received.toSat() > 0
105-
? "+ \(sentAndReceivedValues.received.toSat()) sats"
106-
: "- \(sentAndReceivedValues.sent.toSat() - sentAndReceivedValues.received.toSat()) sats"
107-
)
108-
.font(.subheadline)
109-
.fontWeight(.semibold)
110-
.fontDesign(.rounded)
111-
.lineLimit(1)
112-
.redacted(reason: isRedacted ? .placeholder : [])
100+
let delta = txDetails.balanceDelta
101+
let prefix = delta >= 0 ? "+ " : "- "
102+
let amount = abs(delta)
103+
104+
Text("\(prefix)\(amount) sats")
105+
.font(.subheadline)
106+
.fontWeight(.semibold)
107+
.fontDesign(.rounded)
108+
.lineLimit(1)
109+
.redacted(reason: isRedacted ? .placeholder : [])
113110

114111
}
115112
.padding(.vertical, 15.0)
@@ -122,9 +119,8 @@ struct TransactionItemView: View {
122119
#if DEBUG
123120
#Preview {
124121
TransactionItemView(
125-
canonicalTx: .mock,
126-
isRedacted: false,
127-
sentAndReceivedValues: .mock
122+
txDetails: .mock,
123+
isRedacted: false
128124
)
129125
}
130126
#endif

BDKSwiftExampleWallet/View/Activity/TransactionListView.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ struct TransactionListView: View {
1919
List {
2020
if transactions.isEmpty && walletSyncState == .syncing {
2121
TransactionItemView(
22-
canonicalTx: .mock,
23-
isRedacted: true,
24-
sentAndReceivedValues: .init(
25-
sent: Amount.fromSat(satoshi: UInt64(0)),
26-
received: Amount.fromSat(satoshi: UInt64(0))
27-
)
22+
txDetails: .mock,
23+
isRedacted: true
2824
)
2925
.listRowInsets(EdgeInsets())
3026
.listRowSeparator(.hidden)
@@ -94,24 +90,19 @@ struct TransactionListView: View {
9490
) { item in
9591
let canonicalTx = item
9692
let tx = canonicalTx.transaction
97-
if let sentAndReceivedValues = viewModel.getSentAndReceived(tx: tx) {
93+
if let txDetails = viewModel.getTxDetails(txid: tx.computeTxid()) {
9894

9995
NavigationLink(
10096
destination: TransactionDetailView(
10197
viewModel: .init(
10298
bdkClient: .live
10399
),
104-
amount: sentAndReceivedValues.sent.toSat() == 0
105-
? sentAndReceivedValues.received.toSat()
106-
: sentAndReceivedValues.sent.toSat()
107-
- sentAndReceivedValues.received.toSat(),
108-
canonicalTx: canonicalTx
100+
txDetails: txDetails
109101
)
110102
) {
111103
TransactionItemView(
112-
canonicalTx: canonicalTx,
113-
isRedacted: false,
114-
sentAndReceivedValues: sentAndReceivedValues
104+
txDetails: txDetails,
105+
isRedacted: false
115106
)
116107
}
117108

0 commit comments

Comments
 (0)