Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Oct 28, 2024
1 parent c5806c2 commit 2a55db5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 22 deletions.
69 changes: 69 additions & 0 deletions src/typescript/frontend/src/app/candlesticks/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { fetchPeriodicEventsSince } from "@/queries/market";
import { Period, toPeriod } from "@sdk/index";

Check failure on line 2 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Imports "Period" are only used as type
import { PeriodicStateEventModel } from "@sdk/indexer-v2/types";

Check failure on line 3 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

All imports in the declaration are only used as types. Use `import type`
import { PeriodTypeFromDatabase } from "@sdk/indexer-v2/types/json-types";

Check failure on line 4 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

All imports in the declaration are only used as types. Use `import type`
import { parseInt } from "lodash";
import { NextRequest } from "next/server";

Check failure on line 6 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

All imports in the declaration are only used as types. Use `import type`
import { stringifyJSON } from "utils";

const CANDLESTICKS_LIMIT = 400;

/* eslint-disable-next-line import/no-unused-modules */
export async function GET(

Check failure on line 12 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Replace `⏎··request:·NextRequest,⏎` with `request:·NextRequest`
request: NextRequest,
) {
const searchParams = request.nextUrl.searchParams;
const marketIDStr = searchParams.get("marketID");
const startStr = searchParams.get("start");
const periodStr = searchParams.get("period");
const limitStr = searchParams.get("limit");

if(!marketIDStr || !startStr || !periodStr || !limitStr) {

Check failure on line 21 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Insert `·`
return new Response("", {status: 400});

Check failure on line 22 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Replace `status:·400` with `·status:·400·`
}

if(isNaN(parseInt(marketIDStr))) {

Check failure on line 25 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Insert `·`
return new Response("", {status: 400});

Check failure on line 26 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Replace `status:·400` with `·status:·400·`
}

if(isNaN(parseInt(startStr))) {

Check failure on line 29 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Insert `·`
return new Response("", {status: 400});
}

let period: Period;
try {
period = toPeriod(periodStr as PeriodTypeFromDatabase);
} catch {
return new Response("", {status: 400});
}

if(isNaN(parseInt(limitStr)) || parseInt(limitStr) > CANDLESTICKS_LIMIT) {
return new Response("", {status: 400});
}

const start = new Date(parseInt(startStr) * 1000);
const limit = parseInt(limitStr);
const marketID = parseInt(marketIDStr);

console.log({

Check warning on line 48 in src/typescript/frontend/src/app/candlesticks/route.ts

View workflow job for this annotation

GitHub Actions / pre-commit

Unexpected console statement
start,limit,marketID,period
})

const aggregate: PeriodicStateEventModel[] = [];

while(aggregate.length < limit) {
const data = await fetchPeriodicEventsSince({
marketID,
period,
start,
offset: aggregate.length,
limit: limit - aggregate.length,
});
aggregate.push(...data);
if(data.length < limit) {
break;
}
}

return new Response(stringifyJSON(aggregate));
}
37 changes: 16 additions & 21 deletions src/typescript/frontend/src/components/charts/PrivateChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ import { useEventStore } from "context/event-store-context";
import { getPeriodStartTimeFromTime } from "@sdk/utils";
import { getAptosConfig } from "lib/utils/aptos-client";
import { getSymbolEmojisInString, symbolToEmojis, toMarketEmojiData } from "@sdk/emoji_data";
import { type MarketMetadataModel } from "@sdk/indexer-v2/types";
import { PeriodicStateEventModel, type MarketMetadataModel } from "@sdk/indexer-v2/types";
import { getMarketResource } from "@sdk/markets";
import { Aptos } from "@aptos-labs/ts-sdk";
import { periodEnumToRawDuration, Trigger } from "@sdk/const";
import { fetchAllCandlesticksInTimeRange } from "@/queries/candlesticks";
import {
type LatestBar,
marketToLatestBars,
periodicStateTrackerToLatestBar,
toBar,
} from "@/store/event/candlestick-bars";
import { parseJSON } from "utils";

const configurationData: DatafeedConfiguration = {
supported_resolutions: TV_CHARTING_LIBRARY_RESOLUTIONS,
Expand Down Expand Up @@ -145,22 +145,23 @@ export const Chart = async (props: ChartContainerProps) => {

setTimeout(() => onSymbolResolvedCallback(symbolInfo), 0);
},
getBars: async (symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) => {
getBars: async (_, resolution, periodParams, onHistoryCallback, onErrorCallback) => {

Check warning on line 148 in src/typescript/frontend/src/components/charts/PrivateChart.tsx

View workflow job for this annotation

GitHub Actions / pre-commit

'onErrorCallback' is defined but never used. Allowed unused args must match /^_/u
const { from, to } = periodParams;
try {
const ResolutionToMinutes = {
"1": 1,
"5": 5,
"15": 15,
"30": 30,
"60": 60,
"240": 240,
"1D": 60 * 24,
};
const period = ResolutionStringToPeriod[resolution.toString()];
const periodDuration = periodEnumToRawDuration(period);
const start = new Date(from * 1000);
const end = new Date(to * 1000);
// TODO: Consider that if our data is internally consistent and we run into performance/scalability issues
// with this implementation below (fetching without regard for anything in state), we can store the values in
// state and coalesce that with the data we fetch from the server.
const data = await fetchAllCandlesticksInTimeRange({
marketID: props.marketID,
start,
end,
period,
});
const limit = Math.ceil((Math.ceil(to / 60) - Math.floor(from / 60)) / ResolutionToMinutes[resolution]);
const data: PeriodicStateEventModel[] = await fetch(`/candlesticks?marketID=${props.marketID}&start=${from}&period=${period}&limit=${limit}`)
.then(res => res.text()).then(res => parseJSON(res));

const isFetchForMostRecentBars = end.getTime() - new Date().getTime() > 1000;

Expand Down Expand Up @@ -234,7 +235,7 @@ export const Chart = async (props: ChartContainerProps) => {
const time = BigInt(new Date().getTime()) * 1000n;
const timeAsPeriod = getPeriodStartTimeFromTime(time, periodDuration) / 1000n;
bars.push({
time: Number(timeAsPeriod),
time: Number(timeAsPeriod.toString()),
open: 0,
high: 0,
low: 0,
Expand All @@ -252,12 +253,6 @@ export const Chart = async (props: ChartContainerProps) => {
onHistoryCallback(bars, {
noData: bars.length === 0,
});
} catch (e) {
if (e instanceof Error) {
console.warn("[getBars]: Get error", e);
onErrorCallback(e.message);
}
}
},
subscribeBars: async (
symbolInfo,
Expand Down
2 changes: 1 addition & 1 deletion src/typescript/sdk/src/indexer-v2/types/json-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from "../../emojicoin_dot_fun/types";
import { type Flatten } from "../../types";

type PeriodTypeFromDatabase =
export type PeriodTypeFromDatabase =
| "period_1m"
| "period_5m"
| "period_15m"
Expand Down

0 comments on commit 2a55db5

Please sign in to comment.