Skip to content

Commit

Permalink
refactor: mv try parse currency amount to lib utils (#3152)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Jan 19, 2022
1 parent fd81926 commit 1efda07
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/hooks/useUSDCPrice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Currency, CurrencyAmount, Price, Token, TradeType } from '@uniswap/sdk-core'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { useMemo } from 'react'
import { tryParseAmount } from 'state/swap/hooks'

import { SupportedChainId } from '../constants/chains'
import { DAI_OPTIMISM, USDC, USDC_ARBITRUM, USDC_POLYGON } from '../constants/tokens'
Expand Down Expand Up @@ -87,7 +87,7 @@ export function useStablecoinAmountFromFiatValue(fiatValue: string | null | unde

try {
// parse USD string into CurrencyAmount based on stablecoin decimals
return tryParseAmount(parsedForDecimals, stablecoin)
return tryParseCurrencyAmount(parsedForDecimals, stablecoin)
} catch (error) {
return undefined
}
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useWrapCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Trans } from '@lingui/macro'
import { Currency } from '@uniswap/sdk-core'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import useNativeCurrency from 'lib/hooks/useNativeCurrency'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { useMemo } from 'react'

import { WRAPPED_NATIVE_CURRENCY } from '../constants/tokens'
import { tryParseAmount } from '../state/swap/hooks'
import { TransactionType } from '../state/transactions/actions'
import { useTransactionAdder } from '../state/transactions/hooks'
import { useCurrencyBalance } from '../state/wallet/hooks'
Expand Down Expand Up @@ -61,7 +61,10 @@ export default function useWrapCallback(
const wethContract = useWETHContract()
const balance = useCurrencyBalance(account ?? undefined, inputCurrency ?? undefined)
// 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()

return useMemo(() => {
Expand Down
26 changes: 26 additions & 0 deletions src/lib/utils/tryParseCurrencyAmount.ts
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions src/pages/CreateProposal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BlueCard } from 'components/Card'
import { AutoColumn } from 'components/Column'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import JSBI from 'jsbi'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { Wrapper } from 'pages/Pool/styleds'
import React, { useCallback, useMemo, useState } from 'react'
import {
Expand All @@ -18,7 +19,6 @@ import {
useProposalThreshold,
useUserVotes,
} from 'state/governance/hooks'
import { tryParseAmount } from 'state/swap/hooks'
import styled from 'styled-components/macro'
import { ExternalLink, ThemedText } from 'theme'

Expand Down Expand Up @@ -184,7 +184,7 @@ export default function CreateProposal() {

if (!createProposalCallback || !proposalAction || !currencyValue.isToken) return

const tokenAmount = tryParseAmount(amountValue, currencyValue)
const tokenAmount = tryParseCurrencyAmount(amountValue, currencyValue)
if (!tokenAmount) return

createProposalData.targets = [currencyValue.address]
Expand Down
6 changes: 3 additions & 3 deletions src/state/burn/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Currency, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core'
import { Pair } from '@uniswap/v2-sdk'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import JSBI from 'jsbi'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { ReactNode, useCallback } from 'react'
import { useAppDispatch, useAppSelector } from 'state/hooks'

import { useTotalSupply } from '../../hooks/useTotalSupply'
import { useV2Pair } from '../../hooks/useV2Pairs'
import { AppState } from '../index'
import { tryParseAmount } from '../swap/hooks'
import { useTokenBalances } from '../wallet/hooks'
import { Field, typeInput } from './actions'

Expand Down Expand Up @@ -81,7 +81,7 @@ export function useDerivedBurnInfo(
// user specified a specific amount of liquidity tokens
else if (independentField === Field.LIQUIDITY) {
if (pair?.liquidityToken) {
const independentAmount = tryParseAmount(typedValue, pair.liquidityToken)
const independentAmount = tryParseCurrencyAmount(typedValue, pair.liquidityToken)
if (independentAmount && userLiquidity && !independentAmount.greaterThan(userLiquidity)) {
percentToRemove = new Percent(independentAmount.quotient, userLiquidity.quotient)
}
Expand All @@ -90,7 +90,7 @@ export function useDerivedBurnInfo(
// user specified a specific amount of token a or b
else {
if (tokens[independentField]) {
const independentAmount = tryParseAmount(typedValue, tokens[independentField])
const independentAmount = tryParseCurrencyAmount(typedValue, tokens[independentField])
const liquidityValue = liquidityValues[independentField]
if (independentAmount && liquidityValue && !independentAmount.greaterThan(liquidityValue)) {
percentToRemove = new Percent(independentAmount.quotient, liquidityValue.quotient)
Expand Down
6 changes: 3 additions & 3 deletions src/state/mint/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Currency, CurrencyAmount, Percent, Price, Token } from '@uniswap/sdk-co
import { Pair } from '@uniswap/v2-sdk'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import JSBI from 'jsbi'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { ReactNode, useCallback, useMemo } from 'react'
import { useAppDispatch, useAppSelector } from 'state/hooks'

import { useTotalSupply } from '../../hooks/useTotalSupply'
import { PairState, useV2Pair } from '../../hooks/useV2Pairs'
import { AppState } from '../index'
import { tryParseAmount } from '../swap/hooks'
import { useCurrencyBalances } from '../wallet/hooks'
import { Field, typeInput } from './actions'

Expand Down Expand Up @@ -101,14 +101,14 @@ export function useDerivedMintInfo(
}

// amounts
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseAmount(
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseCurrencyAmount(
typedValue,
currencies[independentField]
)
const dependentAmount: CurrencyAmount<Currency> | undefined = useMemo(() => {
if (noLiquidity) {
if (otherTypedValue && currencies[dependentField]) {
return tryParseAmount(otherTypedValue, currencies[dependentField])
return tryParseCurrencyAmount(otherTypedValue, currencies[dependentField])
}
return undefined
} else if (independentAmount) {
Expand Down
8 changes: 4 additions & 4 deletions src/state/mint/v3/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { usePool } from 'hooks/usePools'
import JSBI from 'jsbi'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { ReactNode, useCallback, useMemo } from 'react'
import { useAppDispatch, useAppSelector } from 'state/hooks'
import { getTickToPrice } from 'utils/getTickToPrice'

import { BIG_INT_ZERO } from '../../../constants/misc'
import { PoolState } from '../../../hooks/usePools'
import { AppState } from '../../index'
import { tryParseAmount } from '../../swap/hooks'
import { useCurrencyBalances } from '../../wallet/hooks'
import {
Bound,
Expand Down Expand Up @@ -170,9 +170,9 @@ export function useV3DerivedMintInfo(
const price: Price<Token, Token> | undefined = useMemo(() => {
// if no liquidity use typed value
if (noLiquidity) {
const parsedQuoteAmount = tryParseAmount(startPriceTypedValue, invertPrice ? token0 : token1)
const parsedQuoteAmount = tryParseCurrencyAmount(startPriceTypedValue, invertPrice ? token0 : token1)
if (parsedQuoteAmount && token0 && token1) {
const baseAmount = tryParseAmount('1', invertPrice ? token1 : token0)
const baseAmount = tryParseCurrencyAmount('1', invertPrice ? token1 : token0)
const price =
baseAmount && parsedQuoteAmount
? new Price(
Expand Down Expand Up @@ -294,7 +294,7 @@ export function useV3DerivedMintInfo(
)

// amounts
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseAmount(
const independentAmount: CurrencyAmount<Currency> | undefined = tryParseCurrencyAmount(
typedValue,
currencies[independentField]
)
Expand Down
4 changes: 2 additions & 2 deletions src/state/stake/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import useActiveWeb3React from 'hooks/useActiveWeb3React'
import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp'
import JSBI from 'jsbi'
import { NEVER_RELOAD, useMultipleContractSingleData } from 'lib/hooks/multicall'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { ReactNode, useMemo } from 'react'

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)

Expand Down Expand Up @@ -254,7 +254,7 @@ export function useDerivedStakeInfo(
} {
const { account } = useActiveWeb3React()

const parsedInput: CurrencyAmount<Token> | undefined = tryParseAmount(typedValue, stakingToken)
const parsedInput: CurrencyAmount<Token> | undefined = tryParseCurrencyAmount(typedValue, stakingToken)

const parsedAmount =
parsedInput && userLiquidityUnstaked && JSBI.lessThanOrEqual(parsedInput.quotient, userLiquidityUnstaked.quotient)
Expand Down
23 changes: 2 additions & 21 deletions src/state/swap/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { parseUnits } from '@ethersproject/units'
import { Trans } from '@lingui/macro'
import { Currency, CurrencyAmount, Percent, TradeType } from '@uniswap/sdk-core'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { useBestTrade } from 'hooks/useBestTrade'
import JSBI from 'jsbi'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { ParsedQs } from 'qs'
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'
import { useAppDispatch, useAppSelector } from 'state/hooks'
Expand Down Expand Up @@ -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 } = {
'0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f': true, // v2 factory
'0xf164fC0Ec4E93095b804a4795bBe1e041497b92a': true, // v2 router 01
Expand Down Expand Up @@ -126,7 +107,7 @@ export function useDerivedSwapInfo(): {

const isExactIn: boolean = independentField === Field.INPUT
const parsedAmount = useMemo(
() => tryParseAmount(typedValue, (isExactIn ? inputCurrency : outputCurrency) ?? undefined),
() => tryParseCurrencyAmount(typedValue, (isExactIn ? inputCurrency : outputCurrency) ?? undefined),
[inputCurrency, isExactIn, outputCurrency, typedValue]
)

Expand Down

0 comments on commit 1efda07

Please sign in to comment.