-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
import { PeriodicStateEventModel } from "@sdk/indexer-v2/types"; | ||
import { PeriodTypeFromDatabase } from "@sdk/indexer-v2/types/json-types"; | ||
import { parseInt } from "lodash"; | ||
import { NextRequest } from "next/server"; | ||
import { stringifyJSON } from "utils"; | ||
|
||
const CANDLESTICKS_LIMIT = 400; | ||
|
||
/* eslint-disable-next-line import/no-unused-modules */ | ||
export async function GET( | ||
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) { | ||
return new Response("", {status: 400}); | ||
} | ||
|
||
if(isNaN(parseInt(marketIDStr))) { | ||
return new Response("", {status: 400}); | ||
} | ||
|
||
if(isNaN(parseInt(startStr))) { | ||
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({ | ||
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters