Skip to content

Commit

Permalink
feat(tokens): tokens and token lists library
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Oct 10, 2023
1 parent e902e36 commit c11771f
Show file tree
Hide file tree
Showing 62 changed files with 1,848 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/cowswap-frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@cowprotocol/widget-lib": ["../../../libs/widget-lib/src/index.ts"],
"@cowprotocol/widget-react": ["../../../libs/widget-react/src/index.ts"],
"@cowprotocol/snackbars": ["../../../libs/snackbars/src/index.ts"],
"@cowprotocol/tokens": ["../../../libs/tokens/src/index.ts"],
"@cowprotocol/wallet": ["../../../libs/wallet/src/index.ts"],
"@cowprotocol/assets/*": ["../../../libs/assets/src/*"],
"@cowprotocol/common-const": ["../../../libs/common-const/src/index.ts"],
Expand Down
20 changes: 20 additions & 0 deletions libs/tokens/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": [
[
"styled-components",
{
"pure": true,
"ssr": true
}
]
]
}
18 changes: 18 additions & 0 deletions libs/tokens/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
1 change: 1 addition & 0 deletions libs/tokens/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Tokens
11 changes: 11 additions & 0 deletions libs/tokens/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'tokens',
preset: '../../jest.preset.js',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/tokens',
}
12 changes: 12 additions & 0 deletions libs/tokens/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@cowprotocol/tokens",
"version": "0.0.1",
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js"
}
}
}
46 changes: 46 additions & 0 deletions libs/tokens/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@cowprotocol/tokens",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/tokens/src",
"projectType": "library",
"tags": [],
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/tokens/**/*.{ts,tsx,js,jsx}"]
}
},
"build": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/libs/tokens"
},
"configurations": {
"development": {
"mode": "development"
},
"production": {
"mode": "production"
}
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/tokens/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
}
}
93 changes: 93 additions & 0 deletions libs/tokens/src/const/tokensList.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"1": [
{
"id": "hc_zMahDEXc_WMKgPAUpK",
"priority": 1,
"enabledByDefault": true,
"url": "https://files.cow.fi/tokens/CowSwap.json"
},
{
"id": "yi4K5DrkWkF3oiWweRjt3",
"priority": 2,
"enabledByDefault": true,
"url": "https://files.cow.fi/tokens/CoinGecko.json"
},
{
"id": "uJcgDR4A3MLQTrDsO34bA",
"priority": 3,
"url": "https://raw.githubusercontent.com/compound-finance/token-list/master/compound.tokenlist.json"
},
{
"id": "ePv0T2u5t0omW7HjPxGCL",
"priority": 4,
"ensName": "tokenlist.aave.eth"
},
{
"id": "vcO-K1VshU97uUeao5p_i",
"priority": 5,
"ensName": "synths.snx.eth"
},
{
"id": "pBJ4ZL5jeZ0DcLw4S-_85",
"priority": 6,
"ensName": "wrapped.tokensoft.eth"
},
{
"id": "4nKiWPc0hM6NgkZgTUnsy",
"priority": 7,
"url": "https://raw.githubusercontent.com/SetProtocol/uniswap-tokenlist/main/set.tokenlist.json"
},
{
"id": "5anQfdMTp5WyI2jklNvFB",
"priority": 8,
"url": "https://raw.githubusercontent.com/opynfinance/opyn-tokenlist/master/opyn-squeeth-tokenlist.json"
},
{
"id": "YBalvtWm1YVzQoe8J2AJa",
"priority": 9,
"url": "https://app.tryroll.com/tokens.json"
},
{
"id": "Pl-AdOGqGki43XDf7EoGV",
"priority": 10,
"ensName": "defi.cmc.eth"
},
{
"id": "JXGSqoOJDDY9hsY0Zn-Ue",
"priority": 11,
"ensName": "stablecoin.cmc.eth"
},
{
"id": "bffPi82n3c4zBzAbUQiV7",
"priority": 12,
"ensName": "t2crtokens.eth"
}
],
"5": [
{
"id": "VZ--hetNymtoeZ5QUVhRT",
"priority": 1,
"enabledByDefault": true,
"url": "https://raw.githubusercontent.com/cowprotocol/token-lists/main/src/public/CowSwapGoerli.json"
},
{
"id": "uJcgDR4A3MLQTrDsO34bA",
"priority": 2,
"url": "https://raw.githubusercontent.com/compound-finance/token-list/master/compound.tokenlist.json"
}
],
"100": [
{
"id": "hc_zMahDEXc_WMKgPAUpK",
"priority": 1,
"enabledByDefault": true,
"url": "https://files.cow.fi/tokens/CowSwap.json"
},
{
"id": "hMVOQpr3d-b-lCpYDDyIM",
"priority": 2,
"enabledByDefault": true,
"url": "https://tokens.honeyswap.org"
}
]
}
4 changes: 4 additions & 0 deletions libs/tokens/src/const/tokensLists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { TokenListsByNetwork } from '../types'
import tokensList from './tokensList.json'

export const DEFAULT_TOKENS_LISTS: TokenListsByNetwork = tokensList
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useActiveTokenListsIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useAtomValue } from 'jotai'
import { activeTokenListsMapAtom } from '../state/tokenLists/tokenListsStateAtom'

export function useActiveTokenListsIds() {
return useAtomValue(activeTokenListsMapAtom)
}
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useAddCustomTokenLists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useSetAtom } from 'jotai'
import { addTokenListAtom } from '../state/tokenLists/tokenListsActionsAtom'

export function useAddCustomTokenLists() {
return useSetAtom(addTokenListAtom)
}
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useAddUnsupportedToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useSetAtom } from 'jotai'
import { addUnsupportedTokenAtom } from '../state/tokens/unsupportedTokensAtom'

export function useAddUnsupportedToken() {
return useSetAtom(addUnsupportedTokenAtom)
}
7 changes: 7 additions & 0 deletions libs/tokens/src/hooks/useAllTokenListsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useAtomValue } from 'jotai'
import { allTokenListsInfoAtom } from '../state/tokenLists/tokenListsStateAtom'
import { TokenListInfo } from '../types'

export function useAllTokenListsInfo(): TokenListInfo[] {
return useAtomValue(allTokenListsInfoAtom)
}
8 changes: 8 additions & 0 deletions libs/tokens/src/hooks/useAllTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useAtomValue } from 'jotai'
import { activeTokensAtom } from '../state/tokens/tokensAtom'

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

export function useAllTokens(): TokenWithLogo[] {
return useAtomValue(activeTokensAtom)
}
18 changes: 18 additions & 0 deletions libs/tokens/src/hooks/useAreThereTokensWithSameSymbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useAtomValue } from 'jotai'
import { useCallback } from 'react'

import { isAddress } from '@cowprotocol/common-utils'
import { tokensBySymbolAtom } from '../state/tokens/tokensAtom'

export function useAreThereTokensWithSameSymbol(): (tokenAddressOrSymbol: string | null | undefined) => boolean {
const tokensBySymbol = useAtomValue(tokensBySymbolAtom)

return useCallback(
(tokenAddressOrSymbol: string | null | undefined) => {
if (!tokenAddressOrSymbol || isAddress(tokenAddressOrSymbol)) return false

return tokensBySymbol[tokenAddressOrSymbol.toLowerCase()]?.length > 1
},
[tokensBySymbol]
)
}
7 changes: 7 additions & 0 deletions libs/tokens/src/hooks/useFavouriteTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useAtomValue } from 'jotai'
import { favouriteTokensListAtom } from '../state/tokens/favouriteTokensAtom'
import { TokenWithLogo } from '@cowprotocol/common-const'

export function useFavouriteTokens(): TokenWithLogo[] {
return useAtomValue(favouriteTokensListAtom)
}
7 changes: 7 additions & 0 deletions libs/tokens/src/hooks/useImportTokenCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TokenWithLogo } from '@cowprotocol/common-const'
import { useSetAtom } from 'jotai'
import { addUserTokenAtom } from '../state/tokens/userAddedTokensAtom'

export function useImportTokenCallback(): (tokens: TokenWithLogo[]) => void {
return useSetAtom(addUserTokenAtom)
}
13 changes: 13 additions & 0 deletions libs/tokens/src/hooks/useIsTradeUnsupported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Currency } from '@uniswap/sdk-core'
import { useIsUnsupportedToken } from './useIsUnsupportedToken'

export function useIsTradeUnsupported(
inputCurrency: Currency | null | undefined,
outputCurrency: Currency | null | undefined
): boolean {
const isUnsupportedToken = useIsUnsupportedToken()
const isInputCurrencyUnsupported = inputCurrency?.isNative ? false : !!isUnsupportedToken(inputCurrency?.address)
const isOutputCurrencyUnsupported = outputCurrency?.isNative ? false : !!isUnsupportedToken(outputCurrency?.address)

return isInputCurrencyUnsupported || isOutputCurrencyUnsupported
}
21 changes: 21 additions & 0 deletions libs/tokens/src/hooks/useIsUnsupportedToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useUnsupportedTokens } from './useUnsupportedTokens'
import { useCallback } from 'react'

export function useIsUnsupportedToken() {
const unsupportedTokens = useUnsupportedTokens()

return useCallback(
(address?: string) => {
const state = address && unsupportedTokens[address.toLowerCase()]

if (state) {
return {
...state,
address,
}
}
return false
},
[unsupportedTokens]
)
}
18 changes: 18 additions & 0 deletions libs/tokens/src/hooks/useIsUnsupportedTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useUnsupportedTokens } from './useUnsupportedTokens'
import { useCallback } from 'react'

export function useIsUnsupportedTokens() {
const unsupportedTokens = useUnsupportedTokens()

return useCallback(
({ sellToken, buyToken }: { sellToken: string | null | undefined; buyToken: string | null | undefined }) => {
if (!unsupportedTokens) return false

return !!(
(sellToken && unsupportedTokens[sellToken.toLowerCase()]) ||
(buyToken && unsupportedTokens[buyToken.toLowerCase()])
)
},
[unsupportedTokens]
)
}
11 changes: 11 additions & 0 deletions libs/tokens/src/hooks/useRemoveTokenCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TokenWithLogo } from '@cowprotocol/common-const'
import { useSetAtom } from 'jotai'
import { removeUserTokenAtom } from '../state/tokens/userAddedTokensAtom'

export function useRemoveTokenCallback(): (token: TokenWithLogo) => void {
const removeUserToken = useSetAtom(removeUserTokenAtom)

return (token: TokenWithLogo) => {
removeUserToken(token)
}
}
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useRemoveTokenList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useSetAtom } from 'jotai'
import { removeTokenListAtom } from '../state/tokenLists/tokenListsActionsAtom'

export function useRemoveTokenList() {
return useSetAtom(removeTokenListAtom)
}
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useRemoveUnsupportedToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useSetAtom } from 'jotai'
import { removeUnsupportedTokenAtom } from '../state/tokens/unsupportedTokensAtom'

export function useRemoveUnsupportedToken() {
return useSetAtom(removeUnsupportedTokenAtom)
}
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useResetFavouriteTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useSetAtom } from 'jotai'
import { resetFavouriteTokensAtom } from '../state/tokens/favouriteTokensAtom'

export function useResetFavouriteTokens() {
return useSetAtom(resetFavouriteTokensAtom)
}
6 changes: 6 additions & 0 deletions libs/tokens/src/hooks/useResetUserTokensCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useSetAtom } from 'jotai'
import { resetUserTokenAtom } from '../state/tokens/userAddedTokensAtom'

export function useResetUserTokensCallback(): () => void {
return useSetAtom(resetUserTokenAtom)
}
Loading

0 comments on commit c11771f

Please sign in to comment.