-
Notifications
You must be signed in to change notification settings - Fork 56
fix(swift-sdk): show combined wallet balance on Wallets list #3537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -644,22 +644,55 @@ struct WalletRowView: View { | |
| let wallet: PersistentWallet | ||
| @EnvironmentObject var platformState: AppState | ||
|
|
||
| /// Per-wallet BLAST-synced platform-address balances. Mirrors | ||
| /// `BalanceCardView` so the summary row sees the same balance as | ||
| /// the detail view — previously the row only summed identity | ||
| /// credits, so a wallet funded purely via Platform Payment | ||
| /// addresses (no identities) showed "Empty". | ||
| @Query private var addressBalances: [PersistentPlatformAddress] | ||
|
|
||
| init(wallet: PersistentWallet) { | ||
| self.wallet = wallet | ||
| let walletId = wallet.walletId | ||
| _addressBalances = Query( | ||
| filter: #Predicate<PersistentPlatformAddress> { $0.walletId == walletId } | ||
| ) | ||
| } | ||
|
|
||
| /// Identities on this wallet — via the SwiftData relationship. | ||
| /// The wallet↔identity relationship is the canonical source | ||
| /// now; no need to filter `appState.identities` by walletId. | ||
| private var identitiesForWallet: [PersistentIdentity] { | ||
| wallet.identities | ||
| } | ||
|
|
||
| /// Platform balance in credits: prefer BLAST address sync, fall | ||
| /// back to summing identity credits when no addresses have been | ||
| /// synced yet. | ||
| private var platformBalance: UInt64 { | ||
| identitiesForWallet.reduce(0) { $0 + UInt64(bitPattern: $1.balance) } | ||
| let blastBalance = addressBalances.reduce(UInt64(0)) { $0 + $1.balance } | ||
| if blastBalance > 0 { return blastBalance } | ||
| return identitiesForWallet.reduce(UInt64(0)) { | ||
| $0 + UInt64(bitPattern: $1.balance) | ||
| } | ||
| } | ||
|
|
||
| private var totalCoreBalance: UInt64 { | ||
| wallet.balanceConfirmed + wallet.balanceUnconfirmed | ||
| + wallet.balanceImmature + wallet.balanceLocked | ||
| } | ||
|
|
||
| /// Combined wallet balance expressed in DASH. Core uses 1e8 | ||
| /// duffs/DASH; Platform uses 1e11 credits/DASH. | ||
| private var combinedDashAmount: Double { | ||
| Double(totalCoreBalance) / 100_000_000.0 | ||
| + Double(platformBalance) / 100_000_000_000.0 | ||
| } | ||
|
|
||
| private var hasAnyBalance: Bool { | ||
| totalCoreBalance > 0 || platformBalance > 0 | ||
| } | ||
|
Comment on lines
+685
to
+694
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Header may render “0 DASH” for tiny platform balances despite
♻️ One option- private func formatDash(_ dash: Double) -> String {
+ private func formatDash(_ dash: Double, maxFractionDigits: Int = 8) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 0
- formatter.maximumFractionDigits = 8
+ formatter.maximumFractionDigits = maxFractionDigits
if let formatted = formatter.string(from: NSNumber(value: dash)) {
return "\(formatted) DASH"
}
- return String(format: "%.8f DASH", dash)
+ return String(format: "%.\(maxFractionDigits)f DASH", dash)
}Also applies to: 787-790 🤖 Prompt for AI Agents |
||
|
|
||
| private var walletIdShort: String { | ||
| let hex = wallet.walletId.prefix(6) | ||
| .map { String(format: "%02x", $0) } | ||
|
|
@@ -680,7 +713,10 @@ struct WalletRowView: View { | |
| } | ||
|
|
||
| private func formatBalance(_ amount: UInt64) -> String { | ||
| let dash = Double(amount) / 100_000_000.0 | ||
| formatDash(Double(amount) / 100_000_000.0) | ||
| } | ||
|
|
||
| private func formatDash(_ dash: Double) -> String { | ||
| let formatter = NumberFormatter() | ||
| formatter.numberStyle = .decimal | ||
| formatter.minimumFractionDigits = 0 | ||
|
|
@@ -748,10 +784,10 @@ struct WalletRowView: View { | |
| } | ||
| } | ||
| Spacer() | ||
| Text(totalCoreBalance == 0 ? "Empty" : formatBalance(totalCoreBalance)) | ||
| Text(hasAnyBalance ? formatDash(combinedDashAmount) : "Empty") | ||
| .font(.subheadline) | ||
| .fontWeight(.medium) | ||
| .foregroundColor(totalCoreBalance == 0 ? .secondary : .primary) | ||
| .foregroundColor(hasAnyBalance ? .primary : .secondary) | ||
| } | ||
|
|
||
| // Row 1: network + created date. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: dashpay/platform
Length of output: 42
🏁 Script executed:
Repository: dashpay/platform
Length of output: 459
🏁 Script executed:
Repository: dashpay/platform
Length of output: 1097
🏁 Script executed:
Repository: dashpay/platform
Length of output: 1995
🏁 Script executed:
Repository: dashpay/platform
Length of output: 13278
🏁 Script executed:
Repository: dashpay/platform
Length of output: 7577
🏁 Script executed:
Repository: dashpay/platform
Length of output: 1390
🏁 Script executed:
Repository: dashpay/platform
Length of output: 42
🏁 Script executed:
Repository: dashpay/platform
Length of output: 1096
🏁 Script executed:
Repository: dashpay/platform
Length of output: 3234
🏁 Script executed:
Repository: dashpay/platform
Length of output: 1384
🏁 Script executed:
Repository: dashpay/platform
Length of output: 1273
Implementation does not achieve parity with BalanceCardView — missing sync state tracking.
The
platformBalancelogic in CoreContentView (lines 669–678) falls back to identity credits wheneverblastBalance == 0, but BalanceCardView only falls back when the wallet has never synced. BalanceCardView queriesPersistentSyncStateto distinguish "synced with zero balance" from "not yet synced", and returns zero credits in the synced-but-empty case. CoreContentView lacks this distinction, so it will incorrectly display stale identity credits on a wallet that has been BLAST-synced but now holds zero platform credits.To match BalanceCardView, add the
syncStatesquery and check:♻️ Proposed change
🤖 Prompt for AI Agents