Skip to content

Commit

Permalink
try fix long number kyberdao approve issue (#2083)
Browse files Browse the repository at this point in the history
* try fix long number approve

* update approve parsed amount

* fix compare float incorrect

* chore
  • Loading branch information
XiaoYhun authored Jul 21, 2023
1 parent 3d5bfa5 commit b1b1813
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
22 changes: 8 additions & 14 deletions src/pages/KyberDAO/StakeKNC/MigrateModal.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions src/pages/KyberDAO/StakeKNC/StakeKNCComponent.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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,
)

Expand Down

0 comments on commit b1b1813

Please sign in to comment.