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
96c66a58
Unverified
Commit
96c66a58
authored
Mar 09, 2022
by
Zach Pomerantz
Committed by
GitHub
Mar 09, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: incorrectly memoized hooks (#3471)
* fix: incorrectly memoized hooks * fix: finish memoizing useUSDCPrice
parent
8c269a6d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
32 additions
and
17 deletions
+32
-17
useCurrentBlockTimestamp.ts
src/hooks/useCurrentBlockTimestamp.ts
+11
-1
useUSDCPrice.ts
src/hooks/useUSDCPrice.ts
+13
-12
index.tsx
src/lib/components/TokenSelect/index.tsx
+2
-1
index.ts
src/lib/hooks/swap/index.ts
+3
-2
useAllowedSlippage.ts
src/lib/hooks/useAllowedSlippage.ts
+3
-1
No files found.
src/hooks/useCurrentBlockTimestamp.ts
View file @
96c66a58
import
{
BigNumber
}
from
'
@ethersproject/bignumber
'
import
{
BigNumber
}
from
'
@ethersproject/bignumber
'
import
{
useSingleCallResult
}
from
'
lib/hooks/multicall
'
import
{
useSingleCallResult
}
from
'
lib/hooks/multicall
'
import
{
useEffect
,
useState
}
from
'
react
'
import
{
useInterfaceMulticall
}
from
'
./useContract
'
import
{
useInterfaceMulticall
}
from
'
./useContract
'
// gets the current timestamp from the blockchain
// gets the current timestamp from the blockchain
export
default
function
useCurrentBlockTimestamp
():
BigNumber
|
undefined
{
export
default
function
useCurrentBlockTimestamp
():
BigNumber
|
undefined
{
const
[
lastBlock
,
setLastBlock
]
=
useState
<
BigNumber
|
undefined
>
()
const
multicall
=
useInterfaceMulticall
()
const
multicall
=
useInterfaceMulticall
()
return
useSingleCallResult
(
multicall
,
'
getCurrentBlockTimestamp
'
)?.
result
?.[
0
]
const
block
:
BigNumber
|
undefined
=
useSingleCallResult
(
multicall
,
'
getCurrentBlockTimestamp
'
)?.
result
?.[
0
]
useEffect
(()
=>
{
// If block xor lastBlock are undefined, or if block has progressed, then update lastBlock.
// This prevents updates when the block doesn't change, because the returned BigNumber will still be referentially unique.
if
(
Boolean
(
block
)
!==
Boolean
(
lastBlock
)
||
(
block
&&
lastBlock
&&
!
block
.
eq
(
lastBlock
)))
{
setLastBlock
(
block
)
}
},
[
block
,
lastBlock
])
return
lastBlock
}
}
src/hooks/useUSDCPrice.ts
View file @
96c66a58
...
@@ -78,17 +78,18 @@ export function useStablecoinAmountFromFiatValue(fiatValue: string | null | unde
...
@@ -78,17 +78,18 @@ export function useStablecoinAmountFromFiatValue(fiatValue: string | null | unde
const
{
chainId
}
=
useActiveWeb3React
()
const
{
chainId
}
=
useActiveWeb3React
()
const
stablecoin
=
chainId
?
STABLECOIN_AMOUNT_OUT
[
chainId
]?.
currency
:
undefined
const
stablecoin
=
chainId
?
STABLECOIN_AMOUNT_OUT
[
chainId
]?.
currency
:
undefined
if
(
fiatValue
===
null
||
fiatValue
===
undefined
||
!
chainId
||
!
stablecoin
)
{
return
useMemo
(()
=>
{
return
undefined
if
(
fiatValue
===
null
||
fiatValue
===
undefined
||
!
chainId
||
!
stablecoin
)
{
}
return
undefined
}
// trim for decimal precision when parsing
const
parsedForDecimals
=
parseFloat
(
fiatValue
).
toFixed
(
stablecoin
.
decimals
).
toString
()
try
{
// trim for decimal precision when parsing
// parse USD string into CurrencyAmount based on stablecoin decimals
const
parsedForDecimals
=
parseFloat
(
fiatValue
).
toFixed
(
stablecoin
.
decimals
).
toString
()
return
tryParseCurrencyAmount
(
parsedForDecimals
,
stablecoin
)
try
{
}
catch
(
error
)
{
// parse USD string into CurrencyAmount based on stablecoin decimals
return
undefined
return
tryParseCurrencyAmount
(
parsedForDecimals
,
stablecoin
)
}
}
catch
(
error
)
{
return
undefined
}
},
[
chainId
,
fiatValue
,
stablecoin
])
}
}
src/lib/components/TokenSelect/index.tsx
View file @
96c66a58
...
@@ -113,6 +113,7 @@ export default function TokenSelect({ value, collapsed, disabled, onSelect }: To
...
@@ -113,6 +113,7 @@ export default function TokenSelect({ value, collapsed, disabled, onSelect }: To
usePrefetchBalances
()
usePrefetchBalances
()
const
[
open
,
setOpen
]
=
useState
(
false
)
const
[
open
,
setOpen
]
=
useState
(
false
)
const
onOpen
=
useCallback
(()
=>
setOpen
(
true
),
[])
const
selectAndClose
=
useCallback
(
const
selectAndClose
=
useCallback
(
(
value
:
Currency
)
=>
{
(
value
:
Currency
)
=>
{
onSelect
(
value
)
onSelect
(
value
)
...
@@ -122,7 +123,7 @@ export default function TokenSelect({ value, collapsed, disabled, onSelect }: To
...
@@ -122,7 +123,7 @@ export default function TokenSelect({ value, collapsed, disabled, onSelect }: To
)
)
return
(
return
(
<>
<>
<
TokenButton
value=
{
value
}
collapsed=
{
collapsed
}
disabled=
{
disabled
}
onClick=
{
()
=>
setOpen
(
true
)
}
/>
<
TokenButton
value=
{
value
}
collapsed=
{
collapsed
}
disabled=
{
disabled
}
onClick=
{
onOpen
}
/>
{
open
&&
(
{
open
&&
(
<
Dialog
color=
"module"
onClose=
{
()
=>
setOpen
(
false
)
}
>
<
Dialog
color=
"module"
onClose=
{
()
=>
setOpen
(
false
)
}
>
<
TokenSelectDialog
value=
{
value
}
onSelect=
{
selectAndClose
}
/>
<
TokenSelectDialog
value=
{
value
}
onSelect=
{
selectAndClose
}
/>
...
...
src/lib/hooks/swap/index.ts
View file @
96c66a58
...
@@ -83,7 +83,7 @@ export function useIsAmountPopulated() {
...
@@ -83,7 +83,7 @@ export function useIsAmountPopulated() {
export
function
useSwapAmount
(
field
:
Field
):
[
string
|
undefined
,
(
amount
:
string
)
=>
void
]
{
export
function
useSwapAmount
(
field
:
Field
):
[
string
|
undefined
,
(
amount
:
string
)
=>
void
]
{
const
amount
=
useAtomValue
(
amountAtom
)
const
amount
=
useAtomValue
(
amountAtom
)
const
isFieldIndependent
=
useIsSwapFieldIndependent
(
field
)
const
isFieldIndependent
=
useIsSwapFieldIndependent
(
field
)
const
value
=
useMemo
(()
=>
(
isFieldIndependent
?
amount
:
undefined
),
[
amount
,
isFieldIndependent
])
const
value
=
isFieldIndependent
?
amount
:
undefined
const
updateSwap
=
useUpdateAtom
(
swapAtom
)
const
updateSwap
=
useUpdateAtom
(
swapAtom
)
const
updateAmount
=
useCallback
(
const
updateAmount
=
useCallback
(
(
amount
:
string
)
=>
(
amount
:
string
)
=>
...
@@ -101,8 +101,9 @@ export function useSwapCurrencyAmount(field: Field): CurrencyAmount<Currency> |
...
@@ -101,8 +101,9 @@ export function useSwapCurrencyAmount(field: Field): CurrencyAmount<Currency> |
const
isAmountPopulated
=
useIsAmountPopulated
()
const
isAmountPopulated
=
useIsAmountPopulated
()
const
[
swapAmount
]
=
useSwapAmount
(
field
)
const
[
swapAmount
]
=
useSwapAmount
(
field
)
const
[
swapCurrency
]
=
useSwapCurrency
(
field
)
const
[
swapCurrency
]
=
useSwapCurrency
(
field
)
const
currencyAmount
=
useMemo
(()
=>
tryParseCurrencyAmount
(
swapAmount
,
swapCurrency
),
[
swapAmount
,
swapCurrency
])
if
(
isFieldIndependent
&&
isAmountPopulated
)
{
if
(
isFieldIndependent
&&
isAmountPopulated
)
{
return
tryParseCurrencyAmount
(
swapAmount
,
swapCurrency
)
return
currencyAmount
}
}
return
return
}
}
src/lib/hooks/useAllowedSlippage.ts
View file @
96c66a58
...
@@ -2,6 +2,7 @@ import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
...
@@ -2,6 +2,7 @@ import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
import
useAutoSlippageTolerance
from
'
hooks/useAutoSlippageTolerance
'
import
useAutoSlippageTolerance
from
'
hooks/useAutoSlippageTolerance
'
import
{
useAtomValue
}
from
'
jotai/utils
'
import
{
useAtomValue
}
from
'
jotai/utils
'
import
{
autoSlippageAtom
,
maxSlippageAtom
}
from
'
lib/state/settings
'
import
{
autoSlippageAtom
,
maxSlippageAtom
}
from
'
lib/state/settings
'
import
{
useMemo
}
from
'
react
'
import
{
InterfaceTrade
}
from
'
state/routing/types
'
import
{
InterfaceTrade
}
from
'
state/routing/types
'
export
function
toPercent
(
maxSlippage
:
number
|
undefined
):
Percent
|
undefined
{
export
function
toPercent
(
maxSlippage
:
number
|
undefined
):
Percent
|
undefined
{
...
@@ -13,7 +14,8 @@ export function toPercent(maxSlippage: number | undefined): Percent | undefined
...
@@ -13,7 +14,8 @@ export function toPercent(maxSlippage: number | undefined): Percent | undefined
/** Returns the user-inputted max slippage. */
/** Returns the user-inputted max slippage. */
export
default
function
useAllowedSlippage
(
trade
:
InterfaceTrade
<
Currency
,
Currency
,
TradeType
>
|
undefined
):
Percent
{
export
default
function
useAllowedSlippage
(
trade
:
InterfaceTrade
<
Currency
,
Currency
,
TradeType
>
|
undefined
):
Percent
{
const
autoSlippage
=
useAutoSlippageTolerance
(
trade
)
const
autoSlippage
=
useAutoSlippageTolerance
(
trade
)
const
maxSlippage
=
toPercent
(
useAtomValue
(
maxSlippageAtom
))
const
maxSlippageValue
=
useAtomValue
(
maxSlippageAtom
)
const
maxSlippage
=
useMemo
(()
=>
toPercent
(
maxSlippageValue
),
[
maxSlippageValue
])
return
useAtomValue
(
autoSlippageAtom
)
?
autoSlippage
:
maxSlippage
??
autoSlippage
return
useAtomValue
(
autoSlippageAtom
)
?
autoSlippage
:
maxSlippage
??
autoSlippage
}
}
...
...
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