Commit 3c3be3c6 authored by Moody Salem's avatar Moody Salem

fix some unit tests

parent 7660943e
......@@ -27,12 +27,12 @@ interface SwapHeaderProps {
trade?: V2Trade | V3Trade | undefined
}
export default function SwapHeader({}: SwapHeaderProps) {
export default function SwapHeader({ trade }: SwapHeaderProps) {
return (
<StyledSwapHeader>
<RowBetween>
<TYPE.black fontWeight={500} fontSize={16} style={{ opacity: '0.6' }}>
Swap
Swap {trade instanceof V2Trade ? '(V2)' : trade instanceof V3Trade ? '(V3)' : ''}
</TYPE.black>
<RowFixed>
{/* Send icon appears here when expert mode is toggled on */}
......
......@@ -305,6 +305,8 @@ export default function Swap({ history }: RouteComponentProps) {
const [showDetails, setShowDetails] = useState<boolean>(true)
const priceImpactTooHigh = priceImpactSeverity > 3 && !isExpertMode
return (
<>
<TokenWarningModal
......@@ -468,17 +470,11 @@ export default function Swap({ history }: RouteComponentProps) {
}}
width="100%"
id="swap-button"
disabled={
!isValid ||
approvalState !== ApprovalState.APPROVED ||
(priceImpactSeverity > 3 && !isExpertMode)
}
disabled={!isValid || approvalState !== ApprovalState.APPROVED || priceImpactTooHigh}
error={isValid && priceImpactSeverity > 2}
>
<Text fontSize={16} fontWeight={500}>
{priceImpactSeverity > 3 && !isExpertMode
? `Price Impact High`
: `Swap${priceImpactSeverity > 2 ? ' Anyway' : ''}`}
{priceImpactTooHigh ? `Price Impact High` : `Swap${priceImpactSeverity > 2 ? ' Anyway' : ''}`}
</Text>
</ButtonError>
</AutoColumn>
......@@ -500,13 +496,13 @@ export default function Swap({ history }: RouteComponentProps) {
}
}}
id="swap-button"
disabled={!isValid || (priceImpactSeverity > 3 && !isExpertMode) || !!swapCallbackError}
disabled={!isValid || priceImpactTooHigh || !!swapCallbackError}
error={isValid && priceImpactSeverity > 2 && !swapCallbackError}
>
<Text fontSize={20} fontWeight={500}>
{swapInputError
? swapInputError
: priceImpactSeverity > 3 && !isExpertMode
: priceImpactTooHigh
? `Price Impact Too High`
: `Swap${priceImpactSeverity > 2 ? ' Anyway' : ''}`}
</Text>
......
import { ChainId, Token, TokenAmount, TradeType } from '@uniswap/sdk-core'
import { ChainId, Percent, Token, TokenAmount, TradeType } from '@uniswap/sdk-core'
import { JSBI, Trade, Pair, Route } from '@uniswap/v2-sdk'
import { computeTradePriceBreakdown } from './prices'
import { computeTradePriceBreakdown, warningSeverity } from './prices'
describe('prices', () => {
const token1 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000001', 18)
......@@ -10,7 +10,7 @@ describe('prices', () => {
const pair12 = new Pair(new TokenAmount(token1, JSBI.BigInt(10000)), new TokenAmount(token2, JSBI.BigInt(20000)))
const pair23 = new Pair(new TokenAmount(token2, JSBI.BigInt(20000)), new TokenAmount(token3, JSBI.BigInt(30000)))
describe('computeTradePriceBreakdown', () => {
describe('#computeTradePriceBreakdown', () => {
it('returns undefined for undefined', () => {
expect(computeTradePriceBreakdown(undefined)).toEqual({
priceImpactWithoutFee: undefined,
......@@ -38,4 +38,22 @@ describe('prices', () => {
).toEqual(new TokenAmount(token1, JSBI.BigInt(5)))
})
})
describe('#warningSeverity', () => {
it('max for undefined', () => {
expect(warningSeverity(undefined)).toEqual(4)
})
it('correct for 0', () => {
expect(warningSeverity(new Percent(0))).toEqual(0)
})
it('correct for 0.5', () => {
expect(warningSeverity(new Percent(5, 1000))).toEqual(0)
})
it('correct for 5', () => {
expect(warningSeverity(new Percent(5, 100))).toEqual(2)
})
it('correct for 50', () => {
expect(warningSeverity(new Percent(5, 10))).toEqual(4)
})
})
})
......@@ -45,7 +45,7 @@ export function computeTradePriceBreakdown(
: CurrencyAmount.ether(realizedLPFee.multiply(trade.inputAmount.raw).quotient))
return { priceImpactWithoutFee: priceImpactWithoutFeePercent, realizedLPFee: realizedLPFeeAmount }
} else {
} else if (trade instanceof V3Trade) {
const realizedLPFee = !trade
? undefined
: ONE_HUNDRED_PERCENT.subtract(
......@@ -67,6 +67,11 @@ export function computeTradePriceBreakdown(
priceImpactWithoutFee: new Percent(0),
realizedLPFee: realizedLPFeeAmount,
}
} else {
return {
priceImpactWithoutFee: undefined,
realizedLPFee: undefined,
}
}
}
......@@ -93,7 +98,7 @@ export function warningSeverity(priceImpact: Percent | undefined): 0 | 1 | 2 | 3
if (!priceImpact) return 4
let impact = IMPACT_TIERS.length
for (const impactLevel of IMPACT_TIERS) {
if (priceImpact.lessThan(impactLevel)) return impact as 0 | 1 | 2 | 3 | 4
if (impactLevel.lessThan(priceImpact)) return impact as 0 | 1 | 2 | 3 | 4
impact--
}
return 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment