fix: make token list version bump error quieter (#6271)

* fix: use console.debug for expected transient error

* fix: add tests

* fix: name and lints
This commit is contained in:
eddie 2023-04-06 10:26:56 -07:00 committed by GitHub
parent 81ced4cb8b
commit 271ef580e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 8 deletions

@ -1,4 +1,4 @@
import { getVersionUpgrade, minVersionBump, VersionUpgrade } from '@uniswap/token-lists'
import { getVersionUpgrade, VersionUpgrade } from '@uniswap/token-lists'
import { useWeb3React } from '@web3-react/core'
import { DEFAULT_LIST_OF_LISTS, UNSUPPORTED_LIST_URLS } from 'constants/lists'
import useInterval from 'lib/hooks/useInterval'
@ -10,6 +10,8 @@ import { useAllLists } from 'state/lists/hooks'
import { useFetchListCallback } from '../../hooks/useFetchListCallback'
import useIsWindowVisible from '../../hooks/useIsWindowVisible'
import { acceptListUpdate } from './actions'
import { shouldAcceptVersionUpdate } from './utils'
export default function Updater(): null {
const { provider } = useWeb3React()
const dispatch = useAppDispatch()
@ -64,14 +66,8 @@ export default function Updater(): null {
throw new Error('unexpected no version bump')
case VersionUpgrade.PATCH:
case VersionUpgrade.MINOR: {
const min = minVersionBump(list.current.tokens, list.pendingUpdate.tokens)
// automatically update minor/patch as long as bump matches the min update
if (bump >= min) {
if (shouldAcceptVersionUpdate(listUrl, list.current, list.pendingUpdate, bump)) {
dispatch(acceptListUpdate(listUrl))
} else {
console.error(
`List at url ${listUrl} could not automatically update because the version bump was only PATCH/MINOR while the update had breaking changes and should have been MAJOR`
)
}
break
}

@ -0,0 +1,64 @@
import { TokenList, VersionUpgrade } from '@uniswap/token-lists'
import { shouldAcceptVersionUpdate } from './utils'
function buildTokenList(count: number): TokenList {
const tokens = []
for (let i = 0; i < count; i++) {
tokens.push({
name: `Token ${i}`,
address: `0x${i.toString().padStart(40, '0')}`,
symbol: `T${i}`,
decimals: 18,
chainId: 1,
logoURI: `https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x${i
.toString()
.padStart(40, '0')}/logo.png`,
})
}
return {
name: 'Defi',
logoURI:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x514910771AF9Ca656af840dff83E8264EcF986CA/logo.png',
keywords: ['defi', 'uniswap'],
timestamp: '2021-03-12T00:00:00.000Z',
version: {
major: 1,
minor: 0,
patch: 0,
},
tokens,
}
}
describe('shouldAcceptMinorVersionUpdate', () => {
it('returns false for patch when tokens have changed', () => {
expect(shouldAcceptVersionUpdate('test_list', buildTokenList(1), buildTokenList(2), VersionUpgrade.PATCH)).toEqual(
false
)
})
it('returns true for patch when tokens are the same', () => {
expect(shouldAcceptVersionUpdate('test_list', buildTokenList(1), buildTokenList(1), VersionUpgrade.PATCH)).toEqual(
true
)
})
it('returns true for minor version bump with tokens added', () => {
expect(shouldAcceptVersionUpdate('test_list', buildTokenList(1), buildTokenList(2), VersionUpgrade.MINOR)).toEqual(
true
)
})
it('returns true for no version bump', () => {
expect(shouldAcceptVersionUpdate('test_list', buildTokenList(1), buildTokenList(2), VersionUpgrade.MINOR)).toEqual(
true
)
})
it('returns false for minor version bump with tokens removed', () => {
expect(shouldAcceptVersionUpdate('test_list', buildTokenList(2), buildTokenList(1), VersionUpgrade.MINOR)).toEqual(
false
)
})
})

19
src/state/lists/utils.ts Normal file

@ -0,0 +1,19 @@
import { minVersionBump, TokenList, VersionUpgrade } from '@uniswap/token-lists'
export function shouldAcceptVersionUpdate(
listUrl: string,
current: TokenList,
update: TokenList,
targetBump: VersionUpgrade.PATCH | VersionUpgrade.MINOR
): boolean {
const min = minVersionBump(current.tokens, update.tokens)
// Automatically update minor/patch as long as bump matches the min update.
if (targetBump >= min) {
return true
} else {
console.debug(
`List at url ${listUrl} could not automatically update because the version bump was only PATCH/MINOR while the update had breaking changes and should have been MAJOR`
)
return false
}
}