Skip to content

Commit 271e8fe

Browse files
committed
chore: improve low gas balance message on Solana
1 parent 674e14e commit 271e8fe

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

src/utils/errors.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import {
1515
} from 'telemetry/types';
1616
import { InsufficientFundsForGasError } from 'sdklegacy';
1717
import { routes, amount as sdkAmount } from '@wormhole-foundation/sdk';
18+
import { chainDisplayName, getGasToken, getTokenSymbol } from 'utils';
1819

1920
// TODO SDKV2
2021
// attempt to capture errors using regex
2122
export const INSUFFICIENT_ALLOWANCE_REGEX = /insufficient token allowance/im;
2223
export const INSUFFICIENT_LAMPORTS_REGEX =
2324
/insufficient lamports.*?(\d+).*?(\d+)/im;
25+
export const SIMULATION_ACCOUNT_NOT_FOUND_REGEX =
26+
/simulation failed:.*accountnotfound/i;
2427
export const USER_REJECTED_REGEX = new RegExp(
2528
'user rejected|rejected the request|rejected from user|user cancel|aborted by user|plugin closed|denied request signature',
2629
'mi',
@@ -50,7 +53,8 @@ export function interpretTransferError(
5053
uiErrorMessage = 'Transfer timed out, please try again';
5154
internalErrorCode = ERR_TIMEOUT;
5255
} else if (InsufficientFundsForGasError.MESSAGE_REGEX.test(e?.message)) {
53-
uiErrorMessage = 'Insufficient funds';
56+
uiErrorMessage =
57+
'Insufficient funds for network fees, please add more funds and try again';
5458
internalErrorCode = ERR_INSUFFICIENT_GAS;
5559
} else if (USER_REJECTED_REGEX.test(e?.message)) {
5660
uiErrorMessage = 'Transfer rejected in wallet, please try again';
@@ -74,17 +78,20 @@ export function interpretTransferError(
7478
: '';
7579
uiErrorMessage = `Amount exceeds Circle limit${limitString}. Please reduce transfer amount.`;
7680
internalErrorCode = ERR_AMOUNT_TOO_LARGE;
77-
} else if (INSUFFICIENT_LAMPORTS_REGEX.test(e?.message)) {
78-
const lamportMatches = e?.message.match(INSUFFICIENT_LAMPORTS_REGEX);
79-
const currentLamports = lamportMatches?.[1];
80-
const requiredLamports = lamportMatches?.[2];
81-
uiErrorMessage = `Insufficient funds for gas.`;
82-
if (currentLamports && requiredLamports) {
83-
const currentAmount = sdkAmount.fromBaseUnits(currentLamports, 9);
84-
const requiredAmount = sdkAmount.fromBaseUnits(requiredLamports, 9);
85-
uiErrorMessage += ` Current balance is ${sdkAmount.display(
86-
currentAmount,
87-
)} SOL, but required ${sdkAmount.display(requiredAmount)} SOL`;
81+
} else if (
82+
SIMULATION_ACCOUNT_NOT_FOUND_REGEX.test(e?.message) ||
83+
INSUFFICIENT_LAMPORTS_REGEX.test(e?.message)
84+
) {
85+
const gasChain = transferDetails.fromChain;
86+
try {
87+
const gasToken = getGasToken(gasChain);
88+
const gasSymbol = getTokenSymbol(gasToken);
89+
const chainName = chainDisplayName(gasChain);
90+
const chainSuffix = chainName ? ` on ${chainName}` : '';
91+
uiErrorMessage = `Insufficient ${gasSymbol} for fees${chainSuffix}, please add more ${gasSymbol} and try again`;
92+
} catch {
93+
uiErrorMessage =
94+
'Insufficient funds for network fees, please add more funds and try again';
8895
}
8996
internalErrorCode = ERR_INSUFFICIENT_GAS;
9097
}

0 commit comments

Comments
 (0)