11"use client" ;
22
3- import { useState , useEffect } from "react" ;
3+ import { useState , useEffect , useMemo } from "react" ;
44import axios from "axios" ;
55import type { SYMBOL } from "../utils/constants" ;
66import { createTrade , findUserAmount } from "../api/trade" ;
77import { getAssetDetails } from "../api/trade" ;
88import type { Asset } from "../types/asset" ;
9- import { toDisplayPriceUSD } from "../utils/utils" ;
9+ import {
10+ calculatePnlCents ,
11+ toDisplayPriceUSD ,
12+ toInternalPrice ,
13+ } from "../utils/utils" ;
1014
1115export default function BuySell ( {
1216 buyPrice,
@@ -69,6 +73,48 @@ export default function BuySell({
6973 } ;
7074 } , [ symbol ] ) ;
7175
76+ const estimatedTpPnlInCents = useMemo ( ( ) => {
77+ if ( ! tpEnabled || ! tpPrice || Number ( tpPrice ) <= 0 ) return 0 ;
78+
79+ // 1. Convert all inputs to the correct scaled-integer format
80+ const openPriceForCalc =
81+ activeTab === "buy"
82+ ? toInternalPrice ( buyPrice )
83+ : toInternalPrice ( sellPrice ) ;
84+ const closePriceForCalc = toInternalPrice ( Number ( tpPrice ) ) ;
85+ const marginForCalc = margin * 100 ; // Convert dollar margin to cents
86+
87+ // 2. Call the exact same robust function used everywhere else
88+ return calculatePnlCents ( {
89+ side : activeTab ,
90+ openPrice : openPriceForCalc ,
91+ closePrice : closePriceForCalc ,
92+ marginCents : marginForCalc ,
93+ leverage : leverage ,
94+ } ) ;
95+ } , [ tpEnabled , tpPrice , activeTab , buyPrice , sellPrice , margin , leverage ] ) ;
96+
97+ const estimatedSlPnlInCents = useMemo ( ( ) => {
98+ if ( ! slEnabled || ! slPrice || Number ( slPrice ) <= 0 ) return 0 ;
99+
100+ // 1. Convert all inputs to the correct scaled-integer format
101+ const openPriceForCalc =
102+ activeTab === "buy"
103+ ? toInternalPrice ( buyPrice )
104+ : toInternalPrice ( sellPrice ) ;
105+ const closePriceForCalc = toInternalPrice ( Number ( slPrice ) ) ;
106+ const marginForCalc = margin * 100 ; // Convert dollar margin to cents
107+
108+ // 2. Call the robust P&L function
109+ return calculatePnlCents ( {
110+ side : activeTab ,
111+ openPrice : openPriceForCalc ,
112+ closePrice : closePriceForCalc ,
113+ marginCents : marginForCalc ,
114+ leverage : leverage ,
115+ } ) ;
116+ } , [ slEnabled , slPrice , activeTab , buyPrice , sellPrice , margin , leverage ] ) ;
117+
72118 const handleSubmitTrade = async ( ) => {
73119 if ( margin <= 0 ) {
74120 setError ( "Margin must be greater than 0" ) ;
@@ -197,7 +243,7 @@ export default function BuySell({
197243 </ div >
198244 < div className = "mt-1 text-base font-semibold text-[#EB483F] flex items-center" >
199245 < span className = "text-xs mr-1" > $</ span >
200- { buyPrice }
246+ { sellPrice }
201247 </ div >
202248 < div className = "absolute w-1 h-full bg-[#EB483F]/40 left-0 top-0" > </ div >
203249 </ div >
@@ -210,7 +256,7 @@ export default function BuySell({
210256 </ div >
211257 < div className = "mt-1 text-base font-semibold text-[#158BF9] flex items-center" >
212258 < span className = "text-xs mr-1" > $</ span >
213- { sellPrice }
259+ { buyPrice }
214260 </ div >
215261 < div className = "absolute w-1 h-full bg-[#158BF9]/40 left-0 top-0" > </ div >
216262 </ div >
@@ -491,20 +537,29 @@ export default function BuySell({
491537 </ div >
492538
493539 { tpEnabled && (
494- < div className = "mt-1.5 flex items-center justify-between" >
495- < div className = "flex items-center gap-1" >
496- < div className = "text-[10px] text-white/50" > Est. Profit:</ div >
540+ < div className = "mt-1.5 space-y-1" >
541+ < div className = "flex items-center justify-between" >
542+ < div className = "flex items-center gap-1" >
543+ < div className = "text-[10px] text-white/50" >
544+ Est. Profit:
545+ </ div >
546+ </ div >
547+ < div
548+ className = { `text-[10px] px-1.5 py-0.5 rounded ${
549+ estimatedTpPnlInCents > 0
550+ ? "text-green-400 bg-green-500/10"
551+ : "text-red-400 bg-red-500/10"
552+ } `}
553+ >
554+ { estimatedTpPnlInCents >= 0 ? "+$" : "-$" }
555+ { toDisplayPriceUSD ( Math . abs ( estimatedTpPnlInCents ) ) . toFixed (
556+ 2
557+ ) }
558+ </ div >
497559 </ div >
498- < div className = "text-[10px] text-green-400 bg-green-500/10 px-1.5 py-0.5 rounded" >
499- +$
500- { tpPrice
501- ? (
502- ( Number ( tpPrice ) -
503- ( activeTab === "buy" ? sellPrice : buyPrice ) ) *
504- margin *
505- leverage
506- ) . toFixed ( 2 )
507- : "0.00" }
560+ < div className = "text-[10px] text-white/40" >
561+ Target: ${ tpPrice } | Current: $
562+ { activeTab === "buy" ? buyPrice : sellPrice }
508563 </ div >
509564 </ div >
510565 ) }
@@ -574,22 +629,28 @@ export default function BuySell({
574629 </ div >
575630
576631 { slEnabled && (
577- < div className = "mt-1.5 flex items-center justify-between" >
578- < div className = "flex items-center gap-1" >
579- < div className = "text-[10px] text-white/50" > Est. Loss:</ div >
632+ < div className = "mt-1.5 space-y-1" >
633+ < div className = "flex items-center justify-between" >
634+ < div className = "flex items-center gap-1" >
635+ < div className = "text-[10px] text-white/50" > Est. Loss:</ div >
636+ </ div >
637+ < div
638+ className = { `text-[10px] px-1.5 py-0.5 rounded ${
639+ estimatedSlPnlInCents < 0
640+ ? "text-red-400 bg-red-500/10"
641+ : "text-green-400 bg-green-500/10"
642+ } `}
643+ >
644+ { /* Always show loss as negative, but use abs for the number */ }
645+ { estimatedSlPnlInCents > 0 ? "+$" : "-$" }
646+ { toDisplayPriceUSD ( Math . abs ( estimatedSlPnlInCents ) ) . toFixed (
647+ 2
648+ ) }
649+ </ div >
580650 </ div >
581- < div className = "text-[10px] text-red-400 bg-red-500/10 px-1.5 py-0.5 rounded" >
582- -$
583- { slPrice
584- ? (
585- Math . abs (
586- Number ( slPrice ) -
587- ( activeTab === "buy" ? sellPrice : buyPrice )
588- ) *
589- margin *
590- leverage
591- ) . toFixed ( 2 )
592- : "0.00" }
651+ < div className = "text-[10px] text-white/40" >
652+ Stop: ${ slPrice } | Current: $
653+ { activeTab === "buy" ? buyPrice : sellPrice }
593654 </ div >
594655 </ div >
595656 ) }
0 commit comments