This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Auto-activate asset when buying crypto #1824
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import Formatters | ||
| import Validators | ||
| import BigInt | ||
| import BalanceService | ||
|
|
||
| @MainActor | ||
| @Observable | ||
|
|
@@ -26,7 +27,9 @@ | |
| private let assetAddress: AssetAddress | ||
| private let currencyFormatter: CurrencyFormatter | ||
| private let valueFormatter = ValueFormatter(locale: .US, style: .medium) | ||
| private let walletId: WalletId | ||
| private let wallet: Wallet | ||
| private let assetsEnabler: any AssetsEnabler | ||
| private var walletId: WalletId { wallet.walletId } | ||
|
|
||
| public let assetQuery: ObservableQuery<AssetRequest> | ||
| var assetData: AssetData { assetQuery.value } | ||
|
|
@@ -43,28 +46,30 @@ | |
| fiatService: any GemAPIFiatService, | ||
| currencyFormatter: CurrencyFormatter = CurrencyFormatter(currencyCode: Currency.usd.rawValue), | ||
| assetAddress: AssetAddress, | ||
| walletId: WalletId, | ||
| wallet: Wallet, | ||
| assetsEnabler: any AssetsEnabler, | ||
| type: FiatQuoteType = .buy, | ||
| amount: Int? = nil | ||
| ) { | ||
| self.fiatService = fiatService | ||
| self.currencyFormatter = currencyFormatter | ||
| self.assetAddress = assetAddress | ||
| self.walletId = walletId | ||
| self.wallet = wallet | ||
| self.assetsEnabler = assetsEnabler | ||
| self.type = type | ||
| self.assetQuery = ObservableQuery(AssetRequest(walletId: walletId, assetId: assetAddress.asset.id), initialValue: .with(asset: assetAddress.asset)) | ||
|
|
||
| let buyOperation = BuyOperation( | ||
| service: fiatService, | ||
| asset: assetAddress.asset, | ||
| currencyFormatter: currencyFormatter, | ||
| walletId: walletId | ||
| ) | ||
| let sellOperation = SellOperation( | ||
| service: fiatService, | ||
| asset: assetAddress.asset, | ||
| currencyFormatter: currencyFormatter, | ||
| walletId: walletId | ||
| ) | ||
|
|
||
| self.buyViewModel = FiatOperationViewModel( | ||
|
|
@@ -191,6 +196,7 @@ | |
| } | ||
|
|
||
| urlState = .data(()) | ||
| Task { await enableAsset() } | ||
| await UIApplication.shared.open(url, options: [:]) | ||
| } catch { | ||
| urlState = .error(error) | ||
|
|
@@ -243,6 +249,15 @@ | |
| BalanceViewModel(asset: asset, balance: assetData.balance, formatter: valueFormatter) | ||
| } | ||
|
|
||
| private func enableAsset() async { | ||
| guard type == .buy else { return } | ||
| do { | ||
| try await assetsEnabler.enableAssets(wallet: wallet, assetIds: [asset.id], enabled: true) | ||
| } catch { | ||
| debugLog("FiatSceneViewModel enableAsset error: \(error)") | ||
| } | ||
| } | ||
|
Comment on lines
+252
to
+259
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. To prevent potential UI blocking from synchronous I/O operations within I recommend changing this function to be synchronous and using private func enableAsset() {
guard type == .buy else { return }
// Capture properties to use in the detached task
let wallet = self.wallet
let asset = self.asset
let assetsEnabler = self.assetsEnabler
Task.detached {
do {
try await assetsEnabler.enableAssets(wallet: wallet, assetIds: [asset.id], enabled: true)
} catch {
debugLog("FiatSceneViewModel enableAsset error: \(error)")
}
}
} |
||
|
|
||
| private func selectAmount(_ amount: Int) { | ||
| currentViewModel.reset() | ||
| currentViewModel.inputValidationModel.update(text: String(amount)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
To avoid potential UI blocking, it's safer to run the asset enabling logic on a background thread. The current implementation with
Taskinherits the main actor context, which could lead to UI freezes ifenableAsset()performs synchronous I/O operations.I'm suggesting a change to the
enableAsset()function to make it synchronous and launch aTask.detachedfor background execution. Consequently, this call site should be updated to a direct synchronous call.