Skip to content

Commit 61e7719

Browse files
authored
Fix #8616: Swap Improvements (#8617)
* For Solana swaps, update `maxRetries` to 2 and `preflightCommitment` to `"processed"` for higher success rates with v4 API. * Fix Solana Swaps not correctly showing loading state when creating a transaction. * Fix possible race condition fetching price quote. Ignore response from prior request if sell amount changes.
1 parent abdd897 commit 61e7719

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
6161
swapResponse = nil
6262
jupiterQuote = nil
6363
braveFee = nil
64+
// price quote requested for a different amount
65+
priceQuoteTask?.cancel()
6466
}
6567
guard !sellAmount.isEmpty, BDouble(sellAmount.normalizedDecimals) != nil else {
6668
state = .idle
@@ -353,13 +355,13 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
353355
}
354356

355357
@MainActor private func createEthSwapTransaction() async -> Bool {
356-
self.isMakingTx = true
357-
defer { self.isMakingTx = false }
358358
guard let accountInfo = self.accountInfo else {
359359
self.state = .error(Strings.Wallet.unknownError)
360360
self.clearAllAmount()
361361
return false
362362
}
363+
self.isMakingTx = true
364+
defer { self.isMakingTx = false }
363365
let coin = accountInfo.coin
364366
let network = await rpcService.network(coin, origin: nil)
365367
guard let swapParams = self.swapParameters(for: .perSellAsset, in: network) else {
@@ -661,6 +663,7 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
661663
self.isUpdatingPriceQuote = true
662664
let (jupiterQuote, swapErrorResponse, _) = await swapService.jupiterQuote(jupiterQuoteParams)
663665
defer { self.isUpdatingPriceQuote = false }
666+
guard !Task.isCancelled else { return }
664667
if let jupiterQuote {
665668
await self.handleSolPriceQuoteResponse(jupiterQuote, swapParams: swapParams)
666669
} else if let swapErrorResponse {
@@ -748,6 +751,8 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
748751
let selectedFromToken else {
749752
return false
750753
}
754+
self.isMakingTx = true
755+
defer { self.isMakingTx = false }
751756
let network = await rpcService.network(.sol, origin: nil)
752757
let jupiterSwapParams: BraveWallet.JupiterSwapParams = .init(
753758
chainId: network.chainId,
@@ -777,8 +782,8 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
777782
fromBase64EncodedTransaction: swapTransactions.swapTransaction,
778783
txType: .solanaSwap,
779784
send: .init(
780-
maxRetries: nil,
781-
preflightCommitment: nil,
785+
maxRetries: .init(maxRetries: 2),
786+
preflightCommitment: "processed",
782787
skipPreflight: .init(skipPreflight: true)
783788
)
784789
)
@@ -831,8 +836,10 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
831836
}
832837
}
833838

839+
private var priceQuoteTask: Task<(), Never>?
834840
func fetchPriceQuote(base: SwapParamsBase) {
835-
Task { @MainActor in
841+
priceQuoteTask?.cancel()
842+
priceQuoteTask = Task { @MainActor in
836843
// reset quotes before fetching new quote
837844
swapResponse = nil
838845
jupiterQuote = nil
@@ -842,6 +849,7 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
842849
return
843850
}
844851
let network = await rpcService.network(accountInfo.coin, origin: nil)
852+
guard !Task.isCancelled else { return }
845853
// Entering a buy amount is disabled for Solana swaps, always use
846854
// `SwapParamsBase.perSellAsset` to fetch quote based on the sell amount.
847855
// `SwapParamsBase.perBuyAsset` is sent when `selectedToToken` is changed.
@@ -871,6 +879,7 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
871879
self.isUpdatingPriceQuote = true
872880
defer { self.isUpdatingPriceQuote = false }
873881
let (swapResponse, swapErrorResponse, _) = await swapService.priceQuote(swapParams)
882+
guard !Task.isCancelled else { return }
874883
if let swapResponse = swapResponse {
875884
await self.handleEthPriceQuoteResponse(swapResponse, base: base, swapParams: swapParams)
876885
} else if let swapErrorResponse = swapErrorResponse {

0 commit comments

Comments
 (0)