Skip to content

Commit

Permalink
feat: Add function to retrieve native currency name
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Feb 14, 2024
1 parent dd428d5 commit e6057f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
30 changes: 29 additions & 1 deletion packages/thirdweb/src/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,38 @@ export async function getChainDecimals(chain: Chain): Promise<number> {
return 18;
});
}
// if we have a symbol, return it
// if we have decimals, return it
return chain.nativeCurrency.decimals;
}

/**
* Retrieves the name of the native currency for a given chain.
* If the chain object does not have a native currency name, it attempts to fetch the chain data and retrieve the native currency name from there.
* If fetching the chain data fails, it falls back to returning "ETH".
* @param chain The chain object for which to retrieve the native currency name.
* @returns A promise that resolves to the native currency name.
* @internal
*/
export async function getChainNativeCurrencyName(
chain: Chain,
): Promise<string> {
if (
typeof chain === "bigint" ||
typeof chain === "number" ||
!chain.nativeCurrency?.name
) {
const chainId = getChainIdFromChain(chain);
return getChainDataForChainId(chainId)
.then((data) => data.nativeCurrency.name)
.catch(() => {
// if we fail to fetch the chain data, return 18 as a fallback (most likely it's 18)
return "ETH";
});
}
// if we have a name, return it
return chain.nativeCurrency.name;
}

type FetchChainResponse =
| {
data: ApiChain;
Expand Down
15 changes: 10 additions & 5 deletions packages/thirdweb/src/wallets/utils/getTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getChainSymbol,
type Chain,
getChainDecimals,
getChainNativeCurrencyName,
} from "../../chain/index.js";
import type { ThirdwebClient } from "../../client/client.js";
import { getContract } from "../../contract/index.js";
Expand All @@ -24,6 +25,7 @@ type GetTokenBalanceResult = {
decimals: number;
displayValue: string;
symbol: string;
name: string;
};

/**
Expand Down Expand Up @@ -54,16 +56,19 @@ export async function getTokenBalance(
// native token case
const rpcRequest = getRpcClient({ client, chain });

const [nativeSymbol, nativeDecimals, nativeBalance] = await Promise.all([
getChainSymbol(chain),
getChainDecimals(chain),
eth_getBalance(rpcRequest, { address: account.address }),
]);
const [nativeSymbol, nativeDecimals, nativeName, nativeBalance] =
await Promise.all([
getChainSymbol(chain),
getChainDecimals(chain),
getChainNativeCurrencyName(chain),
eth_getBalance(rpcRequest, { address: account.address }),
]);

return {
value: nativeBalance,
decimals: nativeDecimals,
displayValue: formatUnits(nativeBalance, nativeDecimals),
symbol: nativeSymbol,
name: nativeName,
};
}

0 comments on commit e6057f9

Please sign in to comment.