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

Manually setting candle mainnet start time #130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 12 additions & 10 deletions src/shared/api/server/candles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ export async function GET(req: NextRequest): Promise<NextResponse<CandleApiRespo
}

// Need to query both directions and aggregate results
const candlesFwd = await pindexer.candles(
baseAssetMetadata.penumbraAssetId,
quoteAssetMetadata.penumbraAssetId,
durationWindow,
);
const candlesReverse = await pindexer.candles(
quoteAssetMetadata.penumbraAssetId,
baseAssetMetadata.penumbraAssetId,
durationWindow,
);
const candlesFwd = await pindexer.candles({
baseAsset: baseAssetMetadata.penumbraAssetId,
quoteAsset: quoteAssetMetadata.penumbraAssetId,
window: durationWindow,
chainId,
});
const candlesReverse = await pindexer.candles({
baseAsset: quoteAssetMetadata.penumbraAssetId,
quoteAsset: baseAssetMetadata.penumbraAssetId,
window: durationWindow,
chainId,
});

const mergedCandles = mergeCandles(
{ metadata: baseAssetMetadata, candles: candlesFwd },
Expand Down
29 changes: 23 additions & 6 deletions src/shared/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pkg from 'pg';
import { Pool, types } from 'pg';
import fs from 'fs';
import { Kysely, PostgresDialect } from 'kysely';
import { DB, DurationWindow } from '@/shared/database/schema.ts';
import { AssetId } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb';
const { Pool, types } = pkg;

const MAINNET_CHAIN_ID = 'penumbra-1';

class Pindexer {
private db: Kysely<DB>;
Expand Down Expand Up @@ -42,15 +43,31 @@ class Pindexer {
.execute();
}

async candles(baseAsset: AssetId, quoteAsset: AssetId, window: DurationWindow) {
return this.db
async candles({
baseAsset,
quoteAsset,
window,
chainId,
}: {
baseAsset: AssetId;
quoteAsset: AssetId;
window: DurationWindow;
chainId: string;
}) {
let query = this.db
.selectFrom('dex_ex_price_charts')
.innerJoin('dex_ex_candlesticks', 'candlestick_id', 'dex_ex_candlesticks.id')
.select(['start_time', 'open', 'close', 'low', 'high', 'swap_volume', 'direct_volume'])
.where('the_window', '=', window)
.where('asset_start', '=', Buffer.from(baseAsset.inner))
.where('asset_end', '=', Buffer.from(quoteAsset.inner))
.execute();
.where('asset_end', '=', Buffer.from(quoteAsset.inner));

// Due to a lot of price volatility at the launch of the chain, manually setting start date a few days later
if (chainId === MAINNET_CHAIN_ID) {
query = query.where('start_time', '>=', new Date('2024-08-06'));
}

return query.execute();
}
}

Expand Down