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): migrate user added data from legacy state #3267

Merged
merged 125 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
5fe0f55
refactor(tokens): clean up token consts and use TokenWithLogo
shoom3301 Oct 10, 2023
cb6a735
feat(tokens): tokens list, search and management ui
shoom3301 Oct 10, 2023
46df189
feat(tokens): wire up new tokens lib into app
shoom3301 Oct 10, 2023
9f5cf03
fix(tokens): fix e2e tests for tokens updates
shoom3301 Oct 10, 2023
649cfb3
refactor(tokens): remove legacy code
shoom3301 Oct 10, 2023
f6b11ce
chore: update npm deps
shoom3301 Oct 10, 2023
51f9d35
Merge branch 'refactor/token-search-ui' of https://github.com/cowprot…
shoom3301 Oct 10, 2023
c7992aa
Merge branch 'refactor/tokens-lib-wiring' of https://github.com/cowpr…
shoom3301 Oct 10, 2023
168fe41
Merge branch 'refactor/tokens-e2e-fix' of https://github.com/cowproto…
shoom3301 Oct 10, 2023
3ebba78
chore: revert changes
shoom3301 Oct 10, 2023
93bd06d
chore: revert changes
shoom3301 Oct 10, 2023
df4a619
chore: revert changes
shoom3301 Oct 10, 2023
06e0546
chore: revert changes
shoom3301 Oct 10, 2023
7c9bd67
chore: revert changes
shoom3301 Oct 10, 2023
bfae888
Merge branch 'refactor/tokens-lib-wiring-2' of https://github.com/cow…
shoom3301 Oct 10, 2023
6a838f7
chore: revert changes
shoom3301 Oct 10, 2023
7d9f403
chore: revert changes
shoom3301 Oct 10, 2023
ddd3518
chore: revert changes
shoom3301 Oct 10, 2023
9b0c394
Merge branch 'refactor/tokens-lib-wiring-5' of https://github.com/cow…
shoom3301 Oct 10, 2023
63b42de
Merge branch 'refactor/tokens-e2e-fix' of https://github.com/cowproto…
shoom3301 Oct 10, 2023
bdf5b73
Merge branch 'refactor/tokens-remove-legacy' of https://github.com/co…
shoom3301 Oct 10, 2023
840af3f
fix: fix tokens list loading state
shoom3301 Oct 10, 2023
d1f8851
fix: fix token store updating inconsistency
shoom3301 Oct 10, 2023
70b87f9
chore: move DEFAULT_FAVOURITE_TOKENS to separate file
shoom3301 Oct 10, 2023
58b890f
refactor: refactor useSearchToken() hook
shoom3301 Oct 10, 2023
614feea
chore: fix tokens lib exports
shoom3301 Oct 10, 2023
b5c1508
chore: refactor TokensListsUpdater
shoom3301 Oct 10, 2023
c3cc4a8
fix: remove duplicates from token search results
shoom3301 Oct 10, 2023
e1626e7
fix(tokens): fix lists toggling
shoom3301 Oct 10, 2023
16ef0a0
fix(tokens): fix list importing
shoom3301 Oct 10, 2023
2a8d3d7
refactor(tokens): refactor tokens state
shoom3301 Oct 11, 2023
7083d15
Merge branch 'refactor/tokens-lib' of https://github.com/cowprotocol/…
shoom3301 Oct 11, 2023
c5a84b8
Merge branch 'refactor/tokens-consts' of https://github.com/cowprotoc…
shoom3301 Oct 11, 2023
ea11de1
refactor: refactor tokens UI
shoom3301 Oct 11, 2023
a2772af
Merge branch 'refactor/token-search-ui' of https://github.com/cowprot…
shoom3301 Oct 11, 2023
a6f810f
Merge branch 'refactor/tokens-lib-wiring-1' of https://github.com/cow…
shoom3301 Oct 11, 2023
863e141
Merge branch 'refactor/tokens-lib-wiring-2' of https://github.com/cow…
shoom3301 Oct 11, 2023
e82060c
Merge branch 'refactor/tokens-lib-wiring-3' of https://github.com/cow…
shoom3301 Oct 11, 2023
38feb47
Merge branch 'refactor/tokens-lib-wiring-4' of https://github.com/cow…
shoom3301 Oct 11, 2023
998ab1f
Merge branch 'refactor/tokens-lib-wiring-5' of https://github.com/cow…
shoom3301 Oct 11, 2023
9402853
Merge branch 'refactor/tokens-e2e-fix' of https://github.com/cowproto…
shoom3301 Oct 11, 2023
d8e4330
Merge branch 'refactor/tokens-remove-legacy' of https://github.com/co…
shoom3301 Oct 11, 2023
7287a63
feat(tokens): lists analytics events
shoom3301 Oct 11, 2023
e7573de
refactor: group and rename entities
shoom3301 Oct 11, 2023
e6be7d3
chore: fix imports
shoom3301 Oct 11, 2023
c684d98
chore: fix cosmos fixtures
shoom3301 Oct 11, 2023
822d170
chore: remove unused code
shoom3301 Oct 11, 2023
0c184d9
Merge branch 'refactor/tokens-lib' of https://github.com/cowprotocol/…
shoom3301 Oct 11, 2023
2f23d0a
Merge branch 'refactor/tokens-consts' of https://github.com/cowprotoc…
shoom3301 Oct 11, 2023
0f6929d
refactor: refactor tokens UI structure
shoom3301 Oct 11, 2023
4b267a9
Merge branch 'refactor/token-search-ui' of https://github.com/cowprot…
shoom3301 Oct 11, 2023
3e3479f
Merge branch 'refactor/tokens-lib-wiring-1' of https://github.com/cow…
shoom3301 Oct 11, 2023
72c04de
Merge branch 'refactor/tokens-lib-wiring-2' of https://github.com/cow…
shoom3301 Oct 11, 2023
a79006e
Merge branch 'refactor/tokens-lib-wiring-3' of https://github.com/cow…
shoom3301 Oct 11, 2023
cfe1c48
Merge branch 'refactor/tokens-lib-wiring-4' of https://github.com/cow…
shoom3301 Oct 11, 2023
7aae2d9
Merge branch 'refactor/tokens-lib-wiring-5' of https://github.com/cow…
shoom3301 Oct 11, 2023
5399b89
Merge branch 'refactor/tokens-e2e-fix' of https://github.com/cowproto…
shoom3301 Oct 11, 2023
9701d07
Merge branch 'refactor/tokens-remove-legacy' of https://github.com/co…
shoom3301 Oct 11, 2023
2592593
chore: fix import
shoom3301 Oct 11, 2023
db8105f
chore: fix typo
shoom3301 Oct 11, 2023
4a28d8c
chore: trim input value
shoom3301 Oct 11, 2023
caeeeb7
chore: fix list removing
shoom3301 Oct 11, 2023
c1ac055
chore: diagrams
shoom3301 Oct 11, 2023
6837b14
refactor: simplify unsupported token hooks
shoom3301 Oct 12, 2023
cc53a88
refactor: simplify useSearchToken
shoom3301 Oct 12, 2023
3127c0e
refactor: rename isTokenAlreadyFoundByAddress
shoom3301 Oct 12, 2023
7370a0b
refactor: use lastUpdateTimeAtom instead of localStorage
shoom3301 Oct 12, 2023
4ab7dbd
chore: remove TOKENS_LISTS_UPDATER_INTERVAL duplicate
shoom3301 Oct 12, 2023
7400345
refactor: use TokenWithLogo.fromToken
shoom3301 Oct 12, 2023
9fc64a1
fix: fix FavouriteTokensList mobile view
shoom3301 Oct 16, 2023
437ca57
fix: fix mobile appearance
shoom3301 Oct 16, 2023
e02d368
fix: select token after import
shoom3301 Oct 16, 2023
45feae5
fix: search token by symbol for autoimporting
shoom3301 Oct 16, 2023
5552b69
feat: add trust as a fallback to logo urls
shoom3301 Oct 16, 2023
734c5f4
refactor: simplify TokenSearchResponse
shoom3301 Oct 16, 2023
20af0e7
chore: docs for complex entities
shoom3301 Oct 16, 2023
c11ac54
chore: cache inputLowerCase
shoom3301 Oct 16, 2023
e1bae58
chore: rename tokenMapToListWithLogo
shoom3301 Oct 16, 2023
cbeaa9b
chore: remove unused files
shoom3301 Oct 17, 2023
010dcb2
feat: reset unsupported tokens periodically
shoom3301 Oct 17, 2023
f07e036
chore: remove excessive files
shoom3301 Oct 17, 2023
34e06de
fix: use token with logo in orders state
shoom3301 Oct 17, 2023
9aec764
chore: remove excessive files
shoom3301 Oct 17, 2023
5537c79
Merge branch 'refactor/tokens-e2e-fix' of https://github.com/cowproto…
shoom3301 Oct 17, 2023
00a9f85
Merge branch 'refactor/tokens-remove-legacy' of https://github.com/co…
shoom3301 Oct 17, 2023
bf7490c
Merge branch 'refactor/tokens-remove-legacy' of https://github.com/co…
shoom3301 Oct 17, 2023
b12a429
refactor: simplify token lists state
shoom3301 Oct 17, 2023
2243394
fix: remove local logos for tokens
shoom3301 Oct 17, 2023
3a8ffc4
chore: fix test
shoom3301 Oct 17, 2023
19e7d6d
chore: change refreshInterval for native balance
shoom3301 Oct 17, 2023
b3cc7ca
refactor: fix press enter handler
shoom3301 Oct 17, 2023
c4cc71e
chore: fix build
shoom3301 Oct 17, 2023
55d2528
chore: fix test
shoom3301 Oct 17, 2023
b2dd6b3
chore: fix text
shoom3301 Oct 17, 2023
099c3bc
chore: fix OrderLogPopupMixData
shoom3301 Oct 17, 2023
cc8982e
Merge branch 'refactor/tokens-lib' of https://github.com/cowprotocol/…
shoom3301 Oct 23, 2023
35a98b3
chore: rename cowprotocolTokenLogoUrl
shoom3301 Oct 23, 2023
c0e144f
Merge branch 'refactor/tokens-consts' of https://github.com/cowprotoc…
shoom3301 Oct 23, 2023
2ebbc3b
Merge branch 'refactor/token-search-ui' of https://github.com/cowprot…
shoom3301 Oct 23, 2023
17a7ca2
chore: update tokens list ui
shoom3301 Oct 23, 2023
7d39ad5
Merge branch 'refactor/token-search-ui' of https://github.com/cowprot…
shoom3301 Oct 23, 2023
f424238
Merge branch 'refactor/tokens-lib-wiring-1' of https://github.com/cow…
shoom3301 Oct 23, 2023
3224445
chore: fix merge
shoom3301 Oct 23, 2023
0badc64
Merge branch 'refactor/tokens-lib-wiring-2' of https://github.com/cow…
shoom3301 Oct 23, 2023
8f60b4e
Merge branch 'refactor/tokens-lib-wiring-3' of https://github.com/cow…
shoom3301 Oct 23, 2023
1e701c6
Merge branch 'refactor/tokens-lib-wiring-4' of https://github.com/cow…
shoom3301 Oct 23, 2023
66a4811
Merge branch 'refactor/tokens-lib-wiring-5' of https://github.com/cow…
shoom3301 Oct 23, 2023
e3fdbf8
Merge remote-tracking branch 'origin/refactor/tokens-e2e-fix' into re…
shoom3301 Oct 23, 2023
3af422f
Merge branch 'refactor/tokens-e2e-fix' of https://github.com/cowproto…
shoom3301 Oct 23, 2023
4c73cca
chore: remove unused files
shoom3301 Oct 23, 2023
2903c28
chore: merge develop
shoom3301 Oct 23, 2023
40c0d9d
chore: fix build
shoom3301 Oct 23, 2023
78dc458
chore: fix build
shoom3301 Oct 23, 2023
b3192bd
chore: fix getTokenLogoFallbacks
shoom3301 Oct 23, 2023
3d28e21
chore: fix lists overriding and search input
shoom3301 Oct 23, 2023
6a7a227
feat: loader for list search
shoom3301 Oct 23, 2023
8602757
chore: fix token logo rounding
shoom3301 Oct 24, 2023
31d3eb3
feat(tokens): migrate user added data from legacy state
shoom3301 Oct 24, 2023
d402e48
chore: fix migration for empty storage
shoom3301 Oct 24, 2023
f4af72b
chore: fix tokens sorting
shoom3301 Oct 24, 2023
0a15bcd
Merge branch 'refactor/tokens-post-review-fixes' of https://github.co…
shoom3301 Oct 24, 2023
31b33ae
fix: use strict search for auto-imported tokens
shoom3301 Oct 26, 2023
a37ebc2
Merge branch 'refactor/tokens-lib' of https://github.com/cowprotocol/…
shoom3301 Oct 26, 2023
cb361a6
Merge branch 'refactor/tokens-lib' of https://github.com/cowprotocol/…
shoom3301 Oct 26, 2023
71079b7
chore: simplify code
shoom3301 Oct 27, 2023
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
1 change: 1 addition & 0 deletions libs/tokens/src/const/defaultFavouriteTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const tokensListToMap = (list: TokenWithLogo[]) =>
}
return acc
}, {})

export const DEFAULT_FAVOURITE_TOKENS: Record<SupportedChainId, TokensMap> = {
[SupportedChainId.MAINNET]: tokensListToMap([
DAI,
Expand Down
4 changes: 2 additions & 2 deletions libs/tokens/src/hooks/tokens/useSearchNonExistentToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useAtomValue } from 'jotai'
import { useMemo } from 'react'

import { TokenWithLogo } from '@cowprotocol/common-const'
import { isTruthy } from '@cowprotocol/common-utils'
import { doesTokenMatchSymbolOrAddress } from '@cowprotocol/common-utils'

import { tokenListsUpdatingAtom } from '../../state/tokenLists/tokenListsStateAtom'
import { useSearchToken } from './useSearchToken'
Expand All @@ -23,7 +23,7 @@ export function useSearchNonExistentToken(tokenId: string | null): TokenWithLogo
return (
[foundToken.inactiveListsResult, foundToken.externalApiResult, foundToken.blockchainResult]
.flat()
.filter(isTruthy)[0] || null
.filter((token) => !!token && doesTokenMatchSymbolOrAddress(token, inputTokenToSearch))[0] || null
)
}, [inputTokenToSearch, foundToken])
}
6 changes: 6 additions & 0 deletions libs/tokens/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { tokensLegacyStateMigration } from './migrations/tokensLegacyStateMigration'

// Run migrations first of all
// TODO: remove it after 30.01.2024
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's enough to keep the migration for the next 3 months

tokensLegacyStateMigration()

// Updaters
export { TokensListsUpdater } from './updaters/TokensListsUpdater'
export { UnsupportedTokensUpdater } from './updaters/UnsupportedTokensUpdater'
Expand Down
101 changes: 101 additions & 0 deletions libs/tokens/src/migrations/tokensLegacyStateMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ListsSourcesByNetwork, TokenListsState } from '../types'
import { SupportedChainId } from '@cowprotocol/cow-sdk'
import { DEFAULT_TOKENS_LISTS } from '../const/tokensLists'

const MIGRATION_KEY = 'TOKENS_REFACTORING_MIGRATED'

export function tokensLegacyStateMigration() {
// Run migration only once
if (localStorage.getItem(MIGRATION_KEY)) return

try {
migrateLegacyTokensInUserState()
} catch (error) {
console.error('Cannot migrate LegacyTokensInUserState', error)
}

try {
migrateLegacyTokenLists()
} catch (error) {
console.error('Cannot migrate LegacyTokenLists', error)
}

localStorage.setItem(MIGRATION_KEY, '1')
}

// Move user-added tokens from legacy redux store (UserState) to a new atom (userAddedTokensAtom)
// And purge the irrelevant localStorage state
function migrateLegacyTokensInUserState() {
const userStateRaw = localStorage.getItem('redux_localstorage_simple_user')

if (!userStateRaw) return

const userState = JSON.parse(userStateRaw)

if (userState?.tokens && Object.keys(userState.tokens).length > 0) {
localStorage.setItem('userAddedTokensAtom:v1', JSON.stringify(userState.tokens))
}

// Cleanup legacy redux store (UserState)
delete userState.favouriteTokens
delete userState.tokens
delete userState.pairs

localStorage.setItem('redux_localstorage_simple_user', JSON.stringify(userState))
}

// Copy user-added lists from the legacy state to a new atom (userAddedListsSourcesAtom + listsStatesByChainAtom)
// And purge the irrelevant localStorage state
function migrateLegacyTokenLists() {
const listsStateRaw = localStorage.getItem('redux_localstorage_simple_lists')

if (!listsStateRaw) return

const listsState = JSON.parse(listsStateRaw)

const userAddedListsSources: ListsSourcesByNetwork = {
[SupportedChainId.MAINNET]: [],
[SupportedChainId.GNOSIS_CHAIN]: [],
[SupportedChainId.GOERLI]: [],
}

const tokenListsState: TokenListsState = {
[SupportedChainId.MAINNET]: {},
[SupportedChainId.GNOSIS_CHAIN]: {},
[SupportedChainId.GOERLI]: {},
}

Object.keys(listsState).forEach((chainIdStr) => {
const networkLists = listsState[chainIdStr]

if (!networkLists.byUrl) return

const chainId = +chainIdStr as SupportedChainId
const defaultLists = DEFAULT_TOKENS_LISTS[chainId]

Object.keys(networkLists.byUrl).forEach((listSource) => {
const list = networkLists.byUrl[listSource]

if (!list || !list.current) return

const isListInDefaults = defaultLists.find((defaultList) => defaultList.source === listSource)

if (isListInDefaults) return

userAddedListsSources[chainId].push({
source: listSource,
})

tokenListsState[chainId][listSource] = {
source: listSource,
list: list.current,
isEnabled: !!networkLists.activeListUrls?.includes(listSource),
}
})
})

localStorage.setItem('userAddedTokenListsAtom:v2', JSON.stringify(userAddedListsSources))
localStorage.setItem('allTokenListsInfoAtom:v2', JSON.stringify(tokenListsState))

localStorage.removeItem('redux_localstorage_simple_lists')
}
4 changes: 1 addition & 3 deletions libs/tokens/src/state/tokenLists/tokenListsStateAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'
import { SupportedChainId } from '@cowprotocol/cow-sdk'

import { ListsSourcesByNetwork, ListState } from '../../types'
import { ListsSourcesByNetwork, TokenListsState } from '../../types'
import { DEFAULT_TOKENS_LISTS } from '../../const/tokensLists'
import { environmentAtom } from '../environmentAtom'

type TokenListsState = Record<SupportedChainId, { [source: string]: ListState }>

export const userAddedListsSourcesAtom = atomWithStorage<ListsSourcesByNetwork>('userAddedTokenListsAtom:v2', {
[SupportedChainId.MAINNET]: [],
[SupportedChainId.GNOSIS_CHAIN]: [],
Expand Down
4 changes: 3 additions & 1 deletion libs/tokens/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type ListSourceConfig = {
source: string
}

export type ListsSourcesByNetwork = Record<SupportedChainId, ReadonlyArray<ListSourceConfig>>
export type ListsSourcesByNetwork = Record<SupportedChainId, Array<ListSourceConfig>>

export type TokensMap = { [address: string]: TokenInfo }

Expand All @@ -21,3 +21,5 @@ export interface ListState {
priority?: number
isEnabled?: boolean
}

export type TokenListsState = Record<SupportedChainId, { [source: string]: ListState }>
Loading