Skip to content

Commit

Permalink
feat: thorchain LP USDT allowance reset
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Sep 2, 2024
1 parent 1a906b6 commit 33db74b
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
assetIdToPoolAssetId,
poolAssetIdToAssetId,
} from '@shapeshiftoss/swapper/dist/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers'
import type { Asset, KnownChainIds, MarketData } from '@shapeshiftoss/types'
import type { Asset, MarketData } from '@shapeshiftoss/types'
import { TxStatus } from '@shapeshiftoss/unchained-client'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import BigNumber from 'bignumber.js'
Expand All @@ -33,7 +33,6 @@ import { BiErrorCircle, BiSolidBoltCircle } from 'react-icons/bi'
import { FaPlus } from 'react-icons/fa'
import { useTranslate } from 'react-polyglot'
import { reactQueries } from 'react-queries'
import { useAllowance } from 'react-queries/hooks/useAllowance'
import { useIsTradingActive } from 'react-queries/hooks/useIsTradingActive'
import { useHistory } from 'react-router'
import { WarningAcknowledgement } from 'components/Acknowledgement/Acknowledgement'
Expand All @@ -47,6 +46,7 @@ import { Row } from 'components/Row/Row'
import { SlideTransition } from 'components/SlideTransition'
import { RawText, Text } from 'components/Text'
import type { TextPropTypes } from 'components/Text/Text'
import { useIsApprovalRequired } from 'hooks/queries/useIsApprovalRequired'
import { useBrowserRouter } from 'hooks/useBrowserRouter/useBrowserRouter'
import { useFeatureFlag } from 'hooks/useFeatureFlag/useFeatureFlag'
import { useIsSmartContractAddress } from 'hooks/useIsSmartContractAddress/useIsSmartContractAddress'
Expand All @@ -69,7 +69,6 @@ import {
isSome,
isToken,
} from 'lib/utils'
import { getSupportedEvmChainIds } from 'lib/utils/evm'
import { THOR_PRECISION } from 'lib/utils/thorchain/constants'
import { useSendThorTx } from 'lib/utils/thorchain/hooks/useSendThorTx'
import { useThorchainFromAddress } from 'lib/utils/thorchain/hooks/useThorchainFromAddress'
Expand Down Expand Up @@ -181,7 +180,7 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
const previousOpportunityId = usePrevious(activeOpportunityId)

const [approvalTxId, setApprovalTxId] = useState<string | null>(null)
const [isApprovalRequired, setIsApprovalRequired] = useState<boolean>(false)
const [isApprovalRequired, setIsApprovalRequired] = useState<boolean | undefined>(false)
const [runeTxFeeCryptoBaseUnit, setRuneTxFeeCryptoBaseUnit] = useState<string | undefined>()
const [poolAssetTxFeeCryptoBaseUnit, setPoolAssetTxFeeCryptoBaseUnit] = useState<
string | undefined
Expand Down Expand Up @@ -558,11 +557,29 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
action: 'addLiquidity',
enableEstimateFees: Boolean(
bnOrZero(actualAssetDepositAmountCryptoPrecision).gt(0) &&
!isApprovalRequired &&
isApprovalRequired === false &&
incompleteSide !== AsymSide.Rune,
),
})

const {
allowanceCryptoBaseUnitResult,
isApprovalRequired: _isApprovalRequired,
isAllowanceResetRequired,
} = useIsApprovalRequired({
amountCryptoBaseUnit:
poolAsset && actualAssetDepositAmountCryptoPrecision
? toBaseUnit(actualAssetDepositAmountCryptoPrecision, poolAsset.precision)
: undefined,
assetId: poolAsset?.assetId,
from: poolAssetAccountAddress,
spender: poolAssetInboundAddress,
})

useEffect(() => {
setIsApprovalRequired(_isApprovalRequired)
}, [_isApprovalRequired])

const hasEnoughAssetBalance = useMemo(() => {
if (incompleteSide === AsymSide.Rune) return true

Expand Down Expand Up @@ -600,7 +617,7 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
assetId: poolAsset?.assetId,
spender: poolAssetInboundAddress,
amountCryptoBaseUnit: toBaseUnit(
actualAssetDepositAmountCryptoPrecision,
isAllowanceResetRequired ? '0' : actualAssetDepositAmountCryptoPrecision,
poolAsset?.precision ?? 0,
BigNumber.ROUND_UP,
),
Expand Down Expand Up @@ -646,34 +663,6 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
queryClient,
])

const { data: allowanceData, isLoading: isAllowanceDataLoading } = useAllowance({
assetId: poolAsset?.assetId,
spender: poolAssetInboundAddress,
from: poolAssetAccountAddress,
})

const _isApprovalRequired = useMemo(() => {
if (!confirmedQuote) return false
if (!poolAsset) return false
if (incompleteSide === AsymSide.Rune) return false
if (!isToken(poolAsset.assetId)) return false

const supportedEvmChainIds = getSupportedEvmChainIds()
if (!supportedEvmChainIds.includes(fromAssetId(poolAsset.assetId).chainId as KnownChainIds))
return false

const allowanceCryptoPrecision = fromBaseUnit(allowanceData ?? '0', poolAsset.precision)
return bnOrZero(actualAssetDepositAmountCryptoPrecision).gt(allowanceCryptoPrecision)
}, [
actualAssetDepositAmountCryptoPrecision,
allowanceData,
confirmedQuote,
incompleteSide,
poolAsset,
])

useEffect(() => setIsApprovalRequired(_isApprovalRequired), [_isApprovalRequired])

// Pool asset fee/balance/sweep data and checks

useEffect(() => {
Expand Down Expand Up @@ -1428,10 +1417,15 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
const confirmCopy = useMemo(() => {
if (errorCopy) return errorCopy
if (poolAsset && isApprovalRequired)
return translate(`transactionRow.parser.erc20.approveSymbol`, { symbol: poolAsset.symbol })
return translate(
isAllowanceResetRequired
? 'trade.resetAllowance'
: `transactionRow.parser.erc20.approveSymbol`,
{ symbol: poolAsset.symbol },
)

return translate('pools.addLiquidity')
}, [errorCopy, isApprovalRequired, poolAsset, translate])
}, [errorCopy, isAllowanceResetRequired, isApprovalRequired, poolAsset, translate])

const divider = useMemo(() => <StackDivider borderColor='border.base' />, [])

Expand Down Expand Up @@ -1604,7 +1598,7 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
isVotingPowerLoading ||
isTradingActiveLoading ||
isSmartContractAccountAddressLoading ||
isAllowanceDataLoading ||
allowanceCryptoBaseUnitResult.isLoading ||
isApprovalTxPending ||
(isSweepNeeded === undefined && isSweepNeededLoading && !isApprovalRequired) ||
(runeTxFeeCryptoBaseUnit === undefined && isEstimatedPoolAssetFeesDataLoading)
Expand Down

0 comments on commit 33db74b

Please sign in to comment.