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

feat(tokens): tokens and token lists library #3189

Merged
merged 14 commits into from
Oct 27, 2023
Merged

Conversation

shoom3301
Copy link
Collaborator

@shoom3301 shoom3301 commented Oct 10, 2023

Summary

The new tokens lib serves to provide a relevant list of tokens.
Globally, there are two groups of entities: lists and tokens.

  1. The source of everything is libs/tokens/src/const/tokensList.json, it contains urls/ensNames of lists with priority and enabledByDefault flags. The content of the json file is stored into defaultListsSourcesAtom.
    There is also userAddedListsSourcesAtom with the same data added by user.
    It looks like:
image
  1. TokensListsUpdater fetches lists sources once in 6 hours and stores results into listsStatesByChainAtom.
    Example of the state inside:
image
  1. To detect if a list is enabled we check isEnabled value in the list state, if it's not set, then we fallback to enabledByDefault in the list source. See more in listsEnabledStateAtom

  2. Now we split tokens into two groups: active and inactive, depending on is a list enabled (see the previous point). In the "Select a token" modal we display only tokens from active lists by default. We use tokens from inactive lists only to search for a token. Splitting into active/inactive is concentrated in tokensStateAtom.

  3. We also have user-added tokens and favorite tokens. Those entities are represented as CRUD atoms: userAddedTokensAtom and favouriteTokensAtom. We add then into general active tokens list as well (activeTokensAtom).

  4. The lib also provides a component to display a token logo - TokenLogo. The main goal of the component is to find a valid URL for a logo. A token from some list might have an invalid URL or even don't have it, in this case we try other sources of images. See details below.

  5. One of the most complex things in the lib is useSearchToken. The hook is searching into 4 sources: active lists, inactive lists, external API, and blockchain. useSWR is widely used inside to cache search results.

  6. Any entity collection contains sets of data per network. I've chosen jotai style to derive data for a current network. It has one huge advantage - caching. If we use react hooks to derive data for a current network, it means we will calculate data on every subscription. On the other side, jotai calculates it once. To not spoil the lib dependencies I've added environmentAtom that actually contains only one property - chainId. The value is persisted from TokensListsUpdater.

Details

1. TokensListsUpdater

image

2. TokenLogo

image

3. Lists and tokens state

image image

4. Token search

image

To Test

Please, test everything in #3201

@vercel
Copy link

vercel bot commented Oct 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
swap-dev ✅ Ready (Inspect) Visit Preview Oct 27, 2023 11:53am

🌃 Cosmos ↗︎

@shoom3301 shoom3301 changed the title feat(tokens): tokens and token lists library [1] feat(tokens): tokens and token lists library Oct 10, 2023
@shoom3301 shoom3301 self-assigned this Oct 10, 2023
Copy link
Collaborator

@alfetopito alfetopito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments.
Obviously did not test ;)

libs/tokens/src/const/tokensList.json Outdated Show resolved Hide resolved
libs/tokens/src/hooks/tokens/useSearchToken.ts Outdated Show resolved Hide resolved
libs/tokens/src/index.ts Outdated Show resolved Hide resolved
libs/tokens/src/updaters/TokensListsUpdater/index.ts Outdated Show resolved Hide resolved
libs/tokens/src/utils/getTokenLogoUrls.ts Outdated Show resolved Hide resolved
libs/tokens/src/utils/tokenWithLogoFromToken.ts Outdated Show resolved Hide resolved
libs/tokens/src/utils/validateTokenList.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is remarkable! This is really a great job.
I need to still review it a bit more, but here are some small initial comments.

The first impression of the lib API and state looks great to me!

libs/tokens/src/const/tokensLists.ts Show resolved Hide resolved
libs/tokens/src/const/tokensList.json Outdated Show resolved Hide resolved
libs/tokens/src/state/tokens/allTokensAtom.ts Show resolved Hide resolved
libs/tokens/src/state/tokens/allTokensAtom.ts Outdated Show resolved Hide resolved
libs/tokens/src/utils/tokenMapToList.ts Outdated Show resolved Hide resolved
libs/tokens/src/hooks/tokens/useSearchToken.ts Outdated Show resolved Hide resolved
libs/tokens/src/hooks/tokens/useSearchToken.ts Outdated Show resolved Hide resolved

// Types
export * from './types'
export type { TokensByAddress, TokensBySymbol } from './state/tokens/allTokensAtom'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would say, these types belong to th types.ts if they are part of the interface.
Then, it just happens that you actually also use them in the atom because in this case you made the persistance to match the interface

Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SOFT APPROVE!
I really like the changes. The interface is super good!

One question. If i disable all lists, shouldn't i have zero tokens? I've disabled it, but I still have a lot of tokens:

image

@socket-security
Copy link

socket-security bot commented Oct 23, 2023

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

* feat(tokens): tokens list, search and management ui (#3191)

* refactor(tokens): remove excessive types and consts (#3196)

* refactor(tokens): integrate TokenLogo component (#3197)

* refactor(tokens): wire up components to new hooks (#3198)

* refactor(tokens): remove Uniswap currency entities usage (#3199)

* refactor(tokens): use new tokens UI and logic by default (#3200)

* fix(tokens): fix e2e tests for tokens updates (#3193)

* refactor(tokens): remove legacy code (#3194)

* fix(tokens): fix tokens list loading state (#3201)
@shoom3301 shoom3301 changed the title [1] feat(tokens): tokens and token lists library feat(tokens): tokens and token lists library Oct 26, 2023
… refactor/tokens-lib

# Conflicts:
#	apps/cowswap-frontend/src/common/pure/CurrencyLogo/index.tsx
#	apps/cowswap-frontend/src/common/updaters/ListsUpdater.ts
#	apps/cowswap-frontend/src/legacy/components/SearchModal/CommonBases/CommonBasesMod.tsx
#	apps/cowswap-frontend/src/legacy/components/SearchModal/CommonBases/index.ts
#	apps/cowswap-frontend/src/modules/tokensList/pure/TokenLogo/index.tsx
#	apps/cowswap-frontend/src/modules/tokensList/pure/TokenSourceTitle/index.tsx
#	apps/widget-configurator/src/app/configurator/index.tsx
#	libs/tokens/src/utils/validateTokenList.ts
#	package.json
#	yarn.lock
@socket-security
Copy link

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@tanstack/react-virtual 3.0.0-beta.68 None +1 611 kB tannerlinsley

🚮 Removed packages: @types/[email protected], @uniswap/[email protected], [email protected], [email protected], [email protected]

… refactor/tokens-lib

# Conflicts:
#	apps/cowswap-frontend/src/modules/swap/containers/SwapWidget/index.tsx
@shoom3301 shoom3301 merged commit 4880bdc into develop Oct 27, 2023
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 27, 2023
@alfetopito alfetopito deleted the refactor/tokens-lib branch October 31, 2023 10:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants