-
Notifications
You must be signed in to change notification settings - Fork 905
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
feat(wallet): Bitcoin Balance Fetching #23117
Conversation
f75f76b
to
efee4f6
Compare
efee4f6
to
04c2f87
Compare
ios/brave-ios/Sources/BraveWallet/Extensions/BraveWalletExtensions.swift
Outdated
Show resolved
Hide resolved
ios/brave-ios/Sources/BraveWallet/Crypto/Stores/AccountActivityStore.swift
Outdated
Show resolved
Hide resolved
ios/brave-ios/Tests/BraveWalletTests/AssetDetailStoreTests.swift
Outdated
Show resolved
Hide resolved
ios/brave-ios/Tests/BraveWalletTests/AssetDetailStoreTests.swift
Outdated
Show resolved
Hide resolved
} | ||
} | ||
return await group.reduce([AccountBalance](), { $0 + $1 }) | ||
} | ||
for tokenBalance in tokenBalances { | ||
if let index = accountAssetViewModels.firstIndex(where: { | ||
$0.account.address == tokenBalance.account.address | ||
$0.account.cacheBalanceKey == tokenBalance.account.cacheBalanceKey | ||
}) { | ||
accountAssetViewModels[index].decimalBalance = tokenBalance.balance ?? 0.0 | ||
accountAssetViewModels[index].balance = String(format: "%.4f", tokenBalance.balance ?? 0.0) |
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.
Not specific to Bitcoin, but we need to update balance
to be a Double
or BDouble
on this view model and format as a String in UI code instead of view model. Right now the totalBalance
computed property uses this string to calculate the total balance, but this value is rounded to 4 decimal places, however total balance is being displayed with 6 decimal places.
So if I have a balance of 0.00001 BTC it will show as 0.000000 BTC total balance.
Since this isn't specific to Bitcoin, I opened a separate issue for this rounding bug:
brave/brave-browser#37670
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.
There is actually an idea somehere deep in backlog to return balance in weis, satoshis, lamports, etc. So any rounding happens only when we want to display that balance on screen.
Using double in money related ares is discouraged in general
/// Cache of token balances for each account. [account.cacheBalanceKey: [token.id: balance]] | ||
private var tokenBalancesCache: [String: [String: 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.
Is there a thing like branded types in typescrpit but for swift?
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 think thats typealias
in swift. i will update. thanks!
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.
@nuo-xu I'm not sure a typealias
would work the same here, maybe I am misunderstanding. A typealias would essentially be typealias TokenBalanceKey = String
, but would only validate the type a String
, not validate the key is the expected value/format (both address
and accountId.uniqueKey
are String
type).
I think better approach for us would be
- dropping
tokenBalancesCache
from each store/view model & fetch from cache when balance is needed- Not sure we need to fetch from CoreData cache, then cache again in the store/view model. If we need in memory cache dictionary likely better in
WalletUserAssetManager
itself so all stores/view models can share it.
- Not sure we need to fetch from CoreData cache, then cache again in the store/view model. If we need in memory cache dictionary likely better in
- Add helper methods as needed in WalletUserAssetManager which accept a
BlockchainToken
,AccountInfo
, etc. so the cache manager itself has control of the keys used in the cache(s).- We already have some/most of these I believe
pseudocode:
func getBalances(for account: BraveWallet.AccountInfo) -> Double? {
return balancesCache[account.accountId.uniqueKey]
}
let availableSatoshiString = String(btcBalance.availableBalance) | ||
let formatter = WeiFormatter(decimalFormatStyle: .decimals(precision: 8)) | ||
if let valueString = formatter.decimalString( | ||
for: availableSatoshiString, | ||
radix: .decimal, | ||
decimals: 8 | ||
) { | ||
return Double(valueString) | ||
} else { | ||
return 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.
In most contexts we need total balance
, in a context of send screen we need available balance
. For desktop ui there is also a popup which shows all three lines of complex BTC balance.
FYI
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 will update this using totalBalance
. we have a separate issue to integrate bitcoin balance full info brave/brave-browser#37640
will keep in mind to use availableBalance
in send tx.
thanks!
… address some review comments.
ios/brave-ios/Sources/BraveWallet/Crypto/Stores/AccountActivityStore.swift
Outdated
Show resolved
Hide resolved
[puLL-Merge] - brave/brave-core@23117 DescriptionThis PR updates the codebase to use ChangesChanges
Security Hotspots
|
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.
LGTM for post merge sec review
Resolves brave/brave-browser#36966
iOS starts fetching bitcoin balance for bitcoin accounts both mainnet and testnet using
BraveWalletBitcoinWalletService
Wallet will display using
availableBalance
.pendingBalance
UI will be handled in brave/brave-browser#37640Submitter Checklist:
QA/Yes
orQA/No
;release-notes/include
orrelease-notes/exclude
;OS/...
) to the associated issuenpm run test -- brave_browser_tests
,npm run test -- brave_unit_tests
wikinpm run presubmit
wiki,npm run gn_check
,npm run tslint
git rebase master
(if needed)Reviewer Checklist:
gn
After-merge Checklist:
changes has landed on
Test Plan:
In order to test this PR, you need to set up a feature flag in two ways:
If you are building this PR locally on your Xcode, duplicate Debug scheme and add --enable-features=BraveWalletBitcoin in your Arguments Passed On Launch, then build.
If you are testing using a TF build. Add BraveWalletBitcoin under Settings/BraveCore Switches/Enable Features (Enable Enable Features and a fresh launch is required)
1. Check bitcoin balance displaying
Bitcoin balance will be displayed in
you can either create a new bitcoin and check if balance is correctly displayed as $0.00
or you can restore a wallet with bitcoin accounts that have balances and check if balance is correctly displayed with the right amount.
2. Check bitcoin accounts in filter selection