Skip to content

Commit

Permalink
normalize chart vals
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Dec 12, 2024
1 parent 4776cf7 commit a975bb5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/shared/api/server/candles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export async function GET(req: NextRequest): Promise<NextResponse<CandleApiRespo
chainId,
});

const response = insertEmptyCandles(durationWindow, candles.map(dbCandleToOhlc));
const displayAdjusted = candles.map(c =>
dbCandleToOhlc(c, baseAssetMetadata, quoteAssetMetadata),
);
const response = insertEmptyCandles(durationWindow, displayAdjusted);

return NextResponse.json(response);
}
14 changes: 8 additions & 6 deletions src/shared/api/server/candles/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { OhlcData, UTCTimestamp } from 'lightweight-charts';
import { DbCandle } from '@/shared/api/server/candles/types.ts';
import { addDurationWindow, DurationWindow } from '@/shared/utils/duration.ts';
import { Metadata } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb';
import { calculateDisplayPrice } from '@/shared/utils/price-conversion.ts';

export interface CandleWithVolume {
ohlc: OhlcData<UTCTimestamp>;
volume: number;
}

export const dbCandleToOhlc = (c: DbCandle): CandleWithVolume => {
export const dbCandleToOhlc = (c: DbCandle, base: Metadata, quote: Metadata): CandleWithVolume => {
return {
ohlc: {
close: c.close,
high: c.high,
low: c.low,
open: c.open,
close: calculateDisplayPrice(c.close, base, quote),
high: calculateDisplayPrice(c.high, base, quote),
low: calculateDisplayPrice(c.low, base, quote),
open: calculateDisplayPrice(c.open, base, quote),
time: (c.start_time.getTime() / 1000) as UTCTimestamp,
},
volume: c.direct_volume + c.swap_volume,
volume: calculateDisplayPrice(c.direct_volume + c.swap_volume, base, quote),
};
};

Expand Down

0 comments on commit a975bb5

Please sign in to comment.