11import { useEffect , useState } from "react" ;
2- import { WalletToken , useNetworkContext } from "@waveshq/walletkit-ui" ;
2+ import {
3+ WalletToken ,
4+ useNetworkContext ,
5+ useWhaleApiClient ,
6+ } from "@waveshq/walletkit-ui" ;
37import { utils } from "ethers" ;
48import { batch , useSelector } from "react-redux" ;
59import { RootState } from "@store" ;
@@ -12,13 +16,15 @@ import { useEVMProvider } from "@contexts/EVMProvider";
1216import { useIsFocused } from "@react-navigation/native" ;
1317import { getAddressFromDST20TokenId } from "@api/transaction/transfer_domain" ;
1418import { useCustomServiceProviderContext } from "@contexts/CustomServiceProvider" ;
19+ import { getPaginatedResponse } from "@waveshq/walletkit-core" ;
1520
1621interface AssociatedToken {
1722 [ key : string ] : TokenData ;
1823}
1924
2025export function useEvmTokenBalances ( ) : { evmTokens : WalletToken [ ] } {
2126 const { evmAddress } = useWalletContext ( ) ;
27+ const client = useWhaleApiClient ( ) ;
2228 const [ evmTokens , setEvmTokens ] = useState < WalletToken [ ] > ( [ ] ) ;
2329 const [ allTokensWithAddress , setAllTokensWithAddress ] =
2430 useState < AssociatedToken > ( { } ) ;
@@ -29,14 +35,25 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } {
2935 const logger = useLogger ( ) ;
3036 const isFocused = useIsFocused ( ) ;
3137
32- const { allTokens } = useSelector ( ( state : RootState ) => state . wallet ) ;
33- const tokenIds = Object . keys ( allTokens ) . reduce ( ( current : string [ ] , key ) => {
34- const token = allTokens [ key ] ;
38+ const [ allTokens , setAllTokens ] = useState < TokenData [ ] > ( [ ] ) ;
39+ const getAllTokens = async ( ) => {
40+ const allTokens : TokenData [ ] = await getPaginatedResponse < TokenData > (
41+ ( limit , next ) => client . tokens . list ( limit , next ) ,
42+ ) ;
43+ setAllTokens ( allTokens ?. filter ( ( token ) => token . isDAT ) ) ;
44+ } ;
45+
46+ useEffect ( ( ) => {
47+ void getAllTokens ( ) ;
48+ } , [ isFocused ] ) ;
49+
50+ const tokenIds = allTokens . reduce ( ( current : string [ ] , token ) => {
3551 if ( token . id !== "0" && token . isDAT && ! token . isLPS ) {
3652 return [ ...current , token . id ] ;
3753 }
3854 return current ;
3955 } , [ ] ) ;
56+
4057 const { evmWalletDetails, evmTokenBalances } = useSelector (
4158 ( state : RootState ) => state . evm ,
4259 ) ;
@@ -114,8 +131,7 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } {
114131
115132 useEffect ( ( ) => {
116133 setAllTokensWithAddress (
117- Object . keys ( allTokens ) . reduce ( ( current , each ) => {
118- const tokenDetails = allTokens [ each ] ;
134+ allTokens ?. reduce ( ( current , tokenDetails ) => {
119135 const key = getAddressFromDST20TokenId ( tokenDetails . id ) ;
120136 return Object . assign ( current , { [ key ] : tokenDetails } ) ;
121137 } , { } ) ,
0 commit comments