-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app): trade panel enhancements (#2798)
Trade Panel Enhancements
- Loading branch information
Showing
6 changed files
with
92 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 27 additions & 11 deletions
38
packages/app/src/sections/futures/Trade/ShowPercentage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,61 @@ | ||
import { PositionSide } from '@kwenta/sdk/types' | ||
import { formatPercent } from '@kwenta/sdk/utils' | ||
import { formatPercent, formatDollars } from '@kwenta/sdk/utils' | ||
import Wei, { wei } from '@synthetixio/wei' | ||
import { useMemo } from 'react' | ||
import styled from 'styled-components' | ||
|
||
import { Body } from 'components/Text' | ||
|
||
type ShowPercentageProps = { | ||
targetPrice: string | ||
isStopLoss?: boolean | ||
currentPrice: Wei | ||
price: Wei | ||
leverageSide: PositionSide | string | undefined | ||
leverageWei: Wei | ||
sizeWei: Wei | ||
} | ||
|
||
const ShowPercentage: React.FC<ShowPercentageProps> = ({ | ||
targetPrice, | ||
isStopLoss = false, | ||
currentPrice, | ||
price, | ||
leverageSide, | ||
leverageWei, | ||
sizeWei, | ||
}) => { | ||
const calculatePercentage = useMemo(() => { | ||
if (!targetPrice || !currentPrice || !leverageSide) return '' | ||
const [calculatePercentage, calculatePL] = useMemo(() => { | ||
if (!targetPrice || !price || !leverageSide || !sizeWei) return '' | ||
const priceWei = wei(targetPrice) | ||
|
||
const diff = | ||
leverageSide === 'short' | ||
? isStopLoss | ||
? priceWei.sub(currentPrice) | ||
: currentPrice.sub(priceWei) | ||
? priceWei.sub(price) | ||
: price.sub(priceWei) | ||
: isStopLoss | ||
? currentPrice.sub(priceWei) | ||
: priceWei.sub(currentPrice) | ||
? price.sub(priceWei) | ||
: priceWei.sub(price) | ||
|
||
const percentage = diff.div(price).mul(leverageWei) | ||
const profitLoss = sizeWei.mul(percentage.div(leverageWei)).mul(isStopLoss ? -1 : 1) | ||
|
||
return formatPercent(diff.div(currentPrice).mul(leverageWei)) | ||
}, [currentPrice, isStopLoss, leverageSide, leverageWei, targetPrice]) | ||
return [formatPercent(percentage), formatDollars(profitLoss, { sign: isStopLoss ? '' : '+' })] | ||
}, [price, isStopLoss, leverageSide, leverageWei, targetPrice, sizeWei]) | ||
|
||
return ( | ||
<Body size="large" mono> | ||
<ProfitLoss isStopLoss={isStopLoss}>{calculatePL}</ProfitLoss> | ||
{calculatePercentage} | ||
</Body> | ||
) | ||
} | ||
|
||
const ProfitLoss = styled.span<{ isStopLoss: boolean }>` | ||
margin-right: 0.7rem; | ||
color: ${({ theme, isStopLoss }) => | ||
isStopLoss | ||
? theme.colors.selectedTheme.newTheme.text.negative | ||
: theme.colors.selectedTheme.newTheme.text.positive}; | ||
` | ||
|
||
export default ShowPercentage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters