import { ChainId , Pair , Token } from '@pancakeswap-libs/sdk' ; import flatMap from 'lodash.flatmap' ; import { useCallback , useMemo } from 'react' ; import { shallowEqual , useDispatch , useSelector } from 'react-redux' ; import { BASES_TO_TRACK_LIQUIDITY_FOR , PINNED_PAIRS } from '../../constants' ; import { useActiveWeb3React } from '../../hooks' ; import { useAllTokens } from '../../hooks/Tokens' ; import { AppDispatch , AppState } from '../index' ; import { addSerializedPair , addSerializedToken , removeSerializedToken , SerializedPair , SerializedToken , updateUserDarkMode , updateUserDeadline , updateUserExpertMode , updateUserSlippageTolerance , muteAudio , unmuteAudio , } from './actions' ; import { setThemeCache } from '../../utils/theme' ; function serializeToken ( token ) { return { chainId : token . chainId , address : token . address , decimals : token . decimals , symbol : token . symbol , name : token . name , } ; } function deserializeToken ( serializedToken ) { return new Token ( serializedToken . chainId , serializedToken . address , serializedToken . decimals , serializedToken . symbol , serializedToken . name ) ; } export function useIsDarkMode ( ) { const { userDarkMode , matchesDarkMode } = useSelector < AppState , { userDarkMode : boolean | null ; matchesDarkMode : boolean ; } > ( ( { user : { matchesDarkMode , userDarkMode } } ) => ( { userDarkMode , matchesDarkMode , } ) , shallowEqual ) ; return userDarkMode === null ? matchesDarkMode : userDarkMode ; } export function useDarkModeManager ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const { userDarkMode } = useSelector < AppState , { userDarkMode : boolean | null ; } > ( ( { user : { userDarkMode } } ) => ( { userDarkMode , } ) , shallowEqual ) ; const darkMode = useIsDarkMode ( ) ; const toggleSetDarkMode = useCallback ( ( ) => { setThemeCache ( ! userDarkMode ) ; dispatch ( updateUserDarkMode ( { userDarkMode : ! userDarkMode } ) ) ; } , [ userDarkMode , dispatch ] ) ; return [ darkMode , toggleSetDarkMode ] ; } export function useAudioModeManager ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const audioPlay = useSelector < AppState , AppState [ 'user' ] [ 'audioPlay' ] > ( ( state ) => state . user . audioPlay ) ; const toggleSetAudioMode = useCallback ( ( ) => { if ( audioPlay ) { dispatch ( muteAudio ( ) ) ; } else { dispatch ( unmuteAudio ( ) ) ; } } , [ audioPlay , dispatch ] ) ; return [ audioPlay , toggleSetAudioMode ] ; } export function useIsExpertMode ( ) { return useSelector < AppState , AppState [ 'user' ] [ 'userExpertMode' ] > ( ( state ) => state . user . userExpertMode ) ; } export function useExpertModeManager ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const expertMode = useIsExpertMode ( ) ; const toggleSetExpertMode = useCallback ( ( ) => { dispatch ( updateUserExpertMode ( { userExpertMode : ! expertMode } ) ) ; } , [ expertMode , dispatch ] ) ; return [ expertMode , toggleSetExpertMode ] ; } export function useUserSlippageTolerance ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const userSlippageTolerance = useSelector < AppState , AppState [ 'user' ] [ 'userSlippageTolerance' ] > ( ( state ) => { return state . user . userSlippageTolerance ; } ) ; const setUserSlippageTolerance = useCallback ( ( slippageTolerance ) => { dispatch ( updateUserSlippageTolerance ( { userSlippageTolerance : slippageTolerance } ) ) ; } , [ dispatch ] ) ; return [ userSlippageTolerance , setUserSlippageTolerance ] ; } export function useUserDeadline ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; const userDeadline = useSelector < AppState , AppState [ 'user' ] [ 'userDeadline' ] > ( ( state ) => { return state . user . userDeadline ; } ) ; const setUserDeadline = useCallback ( ( deadline ) => { dispatch ( updateUserDeadline ( { userDeadline : deadline } ) ) ; } , [ dispatch ] ) ; return [ userDeadline , setUserDeadline ] ; } export function useAddUserToken ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( token ) => { dispatch ( addSerializedToken ( { serializedToken : serializeToken ( token ) } ) ) ; } , [ dispatch ] ) ; } export function useRemoveUserAddedToken ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( chainId , address ) => { dispatch ( removeSerializedToken ( { chainId , address } ) ) ; } , [ dispatch ] ) ; } export function useUserAddedTokens ( ) { const { chainId } = useActiveWeb3React ( ) ; const serializedTokensMap = useSelector < AppState , AppState [ 'user' ] [ 'tokens' ] > ( ( { user : { tokens } } ) => tokens ) ; return useMemo ( ( ) => { if ( ! chainId ) return [ ] ; return Object . values ( serializedTokensMap [ chainId as ChainId ] ?? { } ) . map ( deserializeToken ) ; } , [ serializedTokensMap , chainId ] ) ; } function serializePair ( pair ) { return { token0 : serializeToken ( pair . token0 ) , token1 : serializeToken ( pair . token1 ) , } ; } export function usePairAdder ( ) { const dispatch = useDispatch < AppDispatch > ( ) ; return useCallback ( ( pair ) => { dispatch ( addSerializedPair ( { serializedPair : serializePair ( pair ) } ) ) ; } , [ dispatch ] ) ; } param tokenA param tokenB export function toV2LiquidityToken ( [ tokenA , tokenB ] ) { return new Token ( tokenA . chainId , Pair . getAddress ( tokenA , tokenB ) , 18 , 'Cake-LP' , 'Pancake LPs' ) ; } /**
 * Returns all the pairs of tokens that are tracked by the user for the current chain ID.
 */ export function useTrackedTokenPairs ( ) { const { chainId } = useActiveWeb3React ( ) ; const tokens = useAllTokens ( ) ; const pinnedPairs = useMemo ( ( ) => ( chainId ? PINNED_PAIRS [ chainId ] ?? [ ] : [ ] ) , [ chainId ] ) ; const generatedPairs = useMemo ( ( ) => chainId ? flatMap ( Object . keys ( tokens ) , ( tokenAddress ) => { const token = tokens [ tokenAddress ] ; return ( ( BASES_TO_TRACK_LIQUIDITY_FOR [ chainId ] ?? [ ] ) . map ( ( base ) => { if ( base . address === token . address ) { return null ; } return [ base , token ] ; } ) . filter ( ( p ) : p is [ Token , Token ] => p !== null ) ) ; } ) : [ ] , [ tokens , chainId ] ) ; const savedSerializedPairs = useSelector < AppState , AppState [ 'user' ] [ 'pairs' ] > ( ( { user : { pairs } } ) => pairs ) ; const userPairs = useMemo ( ( ) => { if ( ! chainId || ! savedSerializedPairs ) return [ ] ; const forChain = savedSerializedPairs [ chainId ] ; if ( ! forChain ) return [ ] ; return Object . keys ( forChain ) . map ( ( pairId ) => { return [ deserializeToken ( forChain [ pairId ] . token0 ) , deserializeToken ( forChain [ pairId ] . token1 ) ] ; } ) ; } , [ savedSerializedPairs , chainId ] ) ; const combinedList = useMemo ( ( ) => userPairs . concat ( generatedPairs ) . concat ( pinnedPairs ) , [ generatedPairs , pinnedPairs , userPairs , ] ) ; return useMemo ( ( ) => { const keyed = combinedList . reduce < { [ key ] : [ Token , Token ] ; } > ( ( memo , [ tokenA , tokenB ] ) => { const sorted = tokenA . sortsBefore ( tokenB ) ; const key = sorted ? `${ tokenA . address }:${ tokenB . address }` : `${ tokenB . address }:${ tokenA . address }` ; if ( memo [ key ] ) return memo ; memo [ key ] = sorted ? [ tokenA , tokenB ] : [ tokenB , tokenA ] ; return memo ; } , { } ) ; return Object . keys ( keyed ) . map ( ( key ) => keyed [ key ] ) ; } , [ combinedList ] ) ; }