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
78b6f5c7
Unverified
Commit
78b6f5c7
authored
Mar 11, 2022
by
Zach Pomerantz
Committed by
GitHub
Mar 11, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: destructure currencies for ref equality (#3498)
parent
f9fb71a8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
38 deletions
+25
-38
useSwapInfo.tsx
src/lib/hooks/swap/useSwapInfo.tsx
+25
-38
No files found.
src/lib/hooks/swap/useSwapInfo.tsx
View file @
78b6f5c7
...
@@ -36,66 +36,53 @@ const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = {
...
@@ -36,66 +36,53 @@ const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = {
// from the current swap inputs, compute the best trade and return it.
// from the current swap inputs, compute the best trade and return it.
function
useComputeSwapInfo
():
SwapInfo
{
function
useComputeSwapInfo
():
SwapInfo
{
const
{
account
}
=
useActiveWeb3React
()
const
{
account
}
=
useActiveWeb3React
()
const
{
type
:
wrapType
}
=
useWrapCallback
()
const
isWrapping
=
wrapType
===
WrapType
.
WRAP
||
wrapType
===
WrapType
.
UNWRAP
const
{
const
{
independentField
,
independentField
,
amount
,
amount
,
[
Field
.
INPUT
]:
inputCurrency
,
[
Field
.
INPUT
]:
inputCurrency
,
[
Field
.
OUTPUT
]:
outputCurrency
,
[
Field
.
OUTPUT
]:
outputCurrency
,
}
=
useAtomValue
(
swapAtom
)
}
=
useAtomValue
(
swapAtom
)
const
isExactIn
=
independentField
===
Field
.
INPUT
const
feeOptions
=
useAtomValue
(
feeOptionsAtom
)
const
feeOptions
=
useAtomValue
(
feeOptionsAtom
)
const
to
=
account
const
relevantTokenBalances
=
useCurrencyBalances
(
account
,
useMemo
(()
=>
[
inputCurrency
??
undefined
,
outputCurrency
??
undefined
],
[
inputCurrency
,
outputCurrency
])
)
const
isExactIn
=
independentField
===
Field
.
INPUT
const
parsedAmount
=
useMemo
(
const
parsedAmount
=
useMemo
(
()
=>
tryParseCurrencyAmount
(
amount
,
(
isExactIn
?
inputCurrency
:
outputCurrency
)
??
undefined
),
()
=>
tryParseCurrencyAmount
(
amount
,
(
isExactIn
?
inputCurrency
:
outputCurrency
)
??
undefined
),
[
inputCurrency
,
isExactIn
,
outputCurrency
,
amount
]
[
inputCurrency
,
isExactIn
,
outputCurrency
,
amount
]
)
)
// TODO(ianlapham): this would eventually be replaced with routing api logic.
//@TODO(ianlapham): this would eventually be replaced with routing api logic.
const
trade
=
useBestTrade
(
const
trade
=
useBestTrade
(
isExactIn
?
TradeType
.
EXACT_INPUT
:
TradeType
.
EXACT_OUTPUT
,
isExactIn
?
TradeType
.
EXACT_INPUT
:
TradeType
.
EXACT_OUTPUT
,
parsedAmount
,
parsedAmount
,
(
isExactIn
?
outputCurrency
:
inputCurrency
)
??
undefined
(
isExactIn
?
outputCurrency
:
inputCurrency
)
??
undefined
)
)
const
currencies
=
useMemo
(
()
=>
({
[
Field
.
INPUT
]:
inputCurrency
??
undefined
,
[
Field
.
OUTPUT
]:
outputCurrency
??
undefined
,
}),
[
inputCurrency
,
outputCurrency
]
)
const
currencyBalances
=
useMemo
(
()
=>
({
[
Field
.
INPUT
]:
relevantTokenBalances
[
0
],
[
Field
.
OUTPUT
]:
relevantTokenBalances
[
1
],
}),
[
relevantTokenBalances
]
)
// Use same amount for input and output if user is wrapping.
const
{
type
:
wrapType
}
=
useWrapCallback
()
const
isWrapping
=
wrapType
===
WrapType
.
WRAP
||
wrapType
===
WrapType
.
UNWRAP
const
tradeCurrencyAmounts
=
useMemo
(
const
tradeCurrencyAmounts
=
useMemo
(
()
=>
({
()
=>
({
// Use same amount for input and output if user is wrapping.
[
Field
.
INPUT
]:
isWrapping
?
parsedAmount
:
trade
.
trade
?.
inputAmount
,
[
Field
.
INPUT
]:
isWrapping
?
parsedAmount
:
trade
.
trade
?.
inputAmount
,
[
Field
.
OUTPUT
]:
isWrapping
?
parsedAmount
:
trade
.
trade
?.
outputAmount
,
[
Field
.
OUTPUT
]:
isWrapping
?
parsedAmount
:
trade
.
trade
?.
outputAmount
,
}),
}),
[
isWrapping
,
parsedAmount
,
trade
.
trade
?.
inputAmount
,
trade
.
trade
?.
outputAmount
]
[
isWrapping
,
parsedAmount
,
trade
.
trade
?.
inputAmount
,
trade
.
trade
?.
outputAmount
]
)
)
const
slippage
=
useSlippage
(
trade
.
trade
)
const
slippage
=
useSlippage
(
trade
.
trade
)
const
currencies
=
useMemo
(
()
=>
({
[
Field
.
INPUT
]:
inputCurrency
,
[
Field
.
OUTPUT
]:
outputCurrency
}),
[
inputCurrency
,
outputCurrency
]
)
const
[
inputCurrencyBalance
,
outputCurrencyBalance
]
=
useCurrencyBalances
(
account
,
useMemo
(()
=>
[
inputCurrency
,
outputCurrency
],
[
inputCurrency
,
outputCurrency
])
)
const
currencyBalances
=
useMemo
(
()
=>
({
[
Field
.
INPUT
]:
inputCurrencyBalance
,
[
Field
.
OUTPUT
]:
outputCurrencyBalance
,
}),
[
inputCurrencyBalance
,
outputCurrencyBalance
]
)
const
inputError
=
useMemo
(()
=>
{
const
inputError
=
useMemo
(()
=>
{
let
inputError
:
ReactNode
|
undefined
let
inputError
:
ReactNode
|
undefined
...
@@ -111,11 +98,11 @@ function useComputeSwapInfo(): SwapInfo {
...
@@ -111,11 +98,11 @@ function useComputeSwapInfo(): SwapInfo {
inputError
=
inputError
??
<
Trans
>
Enter an amount
</
Trans
>
inputError
=
inputError
??
<
Trans
>
Enter an amount
</
Trans
>
}
}
const
formatted
To
=
isAddress
(
to
)
const
formatted
Address
=
isAddress
(
account
)
if
(
!
to
||
!
formattedTo
)
{
if
(
!
account
||
!
formattedAddress
)
{
inputError
=
inputError
??
<
Trans
>
Enter a recipient
</
Trans
>
inputError
=
inputError
??
<
Trans
>
Enter a recipient
</
Trans
>
}
else
{
}
else
{
if
(
BAD_RECIPIENT_ADDRESSES
[
formatted
To
])
{
if
(
BAD_RECIPIENT_ADDRESSES
[
formatted
Address
])
{
inputError
=
inputError
??
<
Trans
>
Invalid recipient
</
Trans
>
inputError
=
inputError
??
<
Trans
>
Invalid recipient
</
Trans
>
}
}
}
}
...
@@ -128,7 +115,7 @@ function useComputeSwapInfo(): SwapInfo {
...
@@ -128,7 +115,7 @@ function useComputeSwapInfo(): SwapInfo {
}
}
return
inputError
return
inputError
},
[
account
,
slippage
.
allowed
,
currencies
,
currencyBalances
,
parsedAmount
,
t
o
,
t
rade
.
trade
])
},
[
account
,
slippage
.
allowed
,
currencies
,
currencyBalances
,
parsedAmount
,
trade
.
trade
])
return
useMemo
(
return
useMemo
(
()
=>
({
()
=>
({
...
...
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