1+ import z from "zod" ;
2+ import { getChainMetadata } from "../../../chains/utils.js" ;
13import { isNativeTokenAddress } from "../../../constants/addresses.js" ;
24import type { BaseTransactionOptions } from "../../../transaction/types.js" ;
35import { name } from "../../common/read/name.js" ;
46import { symbol } from "../../common/read/symbol.js" ;
57import { decimals } from "../__generated__/IERC20/read/decimals.js" ;
68
9+ const NATIVE_CURRENCY_SCHEMA = z . object ( {
10+ name : z . string ( ) . default ( "Ether" ) ,
11+ symbol : z . string ( ) . default ( "ETH" ) ,
12+ decimals : z . number ( ) . default ( 18 ) ,
13+ } ) ;
14+
715/**
816 * @extension ERC20
917 */
@@ -30,13 +38,18 @@ export async function getCurrencyMetadata(
3038) : Promise < GetCurrencyMetadataResult > {
3139 // if the contract is the native token, return the native currency metadata
3240 if ( isNativeTokenAddress ( options . contract . address ) ) {
33- return {
34- decimals : 18 ,
35- name : "Ether" ,
36- symbol : "ETH" ,
37- // overwrite with native currency of the chain if available
38- ...options . contract . chain . nativeCurrency ,
39- } ;
41+ // if the chain definition does not have a native currency, attempt to fetch it from the API
42+ if ( ! options . contract . chain . nativeCurrency ) {
43+ try {
44+ const chain = await getChainMetadata ( options . contract . chain ) ;
45+ // return the native currency of the chain
46+ return NATIVE_CURRENCY_SCHEMA . parse ( chain . nativeCurrency ) ;
47+ } catch {
48+ // no-op, fall through to the default values below
49+ }
50+ }
51+
52+ return NATIVE_CURRENCY_SCHEMA . parse ( options . contract . chain . nativeCurrency ) ;
4053 }
4154
4255 try {
0 commit comments