refactor: mv try parse currency amount to lib utils (#3152)
This commit is contained in:
parent
fd819260f9
commit
1efda07e7a
@ -1,7 +1,7 @@
|
|||||||
import { Currency, CurrencyAmount, Price, Token, TradeType } from '@uniswap/sdk-core'
|
import { Currency, CurrencyAmount, Price, Token, TradeType } from '@uniswap/sdk-core'
|
||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { tryParseAmount } from 'state/swap/hooks'
|
|
||||||
|
|
||||||
import { SupportedChainId } from '../constants/chains'
|
import { SupportedChainId } from '../constants/chains'
|
||||||
import { DAI_OPTIMISM, USDC, USDC_ARBITRUM, USDC_POLYGON } from '../constants/tokens'
|
import { DAI_OPTIMISM, USDC, USDC_ARBITRUM, USDC_POLYGON } from '../constants/tokens'
|
||||||
@ -87,7 +87,7 @@ export function useStablecoinAmountFromFiatValue(fiatValue: string | null | unde
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// parse USD string into CurrencyAmount based on stablecoin decimals
|
// parse USD string into CurrencyAmount based on stablecoin decimals
|
||||||
return tryParseAmount(parsedForDecimals, stablecoin)
|
return tryParseCurrencyAmount(parsedForDecimals, stablecoin)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,10 @@ import { Trans } from '@lingui/macro'
|
|||||||
import { Currency } from '@uniswap/sdk-core'
|
import { Currency } from '@uniswap/sdk-core'
|
||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
import useNativeCurrency from 'lib/hooks/useNativeCurrency'
|
import useNativeCurrency from 'lib/hooks/useNativeCurrency'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
|
||||||
import { WRAPPED_NATIVE_CURRENCY } from '../constants/tokens'
|
import { WRAPPED_NATIVE_CURRENCY } from '../constants/tokens'
|
||||||
import { tryParseAmount } from '../state/swap/hooks'
|
|
||||||
import { TransactionType } from '../state/transactions/actions'
|
import { TransactionType } from '../state/transactions/actions'
|
||||||
import { useTransactionAdder } from '../state/transactions/hooks'
|
import { useTransactionAdder } from '../state/transactions/hooks'
|
||||||
import { useCurrencyBalance } from '../state/wallet/hooks'
|
import { useCurrencyBalance } from '../state/wallet/hooks'
|
||||||
@ -61,7 +61,10 @@ export default function useWrapCallback(
|
|||||||
const wethContract = useWETHContract()
|
const wethContract = useWETHContract()
|
||||||
const balance = useCurrencyBalance(account ?? undefined, inputCurrency ?? undefined)
|
const balance = useCurrencyBalance(account ?? undefined, inputCurrency ?? undefined)
|
||||||
// we can always parse the amount typed as the input currency, since wrapping is 1:1
|
// we can always parse the amount typed as the input currency, since wrapping is 1:1
|
||||||
const inputAmount = useMemo(() => tryParseAmount(typedValue, inputCurrency ?? undefined), [inputCurrency, typedValue])
|
const inputAmount = useMemo(
|
||||||
|
() => tryParseCurrencyAmount(typedValue, inputCurrency ?? undefined),
|
||||||
|
[inputCurrency, typedValue]
|
||||||
|
)
|
||||||
const addTransaction = useTransactionAdder()
|
const addTransaction = useTransactionAdder()
|
||||||
|
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
|
26
src/lib/utils/tryParseCurrencyAmount.ts
Normal file
26
src/lib/utils/tryParseCurrencyAmount.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { parseUnits } from '@ethersproject/units'
|
||||||
|
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
|
||||||
|
import JSBI from 'jsbi'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a CurrencyAmount from the passed string.
|
||||||
|
* Returns the CurrencyAmount, or undefined if parsing fails.
|
||||||
|
*/
|
||||||
|
export default function tryParseCurrencyAmount<T extends Currency>(
|
||||||
|
value?: string,
|
||||||
|
currency?: T
|
||||||
|
): CurrencyAmount<T> | undefined {
|
||||||
|
if (!value || !currency) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const typedValueParsed = parseUnits(value, currency.decimals).toString()
|
||||||
|
if (typedValueParsed !== '0') {
|
||||||
|
return CurrencyAmount.fromRawAmount(currency, JSBI.BigInt(typedValueParsed))
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// fails if the user specifies too many decimal places of precision (or maybe exceed max uint?)
|
||||||
|
console.debug(`Failed to parse input amount: "${value}"`, error)
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
@ -7,6 +7,7 @@ import { BlueCard } from 'components/Card'
|
|||||||
import { AutoColumn } from 'components/Column'
|
import { AutoColumn } from 'components/Column'
|
||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
import JSBI from 'jsbi'
|
import JSBI from 'jsbi'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { Wrapper } from 'pages/Pool/styleds'
|
import { Wrapper } from 'pages/Pool/styleds'
|
||||||
import React, { useCallback, useMemo, useState } from 'react'
|
import React, { useCallback, useMemo, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
@ -18,7 +19,6 @@ import {
|
|||||||
useProposalThreshold,
|
useProposalThreshold,
|
||||||
useUserVotes,
|
useUserVotes,
|
||||||
} from 'state/governance/hooks'
|
} from 'state/governance/hooks'
|
||||||
import { tryParseAmount } from 'state/swap/hooks'
|
|
||||||
import styled from 'styled-components/macro'
|
import styled from 'styled-components/macro'
|
||||||
import { ExternalLink, ThemedText } from 'theme'
|
import { ExternalLink, ThemedText } from 'theme'
|
||||||
|
|
||||||
@ -184,7 +184,7 @@ export default function CreateProposal() {
|
|||||||
|
|
||||||
if (!createProposalCallback || !proposalAction || !currencyValue.isToken) return
|
if (!createProposalCallback || !proposalAction || !currencyValue.isToken) return
|
||||||
|
|
||||||
const tokenAmount = tryParseAmount(amountValue, currencyValue)
|
const tokenAmount = tryParseCurrencyAmount(amountValue, currencyValue)
|
||||||
if (!tokenAmount) return
|
if (!tokenAmount) return
|
||||||
|
|
||||||
createProposalData.targets = [currencyValue.address]
|
createProposalData.targets = [currencyValue.address]
|
||||||
|
@ -3,13 +3,13 @@ import { Currency, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core'
|
|||||||
import { Pair } from '@uniswap/v2-sdk'
|
import { Pair } from '@uniswap/v2-sdk'
|
||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
import JSBI from 'jsbi'
|
import JSBI from 'jsbi'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { ReactNode, useCallback } from 'react'
|
import { ReactNode, useCallback } from 'react'
|
||||||
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
||||||
|
|
||||||
import { useTotalSupply } from '../../hooks/useTotalSupply'
|
import { useTotalSupply } from '../../hooks/useTotalSupply'
|
||||||
import { useV2Pair } from '../../hooks/useV2Pairs'
|
import { useV2Pair } from '../../hooks/useV2Pairs'
|
||||||
import { AppState } from '../index'
|
import { AppState } from '../index'
|
||||||
import { tryParseAmount } from '../swap/hooks'
|
|
||||||
import { useTokenBalances } from '../wallet/hooks'
|
import { useTokenBalances } from '../wallet/hooks'
|
||||||
import { Field, typeInput } from './actions'
|
import { Field, typeInput } from './actions'
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ export function useDerivedBurnInfo(
|
|||||||
// user specified a specific amount of liquidity tokens
|
// user specified a specific amount of liquidity tokens
|
||||||
else if (independentField === Field.LIQUIDITY) {
|
else if (independentField === Field.LIQUIDITY) {
|
||||||
if (pair?.liquidityToken) {
|
if (pair?.liquidityToken) {
|
||||||
const independentAmount = tryParseAmount(typedValue, pair.liquidityToken)
|
const independentAmount = tryParseCurrencyAmount(typedValue, pair.liquidityToken)
|
||||||
if (independentAmount && userLiquidity && !independentAmount.greaterThan(userLiquidity)) {
|
if (independentAmount && userLiquidity && !independentAmount.greaterThan(userLiquidity)) {
|
||||||
percentToRemove = new Percent(independentAmount.quotient, userLiquidity.quotient)
|
percentToRemove = new Percent(independentAmount.quotient, userLiquidity.quotient)
|
||||||
}
|
}
|
||||||
@ -90,7 +90,7 @@ export function useDerivedBurnInfo(
|
|||||||
// user specified a specific amount of token a or b
|
// user specified a specific amount of token a or b
|
||||||
else {
|
else {
|
||||||
if (tokens[independentField]) {
|
if (tokens[independentField]) {
|
||||||
const independentAmount = tryParseAmount(typedValue, tokens[independentField])
|
const independentAmount = tryParseCurrencyAmount(typedValue, tokens[independentField])
|
||||||
const liquidityValue = liquidityValues[independentField]
|
const liquidityValue = liquidityValues[independentField]
|
||||||
if (independentAmount && liquidityValue && !independentAmount.greaterThan(liquidityValue)) {
|
if (independentAmount && liquidityValue && !independentAmount.greaterThan(liquidityValue)) {
|
||||||
percentToRemove = new Percent(independentAmount.quotient, liquidityValue.quotient)
|
percentToRemove = new Percent(independentAmount.quotient, liquidityValue.quotient)
|
||||||
|
@ -3,13 +3,13 @@ import { Currency, CurrencyAmount, Percent, Price, Token } from '@uniswap/sdk-co
|
|||||||
import { Pair } from '@uniswap/v2-sdk'
|
import { Pair } from '@uniswap/v2-sdk'
|
||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
import JSBI from 'jsbi'
|
import JSBI from 'jsbi'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { ReactNode, useCallback, useMemo } from 'react'
|
import { ReactNode, useCallback, useMemo } from 'react'
|
||||||
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
||||||
|
|
||||||
import { useTotalSupply } from '../../hooks/useTotalSupply'
|
import { useTotalSupply } from '../../hooks/useTotalSupply'
|
||||||
import { PairState, useV2Pair } from '../../hooks/useV2Pairs'
|
import { PairState, useV2Pair } from '../../hooks/useV2Pairs'
|
||||||
import { AppState } from '../index'
|
import { AppState } from '../index'
|
||||||
import { tryParseAmount } from '../swap/hooks'
|
|
||||||
import { useCurrencyBalances } from '../wallet/hooks'
|
import { useCurrencyBalances } from '../wallet/hooks'
|
||||||
import { Field, typeInput } from './actions'
|
import { Field, typeInput } from './actions'
|
||||||
|
|
||||||
@ -101,14 +101,14 @@ export function useDerivedMintInfo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// amounts
|
// amounts
|
||||||
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseAmount(
|
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseCurrencyAmount(
|
||||||
typedValue,
|
typedValue,
|
||||||
currencies[independentField]
|
currencies[independentField]
|
||||||
)
|
)
|
||||||
const dependentAmount: CurrencyAmount<Currency> | undefined = useMemo(() => {
|
const dependentAmount: CurrencyAmount<Currency> | undefined = useMemo(() => {
|
||||||
if (noLiquidity) {
|
if (noLiquidity) {
|
||||||
if (otherTypedValue && currencies[dependentField]) {
|
if (otherTypedValue && currencies[dependentField]) {
|
||||||
return tryParseAmount(otherTypedValue, currencies[dependentField])
|
return tryParseCurrencyAmount(otherTypedValue, currencies[dependentField])
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
} else if (independentAmount) {
|
} else if (independentAmount) {
|
||||||
|
@ -14,6 +14,7 @@ import {
|
|||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
import { usePool } from 'hooks/usePools'
|
import { usePool } from 'hooks/usePools'
|
||||||
import JSBI from 'jsbi'
|
import JSBI from 'jsbi'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { ReactNode, useCallback, useMemo } from 'react'
|
import { ReactNode, useCallback, useMemo } from 'react'
|
||||||
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
||||||
import { getTickToPrice } from 'utils/getTickToPrice'
|
import { getTickToPrice } from 'utils/getTickToPrice'
|
||||||
@ -21,7 +22,6 @@ import { getTickToPrice } from 'utils/getTickToPrice'
|
|||||||
import { BIG_INT_ZERO } from '../../../constants/misc'
|
import { BIG_INT_ZERO } from '../../../constants/misc'
|
||||||
import { PoolState } from '../../../hooks/usePools'
|
import { PoolState } from '../../../hooks/usePools'
|
||||||
import { AppState } from '../../index'
|
import { AppState } from '../../index'
|
||||||
import { tryParseAmount } from '../../swap/hooks'
|
|
||||||
import { useCurrencyBalances } from '../../wallet/hooks'
|
import { useCurrencyBalances } from '../../wallet/hooks'
|
||||||
import {
|
import {
|
||||||
Bound,
|
Bound,
|
||||||
@ -170,9 +170,9 @@ export function useV3DerivedMintInfo(
|
|||||||
const price: Price<Token, Token> | undefined = useMemo(() => {
|
const price: Price<Token, Token> | undefined = useMemo(() => {
|
||||||
// if no liquidity use typed value
|
// if no liquidity use typed value
|
||||||
if (noLiquidity) {
|
if (noLiquidity) {
|
||||||
const parsedQuoteAmount = tryParseAmount(startPriceTypedValue, invertPrice ? token0 : token1)
|
const parsedQuoteAmount = tryParseCurrencyAmount(startPriceTypedValue, invertPrice ? token0 : token1)
|
||||||
if (parsedQuoteAmount && token0 && token1) {
|
if (parsedQuoteAmount && token0 && token1) {
|
||||||
const baseAmount = tryParseAmount('1', invertPrice ? token1 : token0)
|
const baseAmount = tryParseCurrencyAmount('1', invertPrice ? token1 : token0)
|
||||||
const price =
|
const price =
|
||||||
baseAmount && parsedQuoteAmount
|
baseAmount && parsedQuoteAmount
|
||||||
? new Price(
|
? new Price(
|
||||||
@ -294,7 +294,7 @@ export function useV3DerivedMintInfo(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// amounts
|
// amounts
|
||||||
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseAmount(
|
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseCurrencyAmount(
|
||||||
typedValue,
|
typedValue,
|
||||||
currencies[independentField]
|
currencies[independentField]
|
||||||
)
|
)
|
||||||
|
@ -7,10 +7,10 @@ import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
|||||||
import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp'
|
import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp'
|
||||||
import JSBI from 'jsbi'
|
import JSBI from 'jsbi'
|
||||||
import { NEVER_RELOAD, useMultipleContractSingleData } from 'lib/hooks/multicall'
|
import { NEVER_RELOAD, useMultipleContractSingleData } from 'lib/hooks/multicall'
|
||||||
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { ReactNode, useMemo } from 'react'
|
import { ReactNode, useMemo } from 'react'
|
||||||
|
|
||||||
import { DAI, UNI, USDC, USDT, WBTC, WRAPPED_NATIVE_CURRENCY } from '../../constants/tokens'
|
import { DAI, UNI, USDC, USDT, WBTC, WRAPPED_NATIVE_CURRENCY } from '../../constants/tokens'
|
||||||
import { tryParseAmount } from '../swap/hooks'
|
|
||||||
|
|
||||||
const STAKING_REWARDS_INTERFACE = new Interface(STAKING_REWARDS_ABI)
|
const STAKING_REWARDS_INTERFACE = new Interface(STAKING_REWARDS_ABI)
|
||||||
|
|
||||||
@ -254,7 +254,7 @@ export function useDerivedStakeInfo(
|
|||||||
} {
|
} {
|
||||||
const { account } = useActiveWeb3React()
|
const { account } = useActiveWeb3React()
|
||||||
|
|
||||||
const parsedInput: CurrencyAmount<Token> | undefined = tryParseAmount(typedValue, stakingToken)
|
const parsedInput: CurrencyAmount<Token> | undefined = tryParseCurrencyAmount(typedValue, stakingToken)
|
||||||
|
|
||||||
const parsedAmount =
|
const parsedAmount =
|
||||||
parsedInput && userLiquidityUnstaked && JSBI.lessThanOrEqual(parsedInput.quotient, userLiquidityUnstaked.quotient)
|
parsedInput && userLiquidityUnstaked && JSBI.lessThanOrEqual(parsedInput.quotient, userLiquidityUnstaked.quotient)
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import { parseUnits } from '@ethersproject/units'
|
|
||||||
import { Trans } from '@lingui/macro'
|
import { Trans } from '@lingui/macro'
|
||||||
import { Currency, CurrencyAmount, Percent, TradeType } from '@uniswap/sdk-core'
|
import { Currency, CurrencyAmount, Percent, TradeType } from '@uniswap/sdk-core'
|
||||||
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
||||||
import { useBestTrade } from 'hooks/useBestTrade'
|
import { useBestTrade } from 'hooks/useBestTrade'
|
||||||
import JSBI from 'jsbi'
|
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
|
||||||
import { ParsedQs } from 'qs'
|
import { ParsedQs } from 'qs'
|
||||||
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'
|
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'
|
||||||
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
import { useAppDispatch, useAppSelector } from 'state/hooks'
|
||||||
@ -68,24 +67,6 @@ export function useSwapActionHandlers(): {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to parse a user entered amount for a given token
|
|
||||||
export function tryParseAmount<T extends Currency>(value?: string, currency?: T): CurrencyAmount<T> | undefined {
|
|
||||||
if (!value || !currency) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const typedValueParsed = parseUnits(value, currency.decimals).toString()
|
|
||||||
if (typedValueParsed !== '0') {
|
|
||||||
return CurrencyAmount.fromRawAmount(currency, JSBI.BigInt(typedValueParsed))
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// should fail if the user specifies too many decimal places of precision (or maybe exceed max uint?)
|
|
||||||
console.debug(`Failed to parse input amount: "${value}"`, error)
|
|
||||||
}
|
|
||||||
// necessary for all paths to return a value
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = {
|
const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = {
|
||||||
'0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f': true, // v2 factory
|
'0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f': true, // v2 factory
|
||||||
'0xf164fC0Ec4E93095b804a4795bBe1e041497b92a': true, // v2 router 01
|
'0xf164fC0Ec4E93095b804a4795bBe1e041497b92a': true, // v2 router 01
|
||||||
@ -126,7 +107,7 @@ export function useDerivedSwapInfo(): {
|
|||||||
|
|
||||||
const isExactIn: boolean = independentField === Field.INPUT
|
const isExactIn: boolean = independentField === Field.INPUT
|
||||||
const parsedAmount = useMemo(
|
const parsedAmount = useMemo(
|
||||||
() => tryParseAmount(typedValue, (isExactIn ? inputCurrency : outputCurrency) ?? undefined),
|
() => tryParseCurrencyAmount(typedValue, (isExactIn ? inputCurrency : outputCurrency) ?? undefined),
|
||||||
[inputCurrency, isExactIn, outputCurrency, typedValue]
|
[inputCurrency, isExactIn, outputCurrency, typedValue]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user