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
ee693573
Unverified
Commit
ee693573
authored
May 03, 2021
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some minor refactoring with price impact functions
parent
ce7f94f1
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
96 deletions
+30
-96
AdvancedSwapDetails.tsx
src/components/swap/AdvancedSwapDetails.tsx
+11
-18
SwapModalFooter.tsx
src/components/swap/SwapModalFooter.tsx
+2
-9
hooks.ts
src/state/swap/hooks.ts
+1
-7
prices.test.ts
src/utils/prices.test.ts
+4
-4
prices.ts
src/utils/prices.ts
+12
-58
No files found.
src/components/swap/AdvancedSwapDetails.tsx
View file @
ee693573
...
...
@@ -3,7 +3,7 @@ import { Trade as V3Trade } from '@uniswap/v3-sdk'
import
React
,
{
useContext
}
from
'
react
'
import
{
ThemeContext
}
from
'
styled-components
'
import
{
TYPE
}
from
'
../../theme
'
import
{
compute
TradePriceBreakdown
}
from
'
../../utils/prices
'
import
{
compute
RealizedLPFeeAmount
}
from
'
../../utils/prices
'
import
{
AutoColumn
}
from
'
../Column
'
import
{
RowBetween
,
RowFixed
}
from
'
../Row
'
import
SwapRoute
from
'
./SwapRoute
'
...
...
@@ -15,12 +15,7 @@ export interface AdvancedSwapDetailsProps {
export
function
AdvancedSwapDetails
({
trade
}:
AdvancedSwapDetailsProps
)
{
const
theme
=
useContext
(
ThemeContext
)
const
{
realizedLPFee
}
=
computeTradePriceBreakdown
(
trade
)
const
showRoute
=
Boolean
(
(
trade
&&
trade
instanceof
V2Trade
&&
trade
.
route
.
pairs
.
length
>
1
)
||
(
trade
instanceof
V3Trade
&&
trade
.
route
.
pools
.
length
>
1
)
)
const
realizedLPFee
=
computeRealizedLPFeeAmount
(
trade
)
return
!
trade
?
null
:
(
<
AutoColumn
gap=
"8px"
>
...
...
@@ -35,18 +30,16 @@ export function AdvancedSwapDetails({ trade }: AdvancedSwapDetailsProps) {
</
TYPE
.
black
>
</
RowBetween
>
{
showRoute
&&
(
<
RowBetween
>
<
RowFixed
>
<
TYPE
.
black
fontSize=
{
12
}
fontWeight=
{
400
}
color=
{
theme
.
text2
}
>
Route
</
TYPE
.
black
>
</
RowFixed
>
<
TYPE
.
black
fontSize=
{
12
}
color=
{
theme
.
text1
}
>
<
SwapRoute
trade=
{
trade
}
/>
<
RowBetween
>
<
RowFixed
>
<
TYPE
.
black
fontSize=
{
12
}
fontWeight=
{
400
}
color=
{
theme
.
text2
}
>
Route
</
TYPE
.
black
>
</
RowBetween
>
)
}
</
RowFixed
>
<
TYPE
.
black
fontSize=
{
12
}
color=
{
theme
.
text1
}
>
<
SwapRoute
trade=
{
trade
}
/>
</
TYPE
.
black
>
</
RowBetween
>
</
AutoColumn
>
)
}
src/components/swap/SwapModalFooter.tsx
View file @
ee693573
import
{
Trade
as
V2Trade
}
from
'
@uniswap/v2-sdk
'
import
{
Trade
as
V3Trade
}
from
'
@uniswap/v3-sdk
'
import
React
,
{
useMemo
}
from
'
react
'
import
React
from
'
react
'
import
{
Text
}
from
'
rebass
'
import
{
computeTradePriceBreakdown
,
warningSeverity
}
from
'
../../utils/prices
'
import
{
ButtonError
}
from
'
../Button
'
import
{}
from
'
../Column
'
import
{
AutoRow
}
from
'
../Row
'
import
{
SwapCallbackError
}
from
'
./styleds
'
export
default
function
SwapModalFooter
({
trade
,
onConfirm
,
swapErrorMessage
,
disabledConfirm
,
...
...
@@ -20,21 +17,17 @@ export default function SwapModalFooter({
swapErrorMessage
:
string
|
undefined
disabledConfirm
:
boolean
})
{
const
{
priceImpactWithoutFee
}
=
useMemo
(()
=>
computeTradePriceBreakdown
(
trade
),
[
trade
])
const
severity
=
warningSeverity
(
priceImpactWithoutFee
)
return
(
<>
<
AutoRow
>
<
ButtonError
onClick=
{
onConfirm
}
disabled=
{
disabledConfirm
}
error=
{
severity
>
2
}
style=
{
{
margin
:
'
10px 0 0 0
'
}
}
id=
"confirm-swap-or-send"
>
<
Text
fontSize=
{
20
}
fontWeight=
{
500
}
>
{
severity
>
2
?
'
Swap Anyway
'
:
'
Confirm Swap
'
}
Confirm Swap
</
Text
>
</
ButtonError
>
...
...
src/state/swap/hooks.ts
View file @
ee693573
...
...
@@ -17,7 +17,6 @@ import { useCurrencyBalances } from '../wallet/hooks'
import
{
Field
,
replaceSwapState
,
selectCurrency
,
setRecipient
,
switchCurrencies
,
typeInput
}
from
'
./actions
'
import
{
SwapState
}
from
'
./reducer
'
import
{
useUserSlippageTolerance
}
from
'
../user/hooks
'
import
{
computeSlippageAdjustedAmounts
}
from
'
../../utils/prices
'
export
function
useSwapState
():
AppState
[
'
swap
'
]
{
return
useSelector
<
AppState
,
AppState
[
'
swap
'
]
>
((
state
)
=>
state
.
swap
)
...
...
@@ -188,13 +187,8 @@ export function useDerivedSwapInfo(): {
const
[
allowedSlippage
]
=
useUserSlippageTolerance
()
const
slippageAdjustedAmounts
=
v2Trade
&&
allowedSlippage
&&
computeSlippageAdjustedAmounts
(
v2Trade
,
allowedSlippage
)
// compare input balance to max input based on version
const
[
balanceIn
,
amountIn
]
=
[
currencyBalances
[
Field
.
INPUT
],
slippageAdjustedAmounts
?
slippageAdjustedAmounts
[
Field
.
INPUT
]
:
null
,
]
const
[
balanceIn
,
amountIn
]
=
[
currencyBalances
[
Field
.
INPUT
],
v2Trade
?.
maximumAmountIn
(
allowedSlippage
)]
if
(
balanceIn
&&
amountIn
&&
balanceIn
.
lessThan
(
amountIn
))
{
inputError
=
'
Insufficient
'
+
amountIn
.
currency
.
symbol
+
'
balance
'
...
...
src/utils/prices.test.ts
View file @
ee693573
import
{
ChainId
,
Percent
,
Token
,
TokenAmount
,
TradeType
}
from
'
@uniswap/sdk-core
'
import
{
JSBI
,
Trade
,
Pair
,
Route
}
from
'
@uniswap/v2-sdk
'
import
{
compute
TradePriceBreakdown
,
warningSeverity
}
from
'
./prices
'
import
{
compute
RealizedLPFeeAmount
,
warningSeverity
}
from
'
./prices
'
describe
(
'
prices
'
,
()
=>
{
const
token1
=
new
Token
(
ChainId
.
MAINNET
,
'
0x0000000000000000000000000000000000000001
'
,
18
)
...
...
@@ -12,7 +12,7 @@ describe('prices', () => {
describe
(
'
#computeTradePriceBreakdown
'
,
()
=>
{
it
(
'
returns undefined for undefined
'
,
()
=>
{
expect
(
compute
TradePriceBreakdown
(
undefined
)).
toEqual
({
expect
(
compute
RealizedLPFeeAmount
(
undefined
)).
toEqual
({
priceImpactWithoutFee
:
undefined
,
realizedLPFee
:
undefined
,
})
...
...
@@ -20,7 +20,7 @@ describe('prices', () => {
it
(
'
correct realized lp fee for single hop
'
,
()
=>
{
expect
(
compute
TradePriceBreakdown
(
compute
RealizedLPFeeAmount
(
new
Trade
(
new
Route
([
pair12
],
token1
),
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
1000
)),
TradeType
.
EXACT_INPUT
)
).
realizedLPFee
).
toEqual
(
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
3
)))
...
...
@@ -28,7 +28,7 @@ describe('prices', () => {
it
(
'
correct realized lp fee for double hop
'
,
()
=>
{
expect
(
compute
TradePriceBreakdown
(
compute
RealizedLPFeeAmount
(
new
Trade
(
new
Route
([
pair12
,
pair23
],
token1
),
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
1000
)),
...
...
src/utils/prices.ts
View file @
ee693573
import
JSBI
from
'
jsbi
'
import
{
BLOCKED_PRICE_IMPACT_NON_EXPERT
,
ZERO_PERCENT
}
from
'
../constants
'
import
{
CurrencyAmount
,
Fraction
,
Percent
,
TokenAmount
}
from
'
@uniswap/sdk-core
'
import
{
Trade
as
V2Trade
}
from
'
@uniswap/v2-sdk
'
import
{
Trade
as
V3Trade
}
from
'
@uniswap/v3-sdk
'
import
{
ALLOWED_PRICE_IMPACT_HIGH
,
ALLOWED_PRICE_IMPACT_LOW
,
ALLOWED_PRICE_IMPACT_MEDIUM
}
from
'
../constants
'
import
{
Field
}
from
'
../state/swap/actions
'
import
{
ALLOWED_PRICE_IMPACT_HIGH
,
ALLOWED_PRICE_IMPACT_LOW
,
ALLOWED_PRICE_IMPACT_MEDIUM
,
BLOCKED_PRICE_IMPACT_NON_EXPERT
,
}
from
'
../constants
'
const
THIRTY_BIPS_FEE
=
new
Percent
(
JSBI
.
BigInt
(
30
),
JSBI
.
BigInt
(
10000
))
const
ONE_HUNDRED_PERCENT
=
new
Percent
(
JSBI
.
BigInt
(
10000
),
JSBI
.
BigInt
(
10000
))
const
INPUT_FRACTION_AFTER_FEE
=
ONE_HUNDRED_PERCENT
.
subtract
(
THIRTY_BIPS_FEE
)
// computes price breakdown for the trade
export
function
computeTradePriceBreakdown
(
trade
?:
V2Trade
|
V3Trade
|
null
):
{
priceImpactWithoutFee
:
Percent
|
undefined
;
realizedLPFee
:
CurrencyAmount
|
undefined
|
null
}
{
export
function
computeRealizedLPFeeAmount
(
trade
?:
V2Trade
|
V3Trade
|
null
):
CurrencyAmount
|
undefined
{
if
(
trade
instanceof
V2Trade
)
{
// for each hop in our trade, take away the x*y=k price impact from 0.3% fees
// e.g. for 3 tokens/2 hops: 1 - ((1 - .03) * (1-.03))
...
...
@@ -27,23 +27,14 @@ export function computeTradePriceBreakdown(
)
)
// remove lp fees from price impact
const
priceImpactWithoutFeeFraction
=
trade
&&
realizedLPFee
?
trade
.
priceImpact
.
subtract
(
realizedLPFee
)
:
undefined
// the x*y=k impact
const
priceImpactWithoutFeePercent
=
priceImpactWithoutFeeFraction
?
new
Percent
(
priceImpactWithoutFeeFraction
?.
numerator
,
priceImpactWithoutFeeFraction
?.
denominator
)
:
undefined
// the amount of the input that accrues to LPs
const
realizedLPFeeAmount
=
return
(
realizedLPFee
&&
trade
&&
(
trade
.
inputAmount
instanceof
TokenAmount
?
new
TokenAmount
(
trade
.
inputAmount
.
token
,
realizedLPFee
.
multiply
(
trade
.
inputAmount
.
raw
).
quotient
)
:
CurrencyAmount
.
ether
(
realizedLPFee
.
multiply
(
trade
.
inputAmount
.
raw
).
quotient
))
return
{
priceImpactWithoutFee
:
priceImpactWithoutFeePercent
,
realizedLPFee
:
realizedLPFeeAmount
}
)
}
else
if
(
trade
instanceof
V3Trade
)
{
const
realizedLPFee
=
!
trade
?
undefined
...
...
@@ -54,35 +45,15 @@ export function computeTradePriceBreakdown(
ONE_HUNDRED_PERCENT
)
)
const
realizedLPFeeAmount
=
return
(
realizedLPFee
&&
trade
&&
(
trade
.
inputAmount
instanceof
TokenAmount
?
new
TokenAmount
(
trade
.
inputAmount
.
token
,
realizedLPFee
.
multiply
(
trade
.
inputAmount
.
raw
).
quotient
)
:
CurrencyAmount
.
ether
(
realizedLPFee
.
multiply
(
trade
.
inputAmount
.
raw
).
quotient
))
return
{
// TODO: real price impact
priceImpactWithoutFee
:
new
Percent
(
0
),
realizedLPFee
:
realizedLPFeeAmount
,
}
}
else
{
return
{
priceImpactWithoutFee
:
undefined
,
realizedLPFee
:
undefined
,
}
}
}
// computes the minimum amount out and maximum amount in for a trade given a user specified allowed slippage in bips
export
function
computeSlippageAdjustedAmounts
(
trade
:
V2Trade
|
V3Trade
|
undefined
,
allowedSlippage
:
Percent
):
{
[
field
in
Field
]?:
CurrencyAmount
}
{
return
{
[
Field
.
INPUT
]:
trade
?.
maximumAmountIn
(
allowedSlippage
),
[
Field
.
OUTPUT
]:
trade
?.
minimumAmountOut
(
allowedSlippage
),
)
}
return
undefined
}
const
IMPACT_TIERS
=
[
...
...
@@ -102,20 +73,3 @@ export function warningSeverity(priceImpact: Percent | undefined): WarningSeveri
}
return
0
}
export
function
formatExecutionPrice
(
trade
:
V2Trade
|
V3Trade
|
undefined
,
inverted
:
boolean
|
undefined
,
slippageTolerance
:
Percent
=
ZERO_PERCENT
):
string
{
if
(
!
trade
)
{
return
''
}
return
inverted
?
`
${
trade
.
worstExecutionPrice
(
slippageTolerance
).
invert
().
toSignificant
(
6
)}
${
trade
.
inputAmount
.
currency
.
symbol
}
/
${
trade
.
outputAmount
.
currency
.
symbol
}
`
:
`
${
trade
.
worstExecutionPrice
(
slippageTolerance
).
toSignificant
(
6
)}
${
trade
.
outputAmount
.
currency
.
symbol
}
/
${
trade
.
inputAmount
.
currency
.
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