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
b722a20d
Unverified
Commit
b722a20d
authored
Jul 17, 2023
by
Jack Short
Committed by
GitHub
Jul 17, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: swap divide by zero (#6922)
* fix: swap divide by zero * putting evaluation in memo
parent
9b5261aa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
2 deletions
+46
-2
TradePrice.test.tsx
src/components/swap/TradePrice.test.tsx
+38
-0
TradePrice.tsx
src/components/swap/TradePrice.tsx
+8
-2
No files found.
src/components/swap/TradePrice.test.tsx
0 → 100644
View file @
b722a20d
import
{
Price
,
WETH9
}
from
'
@uniswap/sdk-core
'
import
{
USDC_MAINNET
}
from
'
constants/tokens
'
import
{
fireEvent
,
render
,
screen
}
from
'
test-utils/render
'
import
TradePrice
from
'
./TradePrice
'
const
price
=
new
Price
(
WETH9
[
1
],
USDC_MAINNET
,
100000000000
,
5
)
const
zeroedNumeratorPrice
=
new
Price
(
WETH9
[
1
],
USDC_MAINNET
,
100000000000
,
0
)
const
zeroedDenominatorPrice
=
new
Price
(
WETH9
[
1
],
USDC_MAINNET
,
0
,
5
)
describe
(
'
trade price
'
,
()
=>
{
it
(
'
correctly renders the trade price
'
,
()
=>
{
render
(<
TradePrice
price=
{
price
}
/>)
const
tradePriceToggle
=
screen
.
getByText
(
'
1 USDC = 0.02 WETH
'
)
as
HTMLButtonElement
expect
(
tradePriceToggle
).
toBeInTheDocument
()
fireEvent
.
click
(
tradePriceToggle
)
expect
(
screen
.
getByText
(
'
1 WETH = 50.0 USDC
'
)).
toBeInTheDocument
()
})
it
(
'
handles zeroed numerator
'
,
()
=>
{
render
(<
TradePrice
price=
{
zeroedNumeratorPrice
}
/>)
const
tradePriceToggle
=
screen
.
getByText
(
'
1 USDC = 0 WETH
'
)
as
HTMLButtonElement
expect
(
tradePriceToggle
).
toBeInTheDocument
()
fireEvent
.
click
(
tradePriceToggle
)
expect
(
screen
.
getByText
(
'
1 WETH = 0 USDC
'
)).
toBeInTheDocument
()
})
it
(
'
handles zeroed denominator
'
,
()
=>
{
render
(<
TradePrice
price=
{
zeroedDenominatorPrice
}
/>)
const
tradePriceToggle
=
screen
.
getByText
(
'
1 USDC = 0 WETH
'
)
as
HTMLButtonElement
expect
(
tradePriceToggle
).
toBeInTheDocument
()
fireEvent
.
click
(
tradePriceToggle
)
expect
(
screen
.
getByText
(
'
1 WETH = 0 USDC
'
)).
toBeInTheDocument
()
})
})
src/components/swap/TradePrice.tsx
View file @
b722a20d
...
...
@@ -3,7 +3,7 @@ import { formatNumber, formatPrice, NumberType } from '@uniswap/conedison/format
import
{
Currency
,
Price
}
from
'
@uniswap/sdk-core
'
import
{
useUSDPrice
}
from
'
hooks/useUSDPrice
'
import
tryParseCurrencyAmount
from
'
lib/utils/tryParseCurrencyAmount
'
import
{
useCallback
,
useState
}
from
'
react
'
import
{
useCallback
,
use
Memo
,
use
State
}
from
'
react
'
import
styled
from
'
styled-components/macro
'
import
{
ThemedText
}
from
'
theme
'
...
...
@@ -33,7 +33,13 @@ export default function TradePrice({ price }: TradePriceProps) {
const
{
baseCurrency
,
quoteCurrency
}
=
price
const
{
data
:
usdPrice
}
=
useUSDPrice
(
tryParseCurrencyAmount
(
'
1
'
,
showInverted
?
baseCurrency
:
quoteCurrency
))
const
formattedPrice
=
formatPrice
(
showInverted
?
price
:
price
.
invert
(),
NumberType
.
TokenTx
)
const
formattedPrice
=
useMemo
(()
=>
{
try
{
return
formatPrice
(
showInverted
?
price
:
price
.
invert
(),
NumberType
.
TokenTx
)
}
catch
{
return
'
0
'
}
},
[
price
,
showInverted
])
const
label
=
showInverted
?
`
${
price
.
quoteCurrency
?.
symbol
}
` : `
$
{
price
.
baseCurrency
?.
symbol
}
`
const labelInverted = showInverted ? `
$
{
price
.
baseCurrency
?.
symbol
}
` : `
$
{
price
.
quoteCurrency
?.
symbol
}
`
...
...
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