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

try fix long number kyberdao approve issue #2083

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
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
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
Loading