Skip to content
Open
Changes from all 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
27 changes: 25 additions & 2 deletions app/features/instruction-simulation/model/use-simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ import { getMintDecimals, isTokenProgramBase58 } from '../lib/tokenAccountParsin
import type { SolBalanceChange } from '../lib/types';
import { useEpochInfo } from './use-epoch-info';

function parseSimulationError(err: object | string | null, logs?: string[]): string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for your contribution, but the current approach looks to me like an ad hoc solution.
I'd prefer to see a generalised approach to adopt @solana/errors, for example. Tests are also always welcome

if (!err) return '';

if (typeof err === 'string') {
if (err.includes('InsufficientFundsForFee')) {
return 'Insufficient funds for transaction fee';
}
return err;
}

if (typeof err === 'object') {
if ('InsufficientFundsForFee' in err) {
return 'Insufficient funds for transaction fee';
}
return JSON.stringify(err);
}

if (logs?.some(l => l.includes('InsufficientFundsForFee'))) {
return 'Insufficient funds for transaction fee';
}

return 'Transaction failed';
}

export function useSimulation(
message: VersionedMessage,
accountBalances?: {
Expand Down Expand Up @@ -223,7 +247,6 @@ export function useSimulation(

if (resp.value.logs.length === 0 && typeof resp.value.err === 'string') {
setLogs(null);
setError(resp.value.err);
} else {
// Prettify logs
setLogs(parseProgramLogs(resp.value.logs, resp.value.err, cluster));
Expand All @@ -234,7 +257,7 @@ export function useSimulation(
}
// If the response has an error, the logs will say what it it, so no need to parse here.
if (resp.value.err) {
setError('TransactionError');
setError(parseSimulationError(resp.value.err, resp.value.logs ?? undefined));
}
} catch (err) {
Logger.error(err);
Expand Down