Skip to content

Commit

Permalink
fix: order battlecharts by pnl
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveVodrazka committed Feb 27, 2025
1 parent 20deb04 commit 7cca812
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/components/PnL/getTrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const calculatePnL = (trades: TradeWithPrices[]): PnL[] => {
if (trade.option_side === 1) {
const key = trade.pool_id + trade.strike_price + trade.maturity;
const tokens = trade.premia * (1 - fees) + trade.capital_transfered;
if (openShorts.hasOwnProperty(key)) {
if (openShorts[key] !== undefined) {
openShorts[key] += tokens;
} else {
openShorts[key] = tokens;
Expand Down Expand Up @@ -236,29 +236,43 @@ const calculateLeaderboardData = (
{} as { [key: string]: TradeWithPrices[] }
);

const result = Object.keys(userMap).reduce((acc, key) => {
acc[key] = calculateNotionalVolumeSingleUser(userMap[key]);
const richData = Object.keys(userMap).reduce((acc, address) => {
const trades = userMap[address];
const notionalVolume = calculateNotionalVolumeSingleUser(trades);
const pnl = calculatePnL(trades).at(-1)?.usd || 0;
acc[address] = {
address: standardiseAddress(address),
notionalVolume,
pnl,
position: 0,
};
return acc;
}, {} as { [key: string]: number });
}, {} as { [key: string]: TradeLeaderboardData });

const sortedCallers = Object.entries(result).sort(([, a], [, b]) => b - a);
const sortedCallers = Object.values(richData).sort(
({ pnl: a }, { pnl: b }) => b - a
);

const topUsers = sortedCallers.slice(0, 20); // Extract the top 20 callers

const tradeLeaderboardData = topUsers.map(([address, notionalVolume], i) => ({
address,
notionalVolume,
position: i + 1,
pnl: calculatePnL(userMap[address]).at(-1)?.usd || 0,
}));
const tradeLeaderboardData = topUsers.map(
({ address, notionalVolume, pnl }, i) => ({
address,
notionalVolume,
position: i + 1,
pnl,
})
);

if (!address) {
return [tradeLeaderboardData, undefined];
}

const stdAddress = standardiseAddress(address);

const index = sortedCallers.findIndex(([a, _]) => a === stdAddress);
const index = sortedCallers.findIndex(
({ address }) => address === stdAddress
);

const user = {
address: stdAddress,
Expand Down

0 comments on commit 7cca812

Please sign in to comment.