Skip to content
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

Balance not updating after swapping solana #275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-cameras-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-kit-ui': patch
---

Fix solana balance cached after swapping
47 changes: 42 additions & 5 deletions packages/ui/src/components/widgets/SwapWidgetRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
value: fromBalance,
queryKey: fromBalanceQueryKey,
isLoading: isLoadingFromBalance,
isError: fromBalanceErrorFetching
isError: fromBalanceErrorFetching,
isDuneBalance: fromBalanceIsDune
} = useCurrencyBalance({
chain: fromChain,
address: address,
Expand All @@ -252,7 +253,8 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
const {
value: toBalance,
queryKey: toBalanceQueryKey,
isLoading: isLoadingToBalance
isLoading: isLoadingToBalance,
isDuneBalance: toBalanceIsDune
} = useCurrencyBalance({
chain: toChain,
address: recipient,
Expand All @@ -261,10 +263,45 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
})

const invalidateBalanceQueries = useCallback(() => {
queryClient.invalidateQueries({ queryKey: fromBalanceQueryKey })
queryClient.invalidateQueries({ queryKey: toBalanceQueryKey })
const invalidatePeriodically = (invalidateFn: () => void) => {
let maxRefreshes = 5
let refreshCount = 0
const timer = setInterval(() => {
refreshCount++
if (maxRefreshes === refreshCount) {
clearInterval(timer)
return
}
invalidateFn()
}, 1000)
}

queryClient.invalidateQueries({ queryKey: ['useDuneBalances'] })
}, [queryClient, fromBalanceQueryKey, toBalanceQueryKey, address])

// Dune balances are sometimes stale, because of this we need to aggressively fetch them
// for a predetermined period to make sure we get back a fresh response
if (fromBalanceIsDune) {
invalidatePeriodically(() => {
queryClient.invalidateQueries({ queryKey: fromBalanceQueryKey })
})
} else {
queryClient.invalidateQueries({ queryKey: fromBalanceQueryKey })
}
if (toBalanceIsDune) {
invalidatePeriodically(() => {
queryClient.invalidateQueries({ queryKey: toBalanceQueryKey })
})
} else {
queryClient.invalidateQueries({ queryKey: toBalanceQueryKey })
}
}, [
queryClient,
fromBalanceQueryKey,
toBalanceQueryKey,
toBalanceIsDune,
fromBalanceIsDune,
address
])
const { data: capabilities } = useCapabilities({
query: {
enabled:
Expand Down
12 changes: 8 additions & 4 deletions packages/ui/src/hooks/useCurrencyBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type UseCurrencyBalanceData = {
isLoading: boolean
isError: boolean | GetBalanceErrorType | null
error: boolean | ReadContractErrorType | Error | null
isDuneBalance: boolean
}

// Handle fetching the balance of both native eth and erc20s
Expand Down Expand Up @@ -99,7 +100,7 @@ const useCurrencyBalance = ({
const isLoading = isErc20Currency
? erc20BalanceIsLoading
: ethBalanceIsLoading
return { value, queryKey, isLoading, isError, error }
return { value, queryKey, isLoading, isError, error, isDuneBalance: false }
} else if (chain?.vmType === 'svm') {
if (isValidSvmAddress) {
return {
Expand All @@ -114,15 +115,17 @@ const useCurrencyBalance = ({
queryKey: duneBalances.queryKey,
isLoading: duneBalances.isLoading,
isError: duneBalances.isError,
error: duneBalances.error
error: duneBalances.error,
isDuneBalance: true
}
} else {
return {
value: undefined,
queryKey: duneBalances.queryKey,
isLoading: duneBalances.isLoading,
isError: duneBalances.isError,
error: duneBalances.error
error: duneBalances.error,
isDuneBalance: true
}
}
} else {
Expand All @@ -131,7 +134,8 @@ const useCurrencyBalance = ({
queryKey: duneBalances.queryKey,
isLoading: duneBalances.isLoading,
isError: duneBalances.isError,
error: duneBalances.error
error: duneBalances.error,
isDuneBalance: false
}
}
}
Expand Down