Skip to content

Commit 203f85c

Browse files
chore: removing FE swap quote refresh
1 parent 280305f commit 203f85c

File tree

2 files changed

+20
-108
lines changed

2 files changed

+20
-108
lines changed

ui/pages/Swap.tsx

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import { ReadOnlyAccountSigner } from "@tallyho/tally-background/services/signin
2929
import {
3030
NETWORKS_SUPPORTING_SWAPS,
3131
OPTIMISM,
32-
SECOND,
3332
} from "@tallyho/tally-background/constants"
34-
3533
import {
3634
selectLatestQuoteRequest,
3735
selectSwapBuyAssets,
@@ -52,12 +50,10 @@ import {
5250
getSellAssetAmounts,
5351
getOwnedSellAssetAmounts,
5452
} from "../utils/swap"
55-
import { useOnMount, usePrevious, useInterval } from "../hooks/react-hooks"
53+
import { useOnMount, usePrevious } from "../hooks/react-hooks"
5654
import SharedLoadingDoggo from "../components/Shared/SharedLoadingDoggo"
5755
import SharedBackButton from "../components/Shared/SharedBackButton"
5856

59-
const REFRESH_QUOTE_INTERVAL = 10 * SECOND
60-
6157
export default function Swap(): ReactElement {
6258
const { t } = useTranslation()
6359
const dispatch = useBackgroundDispatch()
@@ -103,7 +99,6 @@ export default function Swap(): ReactElement {
10399
assets: { sellAsset: savedSellAsset, buyAsset: savedBuyAsset },
104100
amount: savedSwapAmount,
105101
} = (!locationAsset && savedQuoteRequest) || {
106-
// ^ If coming from an asset item swap button, let the UI start fresh
107102
assets: { sellAsset: locationAsset },
108103
}
109104

@@ -214,8 +209,6 @@ export default function Swap(): ReactElement {
214209
setSellAsset(newSellAsset)
215210
setSellAmount("")
216211

217-
// Updating the source asset quotes the new source asset against the existing
218-
// target amount.
219212
if (newSellAsset && buyAsset && buyAmount) {
220213
requestQuoteUpdate({
221214
type: "getSellAmount",
@@ -234,8 +227,6 @@ export default function Swap(): ReactElement {
234227
setBuyAsset(newBuyAsset)
235228
setBuyAmount("")
236229

237-
// Updating the target asset quotes the new target asset against the existing
238-
// source amount.
239230
if (sellAsset && newBuyAsset && sellAmount) {
240231
requestQuoteUpdate({
241232
type: "getBuyAmount",
@@ -280,8 +271,6 @@ export default function Swap(): ReactElement {
280271
isSameAsset(quote.sellAsset, sellAsset) &&
281272
isSameAsset(quote.buyAsset, buyAsset)
282273

283-
// Update if quote changes
284-
285274
const prevQuoteTimestamp = usePrevious(quote?.timestamp)
286275

287276
if (
@@ -300,40 +289,7 @@ export default function Swap(): ReactElement {
300289
}
301290
}
302291

303-
const [amountInputHasFocus, setAmountInputHasFocus] = useState(false)
304-
305-
useInterval(() => {
306-
if (!isEnabled(FeatureFlags.SUPPORT_SWAP_QUOTE_REFRESH)) return
307-
308-
const isRecentQuote =
309-
quote &&
310-
// Time passed since last quote
311-
Date.now() - quote.timestamp <= 3 * SECOND
312-
313-
const skipRefresh =
314-
loadingQuote || (isRecentQuote && quoteAppliesToCurrentAssets)
315-
316-
if (
317-
!skipRefresh &&
318-
!amountInputHasFocus &&
319-
sellAsset &&
320-
buyAsset &&
321-
(sellAmount || buyAmount)
322-
) {
323-
const type = sellAmount ? "getBuyAmount" : "getSellAmount"
324-
const amount = sellAmount || buyAmount
325-
326-
requestQuoteUpdate({
327-
type,
328-
amount,
329-
sellAsset,
330-
buyAsset,
331-
})
332-
}
333-
}, REFRESH_QUOTE_INTERVAL)
334-
335292
useOnMount(() => {
336-
// Request a quote on mount
337293
if (sellAsset && buyAsset && sellAmount) {
338294
requestQuoteUpdate({
339295
type: "getBuyAmount",
@@ -357,8 +313,6 @@ export default function Swap(): ReactElement {
357313
sellAsset,
358314
buyAsset,
359315
gasPrice:
360-
// Let's use the gas price from 0x API for Optimism
361-
// to avoid problems with gas price on Optimism Bedrock.
362316
currentNetwork.chainID === OPTIMISM.chainID
363317
? gasPrice
364318
: quote.swapTransactionSettings.networkSettings.values.maxFeePerGas.toString() ??
@@ -393,7 +347,6 @@ export default function Swap(): ReactElement {
393347
<ReadOnlyNotice isLite />
394348
{isEnabled(FeatureFlags.SHOW_TOKEN_FEATURES) &&
395349
isEnabled(FeatureFlags.SHOW_SWAP_REWARDS) && (
396-
// TODO: Add onClick function after design is ready
397350
<SharedIcon
398351
399352
width={20}
@@ -429,13 +382,10 @@ export default function Swap(): ReactElement {
429382
selectedAsset={sellAsset}
430383
isDisabled={loadingSellAmount}
431384
onAssetSelect={updateSellAsset}
432-
onFocus={() => setAmountInputHasFocus(true)}
433-
onBlur={() => setAmountInputHasFocus(false)}
434385
onErrorMessageChange={(error) => setHasError(!!error)}
435386
mainCurrencySign={mainCurrencySign}
436387
onAmountChange={(newAmount, error) => {
437388
setSellAmount(newAmount)
438-
439389
if (!error) {
440390
requestQuoteUpdate({
441391
type: "getBuyAmount",
@@ -466,12 +416,9 @@ export default function Swap(): ReactElement {
466416
priceImpact={quote?.priceDetails?.priceImpact}
467417
isPriceDetailsLoading={isLoadingPriceDetails}
468418
showPriceDetails
469-
// FIXME: Merge master asset list with account balances.
470419
assetsAndAmounts={buyAssets.map((asset) => ({ asset }))}
471420
selectedAsset={buyAsset}
472421
isDisabled={loadingBuyAmount}
473-
onFocus={() => setAmountInputHasFocus(true)}
474-
onBlur={() => setAmountInputHasFocus(false)}
475422
showMaxButton={false}
476423
mainCurrencySign={mainCurrencySign}
477424
onAssetSelect={updateBuyAsset}
@@ -568,12 +515,10 @@ export default function Swap(): ReactElement {
568515
font-weight: 500;
569516
line-height: 32px;
570517
}
571-
572518
.loading_wrapper {
573519
min-height: 73.5px;
574520
margin: 7px 0 10px;
575521
}
576-
577522
.footer {
578523
display: flex;
579524
justify-content: center;

ui/utils/swap.ts

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ import {
1515
SwapQuoteRequest,
1616
} from "@tallyho/tally-background/redux-slices/utils/0x-swap-utils"
1717
import { debounce, DebouncedFunc } from "lodash"
18-
import { useState, useRef, useCallback, useEffect } from "react"
18+
import { useState, useRef, useCallback } from "react"
1919
import { CompleteAssetAmount } from "@tallyho/tally-background/redux-slices/accounts"
2020
import {
2121
isSameAsset,
2222
isTrustedAsset,
2323
} from "@tallyho/tally-background/redux-slices/utils/asset-utils"
24-
import { isEnabled, FeatureFlags } from "@tallyho/tally-background/features"
2524
import { useBackgroundDispatch, useBackgroundSelector } from "../hooks"
2625
import { useValueRef, useIsMounted, useSetState } from "../hooks/react-hooks"
2726

@@ -57,8 +56,10 @@ export async function withRetry<T>(
5756
return await fn()
5857
} catch (error) {
5958
if (attempt < options.maxRetries) {
60-
const backoffTime = options.retryInterval * Math.pow(2, attempt - 1)
61-
await new Promise((resolve) => setTimeout(resolve, backoffTime))
59+
const backoffTime = options.retryInterval * 2 ** (attempt - 1)
60+
await new Promise<void>((resolve) => {
61+
setTimeout(resolve, backoffTime)
62+
})
6263
return attemptFn(attempt + 1)
6364
}
6465
throw error
@@ -183,11 +184,6 @@ export function useSwapQuote(useSwapConfig: {
183184
useSwapConfig.initialSwapSettings,
184185
)
185186

186-
const retryOptions = useSwapConfig.retryOptions || {
187-
maxRetries: 3,
188-
retryInterval: 1000,
189-
}
190-
191187
const [quoteRequestState, setQuoteRequestState] = useSetState<{
192188
quote: QuoteUpdate | null
193189
loading: boolean
@@ -209,38 +205,15 @@ export function useSwapQuote(useSwapConfig: {
209205

210206
const requestId = useRef(0)
211207

212-
useEffect(() => {
213-
if (!isEnabled(FeatureFlags.SUPPORT_SWAP_QUOTE_REFRESH)) return
214-
215-
let refreshInterval: NodeJS.Timeout | undefined
216-
217-
if (quoteRequestState.quote && !quoteRequestState.loading) {
218-
refreshInterval = setInterval(() => {
219-
const currentQuote = quoteRequestState.quote
220-
221-
if (currentQuote) {
222-
requestQuoteUpdate({
223-
type: currentQuote.type,
224-
amount: currentQuote.amount,
225-
sellAsset: currentQuote.sellAsset,
226-
buyAsset: currentQuote.buyAsset,
227-
transactionSettings: currentQuote.swapTransactionSettings,
228-
})
229-
}
230-
}, 30000)
231-
}
232-
233-
return () => {
234-
if (refreshInterval) {
235-
clearInterval(refreshInterval)
236-
}
237-
}
238-
}, [quoteRequestState.quote, quoteRequestState.loading])
239-
240208
const requestQuoteUpdate = useCallback(
241209
async (config: RequestQuoteUpdateConfig) => {
242210
if (!mountedRef.current) return
243211

212+
const retryOptions = useSwapConfig.retryOptions || {
213+
maxRetries: 3,
214+
retryInterval: 1000,
215+
}
216+
244217
const { type, amount, sellAsset, buyAsset } = config
245218

246219
const requestContext = requestContextRef.current
@@ -306,26 +279,20 @@ export function useSwapQuote(useSwapConfig: {
306279
}
307280
},
308281
[
309-
dispatch,
310-
requestContextRef,
311282
mountedRef,
283+
useSwapConfig.retryOptions,
284+
requestContextRef,
312285
setQuoteRequestState,
313-
retryOptions,
286+
dispatch,
314287
],
315288
)
316289

317-
const [debouncedRequest] = useState(() => {
318-
const debouncedFn = debounce(
319-
requestQuoteUpdate,
320-
UPDATE_SWAP_QUOTE_DEBOUNCE_TIME,
321-
{
322-
leading: false,
323-
trailing: true,
324-
},
325-
)
326-
327-
return debouncedFn
328-
})
290+
const [debouncedRequest] = useState(() =>
291+
debounce(requestQuoteUpdate, UPDATE_SWAP_QUOTE_DEBOUNCE_TIME, {
292+
leading: false,
293+
trailing: true,
294+
}),
295+
)
329296

330297
const loadingSellAmount = quoteRequestState.loadingType === "getSellAmount"
331298
const loadingBuyAmount = quoteRequestState.loadingType === "getBuyAmount"

0 commit comments

Comments
 (0)