@ganaka/sdk 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -4400,8 +4400,8 @@ const p = object({
4400
4400
  close: number$1()
4401
4401
  })
4402
4402
  ),
4403
- start_time: string(),
4404
- end_time: string(),
4403
+ start_time: string().nullable(),
4404
+ end_time: string().nullable(),
4405
4405
  interval_in_minutes: number$1()
4406
4406
  })
4407
4407
  })
@@ -7922,8 +7922,7 @@ const fetchCandles = ({
7922
7922
  headers["X-Run-Id"] = runId;
7923
7923
  }
7924
7924
  if (currentTimestamp) {
7925
- const timestampStr = dayjs.tz(currentTimestamp, currentTimezone).format("YYYY-MM-DDTHH:mm:ss");
7926
- headers["X-Current-Timestamp"] = timestampStr;
7925
+ headers["X-Current-Timestamp"] = currentTimestamp;
7927
7926
  }
7928
7927
  if (currentTimezone) {
7929
7928
  headers["X-Current-Timezone"] = currentTimezone;
@@ -7967,8 +7966,7 @@ const fetchQuote = ({
7967
7966
  headers["X-Run-Id"] = runId;
7968
7967
  }
7969
7968
  if (currentTimestamp) {
7970
- const timestampStr = dayjs.tz(currentTimestamp, currentTimezone).format("YYYY-MM-DDTHH:mm:ss");
7971
- headers["X-Current-Timestamp"] = timestampStr;
7969
+ headers["X-Current-Timestamp"] = currentTimestamp;
7972
7970
  }
7973
7971
  if (currentTimezone) {
7974
7972
  headers["X-Current-Timezone"] = currentTimezone;
@@ -8012,8 +8010,7 @@ const fetchQuoteTimeline = ({
8012
8010
  headers["X-Run-Id"] = runId;
8013
8011
  }
8014
8012
  if (currentTimestamp) {
8015
- const timestampStr = dayjs.tz(currentTimestamp, currentTimezone).format("YYYY-MM-DDTHH:mm:ss");
8016
- headers["X-Current-Timestamp"] = timestampStr;
8013
+ headers["X-Current-Timestamp"] = currentTimestamp;
8017
8014
  }
8018
8015
  if (currentTimezone) {
8019
8016
  headers["X-Current-Timezone"] = currentTimezone;
@@ -8057,8 +8054,7 @@ const fetchShortlist = ({
8057
8054
  headers["X-Run-Id"] = runId;
8058
8055
  }
8059
8056
  if (currentTimestamp) {
8060
- const timestampStr = dayjs.tz(currentTimestamp, currentTimezone).format("YYYY-MM-DDTHH:mm:ss");
8061
- headers["X-Current-Timestamp"] = timestampStr;
8057
+ headers["X-Current-Timestamp"] = currentTimestamp;
8062
8058
  }
8063
8059
  if (currentTimezone) {
8064
8060
  headers["X-Current-Timezone"] = currentTimezone;
@@ -8152,7 +8148,7 @@ async function retryWithBackoff(fn, maxRetries = 3, baseDelayMs = 1e3) {
8152
8148
  throw lastError;
8153
8149
  }
8154
8150
  const placeOrder = ({ runId, apiClient }) => async (data) => {
8155
- console.log(data);
8151
+ logger.debug(`data: ${JSON.stringify(data)}`);
8156
8152
  if (runId) {
8157
8153
  try {
8158
8154
  await retryWithBackoff(
@@ -8268,73 +8264,144 @@ class ApiClient {
8268
8264
  }
8269
8265
  }
8270
8266
  }
8271
- function getNextIntervalBoundary(timestamp, intervalMinutes) {
8272
- const date2 = dayjs(timestamp);
8273
- const currentMinute = date2.minute();
8274
- const currentSecond = date2.second();
8275
- const currentMillisecond = date2.millisecond();
8276
- const minutesIntoHour = currentMinute % intervalMinutes;
8277
- if (minutesIntoHour === 0 && currentSecond === 0 && currentMillisecond === 0) {
8278
- return date2.add(intervalMinutes, "minute").toDate();
8279
- }
8280
- const minutesToAdd = intervalMinutes - minutesIntoHour;
8281
- return date2.add(minutesToAdd, "minute").second(0).millisecond(0).toDate();
8282
- }
8267
+ var calendar$1 = { exports: {} };
8268
+ (function(module, exports$1) {
8269
+ !function(e, t2) {
8270
+ module.exports = t2();
8271
+ }(commonjsGlobal, function() {
8272
+ return function(e, t2, a) {
8273
+ var n2 = "h:mm A", d2 = { lastDay: "[Yesterday at] " + n2, sameDay: "[Today at] " + n2, nextDay: "[Tomorrow at] " + n2, nextWeek: "dddd [at] " + n2, lastWeek: "[Last] dddd [at] " + n2, sameElse: "MM/DD/YYYY" };
8274
+ t2.prototype.calendar = function(e2, t3) {
8275
+ var n3 = t3 || this.$locale().calendar || d2, o2 = a(e2 || void 0).startOf("d"), s = this.diff(o2, "d", true), i2 = "sameElse", f2 = s < -6 ? i2 : s < -1 ? "lastWeek" : s < 0 ? "lastDay" : s < 1 ? "sameDay" : s < 2 ? "nextDay" : s < 7 ? "nextWeek" : i2, l2 = n3[f2] || d2[f2];
8276
+ return "function" == typeof l2 ? l2.call(this, a()) : this.format(l2);
8277
+ };
8278
+ };
8279
+ });
8280
+ })(calendar$1);
8281
+ var calendarExports = calendar$1.exports;
8282
+ const calendar = /* @__PURE__ */ getDefaultExportFromCjs(calendarExports);
8283
+ var relativeTime$1 = { exports: {} };
8284
+ (function(module, exports$1) {
8285
+ !function(r2, e) {
8286
+ module.exports = e();
8287
+ }(commonjsGlobal, function() {
8288
+ return function(r2, e, t2) {
8289
+ r2 = r2 || {};
8290
+ var n2 = e.prototype, o2 = { future: "in %s", past: "%s ago", s: "a few seconds", m: "a minute", mm: "%d minutes", h: "an hour", hh: "%d hours", d: "a day", dd: "%d days", M: "a month", MM: "%d months", y: "a year", yy: "%d years" };
8291
+ function i2(r3, e2, t3, o3) {
8292
+ return n2.fromToBase(r3, e2, t3, o3);
8293
+ }
8294
+ t2.en.relativeTime = o2, n2.fromToBase = function(e2, n3, i3, d3, u) {
8295
+ for (var f2, a, s, l2 = i3.$locale().relativeTime || o2, h = r2.thresholds || [{ l: "s", r: 44, d: "second" }, { l: "m", r: 89 }, { l: "mm", r: 44, d: "minute" }, { l: "h", r: 89 }, { l: "hh", r: 21, d: "hour" }, { l: "d", r: 35 }, { l: "dd", r: 25, d: "day" }, { l: "M", r: 45 }, { l: "MM", r: 10, d: "month" }, { l: "y", r: 17 }, { l: "yy", d: "year" }], m2 = h.length, c2 = 0; c2 < m2; c2 += 1) {
8296
+ var y2 = h[c2];
8297
+ y2.d && (f2 = d3 ? t2(e2).diff(i3, y2.d, true) : i3.diff(e2, y2.d, true));
8298
+ var p2 = (r2.rounding || Math.round)(Math.abs(f2));
8299
+ if (s = f2 > 0, p2 <= y2.r || !y2.r) {
8300
+ p2 <= 1 && c2 > 0 && (y2 = h[c2 - 1]);
8301
+ var v2 = l2[y2.l];
8302
+ u && (p2 = u("" + p2)), a = "string" == typeof v2 ? v2.replace("%d", p2) : v2(p2, n3, y2.l, s);
8303
+ break;
8304
+ }
8305
+ }
8306
+ if (n3) return a;
8307
+ var M2 = s ? l2.future : l2.past;
8308
+ return "function" == typeof M2 ? M2(a) : M2.replace("%s", a);
8309
+ }, n2.to = function(r3, e2) {
8310
+ return i2(r3, e2, this, true);
8311
+ }, n2.from = function(r3, e2) {
8312
+ return i2(r3, e2, this);
8313
+ };
8314
+ var d2 = function(r3) {
8315
+ return r3.$u ? t2.utc() : t2();
8316
+ };
8317
+ n2.toNow = function(r3) {
8318
+ return this.to(d2(this), r3);
8319
+ }, n2.fromNow = function(r3) {
8320
+ return this.from(d2(this), r3);
8321
+ };
8322
+ };
8323
+ });
8324
+ })(relativeTime$1);
8325
+ var relativeTimeExports = relativeTime$1.exports;
8326
+ const relativeTime = /* @__PURE__ */ getDefaultExportFromCjs(relativeTimeExports);
8327
+ dayjs.extend(utc);
8328
+ dayjs.extend(timezone);
8329
+ dayjs.extend(calendar);
8330
+ dayjs.extend(relativeTime);
8283
8331
  function sleep(ms) {
8284
8332
  return new Promise((resolve) => setTimeout(resolve, ms));
8285
8333
  }
8286
8334
  async function runMinuteLoop({
8287
- startTime,
8288
- endTime,
8289
- callback,
8290
- intervalMinutes
8335
+ startTimeDayJS,
8336
+ endTimeDayJS,
8337
+ intervalMinutes,
8338
+ callback
8291
8339
  }) {
8292
- const now = /* @__PURE__ */ new Date();
8293
- const isSimulationMode = now < startTime || now > endTime;
8340
+ let currentISTDayJS = dayjs.utc().tz("Asia/Kolkata");
8341
+ logger.debug(`currentIST: ${currentISTDayJS.format("YYYY-MM-DDTHH:mm:ss")}`);
8342
+ logger.debug(`startTime: ${startTimeDayJS.format("YYYY-MM-DDTHH:mm:ss")}`);
8343
+ logger.debug(`endTime: ${endTimeDayJS.format("YYYY-MM-DDTHH:mm:ss")}`);
8344
+ const isSimulationMode = currentISTDayJS.isAfter(endTimeDayJS);
8294
8345
  if (isSimulationMode) {
8295
- console.log("Current time is outside the specified range, simulating loop");
8346
+ logger.info("Current time is after endTime, simulating loop");
8347
+ }
8348
+ if (currentISTDayJS.isBefore(startTimeDayJS)) {
8349
+ const delayUntilStart = startTimeDayJS.diff(currentISTDayJS, "millisecond");
8350
+ logger.info(
8351
+ `Starting loop ${startTimeDayJS.from(currentISTDayJS)} at ${startTimeDayJS.format(
8352
+ "YYYY-MM-DD HH:mm"
8353
+ )}`
8354
+ );
8355
+ await sleep(delayUntilStart);
8356
+ currentISTDayJS = currentISTDayJS.add(delayUntilStart, "millisecond");
8357
+ }
8358
+ if (currentISTDayJS.isAfter(startTimeDayJS) && currentISTDayJS.isBefore(endTimeDayJS)) {
8359
+ startTimeDayJS = currentISTDayJS;
8296
8360
  }
8297
- const startDate = dayjs(startTime);
8298
- const startMinute = startDate.minute();
8361
+ const startMinute = startTimeDayJS.minute();
8299
8362
  const minutesToFirstBoundary = intervalMinutes - startMinute % intervalMinutes;
8300
- const isOnBoundary = startMinute % intervalMinutes === 0 && startDate.second() === 0 && startDate.millisecond() === 0;
8301
- let nextBoundary;
8363
+ const isOnBoundary = startMinute % intervalMinutes === 0 && startTimeDayJS.second() === 0 && startTimeDayJS.millisecond() === 0;
8364
+ let nextBoundaryDayJS;
8302
8365
  if (isOnBoundary) {
8303
- nextBoundary = startTime;
8366
+ nextBoundaryDayJS = startTimeDayJS;
8304
8367
  } else {
8305
- nextBoundary = startDate.add(minutesToFirstBoundary, "minute").second(0).millisecond(0).toDate();
8368
+ nextBoundaryDayJS = startTimeDayJS.add(minutesToFirstBoundary, "minute").second(0).millisecond(0);
8306
8369
  }
8370
+ logger.debug(`nextBoundary: ${nextBoundaryDayJS.format("YYYY-MM-DDTHH:mm:ss")}`);
8307
8371
  if (isSimulationMode) {
8308
- while (nextBoundary <= endTime) {
8372
+ while (nextBoundaryDayJS.isBefore(endTimeDayJS) || nextBoundaryDayJS.isSame(endTimeDayJS)) {
8309
8373
  try {
8310
- await callback(nextBoundary);
8374
+ await callback(nextBoundaryDayJS.format("YYYY-MM-DDTHH:mm:ss"));
8311
8375
  } catch (error) {
8312
8376
  console.error(
8313
- `Error executing callback at ${nextBoundary.toISOString()}:`,
8377
+ `Error executing callback at ${nextBoundaryDayJS.format("YYYY-MM-DDTHH:mm:ss")}:`,
8314
8378
  error
8315
8379
  );
8316
8380
  }
8317
- nextBoundary = dayjs(nextBoundary).add(intervalMinutes, "minute").toDate();
8381
+ nextBoundaryDayJS = nextBoundaryDayJS.add(intervalMinutes, "minute");
8318
8382
  }
8319
8383
  return;
8320
8384
  }
8321
- if (startTime < now && nextBoundary < now) {
8322
- nextBoundary = getNextIntervalBoundary(now, intervalMinutes);
8323
- }
8324
- while (nextBoundary <= endTime) {
8325
- const delay = nextBoundary.getTime() - Date.now();
8385
+ while (nextBoundaryDayJS.isBefore(endTimeDayJS) || nextBoundaryDayJS.isSame(endTimeDayJS)) {
8386
+ const delay = nextBoundaryDayJS.diff(currentISTDayJS, "millisecond");
8326
8387
  if (delay > 0) {
8388
+ logger.info(
8389
+ `Waiting for ${nextBoundaryDayJS.from(
8390
+ currentISTDayJS,
8391
+ true
8392
+ )} to reach next execution time: ${nextBoundaryDayJS.format("YYYY-MM-DD HH:mm:ss")}`
8393
+ );
8327
8394
  await sleep(delay);
8328
8395
  }
8329
8396
  try {
8330
- await callback(nextBoundary);
8397
+ await callback(nextBoundaryDayJS.format("YYYY-MM-DDTHH:mm:ss"));
8331
8398
  } catch (error) {
8332
8399
  console.error(
8333
- `Error executing callback at ${nextBoundary.toISOString()}:`,
8400
+ `Error executing callback at ${nextBoundaryDayJS.format("YYYY-MM-DDTHH:mm:ss")}:`,
8334
8401
  error
8335
8402
  );
8336
8403
  }
8337
- nextBoundary = dayjs(nextBoundary).add(intervalMinutes, "minute").toDate();
8404
+ nextBoundaryDayJS = nextBoundaryDayJS.add(intervalMinutes, "minute");
8338
8405
  }
8339
8406
  }
8340
8407
  dotenv.config();
@@ -8355,10 +8422,15 @@ async function ganaka({
8355
8422
  );
8356
8423
  }
8357
8424
  const apiClient = new ApiClient({ developerToken, apiDomain });
8425
+ const startTimeDayJS = dayjs.tz(startTime, "Asia/Kolkata");
8426
+ const endTimeDayJS = dayjs.tz(endTime, "Asia/Kolkata");
8427
+ if (startTimeDayJS.isAfter(endTimeDayJS)) {
8428
+ throw new Error("Start time cannot be after end time");
8429
+ }
8358
8430
  let runId = null;
8359
8431
  const createRunBody = {
8360
- start_datetime: dayjs.tz(startTime, "Asia/Kolkata").format("YYYY-MM-DDTHH:mm:ss"),
8361
- end_datetime: dayjs.tz(endTime, "Asia/Kolkata").format("YYYY-MM-DDTHH:mm:ss"),
8432
+ start_datetime: startTime,
8433
+ end_datetime: endTime,
8362
8434
  timezone: "Asia/Kolkata"
8363
8435
  };
8364
8436
  try {
@@ -8382,8 +8454,8 @@ async function ganaka({
8382
8454
  }
8383
8455
  try {
8384
8456
  await runMinuteLoop({
8385
- startTime,
8386
- endTime,
8457
+ startTimeDayJS,
8458
+ endTimeDayJS,
8387
8459
  intervalMinutes,
8388
8460
  callback: async (currentTimestamp) => {
8389
8461
  await fn({