Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra db lookup #1645

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions indexer/packages/postgres/__tests__/db/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ describe('helpers', () => {
);

const unrealizedPnl: string = getUnrealizedPnl(
perpetualPosition, defaultPerpetualMarket, marketIdToMarket,
perpetualPosition,
defaultPerpetualMarket,
marketIdToMarket[defaultPerpetualMarket.marketId],
);

expect(unrealizedPnl).toEqual(Big(-50000).toFixed());
Expand All @@ -125,7 +127,9 @@ describe('helpers', () => {
);

const unrealizedPnl: string = getUnrealizedPnl(
perpetualPosition, defaultPerpetualMarket, marketIdToMarket,
perpetualPosition,
defaultPerpetualMarket,
marketIdToMarket[defaultPerpetualMarket.marketId],
);

expect(unrealizedPnl).toEqual(Big(50000).toFixed());
Expand Down
17 changes: 5 additions & 12 deletions indexer/packages/postgres/src/db/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DateTime } from 'luxon';
import { NUM_SECONDS_IN_CANDLE_RESOLUTIONS, ONE_MILLION } from '../constants';
import {
CandleResolution,
FundingIndexMap,
FundingIndexMap, MarketFromDatabase,
MarketsMap,
PerpetualMarketFromDatabase,
PerpetualPositionFromDatabase,
Expand Down Expand Up @@ -73,22 +73,15 @@ export function getUnsettledFunding(
* @param position Perpetual position object from the database, or the updated
* perpetual position subaccountKafkaObject.
* @param perpetualMarketsMap Map of perpetual ids to perpetual market objects from the database.
* @param market Market object from the database.
* @returns Unrealized pnl of the position.
*/
export function getUnrealizedPnl(
position: PerpetualPositionFromDatabase | UpdatedPerpetualPositionSubaccountKafkaObject,
perpetualMarket: PerpetualMarketFromDatabase,
marketsMap: MarketsMap,
market: MarketFromDatabase,
): string {
if (marketsMap[perpetualMarket.marketId] === undefined) {
logger.error({
at: 'getUnrealizedPnl',
message: 'Market is undefined',
marketId: perpetualMarket.marketId,
});
return Big(0).toFixed();
}
if (marketsMap[perpetualMarket.marketId].oraclePrice === undefined) {
if (market.oraclePrice === undefined) {
logger.error({
at: 'getUnrealizedPnl',
message: 'Oracle price is undefined for market',
Expand All @@ -98,7 +91,7 @@ export function getUnrealizedPnl(
}
return (
Big(position.size).times(
Big(marketsMap[perpetualMarket.marketId].oraclePrice!).minus(position.entryPrice),
Big(market.oraclePrice!).minus(position.entryPrice),
)
).toFixed();
}
Expand Down
Loading
Loading