Skip to content

Commit

Permalink
better price formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCaso committed Jan 30, 2024
1 parent 2b200ad commit e5327ce
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions frontend/src/app/components/positions-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface RowProps {
const Row = ({ position, market }: RowProps) => {
const { closePosition } = useManagePosition({ marketAddress: market.address })

const price = formatWithDecimals(position.price, 6)
const positionPnlPercentage = formatPercentage(parseInt(position.pnlPercentage))

const longShortcolor = position.isLong ? 'text-green-500' : 'text-red-500'
Expand All @@ -31,7 +30,9 @@ const Row = ({ position, market }: RowProps) => {
const positionNetValue = collateralUsd + collateralUsd * (parseInt(position.pnlPercentage) / 100)
const pnlUsd = positionNetValue - collateralUsd
const size = collateralAmount * parseInt(position.leverage)

const entryPrice = formatWithDecimals(position.entryPrice, 6)
const marketPrice = formatWithDecimals(position.price, 6)
const liquidationPrice = formatWithDecimals(position.liquidationPrice, 6)

const handleClosePosition = async () => {
Expand Down Expand Up @@ -81,11 +82,11 @@ const Row = ({ position, market }: RowProps) => {
</div>
</td>

<td>{formatDollarAmount(entryPrice)}</td>
<td>{formatDollarAmount(entryPrice, 4)}</td>

<td>{formatDollarAmount(price)}</td>
<td>{formatDollarAmount(marketPrice, 4)}</td>

<td>{formatDollarAmount(liquidationPrice)}</td>
<td>{formatDollarAmount(liquidationPrice, 4)}</td>

<td>
<Button
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/utils/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const formatNumber = (number: number) => {
return number.toLocaleString(undefined, { maximumFractionDigits: 2 })
export const formatNumber = (number: number, precision = 2) => {
return number.toLocaleString(undefined, { maximumFractionDigits: precision })
}

export const formatPercentage = (percentage: number) => {
Expand All @@ -10,8 +10,8 @@ export const formatPercentage = (percentage: number) => {
: `-${percentageFormattedWithoutSign}%`
}

export const formatDollarAmount = (amount: number) => {
return `$${formatNumber(amount)}`
export const formatDollarAmount = (amount: number, precision = 2) => {
return `$${formatNumber(amount, precision)}`
}

export const formatDollarAmountWithSign = (amount: number) => {
Expand Down

0 comments on commit e5327ce

Please sign in to comment.