diff --git a/src/hooks/useApproveCallback.ts b/src/hooks/useApproveCallback.ts index b730a83126..6d6fc39e8e 100644 --- a/src/hooks/useApproveCallback.ts +++ b/src/hooks/useApproveCallback.ts @@ -1,5 +1,4 @@ import { MaxUint256 } from '@ethersproject/constants' -import { TransactionResponse } from '@ethersproject/providers' import { Currency, CurrencyAmount, TokenAmount } from '@kyberswap/ks-sdk-core' import { t } from '@lingui/macro' import JSBI from 'jsbi' diff --git a/src/pages/CreatePool/index.tsx b/src/pages/CreatePool/index.tsx index 98ded952d9..637a6c2958 100644 --- a/src/pages/CreatePool/index.tsx +++ b/src/pages/CreatePool/index.tsx @@ -9,6 +9,7 @@ import { AlertTriangle, Plus } from 'react-feather' import { Link, Navigate, useNavigate, useParams } from 'react-router-dom' import { Flex, Text } from 'rebass' +import { NotificationType } from 'components/Announcement/type' import { ButtonError, ButtonLight, ButtonPrimary } from 'components/Button' import { BlueCard, LightCard } from 'components/Card' import { AutoColumn, ColumnCenter } from 'components/Column' @@ -35,7 +36,7 @@ import useTheme from 'hooks/useTheme' import useTransactionDeadline from 'hooks/useTransactionDeadline' import DisclaimerERC20 from 'pages/AddLiquidityV2/components/DisclaimerERC20' import { Dots, Wrapper } from 'pages/Pool/styleds' -import { useWalletModalToggle } from 'state/application/hooks' +import { useNotify, useWalletModalToggle } from 'state/application/hooks' import { Field } from 'state/mint/actions' import { useDerivedMintInfo, useMintActionHandlers, useMintState } from 'state/mint/hooks' import { useDerivedPairInfo } from 'state/pair/hooks' @@ -47,6 +48,7 @@ import { StyledInternalLink, TYPE } from 'theme' import { calculateGasMargin, calculateSlippageAmount, formattedNum } from 'utils' import { currencyId } from 'utils/currencyId' import { feeRangeCalc, useCurrencyConvertedToNative } from 'utils/dmm' +import { friendlyError } from 'utils/errorMessage' import { getDynamicFeeRouterContract, getStaticFeeRouterContract } from 'utils/getContract' import isZero from 'utils/isZero' import { maxAmountSpend } from 'utils/maxAmountSpend' @@ -184,6 +186,7 @@ export default function CreatePool() { const addTransactionWithType = useTransactionAdder() const addPair = usePairAdderByTokens() + const notify = useNotify() async function onAdd() { // if (!pair) return @@ -300,6 +303,15 @@ export default function CreatePool() { if (!didUserReject(error)) { console.error(error) } + const message = friendlyError(error) + notify( + { + title: t`Create Classic Pool Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) }) } diff --git a/src/state/farms/elastic/hooks.ts b/src/state/farms/elastic/hooks.ts index 9d2b137203..6cfb568033 100644 --- a/src/state/farms/elastic/hooks.ts +++ b/src/state/farms/elastic/hooks.ts @@ -7,6 +7,7 @@ import { Interface } from 'ethers/lib/utils' import { useCallback, useMemo, useState } from 'react' import { useSearchParams } from 'react-router-dom' +import { NotificationType } from 'components/Announcement/type' import ELASTIC_FARM_ABI from 'constants/abis/v2/farm.json' import { ELASTIC_FARM_TYPE, FARM_TAB } from 'constants/index' import { CONTRACT_NOT_FOUND_MSG } from 'constants/messages' @@ -17,6 +18,7 @@ import { useTokens } from 'hooks/Tokens' import { useProAmmNFTPositionManagerContract, useProMMFarmContract } from 'hooks/useContract' import { usePools } from 'hooks/usePools' import { useProAmmPositionsFromTokenIds } from 'hooks/useProAmmPositions' +import { useNotify } from 'state/application/hooks' import { FarmingPool, NFTPosition, UserFarmInfo, UserInfo } from 'state/farms/elastic/types' import { useAppSelector } from 'state/hooks' import { getPoolAddress } from 'state/mint/proamm/utils' @@ -29,6 +31,7 @@ import { } from 'state/transactions/type' import { PositionDetails } from 'types/position' import { calculateGasMargin, isAddressString } from 'utils' +import { friendlyError } from 'utils/errorMessage' import { defaultChainData } from '.' @@ -210,26 +213,40 @@ export const useFarmAction = (address: string) => { const addTransactionWithType = useTransactionAdder() const contract = useProMMFarmContract(address) const posManager = useProAmmNFTPositionManagerContract() + const notify = useNotify() const approve = useCallback(async () => { if (!posManager) { throw new Error(CONTRACT_NOT_FOUND_MSG) } - const estimateGas = await posManager.estimateGas.setApprovalForAll(address, true) - const tx = await posManager.setApprovalForAll(address, true, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.APPROVE, - extraInfo: { - summary: `Elastic Farm`, - contract: address, - }, - }) + try { + const estimateGas = await posManager.estimateGas.setApprovalForAll(address, true) + const tx = await posManager.setApprovalForAll(address, true, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.APPROVE, + extraInfo: { + summary: `Elastic Farm`, + contract: address, + }, + }) - return tx.hash - }, [addTransactionWithType, address, posManager]) + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Approve Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error + } + }, [addTransactionWithType, address, posManager, notify]) // Deposit const deposit = useCallback( @@ -238,24 +255,36 @@ export const useFarmAction = (address: string) => { if (!contract) { throw new Error(CONTRACT_NOT_FOUND_MSG) } + try { + const estimateGas = await contract.estimateGas.deposit(nftIds) + const tx = await contract.deposit(nftIds, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.ELASTIC_DEPOSIT_LIQUIDITY, + extraInfo: getTransactionExtraInfo( + positions, + positionDetails.map(e => e.poolId), + positionDetails.map(e => e.tokenId.toString()), + ), + }) - const estimateGas = await contract.estimateGas.deposit(nftIds) - const tx = await contract.deposit(nftIds, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.ELASTIC_DEPOSIT_LIQUIDITY, - extraInfo: getTransactionExtraInfo( - positions, - positionDetails.map(e => e.poolId), - positionDetails.map(e => e.tokenId.toString()), - ), - }) - - return tx.hash + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Deposit Farm Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error + } }, - [addTransactionWithType, contract], + [addTransactionWithType, contract, notify], ) const withdraw = useCallback( @@ -263,24 +292,37 @@ export const useFarmAction = (address: string) => { if (!contract) { throw new Error(CONTRACT_NOT_FOUND_MSG) } - const nftIds = positionDetails.map(e => e.tokenId) - const estimateGas = await contract.estimateGas.withdraw(nftIds) - const tx = await contract.withdraw(nftIds, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.ELASTIC_WITHDRAW_LIQUIDITY, - extraInfo: getTransactionExtraInfo( - positions, - positionDetails.map(e => e.poolId), - positionDetails.map(e => e.tokenId.toString()), - ), - }) + try { + const nftIds = positionDetails.map(e => e.tokenId) + const estimateGas = await contract.estimateGas.withdraw(nftIds) + const tx = await contract.withdraw(nftIds, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.ELASTIC_WITHDRAW_LIQUIDITY, + extraInfo: getTransactionExtraInfo( + positions, + positionDetails.map(e => e.poolId), + positionDetails.map(e => e.tokenId.toString()), + ), + }) - return tx.hash + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Withdraw Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error + } }, - [addTransactionWithType, contract], + [addTransactionWithType, contract, notify], ) const emergencyWithdraw = useCallback( @@ -288,19 +330,32 @@ export const useFarmAction = (address: string) => { if (!contract) { throw new Error(CONTRACT_NOT_FOUND_MSG) } - const estimateGas = await contract.estimateGas.emergencyWithdraw(nftIds) - const tx = await contract.emergencyWithdraw(nftIds, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.ELASTIC_FORCE_WITHDRAW_LIQUIDITY, - extraInfo: { contract: address }, - }) + try { + const estimateGas = await contract.estimateGas.emergencyWithdraw(nftIds) + const tx = await contract.emergencyWithdraw(nftIds, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.ELASTIC_FORCE_WITHDRAW_LIQUIDITY, + extraInfo: { contract: address }, + }) - return tx.hash + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Withdraw Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error + } }, - [addTransactionWithType, contract, address], + [addTransactionWithType, contract, address, notify], ) const depositAndJoin = useCallback( @@ -308,26 +363,38 @@ export const useFarmAction = (address: string) => { if (!contract) { throw new Error(CONTRACT_NOT_FOUND_MSG) } + try { + const nftIds = selectedNFTs.map(item => item.nftId) - const nftIds = selectedNFTs.map(item => item.nftId) - - const estimateGas = await contract.estimateGas.depositAndJoin(pid, nftIds) - const tx = await contract.depositAndJoin(pid, nftIds, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.STAKE, - extraInfo: getTransactionExtraInfo( - selectedNFTs.map(e => e.position), - selectedNFTs.map(e => e.poolAddress), - nftIds.map(e => e.toString()), - ), - }) + const estimateGas = await contract.estimateGas.depositAndJoin(pid, nftIds) + const tx = await contract.depositAndJoin(pid, nftIds, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.STAKE, + extraInfo: getTransactionExtraInfo( + selectedNFTs.map(e => e.position), + selectedNFTs.map(e => e.poolAddress), + nftIds.map(e => e.toString()), + ), + }) - return tx.hash + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Deposit Farm Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error + } }, - [addTransactionWithType, contract], + [addTransactionWithType, contract, notify], ) const stake = useCallback( @@ -335,27 +402,39 @@ export const useFarmAction = (address: string) => { if (!contract) { throw new Error(CONTRACT_NOT_FOUND_MSG) } + try { + const nftIds = selectedNFTs.map(item => item.nftId) + const liqs = selectedNFTs.map(item => BigNumber.from(item.position.liquidity.toString())) - const nftIds = selectedNFTs.map(item => item.nftId) - const liqs = selectedNFTs.map(item => BigNumber.from(item.position.liquidity.toString())) - - const estimateGas = await contract.estimateGas.join(pid, nftIds, liqs) - const tx = await contract.join(pid, nftIds, liqs, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.STAKE, - extraInfo: getTransactionExtraInfo( - selectedNFTs.map(e => e.position), - selectedNFTs.map(e => e.poolAddress), - nftIds.map(e => e.toString()), - ), - }) + const estimateGas = await contract.estimateGas.join(pid, nftIds, liqs) + const tx = await contract.join(pid, nftIds, liqs, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.STAKE, + extraInfo: getTransactionExtraInfo( + selectedNFTs.map(e => e.position), + selectedNFTs.map(e => e.poolAddress), + nftIds.map(e => e.toString()), + ), + }) - return tx.hash + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Stake Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error + } }, - [addTransactionWithType, contract], + [addTransactionWithType, contract, notify], ) const unstake = useCallback( @@ -381,11 +460,19 @@ export const useFarmAction = (address: string) => { }) return tx.hash - } catch (e) { - console.log(e) + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Unstake Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) } }, - [addTransactionWithType, contract], + [addTransactionWithType, contract, notify], ) const harvest = useCallback( @@ -419,11 +506,19 @@ export const useFarmAction = (address: string) => { } addTransactionWithType({ hash: tx.hash, type: TRANSACTION_TYPE.HARVEST, extraInfo }) return tx - } catch (e) { - console.log(e) + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Harvest Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) } }, - [addTransactionWithType, contract], + [addTransactionWithType, contract, notify], ) return { deposit, withdraw, approve, stake, unstake, harvest, emergencyWithdraw, depositAndJoin } diff --git a/src/state/farms/elasticv2/hooks.ts b/src/state/farms/elasticv2/hooks.ts index 80d1d1a915..7d2572b9a8 100644 --- a/src/state/farms/elasticv2/hooks.ts +++ b/src/state/farms/elasticv2/hooks.ts @@ -1,16 +1,20 @@ +import { t } from '@lingui/macro' import { useCallback, useMemo } from 'react' import { useSearchParams } from 'react-router-dom' import { useLocalStorage } from 'react-use' +import { NotificationType } from 'components/Announcement/type' import FarmV2ABI from 'constants/abis/v2/farmv2.json' import { ELASTIC_FARM_TYPE, FARM_TAB, SORT_DIRECTION } from 'constants/index' import { CONTRACT_NOT_FOUND_MSG } from 'constants/messages' import { useActiveWeb3React } from 'hooks' import { useContract, useProAmmNFTPositionManagerContract } from 'hooks/useContract' +import { useNotify } from 'state/application/hooks' import { useAppSelector } from 'state/hooks' import { useTransactionAdder } from 'state/transactions/hooks' import { TRANSACTION_TYPE } from 'state/transactions/type' import { calculateGasMargin, isAddressString } from 'utils' +import { friendlyError } from 'utils/errorMessage' import { defaultChainData } from '.' import { UserFarmV2Info } from './types' @@ -211,25 +215,38 @@ export const useFarmV2Action = (farmAddress: string) => { const addTransactionWithType = useTransactionAdder() const farmContract = useContract(farmAddress, FarmV2ABI) const posManager = useProAmmNFTPositionManagerContract() + const notify = useNotify() const approve = useCallback(async () => { if (!posManager) { throw new Error(CONTRACT_NOT_FOUND_MSG) } - const estimateGas = await posManager.estimateGas.setApprovalForAll(farmAddress, true) - const tx = await posManager.setApprovalForAll(farmAddress, true, { - gasLimit: calculateGasMargin(estimateGas), - }) - addTransactionWithType({ - hash: tx.hash, - type: TRANSACTION_TYPE.APPROVE, - extraInfo: { - summary: `Elastic Static Farm`, - contract: farmAddress, - }, - }) - return tx.hash - }, [posManager, farmAddress, addTransactionWithType]) + try { + const estimateGas = await posManager.estimateGas.setApprovalForAll(farmAddress, true) + const tx = await posManager.setApprovalForAll(farmAddress, true, { + gasLimit: calculateGasMargin(estimateGas), + }) + addTransactionWithType({ + hash: tx.hash, + type: TRANSACTION_TYPE.APPROVE, + extraInfo: { + summary: `Elastic Static Farm`, + contract: farmAddress, + }, + }) + return tx.hash + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Approve Farm Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + } + }, [posManager, farmAddress, addTransactionWithType, notify]) //Deposit const deposit = useCallback( @@ -247,11 +264,20 @@ export const useFarmV2Action = (farmAddress: string) => { type: TRANSACTION_TYPE.ELASTIC_DEPOSIT_LIQUIDITY, }) return tx.hash - } catch (e) { - throw e + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Deposit Farm Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error } }, - [farmContract, addTransactionWithType, account], + [farmContract, addTransactionWithType, account, notify], ) const updateLiquidity = useCallback( @@ -269,11 +295,20 @@ export const useFarmV2Action = (farmAddress: string) => { type: TRANSACTION_TYPE.ELASTIC_DEPOSIT_LIQUIDITY, }) return tx.hash - } catch (e) { - throw e + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Update Liquidity Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error } }, - [addTransactionWithType, farmContract], + [addTransactionWithType, farmContract, notify], ) const withdraw = useCallback( @@ -292,11 +327,20 @@ export const useFarmV2Action = (farmAddress: string) => { type: TRANSACTION_TYPE.ELASTIC_WITHDRAW_LIQUIDITY, }) return tx.hash - } catch (e) { - throw e + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Withdraw Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error } }, - [addTransactionWithType, farmContract], + [addTransactionWithType, farmContract, notify], ) const harvest = useCallback( @@ -313,11 +357,20 @@ export const useFarmV2Action = (farmAddress: string) => { addTransactionWithType({ hash: tx.hash, type: TRANSACTION_TYPE.HARVEST }) return tx.hash - } catch (e) { - throw e + } catch (error) { + const message = friendlyError(error) + notify( + { + title: t`Harvest Error`, + summary: message, + type: NotificationType.ERROR, + }, + 8000, + ) + throw error } }, - [addTransactionWithType, farmContract], + [addTransactionWithType, farmContract, notify], ) return { approve, deposit, withdraw, harvest, updateLiquidity }