-
Notifications
You must be signed in to change notification settings - Fork 177
fix: handle widgetCustomTokens parameter #7999
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
Open
limitofzero
wants to merge
8
commits into
develop
Choose a base branch
from
fix/filter-widgetCustomTokens
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae14147
fix: handle widgetCustomTokens record from token lists
limitofzero 18a0011
refactor: accept URL for check
limitofzero 90d17e5
Merge branch 'develop' into fix/filter-widgetCustomTokens
limitofzero 63ccfef
feat: handle virtual token list
limitofzero a9ceb66
Merge branch 'fix/filter-widgetCustomTokens' of github.com:cowprotoco…
limitofzero 162210d
fix: filter all virtualLists elements
limitofzero ed327ca
fix: sort array to preserve the order
limitofzero 0499519
Merge branch 'develop' into fix/filter-widgetCustomTokens
limitofzero 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
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
31 changes: 23 additions & 8 deletions
31
libs/balances-and-allowances/src/hooks/useCustomTokensForChain.ts
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 |
|---|---|---|
| @@ -1,23 +1,38 @@ | ||
| import { useMemo } from 'react' | ||
|
|
||
| import { AddressKey, getAddressKey, SupportedChainId } from '@cowprotocol/cow-sdk' | ||
| import { useUserAddedTokens } from '@cowprotocol/tokens' | ||
| import { useUserAddedTokens, useVirtualLists } from '@cowprotocol/tokens' | ||
|
|
||
| const EMPTY_CUSTOM_TOKENS: AddressKey[] = [] | ||
|
|
||
| /** | ||
| * Normalized addresses of user-imported tokens for the given chain. The | ||
| * reference is stable as long as the source atom does not recompute. | ||
| * Normalized addresses of user-imported tokens and widget-provided custom tokens (virtual lists, | ||
| * e.g. `widgetCustomTokens`) for the given chain. Virtual lists aren't fetchable URLs (see | ||
| * `useEnabledTokensListsUrls`), so their tokens are tracked by address here instead. Sorted so the | ||
| * result is deterministic regardless of source insertion order, keeping `useStableStringList` | ||
| * (index-sensitive) from treating a reordered-but-unchanged set as a change. | ||
| */ | ||
| export function useCustomTokensForChain(chainId: SupportedChainId): AddressKey[] { | ||
| const userAddedTokens = useUserAddedTokens() | ||
| const virtualLists = useVirtualLists() | ||
|
|
||
| return useMemo(() => { | ||
| const addresses: AddressKey[] = [] | ||
| const addresses = new Set<AddressKey>() | ||
|
|
||
| for (const token of userAddedTokens) { | ||
| if (token.chainId !== chainId) continue | ||
| addresses.push(getAddressKey(token.address)) | ||
| if (token.chainId === chainId) { | ||
| addresses.add(getAddressKey(token.address)) | ||
| } | ||
| } | ||
|
|
||
| for (const list of Object.values(virtualLists)) { | ||
| for (const token of list.list.tokens) { | ||
| if (token.chainId === chainId) { | ||
| addresses.add(getAddressKey(token.address)) | ||
| } | ||
| } | ||
| } | ||
| return addresses.length === 0 ? EMPTY_CUSTOM_TOKENS : addresses | ||
| }, [userAddedTokens, chainId]) | ||
|
|
||
| return addresses.size === 0 ? EMPTY_CUSTOM_TOKENS : Array.from(addresses).sort() | ||
| }, [userAddedTokens, virtualLists, chainId]) | ||
| } | ||
86 changes: 86 additions & 0 deletions
86
libs/balances-and-allowances/src/hooks/useEnabledTokensListsUrls.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { useListsEnabledState, useVirtualLists } from '@cowprotocol/tokens' | ||
|
|
||
| import { renderHook } from '@testing-library/react' | ||
|
|
||
| import { useEnabledTokensListsUrls } from './useEnabledTokensListsUrls' | ||
|
|
||
| jest.mock('@cowprotocol/tokens', () => ({ | ||
| useListsEnabledState: jest.fn(), | ||
| useVirtualLists: jest.fn(), | ||
| })) | ||
|
|
||
| const useListsEnabledStateMock = jest.requireMock<{ useListsEnabledState: jest.Mock }>( | ||
| '@cowprotocol/tokens', | ||
| ).useListsEnabledState | ||
| const useVirtualListsMock = jest.requireMock<{ useVirtualLists: jest.Mock }>('@cowprotocol/tokens').useVirtualLists | ||
|
|
||
| function mockEnabledState(state: Record<string, boolean>): void { | ||
| useListsEnabledStateMock.mockReturnValue(state as unknown as ReturnType<typeof useListsEnabledState>) | ||
| } | ||
|
|
||
| function mockVirtualListSources(sources: string[]): void { | ||
| const state = Object.fromEntries(sources.map((source) => [source, { source }])) | ||
|
|
||
| useVirtualListsMock.mockReturnValue(state as unknown as ReturnType<typeof useVirtualLists>) | ||
| } | ||
|
|
||
| describe('useEnabledTokensListsUrls', () => { | ||
| beforeEach(() => { | ||
| useListsEnabledStateMock.mockReset() | ||
| useVirtualListsMock.mockReset() | ||
| mockVirtualListSources([]) | ||
| }) | ||
|
|
||
| it('returns an empty array when no lists are enabled', () => { | ||
| mockEnabledState({}) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual([]) | ||
| }) | ||
|
|
||
| it('excludes disabled list urls', () => { | ||
| mockEnabledState({ | ||
| 'https://example.com/list-a.json': true, | ||
| 'https://example.com/list-b.json': false, | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['https://example.com/list-a.json']) | ||
| }) | ||
|
|
||
| it('excludes virtual widget list sources (e.g. widgetCustomTokens)', () => { | ||
| mockEnabledState({ | ||
| 'https://example.com/list-a.json': true, | ||
| widgetCustomTokens: true, | ||
| }) | ||
| mockVirtualListSources(['widgetCustomTokens']) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['https://example.com/list-a.json']) | ||
| }) | ||
|
|
||
| it('keeps non-http(s) sources that are not virtual lists (e.g. ipfs/ipns/ENS)', () => { | ||
| mockEnabledState({ | ||
| 'ipfs://QmSomeHash': true, | ||
| 'tokens.uniswap.eth': true, | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['ipfs://QmSomeHash', 'tokens.uniswap.eth']) | ||
| }) | ||
|
limitofzero marked this conversation as resolved.
|
||
|
|
||
| it('returns enabled list urls sorted alphabetically', () => { | ||
| mockEnabledState({ | ||
| 'https://example.com/z-list.json': true, | ||
| 'https://example.com/a-list.json': true, | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['https://example.com/a-list.json', 'https://example.com/z-list.json']) | ||
| }) | ||
| }) | ||
10 changes: 7 additions & 3 deletions
10
libs/balances-and-allowances/src/hooks/useEnabledTokensListsUrls.ts
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 |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| import { useMemo } from 'react' | ||
|
|
||
| import { useListsEnabledState } from '@cowprotocol/tokens' | ||
| import { useListsEnabledState, useVirtualLists } from '@cowprotocol/tokens' | ||
|
|
||
| // Virtual list sources (e.g. widget-provided `widgetCustomTokens`) are internal identifiers, not | ||
| // fetchable URLs. The BalancesWatcher session API only accepts real list URLs, so those must be | ||
| // filtered out. Their tokens are still tracked — see `useCustomTokensForChain`. | ||
| export function useEnabledTokensListsUrls(): string[] { | ||
| const enabledState = useListsEnabledState() | ||
| const virtualLists = useVirtualLists() | ||
|
|
||
| return useMemo( | ||
| () => | ||
| Object.entries(enabledState) | ||
| .filter(([, enabled]) => enabled === true) | ||
| .filter(([source, enabled]) => enabled === true && !virtualLists[source]) | ||
| .map(([source]) => source) | ||
| .sort(), | ||
| [enabledState], | ||
| [enabledState, virtualLists], | ||
| ) | ||
| } |
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 |
|---|---|---|
|
|
@@ -16,6 +16,12 @@ | |
| "import": "./src/json-utils.ts", | ||
| "require": "./src/json-utils.ts", | ||
| "default": "./src/json-utils.ts" | ||
| }, | ||
| "./safeLink": { | ||
|
Collaborator
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. Great solution! |
||
| "types": "./src/safeLink.ts", | ||
| "import": "./src/safeLink.ts", | ||
| "require": "./src/safeLink.ts", | ||
| "default": "./src/safeLink.ts" | ||
| } | ||
| }, | ||
| "publishConfig": { | ||
|
|
@@ -31,6 +37,11 @@ | |
| "types": "./json-utils.d.ts", | ||
| "import": "./json-utils.mjs", | ||
| "require": "./json-utils.js" | ||
| }, | ||
| "./safeLink": { | ||
| "types": "./safeLink.d.ts", | ||
| "import": "./safeLink.mjs", | ||
| "require": "./safeLink.js" | ||
| } | ||
| } | ||
| }, | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.