diff --git a/indexer/services/scripts/README.md b/indexer/services/scripts/README.md index 2c66497a72..9200e458ec 100644 --- a/indexer/services/scripts/README.md +++ b/indexer/services/scripts/README.md @@ -19,6 +19,9 @@ pnpm run validate-pnl -- \ KAFKA_BROKER_URLS=...kafka.ap-northeast-1.amazonaws.com:9092 \ SERVICE_NAME=scripts pnpm run print-block -- --h 9265388 + +SERVICE_NAME=script pnpm run print-candle-time-boundaries -- \ +--t 2024-02-28T10:01:36.17+00:00 ``` ### EnvVars diff --git a/indexer/services/scripts/package.json b/indexer/services/scripts/package.json index 8bd0c3f51c..7c5aa617f7 100644 --- a/indexer/services/scripts/package.json +++ b/indexer/services/scripts/package.json @@ -10,6 +10,7 @@ "build:watch": "pnpm run build -- --watch", "validate-pnl": "ts-node src/validate-pnl.ts", "print-block": "ts-node src/print-block.ts", + "print-candle-time-boundaries": "ts-node src/print-candle-time-boundaries.ts", "coverage": "pnpm test -- --coverage", "lint": "eslint --ext .ts,.js .", "lint:fix": "eslint --ext .ts,.js . --fix", diff --git a/indexer/services/scripts/src/print-candle-time-boundaries.ts b/indexer/services/scripts/src/print-candle-time-boundaries.ts new file mode 100644 index 0000000000..332db572a5 --- /dev/null +++ b/indexer/services/scripts/src/print-candle-time-boundaries.ts @@ -0,0 +1,48 @@ +import { CandleResolution, NUM_SECONDS_IN_CANDLE_RESOLUTIONS } from '@dydxprotocol-indexer/postgres'; +import { DateTime } from 'luxon'; +import yargs from 'yargs'; + +export function calculateNormalizedCandleStartTime( + time: DateTime, + resolution: CandleResolution, +): DateTime { + const epochSeconds: number = Math.floor(time.toUTC().toSeconds()); + const normalizedTimeSeconds: number = epochSeconds - ( + epochSeconds % NUM_SECONDS_IN_CANDLE_RESOLUTIONS[resolution] + ); + + return DateTime.fromSeconds(normalizedTimeSeconds).toUTC(); +} + +// Get normalized boundaries for a given time and resolutions +function getNormalizedBoundaries(time: string, resolutions: CandleResolution[]): void { + const date = DateTime.fromISO(time); + + resolutions.forEach((resolution: CandleResolution) => { + const startTime = calculateNormalizedCandleStartTime(date, resolution); + const endTime = startTime.plus({ seconds: NUM_SECONDS_IN_CANDLE_RESOLUTIONS[resolution] }); + + console.log(`Resolution: ${resolution}, Start Time: ${startTime.toISO()}, End Time: ${endTime.toISO()}`); + }); +} + +const resolutions: CandleResolution[] = [ + CandleResolution.ONE_DAY, + CandleResolution.FOUR_HOURS, + CandleResolution.ONE_HOUR, + CandleResolution.THIRTY_MINUTES, + CandleResolution.FIFTEEN_MINUTES, + CandleResolution.FIVE_MINUTES, + CandleResolution.ONE_MINUTE, +]; + +const args = yargs.options({ + time: { + type: 'string', + alias: 't', + description: 'Time to compute normalized boundaries for, e.g. 2024-02-28T10:01:36.17+00:00', + required: true, + }, +}).argv; + +getNormalizedBoundaries(args.time, resolutions);