Skip to content

Commit

Permalink
update: integrate stable coin from api (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh authored Aug 10, 2023
1 parent b4eb153 commit 8c9a5eb
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 192 deletions.
11 changes: 5 additions & 6 deletions src/components/KyberAITokenBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import CurrencyLogo from 'components/CurrencyLogo'
import ApeIcon from 'components/Icons/ApeIcon'
import Row, { RowBetween, RowFit } from 'components/Row'
import { APP_PATHS } from 'constants/index'
import { KNC, NativeCurrencies, STABLE_COINS_ADDRESS } from 'constants/tokens'
import { KNC, NativeCurrencies } from 'constants/tokens'
import { useActiveWeb3React } from 'hooks'
import { useStableCoins } from 'hooks/Tokens'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useTheme from 'hooks/useTheme'
import KyberScoreMeter from 'pages/TrueSightV2/components/KyberScoreMeter'
Expand Down Expand Up @@ -44,6 +45,8 @@ const KyberAITokenBanner = ({
const token0 = currencyIn?.wrapped
const token1 = currencyOut?.wrapped

const { isStableCoin } = useStableCoins(chainId)

const { data: tokenInputOverview } = useTokenDetailQuery(
{ address: token0?.address, chain },
{ skip: !token0?.address || !account || !isWhiteList || !chain, refetchOnMountOrArgChange: true },
Expand Down Expand Up @@ -87,11 +90,7 @@ const KyberAITokenBanner = ({

if (!token && !staticMode) return null

if (
staticMode &&
STABLE_COINS_ADDRESS[chainId].some(value => value.toLowerCase() === currencyIn?.wrapped.address.toLowerCase())
)
return null
if (staticMode && isStableCoin(currencyIn?.wrapped.address.toLowerCase())) return null
const staticModeCurrency = !currencyIn || KNC[chainId].equals(currencyIn) ? NativeCurrencies[chainId] : currencyIn
const color = staticMode ? theme.primary : calculateValueToColor(token?.kyberScore || 0, theme)
return (
Expand Down
7 changes: 4 additions & 3 deletions src/components/PoolList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import ListItem from 'components/PoolList/ListItem'
import ShareModal from 'components/ShareModal'
import { ClickableText } from 'components/YieldPools/styleds'
import { AMP_HINT, AMP_LIQUIDITY_HINT, MAX_ALLOW_APY } from 'constants/index'
import { STABLE_COINS_ADDRESS } from 'constants/tokens'
import { useActiveWeb3React } from 'hooks'
import { useStableCoins } from 'hooks/Tokens'
import { SelectPairInstructionWrapper } from 'pages/Pools/styleds'
import { ApplicationModal } from 'state/application/actions'
import { useModalOpen, useOpenModal } from 'state/application/hooks'
Expand Down Expand Up @@ -309,6 +309,7 @@ const PoolList = ({ currencies, searchValue, isShowOnlyActiveFarmPools, onlyShow
const { data: farms } = useActiveAndUniqueFarmsData()

const [currentPage, setCurrentPage] = useState(1)
const { stableCoins } = useStableCoins(chainId)
const sortedFilteredSubgraphPoolsData = useMemo(() => {
let res = [...subgraphPoolsData]

Expand Down Expand Up @@ -347,7 +348,7 @@ const PoolList = ({ currencies, searchValue, isShowOnlyActiveFarmPools, onlyShow
})

if (onlyShowStable) {
const stableList = isEVM ? STABLE_COINS_ADDRESS[chainId]?.map(item => item.toLowerCase()) || [] : []
const stableList = isEVM ? stableCoins?.map(item => item.address.toLowerCase()) || [] : []
res = res.filter(poolData => {
return (
stableList.includes(poolData.token0.id.toLowerCase()) && stableList.includes(poolData.token1.id.toLowerCase())
Expand All @@ -364,8 +365,8 @@ const PoolList = ({ currencies, searchValue, isShowOnlyActiveFarmPools, onlyShow
onlyShowStable,
farms,
searchValue,
chainId,
isEVM,
stableCoins,
])

const startIndex = (currentPage - 1) * ITEM_PER_PAGE
Expand Down
11 changes: 3 additions & 8 deletions src/components/SwapForm/hooks/useCheckStablePairSwap.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { Currency } from '@kyberswap/ks-sdk-core'

import { STABLE_COINS_ADDRESS } from 'constants/tokens'
import { useStableCoins } from 'hooks/Tokens'

const useCheckStablePairSwap = (currencyIn: Currency | undefined, currencyOut: Currency | undefined) => {
const isStablePairSwap = Boolean(
currencyIn &&
currencyOut &&
STABLE_COINS_ADDRESS[currencyIn.chainId].includes(currencyIn.wrapped.address) &&
STABLE_COINS_ADDRESS[currencyOut.chainId].includes(currencyOut.wrapped.address),
)

const { isStableCoin } = useStableCoins(currencyIn?.chainId)
const isStablePairSwap = isStableCoin(currencyIn?.wrapped?.address) && isStableCoin(currencyOut?.wrapped?.address)
return isStablePairSwap
}

Expand Down
125 changes: 0 additions & 125 deletions src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,131 +25,6 @@ export const NativeCurrencies = new Proxy(NativeCurrenciesLocal, {
},
})

export const STABLE_COINS_ADDRESS: { [chainId in ChainId]: string[] } = {
[ChainId.MAINNET]: [
'0x6B175474E89094C44Da98b954EedeAC495271d0F', // DAI
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
'0xdAC17F958D2ee523a2206206994597C13D831ec7', // USDT
'0x4Fabb145d64652a948d72533023f6E7A623C7C53', // BUSD
'0x8D6CeBD76f18E1558D4DB88138e2DeFB3909fAD6', // MAI
'0xB0B195aEFA3650A6908f15CdaC7D92F8a5791B0B', // BOB
'0x99D8a9C45b2ecA8864373A26D1459e3Dff1e17F3', // MIM
],
[ChainId.MATIC]: [
'0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063', // DAI
'0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // usdc
'0xc2132D05D31c914a87C6611C10748AEb04B58e8F', // usdt
'0xa3Fa99A148fA48D14Ed51d610c367C61876997F1', // MAI
'0xB0B195aEFA3650A6908f15CdaC7D92F8a5791B0B', // BOB
'0x49a0400587A7F65072c87c4910449fDcC5c47242', // MIM
],
[ChainId.BSCMAINNET]: [
'0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3', // dai
'0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d', // usdc
'0x55d398326f99059fF775485246999027B3197955', // usdt
'0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', // busd
'0x3F56e0c36d275367b8C502090EDF38289b3dEa0d', // MAI
'0xfE19F0B51438fd612f6FD59C1dbB3eA319f433Ba', // MIM
'0xB0B195aEFA3650A6908f15CdaC7D92F8a5791B0B', // BOB
],
[ChainId.AVAXMAINNET]: [
'0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7', // USDt
'0xc7198437980c041c805A1EDcbA50c1Ce5db95118', // usdt.e
'0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664', // usdc.e
'0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E', // usdc
'0xd586E7F844cEa2F87f50152665BCbc2C279D8d70', // dai.e
'0x3B55E45fD6bd7d4724F5c47E0d1bCaEdd059263e', // MAI
'0x5c49b268c9841AFF1Cc3B0a418ff5c3442eE3F3b', // MAI
'0x130966628846BFd36ff31a822705796e8cb8C18D', // MIM
'0x111111111111ed1D73f860F57b2798b683f2d325', // YUSD
],
[ChainId.FANTOM]: [
'0x8D11eC38a3EB5E956B052f67Da8Bdc9bef8Abf3E', // dai
'0x04068DA6C83AFCFA0e13ba15A6696662335D5B75', // usdc
'0x049d68029688eAbF473097a2fC38ef61633A3C7A', // fusdt
'0xfB98B335551a418cD0737375a2ea0ded62Ea213b', // MAI
'0x82f0B8B456c1A451378467398982d4834b6829c1', // MIM
],
[ChainId.CRONOS]: [
'0xF2001B145b43032AAF5Ee2884e456CCd805F677D', // dai
'0xc21223249CA28397B4B6541dfFaEcC539BfF0c59', // usdc
'0x66e428c3f67a68878562e79A0234c1F83c208770', // usdt
'0xC74D59A548ecf7fc1754bb7810D716E9Ac3e3AE5', // busd
'0x2Ae35c8E3D4bD57e8898FF7cd2bBff87166EF8cb', // MAI
],
[ChainId.ARBITRUM]: [
'0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1', // dai
'0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8', // usdc
'0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9', // usdt
'0x3F56e0c36d275367b8C502090EDF38289b3dEa0d', // MAI
'0xFEa7a6a0B346362BF88A9e4A88416B77a57D6c2A', // MIM
],
[ChainId.BTTC]: [
'0x9B5F27f6ea9bBD753ce3793a07CbA3C74644330d', // usdt_b
'0xdB28719F7f938507dBfe4f0eAe55668903D34a15', // usdt_t
'0xE887512ab8BC60BcC9224e1c3b5Be68E26048B8B', // usdt_e
'0x935faA2FCec6Ab81265B301a30467Bbc804b43d3', // usdc_t
'0xCa424b845497f7204D9301bd13Ff87C0E2e86FCF', // usdc_b
'0xAE17940943BA9440540940DB0F1877f101D39e8b', // usdc_e
'0xe7dC549AE8DB61BDE71F22097BEcc8dB542cA100', // dai_e
'0x17F235FD5974318E4E2a5e37919a209f7c37A6d1', // usdd_t
],
[ChainId.VELAS]: [
'0xe2C120f188eBd5389F71Cf4d9C16d05b62A58993', // usdc
'0x01445C31581c354b7338AC35693AB2001B50b9aE', // usdt
'0xc111c29A988AE0C0087D97b33C6E6766808A3BD3', // busd
],
[ChainId.AURORA]: [
'0xe3520349F477A5F6EB06107066048508498A291b', // Dai
'0xB12BFcA5A55806AaF64E99521918A4bf0fC40802', // usdc
'0x4988a896b1227218e4A686fdE5EabdcAbd91571f', // usdt
'0xdFA46478F9e5EA86d57387849598dbFB2e964b02', // MAI
],
[ChainId.OASIS]: [
'0x80A16016cC4A2E6a2CACA8a4a498b1699fF0f844', // usdc
'0xdC19A122e268128B5eE20366299fc7b5b199C8e3', // usdtet
'0x639A647fbe20b6c8ac19E48E2de44ea792c62c5C', // busd
'0x6Cb9750a92643382e020eA9a170AbB83Df05F30B', // usdt
'0x5a4Ba16C2AeB295822A95280A7c7149E87769E6A', // ceDAI
'0x81ECac0D6Be0550A00FF064a4f9dd2400585FE9c', // ceUSDC
'0x4Bf769b05E832FCdc9053fFFBC78Ca889aCb5E1E', // ceUSDT
],
[ChainId.OPTIMISM]: [
'0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1', // Dai
'0x94b008aA00579c1307B0EF2c499aD98a8ce58e58', // usdt
'0x7F5c764cBc14f9669B88837ca1490cCa17c31607', // usdc
'0xdFA46478F9e5EA86d57387849598dbFB2e964b02', // MAI
'0xB0B195aEFA3650A6908f15CdaC7D92F8a5791B0B', // BOB
],
[ChainId.ZKSYNC]: [
'0x3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4', // USDC
'0x8E86e46278518EFc1C5CEd245cBA2C7e3ef11557', // USD+
'0x2039bb4116B4EFc145Ec4f0e2eA75012D6C0f181', // BUSD
'0x493257fD37EDB34451f62EDf8D2a0C418852bA4C', // USDT
],
[ChainId.LINEA_TESTNET]: [
'0xf56dc6695cF1f5c364eDEbC7Dc7077ac9B586068', // USDC
'0x1990BC6dfe2ef605Bfc08f5A23564dB75642Ad73', // USDT
'0x8741Ba6225A6BF91f9D73531A98A89807857a2B3', // DAI
'0x7d43AABC515C356145049227CeE54B608342c0ad', // BUSD
],
[ChainId.LINEA]: [
'0x7d43aabc515c356145049227cee54b608342c0ad', // BUSD
],
[ChainId.SOLANA]: [
'EjmyN6qEC1Tf1JxiG1ae7UTJhUxSwk1TCWNWqxWV4J6o', // Dai
'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // usdc
'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', // usdt
'9mWRABuz2x6koTPCWiCPM49WUbcrNqGTHBV9T9k7y1o7', // MAI
'HRQke5DKdDo3jV7wnomyiM8AA3EzkVnxMDdo2FQ5XUe1', // MIM
],
[ChainId.GÖRLI]: [],
[ChainId.MUMBAI]: [],
[ChainId.BSCTESTNET]: [],
[ChainId.AVAXTESTNET]: [],
[ChainId.SOLANA_DEVNET]: [],
}

// This list is intentionally different from the list above
// Was requested from product team, to implement Swap fee config
export const STABLE_COIN_ADDRESSES_TO_TAKE_FEE: Record<ChainId, string[]> = {
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import axios from 'axios'
import { arrayify } from 'ethers/lib/utils'
import { useCallback, useMemo } from 'react'
import { useSelector } from 'react-redux'
import { useGetTokenListQuery } from 'services/ksSetting'
import useSWR from 'swr'

import ERC20_INTERFACE, { ERC20_BYTES32_INTERFACE } from 'constants/abis/erc20'
Expand Down Expand Up @@ -383,3 +384,20 @@ export function useCurrencyV2(currencyId: string | undefined, customChainId?: Ch
return tokenInWhitelist || token
}, [chainId, isETH, token, currencyId, tokenInWhitelist])
}

export const useStableCoins = (chainId: ChainId | undefined) => {
const { data } = useGetTokenListQuery({ chainId: chainId as ChainId, isStable: true }, { skip: !chainId })

const stableCoins = useMemo(() => {
return data?.data?.tokens || []
}, [data])

const isStableCoin = useCallback(
(address: string | undefined) => {
if (!address) return false
return stableCoins.some(token => token.address.toLowerCase() === address?.toLowerCase())
},
[stableCoins],
)
return { isStableCoin, stableCoins }
}
6 changes: 4 additions & 2 deletions src/pages/ProAmmPools/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import LocalLoader from 'components/LocalLoader'
import Pagination from 'components/Pagination'
import { Input as PaginationInput } from 'components/Pagination/PaginationInputOnMobile'
import ShareModal from 'components/ShareModal'
import { STABLE_COINS_ADDRESS } from 'constants/tokens'
import { useActiveWeb3React } from 'hooks'
import { useStableCoins } from 'hooks/Tokens'
import { SelectPairInstructionWrapper } from 'pages/Pools/styleds'
import { ApplicationModal } from 'state/application/actions'
import { useModalOpen, useOpenModal } from 'state/application/hooks'
Expand Down Expand Up @@ -184,6 +184,7 @@ export default function ProAmmPoolList({
const cbId = currencies[Field.CURRENCY_B]?.wrapped.address.toLowerCase()

const { chainId, account, isEVM, networkInfo } = useActiveWeb3React()
const { stableCoins } = useStableCoins(chainId)
const userLiquidityPositionsQueryResult = useUserProMMPositions(tokenPriceMap)
const loadingUserPositions = !account ? false : userLiquidityPositionsQueryResult.loading
const userPositions = useMemo(
Expand Down Expand Up @@ -260,7 +261,7 @@ export default function ProAmmPoolList({
}

if (onlyShowStable) {
const stableList = chainId ? STABLE_COINS_ADDRESS[chainId]?.map(item => item.toLowerCase()) || [] : []
const stableList = chainId ? stableCoins?.map(item => item.address.toLowerCase()) || [] : []
filteredPools = filteredPools.filter(poolData => {
return (
stableList.includes(poolData.token0.address.toLowerCase()) &&
Expand All @@ -285,6 +286,7 @@ export default function ProAmmPoolList({
onlyShowStable,
searchValue,
farms,
stableCoins,
chainId,
listComparator,
])
Expand Down
12 changes: 3 additions & 9 deletions src/pages/SwapV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ import {
Wrapper,
} from 'components/swapv2/styleds'
import { AGGREGATOR_WAITING_TIME, APP_PATHS, TIME_TO_REFRESH_SWAP_RATE } from 'constants/index'
import { STABLE_COINS_ADDRESS } from 'constants/tokens'
import { useActiveWeb3React } from 'hooks'
import { useAllTokens, useIsLoadedTokenDefault } from 'hooks/Tokens'
import { useAllTokens, useIsLoadedTokenDefault, useStableCoins } from 'hooks/Tokens'
import { ApprovalState, useApproveCallbackFromTradeV2 } from 'hooks/useApproveCallback'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useParsedQueryString from 'hooks/useParsedQueryString'
Expand Down Expand Up @@ -408,16 +407,11 @@ export default function Swap() {

useSyncTokenSymbolToUrl(currencyIn, currencyOut, onSelectSuggestedPair, isSelectCurrencyManually)
const isLoadedTokenDefault = useIsLoadedTokenDefault()
const { isStableCoin } = useStableCoins(chainId)

const [rawSlippage] = useUserSlippageTolerance()

const isStableCoinSwap = Boolean(
INPUT?.currencyId &&
OUTPUT?.currencyId &&
chainId &&
STABLE_COINS_ADDRESS[chainId].includes(INPUT?.currencyId) &&
STABLE_COINS_ADDRESS[chainId].includes(OUTPUT?.currencyId),
)
const isStableCoinSwap = isStableCoin(INPUT?.currencyId) && isStableCoin(OUTPUT?.currencyId)

useUpdateSlippageInStableCoinSwap()

Expand Down
30 changes: 13 additions & 17 deletions src/pages/SwapV3/useUpdateSlippageInStableCoinSwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { useSelector } from 'react-redux'
import { usePrevious } from 'react-use'

import { DEFAULT_SLIPPAGE, DEFAULT_SLIPPAGE_STABLE_PAIR_SWAP } from 'constants/index'
import { STABLE_COINS_ADDRESS } from 'constants/tokens'
import { useActiveWeb3React } from 'hooks'
import { useStableCoins } from 'hooks/Tokens'
import { AppState } from 'state'
import { Field } from 'state/swap/actions'
import { useUserSlippageTolerance } from 'state/user/hooks'

const useUpdateSlippageInStableCoinSwap = () => {
const { chainId } = useActiveWeb3React()
const { isStableCoin } = useStableCoins(chainId)
const inputCurrencyId = useSelector((state: AppState) => state.swap[Field.INPUT].currencyId)
const previousInputCurrencyId = usePrevious(inputCurrencyId)
const outputCurrencyId = useSelector((state: AppState) => state.swap[Field.OUTPUT].currencyId)
Expand All @@ -21,21 +22,8 @@ const useUpdateSlippageInStableCoinSwap = () => {
rawSlippageRef.current = slippage

useEffect(() => {
const isStableCoinPreviousSwap = Boolean(
chainId &&
previousInputCurrencyId &&
previousOutputCurrencyId &&
STABLE_COINS_ADDRESS[chainId].includes(previousInputCurrencyId) &&
STABLE_COINS_ADDRESS[chainId].includes(previousOutputCurrencyId),
)

const isStableCoinSwap = Boolean(
chainId &&
inputCurrencyId &&
outputCurrencyId &&
STABLE_COINS_ADDRESS[chainId].includes(inputCurrencyId) &&
STABLE_COINS_ADDRESS[chainId].includes(outputCurrencyId),
)
const isStableCoinPreviousSwap = isStableCoin(previousInputCurrencyId) && isStableCoin(previousOutputCurrencyId)
const isStableCoinSwap = isStableCoin(inputCurrencyId) && isStableCoin(outputCurrencyId)

if (isStableCoinPreviousSwap === isStableCoinSwap) {
return
Expand All @@ -49,7 +37,15 @@ const useUpdateSlippageInStableCoinSwap = () => {
if (!isStableCoinSwap && rawSlippageRef.current > DEFAULT_SLIPPAGE) {
setSlippage(DEFAULT_SLIPPAGE)
}
}, [chainId, inputCurrencyId, outputCurrencyId, previousInputCurrencyId, previousOutputCurrencyId, setSlippage])
}, [
isStableCoin,
chainId,
inputCurrencyId,
outputCurrencyId,
previousInputCurrencyId,
previousOutputCurrencyId,
setSlippage,
])
}

export default useUpdateSlippageInStableCoinSwap
Loading

0 comments on commit 8c9a5eb

Please sign in to comment.