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

fix: send not enough funds error handling at submit time #8076

Merged
merged 2 commits into from
Nov 4, 2024
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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

💜

if (jsonError.code === -32000) {
woodenfurniture marked this conversation as resolved.
Show resolved Hide resolved
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
Loading