move the compute price impact function out to a utility file
This commit is contained in:
parent
28c7cfa1f1
commit
ab5114c5f5
@ -1,10 +1,10 @@
|
||||
import { CurrencyAmount, Percent, Price } from '@uniswap/sdk-core'
|
||||
import { Trade as V2Trade } from '@uniswap/v2-sdk'
|
||||
import { Trade as V3Trade } from '@uniswap/v3-sdk'
|
||||
import React, { useContext } from 'react'
|
||||
import { ThemeContext } from 'styled-components'
|
||||
import { useUserSlippageTolerance } from '../../state/user/hooks'
|
||||
import { TYPE } from '../../theme'
|
||||
import { computePriceImpactWithMaximumSlippage } from '../../utils/computePriceImpactWithMaximumSlippage'
|
||||
import { computeRealizedLPFeeAmount } from '../../utils/prices'
|
||||
import { AutoColumn } from '../Column'
|
||||
import { RowBetween, RowFixed } from '../Row'
|
||||
@ -15,21 +15,6 @@ export interface AdvancedSwapDetailsProps {
|
||||
trade?: V2Trade | V3Trade
|
||||
}
|
||||
|
||||
function computePriceImpact(midPrice: Price, inputAmount: CurrencyAmount, outputAmount: CurrencyAmount): Percent {
|
||||
const exactQuote = midPrice.raw.multiply(inputAmount.raw)
|
||||
// calculate slippage := (exactQuote - outputAmount) / exactQuote
|
||||
const slippage = exactQuote.subtract(outputAmount.raw).divide(exactQuote)
|
||||
return new Percent(slippage.numerator, slippage.denominator)
|
||||
}
|
||||
|
||||
function computePriceImpactWithMaximumSlippage(trade: V2Trade | V3Trade, allowedSlippage: Percent): Percent {
|
||||
return computePriceImpact(
|
||||
trade.route.midPrice,
|
||||
trade.maximumAmountIn(allowedSlippage),
|
||||
trade.minimumAmountOut(allowedSlippage)
|
||||
)
|
||||
}
|
||||
|
||||
export function AdvancedSwapDetails({ trade }: AdvancedSwapDetailsProps) {
|
||||
const theme = useContext(ThemeContext)
|
||||
|
||||
|
18
src/utils/computePriceImpactWithMaximumSlippage.tsx
Normal file
18
src/utils/computePriceImpactWithMaximumSlippage.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import { CurrencyAmount, Percent, Price } from '@uniswap/sdk-core'
|
||||
import { Trade as V2Trade } from '@uniswap/v2-sdk'
|
||||
import { Trade as V3Trade } from '@uniswap/v3-sdk'
|
||||
|
||||
function computePriceImpact(midPrice: Price, inputAmount: CurrencyAmount, outputAmount: CurrencyAmount): Percent {
|
||||
const exactQuote = midPrice.raw.multiply(inputAmount.raw)
|
||||
// calculate slippage := (exactQuote - outputAmount) / exactQuote
|
||||
const slippage = exactQuote.subtract(outputAmount.raw).divide(exactQuote)
|
||||
return new Percent(slippage.numerator, slippage.denominator)
|
||||
}
|
||||
|
||||
export function computePriceImpactWithMaximumSlippage(trade: V2Trade | V3Trade, allowedSlippage: Percent): Percent {
|
||||
return computePriceImpact(
|
||||
trade.route.midPrice,
|
||||
trade.maximumAmountIn(allowedSlippage),
|
||||
trade.minimumAmountOut(allowedSlippage)
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user