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

fix some unit tests

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