Skip to content

Commit

Permalink
fix: fix race condition with unsuported tokens (#4762)
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin authored Aug 2, 2024
1 parent 9390d31 commit ee70bb0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isPromiseFulfilled,
onlyResolvesLast,
} from '@cowprotocol/common-utils'
import { PriceQuality } from '@cowprotocol/cow-sdk'
import { PriceQuality, SupportedChainId } from '@cowprotocol/cow-sdk'
import { useAddUnsupportedToken, useIsUnsupportedToken, useRemoveUnsupportedToken } from '@cowprotocol/tokens'

import { useGetGpPriceStrategy } from 'legacy/hooks/useGetGpPriceStrategy'
Expand All @@ -31,7 +31,7 @@ import QuoteApiError, {
interface HandleQuoteErrorParams {
quoteData: QuoteInformationObject | LegacyFeeQuoteParams
error: unknown
addUnsupportedToken: (tokenAddress: string) => void
addUnsupportedToken: (chainId: SupportedChainId, tokenAddress: string) => void
}

type QuoteParamsForFetching = Omit<LegacyQuoteParams, 'strategy'>
Expand All @@ -45,7 +45,7 @@ function handleQuoteError({ quoteData, error, addUnsupportedToken }: HandleQuote

// Add token to unsupported token list
if (unsupportedTokenAddress) {
addUnsupportedToken(unsupportedTokenAddress)
addUnsupportedToken(quoteData.chainId, unsupportedTokenAddress)
}

return 'unsupported-token'
Expand Down Expand Up @@ -82,7 +82,7 @@ function handleQuoteError({ quoteData, error, addUnsupportedToken }: HandleQuote

// Add token to unsupported token list
if (unsupportedTokenAddress) {
addUnsupportedToken(unsupportedTokenAddress)
addUnsupportedToken(quoteData.chainId, unsupportedTokenAddress)
}

return 'unsupported-token'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { getQuoteUnsupportedToken } from '@cowprotocol/common-utils'
import { useAddUnsupportedToken } from '@cowprotocol/tokens'

import QuoteApiError from 'api/cowProtocol/errors/QuoteError'
import { SupportedChainId } from '@cowprotocol/cow-sdk'

export function useProcessUnsupportedTokenError() {
const addGpUnsupportedToken = useAddUnsupportedToken()

return useCallback(
(error: QuoteApiError, quoteParams: { sellToken: string; buyToken: string }) => {
(error: QuoteApiError, quoteParams: { chainId: SupportedChainId; sellToken: string; buyToken: string }) => {
const unsupportedTokenAddress = getQuoteUnsupportedToken(error, quoteParams)

if (unsupportedTokenAddress) {
addGpUnsupportedToken(unsupportedTokenAddress)
addGpUnsupportedToken(quoteParams.chainId, unsupportedTokenAddress)
}
},
[addGpUnsupportedToken]
Expand Down
4 changes: 1 addition & 3 deletions libs/tokens/src/state/tokens/unsupportedTokensAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { mapSupportedNetworks, SupportedChainId } from '@cowprotocol/cow-sdk'
import { UnsupportedTokensState } from '../../types'
import { environmentAtom } from '../environmentAtom'


export const unsupportedTokensAtom = atomWithStorage<Record<SupportedChainId, UnsupportedTokensState>>(
'unsupportedTokensAtom:v2',
mapSupportedNetworks({}),
Expand All @@ -20,8 +19,7 @@ export const currentUnsupportedTokensAtom = atom((get) => {
return get(unsupportedTokensAtom)[chainId]
})

export const addUnsupportedTokenAtom = atom(null, (get, set, tokenAddress: string) => {
const { chainId } = get(environmentAtom)
export const addUnsupportedTokenAtom = atom(null, (get, set, chainId: SupportedChainId, tokenAddress: string) => {
const tokenId = tokenAddress.toLowerCase()
const tokenList = get(unsupportedTokensAtom)

Expand Down

0 comments on commit ee70bb0

Please sign in to comment.