From 740476f524cd18963231e23923fe3d6e970a90c1 Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:03:06 +0800 Subject: [PATCH] fix: send not enough funds error handling at submit time (#8076) Co-authored-by: woody <125113430+woodenfurniture@users.noreply.github.com> --- .../Send/hooks/useFormSend/useFormSend.tsx | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx b/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx index 0edcc8880cb..01f3ed1672d 100644 --- a/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx +++ b/src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx @@ -1,9 +1,12 @@ import { ExternalLinkIcon } from '@chakra-ui/icons' import { Link, Text, useToast } from '@chakra-ui/react' +import { fromAssetId } from '@shapeshiftoss/caip' +import type { ResponseError } from '@shapeshiftoss/unchained-client/src/generated/arbitrum' import { useCallback } from 'react' import { useTranslate } from 'react-polyglot' +import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton' import { useWallet } from 'hooks/useWallet/useWallet' -import { selectAssetById } from 'state/slices/selectors' +import { selectAssetById, selectFeeAssetById } from 'state/slices/selectors' import { store } from 'state/store' import type { SendInput } from '../../Form' @@ -57,6 +60,39 @@ export const useFormSend = () => { // If we're here, we know asset is defined const asset = selectAssetById(store.getState(), sendInput.assetId)! console.error(e) + + if ((e as Error).name === 'ResponseError') { + const error = await (e as ResponseError).response.json() + const jsonError = JSON.parse(error.message) + + const chainAdapterManager = getChainAdapterManager() + + const feeAssetId = chainAdapterManager + .get(fromAssetId(asset.assetId).chainId) + ?.getFeeAssetId() + + const feeAsset = selectFeeAssetById(store.getState(), feeAssetId ?? '') + + // @TODO: as this is the first time we are handling errors from unchained in this scope, we dont have a complete error handling strategy + // If we need to handle more errors, we should create a proper error handler + if (jsonError.code === -32000) { + toast({ + title: translate('modals.send.errorTitle', { + asset: asset.name, + }), + description: translate('modals.send.errors.notEnoughNativeToken', { + asset: feeAsset?.symbol, + }), + status: 'error', + duration: 9000, + isClosable: true, + position: 'top-right', + }) + } + + return + } + toast({ title: translate('modals.send.errorTitle', { asset: asset.name,