1+ import { getChainById } from "@/constants/chains" ;
12import { getInstance } from "@/core/uniDevKitV4Factory" ;
23import { Token } from "@uniswap/sdk-core" ;
3- import { type Address , erc20Abi } from "viem" ;
4+ import { type Address , erc20Abi , zeroAddress } from "viem" ;
45
56/**
67 * Retrieves Token instances for a list of token addresses on a specific chain.
@@ -18,18 +19,22 @@ export async function getTokens({
1819 chainId ?: number ;
1920} ) : Promise < Token [ ] | null > {
2021 const sdk = getInstance ( chainId ) ;
22+ const currentChainId = chainId || sdk . getChainId ( ) ;
23+ const chain = getChainById ( currentChainId ) ;
2124
2225 if ( ! sdk ) {
2326 throw new Error ( "SDK not found. Please create an instance first." ) ;
2427 }
2528
2629 const client = sdk . getClient ( ) ;
2730
28- const calls = addresses . flatMap ( ( address ) => [
29- { address, abi : erc20Abi , functionName : "symbol" } ,
30- { address, abi : erc20Abi , functionName : "name" } ,
31- { address, abi : erc20Abi , functionName : "decimals" } ,
32- ] ) ;
31+ const calls = addresses
32+ . filter ( ( address ) => address !== zeroAddress ) // filter out native currency
33+ . flatMap ( ( address ) => [
34+ { address, abi : erc20Abi , functionName : "symbol" } ,
35+ { address, abi : erc20Abi , functionName : "name" } ,
36+ { address, abi : erc20Abi , functionName : "decimals" } ,
37+ ] ) ;
3338
3439 try {
3540 const results = await client . multicall ( {
@@ -41,12 +46,25 @@ export async function getTokens({
4146 let resultIndex = 0 ;
4247
4348 for ( const address of addresses ) {
44- const symbol = results [ resultIndex ++ ] as string ;
45- const name = results [ resultIndex ++ ] as string ;
46- const decimals = results [ resultIndex ++ ] as number ;
47- tokens . push (
48- new Token ( sdk . getChainId ( ) , address , decimals , symbol , name ) as Token ,
49- ) ;
49+ if ( address === zeroAddress ) {
50+ // For native currency, use chain data from wagmi
51+ const nativeCurrency = chain . nativeCurrency ;
52+ tokens . push (
53+ new Token (
54+ currentChainId ,
55+ address ,
56+ nativeCurrency . decimals ,
57+ nativeCurrency . symbol ,
58+ nativeCurrency . name ,
59+ ) ,
60+ ) ;
61+ } else {
62+ // For ERC20 tokens, use multicall results
63+ const symbol = results [ resultIndex ++ ] as string ;
64+ const name = results [ resultIndex ++ ] as string ;
65+ const decimals = results [ resultIndex ++ ] as number ;
66+ tokens . push ( new Token ( currentChainId , address , decimals , symbol , name ) ) ;
67+ }
5068 }
5169
5270 return tokens ;
0 commit comments