Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
1efda07e
Unverified
Commit
1efda07e
authored
Jan 19, 2022
by
Zach Pomerantz
Committed by
GitHub
Jan 19, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: mv try parse currency amount to lib utils (#3152)
parent
fd819260
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
49 additions
and
39 deletions
+49
-39
useUSDCPrice.ts
src/hooks/useUSDCPrice.ts
+2
-2
useWrapCallback.tsx
src/hooks/useWrapCallback.tsx
+5
-2
tryParseCurrencyAmount.ts
src/lib/utils/tryParseCurrencyAmount.ts
+26
-0
index.tsx
src/pages/CreateProposal/index.tsx
+2
-2
hooks.tsx
src/state/burn/hooks.tsx
+3
-3
hooks.tsx
src/state/mint/hooks.tsx
+3
-3
hooks.tsx
src/state/mint/v3/hooks.tsx
+4
-4
hooks.tsx
src/state/stake/hooks.tsx
+2
-2
hooks.tsx
src/state/swap/hooks.tsx
+2
-21
No files found.
src/hooks/useUSDCPrice.ts
View file @
1efda07e
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
'
...
...
@@ -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
tryParse
Currency
Amount
(
parsedForDecimals
,
stablecoin
)
}
catch
(
error
)
{
return
undefined
}
...
...
src/hooks/useWrapCallback.tsx
View file @
1efda07e
...
...
@@ -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
'
...
...
@@ -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
(()
=>
{
...
...
src/lib/utils/tryParseCurrencyAmount.ts
0 → 100644
View file @
1efda07e
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
}
src/pages/CreateProposal/index.tsx
View file @
1efda07e
...
...
@@ -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
{
...
...
@@ -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
'
...
...
@@ -184,7 +184,7 @@ export default function CreateProposal() {
if
(
!
createProposalCallback
||
!
proposalAction
||
!
currencyValue
.
isToken
)
return
const
tokenAmount
=
tryParseAmount
(
amountValue
,
currencyValue
)
const
tokenAmount
=
tryParse
Currency
Amount
(
amountValue
,
currencyValue
)
if
(
!
tokenAmount
)
return
createProposalData
.
targets
=
[
currencyValue
.
address
]
...
...
src/state/burn/hooks.tsx
View file @
1efda07e
...
...
@@ -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
'
...
...
@@ -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
=
tryParse
Currency
Amount
(
typedValue
,
pair
.
liquidityToken
)
if
(
independentAmount
&&
userLiquidity
&&
!
independentAmount
.
greaterThan
(
userLiquidity
))
{
percentToRemove
=
new
Percent
(
independentAmount
.
quotient
,
userLiquidity
.
quotient
)
}
...
...
@@ -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
=
tryParse
Currency
Amount
(
typedValue
,
tokens
[
independentField
])
const
liquidityValue
=
liquidityValues
[
independentField
]
if
(
independentAmount
&&
liquidityValue
&&
!
independentAmount
.
greaterThan
(
liquidityValue
))
{
percentToRemove
=
new
Percent
(
independentAmount
.
quotient
,
liquidityValue
.
quotient
)
...
...
src/state/mint/hooks.tsx
View file @
1efda07e
...
...
@@ -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
'
...
...
@@ -101,14 +101,14 @@ export function useDerivedMintInfo(
}
// amounts
const
independentAmount
:
CurrencyAmount
<
Currency
>
|
undefined
=
tryParseAmount
(
const
independentAmount
:
CurrencyAmount
<
Currency
>
|
undefined
=
tryParse
Currency
Amount
(
typedValue
,
currencies
[
independentField
]
)
const
dependentAmount
:
CurrencyAmount
<
Currency
>
|
undefined
=
useMemo
(()
=>
{
if
(
noLiquidity
)
{
if
(
otherTypedValue
&&
currencies
[
dependentField
])
{
return
tryParseAmount
(
otherTypedValue
,
currencies
[
dependentField
])
return
tryParse
Currency
Amount
(
otherTypedValue
,
currencies
[
dependentField
])
}
return
undefined
}
else
if
(
independentAmount
)
{
...
...
src/state/mint/v3/hooks.tsx
View file @
1efda07e
...
...
@@ -14,6 +14,7 @@ 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
'
...
...
@@ -21,7 +22,6 @@ 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
,
...
...
@@ -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
=
tryParse
Currency
Amount
(
startPriceTypedValue
,
invertPrice
?
token0
:
token1
)
if
(
parsedQuoteAmount
&&
token0
&&
token1
)
{
const
baseAmount
=
tryParseAmount
(
'
1
'
,
invertPrice
?
token1
:
token0
)
const
baseAmount
=
tryParse
Currency
Amount
(
'
1
'
,
invertPrice
?
token1
:
token0
)
const
price
=
baseAmount
&&
parsedQuoteAmount
?
new
Price
(
...
...
@@ -294,7 +294,7 @@ export function useV3DerivedMintInfo(
)
// amounts
const
independentAmount
:
CurrencyAmount
<
Currency
>
|
undefined
=
tryParseAmount
(
const
independentAmount
:
CurrencyAmount
<
Currency
>
|
undefined
=
tryParse
Currency
Amount
(
typedValue
,
currencies
[
independentField
]
)
...
...
src/state/stake/hooks.tsx
View file @
1efda07e
...
...
@@ -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
)
...
...
@@ -254,7 +254,7 @@ export function useDerivedStakeInfo(
}
{
const
{
account
}
=
useActiveWeb3React
()
const
parsedInput
:
CurrencyAmount
<
Token
>
|
undefined
=
tryParseAmount
(
typedValue
,
stakingToken
)
const
parsedInput
:
CurrencyAmount
<
Token
>
|
undefined
=
tryParse
Currency
Amount
(
typedValue
,
stakingToken
)
const
parsedAmount
=
parsedInput
&&
userLiquidityUnstaked
&&
JSBI
.
lessThanOrEqual
(
parsedInput
.
quotient
,
userLiquidityUnstaked
.
quotient
)
...
...
src/state/swap/hooks.tsx
View file @
1efda07e
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
'
...
...
@@ -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
...
...
@@ -126,7 +107,7 @@ export function useDerivedSwapInfo(): {
const
isExactIn
:
boolean
=
independentField
===
Field
.
INPUT
const
parsedAmount
=
useMemo
(
()
=>
tryParseAmount
(
typedValue
,
(
isExactIn
?
inputCurrency
:
outputCurrency
)
??
undefined
),
()
=>
tryParse
Currency
Amount
(
typedValue
,
(
isExactIn
?
inputCurrency
:
outputCurrency
)
??
undefined
),
[
inputCurrency
,
isExactIn
,
outputCurrency
,
typedValue
]
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment