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 wrong native asset being displayed on SignTransactionSheet #6395

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/components/Transactions/TransactionSimulationCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '@/components/Transactions/constants';
import { ChainId } from '@/state/backendNetworks/types';
import { useBackendNetworksStore } from '@/state/backendNetworks/backendNetworks';
import { ParsedAddressAsset } from '@/entities/tokens';

interface TransactionSimulationCardProps {
chainId: ChainId;
Expand All @@ -41,12 +42,7 @@ interface TransactionSimulationCardProps {
simulation: TransactionSimulationResult | undefined;
simulationError: TransactionErrorType | undefined;
simulationScanResult: TransactionScanResultType | undefined;
walletBalance: {
amount: string | number;
display: string;
isLoaded: boolean;
symbol: string;
};
nativeAsset: ParsedAddressAsset | ReturnType<ReturnType<typeof useBackendNetworksStore.getState>['getChainsNativeAsset']>[ChainId];
}

export const TransactionSimulationCard = ({
Expand All @@ -60,7 +56,7 @@ export const TransactionSimulationCard = ({
simulation,
simulationError,
simulationScanResult,
walletBalance,
nativeAsset,
}: TransactionSimulationCardProps) => {
const cardHeight = useSharedValue(COLLAPSED_CARD_HEIGHT);
const contentHeight = useSharedValue(COLLAPSED_CARD_HEIGHT - CARD_BORDER_WIDTH * 2);
Expand Down Expand Up @@ -164,7 +160,7 @@ export const TransactionSimulationCard = ({
return i18n.t(i18n.l.walletconnect.simulation.simulation_card.titles.simulating);
}
if (isBalanceEnough === false) {
return i18n.t(i18n.l.walletconnect.simulation.simulation_card.titles.not_enough_native_balance, { symbol: walletBalance?.symbol });
return i18n.t(i18n.l.walletconnect.simulation.simulation_card.titles.not_enough_native_balance, { symbol: nativeAsset.symbol });
}
if (txSimulationApiError || isPersonalSignRequest) {
return i18n.t(i18n.l.walletconnect.simulation.simulation_card.titles.simulation_unavailable);
Expand All @@ -183,14 +179,14 @@ export const TransactionSimulationCard = ({
}
return i18n.t(i18n.l.walletconnect.simulation.simulation_card.titles.simulation_result);
}, [
isBalanceEnough,
isLoading,
isBalanceEnough,
txSimulationApiError,
isPersonalSignRequest,
simulationScanResult,
noChanges,
simulationError,
simulationScanResult,
isPersonalSignRequest,
txSimulationApiError,
walletBalance?.symbol,
nativeAsset.symbol,
]);

const isExpanded = useMemo(() => {
Expand Down Expand Up @@ -270,7 +266,7 @@ export const TransactionSimulationCard = ({
{isBalanceEnough === false ? (
<Text color="labelQuaternary" size="13pt" weight="semibold">
{i18n.t(i18n.l.walletconnect.simulation.simulation_card.messages.need_more_native, {
symbol: walletBalance?.symbol,
symbol: nativeAsset.symbol,
network: useBackendNetworksStore.getState().getChainsName()[chainId],
})}
</Text>
Expand Down
25 changes: 17 additions & 8 deletions src/screens/SignTransactionSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const SignTransactionSheet = () => {
const addressToUse = specifiedAddress ?? accountAddress;

const provider = getProvider({ chainId });
const nativeAsset = ethereumUtils.getNetworkNativeAsset({ chainId });
const nativeAsset =
ethereumUtils.getNetworkNativeAsset({ chainId }) ?? useBackendNetworksStore.getState().getChainsNativeAsset()[chainId];
Comment on lines +110 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, is this fallback needed for this specific situation or should it be done like this in other places where you need the native asset? If the latter would be nice to add it directly to the ethereumUtils.getNetworkNativeAsset so this doesn't have to be re-done in other places.

Copy link
Contributor Author

@walmat walmat Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it depends on the scenario. The first call only grabs the native asset (with balance), and the second just grabs the base info from the backend. In this case, we can utilize both, but some places require it with balance so it doesn't necessarily make sense to have the fallback


const isMessageRequest = isMessageDisplayType(transactionDetails.payload.method);
const isPersonalSignRequest = isPersonalSign(transactionDetails.payload.method);
Expand Down Expand Up @@ -135,13 +136,21 @@ export const SignTransactionSheet = () => {
}, [isMessageRequest, transactionDetails?.displayDetails?.request, nativeAsset]);

const walletBalance = useMemo(() => {
if (typeof nativeAsset === 'object' && 'balance' in nativeAsset) {
return {
amount: nativeAsset?.balance?.amount || 0,
display: nativeAsset?.balance?.display || `0 ${nativeAsset?.symbol}`,
isLoaded: nativeAsset?.balance?.display !== undefined,
symbol: nativeAsset?.symbol || 'ETH',
Comment on lines +141 to +144
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these optional chains aren't necessary given the condition

};
}
return {
amount: nativeAsset?.balance?.amount || 0,
display: nativeAsset?.balance?.display || `0 ${nativeAsset?.symbol}`,
isLoaded: nativeAsset?.balance?.display !== undefined,
amount: 0,
display: `0 ${nativeAsset?.symbol}`,
isLoaded: false,
walmat marked this conversation as resolved.
Show resolved Hide resolved
symbol: nativeAsset?.symbol || 'ETH',
};
}, [nativeAsset?.balance?.amount, nativeAsset?.balance?.display, nativeAsset?.symbol]);
}, [nativeAsset]);

const { gasLimit, isValidGas, startPollingGasFees, stopPollingGasFees, updateTxFee, selectedGasFee, gasFeeParamsBySpeed } = useGas();

Expand Down Expand Up @@ -573,11 +582,11 @@ export const SignTransactionSheet = () => {
}

if (!txSimulationLoading && isBalanceEnough === false) {
return i18n.t(i18n.l.walletconnect.simulation.buttons.buy_native_token, { symbol: walletBalance?.symbol });
return i18n.t(i18n.l.walletconnect.simulation.buttons.buy_native_token, { symbol: nativeAsset.symbol });
}

return i18n.t(i18n.l.walletconnect.simulation.buttons.confirm);
}, [txSimulationLoading, isBalanceEnough, isAuthorizing, walletBalance]);
}, [isAuthorizing, txSimulationLoading, isBalanceEnough, nativeAsset.symbol]);

const primaryActionButtonColor = useMemo(() => {
let color = colors.appleBlue;
Expand Down Expand Up @@ -681,7 +690,7 @@ export const SignTransactionSheet = () => {
simulation={simulationResult?.simulationData}
simulationError={simulationResult?.simulationError}
simulationScanResult={simulationResult?.simulationScanResult}
walletBalance={walletBalance}
nativeAsset={nativeAsset}
/>
{isMessageRequest ? (
<TransactionMessageCard
Expand Down
Loading