Skip to content

Commit

Permalink
fix: send not enough funds error handling at submit time (#8076)
Browse files Browse the repository at this point in the history
Co-authored-by: woody <[email protected]>
  • Loading branch information
NeOMakinG and woodenfurniture authored Nov 4, 2024
1 parent 4f91d68 commit 740476f
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/components/Modals/Send/hooks/useFormSend/useFormSend.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 740476f

Please sign in to comment.