From b1b1813ec90c574ec543b2ada5fba4f2659d2df7 Mon Sep 17 00:00:00 2001 From: XiaoYhun Date: Fri, 21 Jul 2023 11:36:57 +0700 Subject: [PATCH] try fix long number kyberdao approve issue (#2083) * try fix long number approve * update approve parsed amount * fix compare float incorrect * chore --- src/pages/KyberDAO/StakeKNC/MigrateModal.tsx | 22 ++++++---------- .../KyberDAO/StakeKNC/StakeKNCComponent.tsx | 25 ++++++++----------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/pages/KyberDAO/StakeKNC/MigrateModal.tsx b/src/pages/KyberDAO/StakeKNC/MigrateModal.tsx index 047840df45..5b70004289 100644 --- a/src/pages/KyberDAO/StakeKNC/MigrateModal.tsx +++ b/src/pages/KyberDAO/StakeKNC/MigrateModal.tsx @@ -1,4 +1,4 @@ -import { ChainId, Token, TokenAmount } from '@kyberswap/ks-sdk-core' +import { ChainId, Token } from '@kyberswap/ks-sdk-core' import { Trans, t } from '@lingui/macro' import { parseUnits } from 'ethers/lib/utils' import { useEffect, useState } from 'react' @@ -10,6 +10,7 @@ import { ButtonLight, ButtonPrimary } from 'components/Button' import { AutoColumn } from 'components/Column' import Modal from 'components/Modal' import Row, { AutoRow, RowBetween } from 'components/Row' +import useParsedAmount from 'components/SwapForm/hooks/useParsedAmount' import { useActiveWeb3React } from 'hooks' import { useKyberDAOInfo, useKyberDaoStakeActions } from 'hooks/kyberdao' import { ApprovalState, useApproveCallback } from 'hooks/useApproveCallback' @@ -49,20 +50,13 @@ export default function MigrateModal({ const { migrate } = useKyberDaoStakeActions() const [value, setValue] = useState('1') const [error, setError] = useState('') - const [approval, approveCallback] = useApproveCallback( - value - ? TokenAmount.fromRawAmount( - new Token( - chainId === ChainId.GÖRLI ? ChainId.GÖRLI : ChainId.MAINNET, - kyberDAOInfo?.KNCLAddress || '', - 18, - 'KNCL', - ), - parseUnits((+value).toFixed(18).toString(), 18).toString(), - ) - : undefined, - kyberDAOInfo?.KNCAddress, + const parsedAmount = useParsedAmount( + new Token(chainId === ChainId.GÖRLI ? ChainId.GÖRLI : ChainId.MAINNET, kyberDAOInfo?.KNCLAddress || '', 18, 'KNCL'), + value, ) + + const [approval, approveCallback] = useApproveCallback(parsedAmount, kyberDAOInfo?.KNCAddress) + const oldKNCBalance = useTokenBalance(kyberDAOInfo?.KNCLAddress || '') useEffect(() => { // Check if too many decimals diff --git a/src/pages/KyberDAO/StakeKNC/StakeKNCComponent.tsx b/src/pages/KyberDAO/StakeKNC/StakeKNCComponent.tsx index 78544d1a66..da369e03a5 100644 --- a/src/pages/KyberDAO/StakeKNC/StakeKNCComponent.tsx +++ b/src/pages/KyberDAO/StakeKNC/StakeKNCComponent.tsx @@ -1,4 +1,4 @@ -import { ChainId, Token, TokenAmount } from '@kyberswap/ks-sdk-core' +import { ChainId, Token } from '@kyberswap/ks-sdk-core' import { Trans, t } from '@lingui/macro' import { formatUnits, parseUnits } from 'ethers/lib/utils' import { lighten } from 'polished' @@ -18,6 +18,7 @@ import WarningIcon from 'components/Icons/WarningIcon' import InfoHelper from 'components/InfoHelper' import Input from 'components/NumericalInput' import Row, { AutoRow, RowBetween, RowFit } from 'components/Row' +import useParsedAmount from 'components/SwapForm/hooks/useParsedAmount' import { MouseoverTooltip } from 'components/Tooltip' import TransactionConfirmationModal, { TransactionErrorContent } from 'components/TransactionConfirmationModal' import { useActiveWeb3React } from 'hooks' @@ -204,6 +205,7 @@ export default function StakeKNCComponent() { const { account, chainId } = useActiveWeb3React() const kyberDAOInfo = useKyberDAOInfo() const { stakedBalance, KNCBalance, delegatedAddress } = useStakingInfo() + console.log('🚀 ~ file: StakeKNCComponent.tsx:208 ~ StakeKNCComponent ~ KNCBalance:', KNCBalance) const { calculateVotingPower } = useVotingInfo() const isDelegated = !!delegatedAddress && delegatedAddress !== account const { stake, unstake, delegate, undelegate } = useKyberDaoStakeActions() @@ -235,8 +237,8 @@ export default function StakeKNCComponent() { if (!inputValue || isNaN(parseFloat(inputValue)) || parseFloat(inputValue) <= 0) { setErrorMessage(t`Invalid amount`) } else if ( - (parseFloat(inputValue) > parseFloat(formatUnits(KNCBalance)) && activeTab === STAKE_TAB.Stake) || - (parseFloat(inputValue) > parseFloat(formatUnits(stakedBalance)) && activeTab === STAKE_TAB.Unstake) + (parseUnits(inputValue, 18).gt(KNCBalance) && activeTab === STAKE_TAB.Stake) || + (parseUnits(inputValue, 18).gt(stakedBalance) && activeTab === STAKE_TAB.Unstake) ) { setErrorMessage(t`Insufficient amount`) } else if (activeTab === STAKE_TAB.Delegate && !isAddress(chainId, delegateAddress)) { @@ -265,18 +267,13 @@ export default function StakeKNCComponent() { const toggleYourTransactions = useToggleModal(ApplicationModal.YOUR_TRANSACTIONS_STAKE_KNC) const { switchToEthereum } = useSwitchToEthereum() const { mixpanelHandler } = useMixpanel() + const parsedAmount = useParsedAmount( + new Token(chainId === ChainId.GÖRLI ? ChainId.GÖRLI : ChainId.MAINNET, kyberDAOInfo?.KNCAddress || '', 18, 'KNC'), + inputValue, + ) + const [approvalKNC, approveCallback] = useApproveCallback( - activeTab === STAKE_TAB.Stake && inputValue - ? TokenAmount.fromRawAmount( - new Token( - chainId === ChainId.GÖRLI ? ChainId.GÖRLI : ChainId.MAINNET, - kyberDAOInfo?.KNCAddress || '', - 18, - 'KNC', - ), - parseUnits((+inputValue).toFixed(18).toString(), 18).toString(), - ) - : undefined, + activeTab === STAKE_TAB.Stake && inputValue ? parsedAmount : undefined, kyberDAOInfo?.staking, )