Skip to content

Commit

Permalink
requested changes 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pldespaigne committed Sep 6, 2023
1 parent 91fae92 commit 523ca93
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 66 deletions.
126 changes: 60 additions & 66 deletions packages/app/src/state/futures/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ import {
PositionSide,
FuturesMarginType,
FuturesMarket,
FuturesPositionHistory,
} from '@kwenta/sdk/types'
import {
truncateTimestamp,
getDisplayAsset,
MarketKeyByAsset,
getMarketName,
} from '@kwenta/sdk/utils'
import { truncateTimestamp, getDisplayAsset } from '@kwenta/sdk/utils'
import { createSelector } from '@reduxjs/toolkit'
import { wei } from '@synthetixio/wei'
import { FuturesPositionTablePosition } from 'types/futures'

import { TradeStatus } from 'sections/futures/types'
import {
Expand Down Expand Up @@ -46,6 +39,7 @@ import {
unserializeTrades,
stopLossValidity,
takeProfitValidity,
formatPositionForTable,
} from 'utils/futures'

import {
Expand Down Expand Up @@ -868,64 +862,46 @@ export const selectPendingOrdersCount = createSelector(
type === FuturesMarginType.CROSS_MARGIN ? asyncCount : delayedOrders.length
)

function formatPosition(
positions: FuturesPositionTablePosition[],
history: FuturesPositionHistory[]
) {
return history
.sort((a, b) => b.timestamp - a.timestamp)
.map((stat, i) => {
const totalDeposit = stat.initialMargin.add(stat.totalDeposits)
const thisPosition = stat.isOpen
? positions.find((p) => p.market.marketKey === stat.marketKey)
: null

const funding = stat.netFunding.add(thisPosition?.activePosition?.accruedFunding ?? ZERO_WEI)
const pnlWithFeesPaid = stat.pnl.sub(stat.feesPaid).add(funding)

return {
...stat,
rank: i + 1,
currencyIconKey: MarketKeyByAsset[stat.asset],
marketShortName: getMarketName(stat.asset),
status: stat.isOpen ? 'Open' : stat.isLiquidated ? 'Liquidated' : 'Closed',
funding,
pnl: pnlWithFeesPaid,
pnlPct: totalDeposit.gt(0)
? `(${pnlWithFeesPaid
.div(stat.initialMargin.add(stat.totalDeposits))
.mul(100)
.toNumber()
.toFixed(2)}%)`
: '0%',
}
})
}

export const selectLeaderBoardTableData = createSelector(
selectFuturesPositions,
selectPositionHistoryForSelectedTrader,
(positions, history) => formatPosition(positions, history)
(positions, history) => formatPositionForTable(positions, history)
)

export const selectPositionsHistoryTableData = createSelector(
selectFuturesPositions,
selectUsersPositionHistory,
(positions, history) => formatPosition(positions, history)
(positions, history) => formatPositionForTable(positions, history)
)

export const selectPositionsCsvData = createSelector(selectPositionsHistoryTableData, (data) => {
let csvData =
'Date/Time,Market,Status,Trades,Total Volume,Realized P&L USD,Realized P&L %,Funding,Tx Hash\n'
data.forEach(
(row: (typeof data)[number]) =>
(csvData += `${new Date(row.timestamp).toISOString()},${row.marketShortName},${row.status},${
row.trades
},${row.totalVolume.toNumber()},${row.pnl.toNumber()},${row.pnlPct.slice(
1,
-2
)},${row.funding.toNumber()},${row.transactionHash}\n`)
)
const csvCols = [
'Date/Time',
'Market',
'Status',
'Trades',
'Total Volume',
'Realized P&L USD',
'Realized P&L %',
'Funding',
'Tx Hash',
]
let csvData = csvCols.join(',') + '\n'

data.forEach((row: (typeof data)[number]) => {
const rowItems = [
new Date(row.timestamp).toISOString(),
row.marketShortName,
row.status,
row.trades,
row.totalVolume.toNumber(),
row.pnl.toNumber(),
row.pnlPct.slice(1, -2),
row.funding.toNumber(),
row.transactionHash,
]
csvData += rowItems.join(',') + '\n'
})
return csvData
})

Expand Down Expand Up @@ -959,16 +935,34 @@ export const selectTradesHistoryTableData = createSelector(
)

export const selectTradesCsvData = createSelector(selectTradesHistoryTableData, (data) => {
let csvData = 'Market,Side,Date/Time,Asset Price,Type,Amount,Value,PnL USD,Fees,Tx Hash\n'
data.forEach(
(row: (typeof data)[number]) =>
(csvData += `${row.displayAsset},${row.side},${new Date(
row.time
).toISOString()},${row.price.toNumber()},${
row.orderType
},${row.amount.toNumber()},${row.notionalValue.toNumber()},${row.netPnl.toNumber()},${row.feesPaid.toNumber()},${
row.id
}\n`)
)
const csvCols = [
'Market',
'Side',
'Date/Time',
'Asset Price',
'Type',
'Amount',
'Value',
'PnL USD',
'Fees',
'Tx Hash',
]
let csvData = csvCols.join(',') + '\n'

data.forEach((row: (typeof data)[number]) => {
const rowItems = [
row.displayAsset,
row.side,
new Date(row.time).toISOString(),
row.price.toNumber(),
row.orderType,
row.amount.toNumber(),
row.notionalValue.toNumber(),
row.netPnl.toNumber(),
row.feesPaid.toNumber(),
row.id,
]
csvData += rowItems.join(',') + '\n'
})
return csvData
})
36 changes: 36 additions & 0 deletions packages/app/src/utils/futures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ZERO_WEI } from '@kwenta/sdk/dist/constants'
import {
DelayedOrder,
FuturesMarket,
Expand All @@ -24,6 +25,7 @@ import {
} from '@kwenta/sdk/utils'
import Wei, { wei } from '@synthetixio/wei'
import { TFunction } from 'i18next'
import { FuturesPositionTablePosition } from 'types/futures'

import { DelayedOrderWithDetails, TradeSizeInputs } from 'state/futures/common/types'
import { SmartMarginBalanceInfo } from 'state/futures/smartMargin/types'
Expand Down Expand Up @@ -618,3 +620,37 @@ export const stopLossValidity = (
showWarning: percent.lt(SL_LIQ_PERCENT_WARN),
}
}

export function formatPositionForTable(
positions: FuturesPositionTablePosition[],
history: FuturesPositionHistory[]
) {
return history
.sort((a, b) => b.timestamp - a.timestamp)
.map((stat, i) => {
const totalDeposit = stat.initialMargin.add(stat.totalDeposits)
const thisPosition = stat.isOpen
? positions.find((p) => p.market.marketKey === stat.marketKey)
: null

const funding = stat.netFunding.add(thisPosition?.activePosition?.accruedFunding ?? ZERO_WEI)
const pnlWithFeesPaid = stat.pnl.sub(stat.feesPaid).add(funding)

return {
...stat,
rank: i + 1,
currencyIconKey: MarketKeyByAsset[stat.asset],
marketShortName: getMarketName(stat.asset),
status: stat.isOpen ? 'Open' : stat.isLiquidated ? 'Liquidated' : 'Closed',
funding,
pnl: pnlWithFeesPaid,
pnlPct: totalDeposit.gt(0)
? `(${pnlWithFeesPaid
.div(stat.initialMargin.add(stat.totalDeposits))
.mul(100)
.toNumber()
.toFixed(2)}%)`
: '0%',
}
})
}

0 comments on commit 523ca93

Please sign in to comment.