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

Add a filter for insufficient funds #3627

Merged
merged 1 commit into from
Jul 5, 2023
Merged
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
34 changes: 20 additions & 14 deletions src/composables/useTransactionErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { useI18n } from 'vue-i18n';
import { TransactionError } from '@/types/transactions';

export function isUserRejected(error): boolean {
if (!error) return false;

const userRejectionMessages = [
const messages = [
/user rejected transaction/,
/request rejected/,
/user rejected methods./,
Expand All @@ -19,42 +17,49 @@ export function isUserRejected(error): boolean {
/user disapproved requested methods/,
];

return isErrorType(error, messages);
}

export function isUserNotEnoughGas(error): boolean {
const messages = [/insufficient funds for gas/];

return isErrorType(error, messages);
}

function isErrorType(error, messages: RegExp[]): boolean {
if (!error) return false;

if (
typeof error === 'string' &&
userRejectionMessages.some(msg => msg.test(error.toLowerCase()))
messages.some(msg => msg.test(error.toLowerCase()))
)
return true;

if (
error.message &&
userRejectionMessages.some(msg => msg.test(error.message.toLowerCase()))
messages.some(msg => msg.test(error.message.toLowerCase()))
)
return true;

if (
typeof error.reason === 'string' &&
userRejectionMessages.some(msg => msg.test(error.reason.toLowerCase()))
messages.some(msg => msg.test(error.reason.toLowerCase()))
)
return true;

if (
error.cause?.message &&
userRejectionMessages.some(msg =>
msg.test(error.cause.message.toLowerCase())
)
messages.some(msg => msg.test(error.cause.message.toLowerCase()))
)
return true;

if (
typeof error.cause === 'string' &&
userRejectionMessages.some(msg => msg.test(error.cause.toLowerCase()))
messages.some(msg => msg.test(error.cause.toLowerCase()))
)
return true;

if (
error.b &&
userRejectionMessages.some(msg => msg.test(error.b.toLowerCase()))
)
if (error.b && messages.some(msg => msg.test(error.b.toLowerCase())))
return true;

if (error?.code && error.code === 4001) {
Expand Down Expand Up @@ -102,6 +107,7 @@ export default function useTransactionErrors() {
*/
function parseError(error): TransactionError | null {
if (isUserRejected(error)) return null; // User rejected transaction
if (isUserNotEnoughGas(error)) return null; // User does not have enough gas
if (error?.code && error.code === 'UNPREDICTABLE_GAS_LIMIT')
return cannotEstimateGasError;

Expand Down