Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Initial Decimal Display Issue and Improve Gas Payment Formatting in Hyperlane Explorer #138

Merged
merged 6 commits into from
Nov 14, 2024
Merged
14 changes: 10 additions & 4 deletions src/features/messages/cards/GasDetailsCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js';
import { utils } from 'ethers';
import Image from 'next/image';
import { useMemo, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';

import { fromWei, toTitleCase } from '@hyperlane-xyz/utils';
import { Tooltip } from '@hyperlane-xyz/widgets';
Expand Down Expand Up @@ -29,14 +29,20 @@ export function GasDetailsCard({ message, blur, igpPayments = {} }: Props) {
const unitOptions = useMemo(() => {
const originMetadata = multiProvider.tryGetChainMetadata(message.originDomainId);
const nativeCurrencyName = originMetadata?.nativeToken?.symbol || 'Eth';
const nativeDecimals = originMetadata?.nativeToken?.decimals || 18;
return [
{ value: 18, display: toTitleCase(nativeCurrencyName) },
{ value: nativeDecimals, display: toTitleCase(nativeCurrencyName) },
{ value: 9, display: 'Gwei' },
{ value: 0, display: 'Wei' },
];
}, [message, multiProvider]);

const [decimals, setDecimals] = useState(unitOptions[0].value);
const [decimals, setDecimals] = useState<number>(unitOptions[0].value);

// Update `decimals` after the component mounts to ensure it matches the initial `unitOptions` value
useEffect(() => {
setDecimals(unitOptions[0].value);
}, [unitOptions]);
jmrossy marked this conversation as resolved.
Show resolved Hide resolved

const { totalGasAmount, paymentFormatted, numPayments, avgPrice, paymentsWithAddr } =
useMemo(() => {
Expand Down Expand Up @@ -179,7 +185,7 @@ function computeAvgGasPrice(
const gasBN = new BigNumber(gasAmount);
const paymentBN = new BigNumber(payment);
if (gasBN.isZero() || paymentBN.isZero()) return null;
const wei = paymentBN.div(gasAmount).toFixed(0);
const wei = paymentBN.div(gasBN).toFixed(0);
const formatted = utils.formatUnits(wei, decimals).toString();
return { wei, formatted };
} catch (error) {
Expand Down
Loading