-
Notifications
You must be signed in to change notification settings - Fork 296
wallet: Add fiat pricing to Wallet View #3001
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
base: master
Are you sure you want to change the base?
Conversation
fca8e92
to
97744fe
Compare
On Fri, Apr 25, 2025 at 09:23:56AM -0600, ericholguin wrote:
This PR adds the fiat price to user's wallet balance.
Using Coinbase API to get BTC current price and the user's locale (region)
to determine their currency.
Changelog-Added: Added fiat pricing to wallet balance
Signed-off-by: ericholguin ***@***.***>
Closes: #3001
---
LGTM
Reviewed-by: William Casarin ***@***.***>
|
o1 review:
From: ***@***.***
To: ***@***.***
Subject: Review for PR wallet: Add fiat pricing to Wallet View #3001
#3001
Review for "wallet: Add fiat pricing to Wallet View" #3001
by ericholguin
Here are a few observations about potential issues in this PR:
1. Locale.currency force-unwrap: The code uses 'Locale.current.currency!.identifier', which could lead to a crash if the locale lacks a recognized currency. It might be safer to handle cases where this could be nil.
2. Error handling for fetch: In CoinbaseModel, if the network call fails, there's only a printed error. You may want to provide a user-facing fallback or
more robust error handling.
|
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.
Pull Request Overview
This PR adds fiat pricing to the wallet balance by integrating Coinbase API pricing data and displaying the converted value in the wallet UI. Key changes include:
- Adjusting the vertical spacing in WalletView for enhanced layout consistency.
- Updating BalanceView to instantiate and display fiat pricing from a new CoinbaseModel.
- Introducing CoinbaseModel to handle BTC price fetching, caching, and conversion.
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
damus/Views/Wallet/WalletView.swift | Increased VStack spacing to improve UI layout |
damus/Views/Wallet/BalanceView.swift | Added CoinbaseModel instantiation and fiat price display |
damus/Models/CoinbaseModel.swift | New model to fetch, cache, and convert BTC to fiat pricing |
Files not reviewed (1)
- damus.xcodeproj/project.pbxproj: Language not supported
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.
potential crash
7400b0b
to
463c2d1
Compare
This PR adds the fiat price to user's wallet balance. Using Coinbase API to get BTC current price and the user's locale (region) to determine their currency. Changelog-Added: Added fiat pricing to wallet balance Signed-off-by: ericholguin <[email protected]>
463c2d1
to
d3f0bff
Compare
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.
Thanks @ericholguin! I added some comments regarding a few concerns I have with the changes.
if symbol.isEmpty || symbol == currency { | ||
return amount + " " + currency | ||
} | ||
return symbol + amount + " " + currency |
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.
This seems to be making some assumptions about currency value formatting that might not be true for some locales.
I believe Apple provides a way to format currencies in a simpler and localization-friendly way, by using numberStyle = .currency
instead of numberStyle = .decimal
:
|
||
struct CoinbasePrice: Codable { | ||
let amount: Double | ||
} |
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.
nitpick tip: The structs CoinbasePrice
and CoinbaseResponse
exist in the context of CoinbaseModel
, so we can move them inside CoinbaseModel
to make namespace semantics more explicit and avoid having too many names in the global namespace:
struct CoinbaseModel {
...
struct Response: Codable {
let data: Price
struct Price: Codable {
let amount: Double
}
}
}
|
||
func satsToFiat(input: Int64) -> String { | ||
guard let btcPrice = btcPrice, input > 0 else { | ||
return "" |
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.
We should use more expressive semantics of the result, and let the view choose how to handle failures.
Instead of defaulting to an empty string or value when something fails, we should:
- throw errors if something unexpected happened, and then let the view choose whether to show or suppress those errors.
- Return
nil
if there is no value available.
self.btcPrice = decoded.data.amount | ||
} | ||
} catch { | ||
print("Failed to fetch BTC price from Coinbase: \(error)") |
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.
I am a bit worried about how this error is being handled. If something is broken or the API stops working, the interface will just keep displaying the last known value forever, which might be misleading and hard to notice.
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.
Pull Request Overview
This PR adds fiat pricing to the user's wallet balance by fetching BTC spot prices from Coinbase and displaying converted values in the Wallet UI.
- Increased spacing in the wallet view to make room for the new fiat price line
- Introduced a
CoinbaseModel
to fetch, cache, and convert sats to local fiat - Integrated the fiat conversion into
BalanceView
with on-appear load and fetch calls
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
File | Description |
---|---|
damus/Views/Wallet/WalletView.swift | Adjusted VStack spacing to accommodate the added fiat line |
damus/Views/Wallet/BalanceView.swift | Added @StateObject for CoinbaseModel and fiat price display |
damus/Models/CoinbaseModel.swift | New model for fetching/caching BTC price and converting sats |
damus.xcodeproj/project.pbxproj | Registered CoinbaseModel.swift in build settings |
Comments suppressed due to low confidence (2)
damus/Views/Wallet/BalanceView.swift:12
- [nitpick] The variable name
coinbase
is a bit ambiguous; consider renaming it to something likecoinbaseViewModel
orpriceModel
to clarify its role.
@StateObject private var coinbase = CoinbaseModel()
damus/Models/CoinbaseModel.swift:1
- New logic for price fetching, caching, and conversion lacks unit tests. Consider adding tests for
satsToFiat
, caching behavior, andfetchFromCoinbase
error handling.
//
Text(coinbase.satsToFiat(input: balance)) | ||
.fontWeight(.medium) | ||
.foregroundStyle(DamusColors.neutral6) |
Copilot
AI
May 26, 2025
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.
If satsToFiat
returns an empty string (e.g., zero balance or missing price), this Text
will render blank space. Consider conditionally showing the view only when the string is non-empty.
Text(coinbase.satsToFiat(input: balance)) | |
.fontWeight(.medium) | |
.foregroundStyle(DamusColors.neutral6) | |
let fiatValue = coinbase.satsToFiat(input: balance) | |
if !fiatValue.isEmpty { | |
Text(fiatValue) | |
.fontWeight(.medium) | |
.foregroundStyle(DamusColors.neutral6) | |
} |
Copilot uses AI. Check for mistakes.
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.
Some items still need addressing. Please let me know if there are any questions on the comments placed.
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.
Decimal
should be used instead of Double
to avoid precision issues.
|
||
class CoinbaseModel: ObservableObject { | ||
|
||
@Published var btcPrice: Double? = nil { |
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.
@Published var btcPrice: Double? = nil { | |
@Published var btcPrice: Decimal? = nil { |
guard let btcPrice = btcPrice, input > 0 else { | ||
return "" | ||
} | ||
let fiat = (Double(input) / 100_000_000) * btcPrice |
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.
let fiat = (Double(input) / 100_000_000) * btcPrice | |
let fiat = (Decimal(input) / 100_000_000) * btcPrice |
} | ||
} | ||
|
||
private func cachePrice(_ price: Double) { |
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.
private func cachePrice(_ price: Double) { | |
private func cachePrice(_ price: Decimal) { |
} | ||
|
||
struct CoinbasePrice: Codable { | ||
let amount: Double |
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.
let amount: Double | |
let amount: Decimal |
Summary
This PR adds the fiat price to user's wallet balance. Using Coinbase API to get BTC current price and the user's locale (region) to determine their currency.
Changelog-Added: Added fiat pricing to wallet balance
Checklist
Closes:
orFixes:
tags in the commit messages wherever applicable, or made sure those are not needed. See Submitting patchesTest report
Please provide a test report for the changes in this PR. You can use the template below, but feel free to modify it as needed.
Device: [Please specify the device you used for testing]
iPhone 16 Pro
iOS: [Please specify the iOS version you used for testing]
18.3.2
Damus: [Please specify the Damus version or commit hash you used for testing]
1.14 (1) 54d6161
Setup: [Please provide a brief description of the setup you used for testing, if applicable]
XCode Simulator / Local Device
Steps: [Please provide a list of steps you took to test the changes in this PR]
Open Wallet Balance verify there is a fiat price below the wallet balance and verify it is in your regional currency.
Results:
Other notes
[Please provide any other information that you think is relevant to this PR.]