import React , { useContext , useMemo } from "react" ; import { TokenInfo } from "@solana/spl-token-registry" ; import { SOL_MINT } from "../utils/pubkeys" ; type TokenListContext = { tokenMap : Map < string , TokenInfo > ; wormholeMap : Map < string , TokenInfo > ; solletMap : Map < string , TokenInfo > ; swappableTokens : TokenInfo [ ] ; swappableTokensSollet : TokenInfo [ ] ; swappableTokensWormhole : TokenInfo [ ] ; } ; const _TokenListContext = React . createContext < null | TokenListContext > ( null ) ; export const SPL_REGISTRY_SOLLET_TAG = "wrapped-sollet" ; export const SPL_REGISTRY_WORM_TAG = "wormhole" ; const SOL_TOKEN_INFO = { chainId : 101 , address : SOL_MINT . toString ( ) , name : "Native SOL" , decimals : "9" , symbol : "SOL" , logoURI : "https://cdn.jsdelivr.net/gh/trustwallet/assets@master/blockchains/solana/info/logo.png" , tags : [ ] , extensions : { website : "https://solana.com/" , serumV3Usdc : "9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT" , serumV3Usdt : "HWHvQhFmJB3NUcu1aihKmrKegfVxBEHzwVX6yZCKEsi1" , coingeckoId : "solana" , waterfallbot : "https://t.me/SOLwaterfall" , } , } ; export function TokenListContextProvider ( props ) { const tokenList = useMemo ( ( ) => { const list = props . tokenList . filterByClusterSlug ( "mainnet-beta" ) . getList ( ) ; list . push ( SOL_TOKEN_INFO ) ; return list ; } , [ props . tokenList ] ) ; const tokenMap = useMemo ( ( ) => { const tokenMap = new Map ( ) ; tokenList . forEach ( ( t ) => { tokenMap . set ( t . address , t ) ; } ) ; return tokenMap ; } , [ tokenList ] ) ; const swappableTokens = useMemo ( ( ) => { const tokens = tokenList . filter ( ( t ) => { const isUsdxQuoted = t . extensions ?. serumV3Usdt || t . extensions ?. serumV3Usdc ; return isUsdxQuoted ; } ) ; tokens . sort ( ( a , b ) => a . symbol < b . symbol ? - 1 : a . symbol > b . symbol ? 1 : 0 ) ; return tokens ; } , [ tokenList , tokenMap ] ) ; const [ swappableTokensSollet , solletMap ] = useMemo ( ( ) => { const tokens = tokenList . filter ( ( t ) => { const isSollet = t . tags ?. includes ( SPL_REGISTRY_SOLLET_TAG ) ; return isSollet ; } ) ; tokens . sort ( ( a , b ) => a . symbol < b . symbol ? - 1 : a . symbol > b . symbol ? 1 : 0 ) ; return [ tokens , new Map < string , TokenInfo > ( tokens . map ( ( t ) => [ t . address , t ] ) ) , ] ; } , [ tokenList ] ) ; const [ swappableTokensWormhole , wormholeMap ] = useMemo ( ( ) => { const tokens = tokenList . filter ( ( t ) => { const isSollet = t . tags ?. includes ( SPL_REGISTRY_WORM_TAG ) ; return isSollet ; } ) ; tokens . sort ( ( a , b ) => a . symbol < b . symbol ? - 1 : a . symbol > b . symbol ? 1 : 0 ) ; return [ tokens , new Map < string , TokenInfo > ( tokens . map ( ( t ) => [ t . address , t ] ) ) , ] ; } , [ tokenList ] ) ; return ( < _TokenListContext . Provider value = { { tokenMap , wormholeMap , solletMap , swappableTokens , swappableTokensWormhole , swappableTokensSollet , } } > { props . children } < /_TokenListContext.Provider>); } export function useTokenListContext ( ) { const ctx = useContext ( _TokenListContext ) ; if ( ctx === null ) { throw new Error ( "Context not available" ) ; } return ctx ; } export function useTokenMap ( ) { const { tokenMap } = useTokenListContext ( ) ; return tokenMap ; } export function useSwappableTokens ( ) { const { swappableTokens , swappableTokensWormhole , swappableTokensSollet } = useTokenListContext ( ) ; return { swappableTokens , swappableTokensWormhole , swappableTokensSollet } ; }