@gearbox-protocol/sdk 9.13.1 → 9.14.0

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.
@@ -394,7 +394,7 @@ class GearboxSDK {
394
394
  /**
395
395
  * Reloads markets states based on events from last processed block to new block (defaults to latest block)
396
396
  * @param opts
397
- * @returns
397
+ * @returns true if successful, false if was skipped or failed
398
398
  */
399
399
  async syncState(opts) {
400
400
  let {
@@ -415,17 +415,18 @@ class GearboxSDK {
415
415
  timestamp = block.timestamp;
416
416
  }
417
417
  if (blockNumber <= this.currentBlock) {
418
- return;
418
+ return false;
419
419
  }
420
420
  if (this.#syncing) {
421
421
  this.logger?.warn(
422
422
  `cannot sync to ${blockNumber}, already syncing at ${this.currentBlock}`
423
423
  );
424
- return;
424
+ return false;
425
425
  }
426
426
  this.#syncing = true;
427
427
  const prevBlock = this.currentBlock;
428
428
  const prevTimestamp = this.timestamp;
429
+ let success = false;
429
430
  try {
430
431
  let delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
431
432
  this.logger?.debug(
@@ -480,6 +481,7 @@ class GearboxSDK {
480
481
  this.logger?.debug(
481
482
  `synced state to block ${blockNumber} (delta ${delta}s)`
482
483
  );
484
+ success = true;
483
485
  } catch (e) {
484
486
  this.logger?.error(
485
487
  `sync state to block ${blockNumber} with ts ${timestamp} failed, reverting to old block ${prevBlock} with ts ${prevTimestamp}: ${e}`
@@ -489,6 +491,7 @@ class GearboxSDK {
489
491
  } finally {
490
492
  this.#syncing = false;
491
493
  }
494
+ return success;
492
495
  }
493
496
  get provider() {
494
497
  return this.#provider;
@@ -114,7 +114,7 @@ class RedstoneUpdater extends import_base.SDKConstruct {
114
114
  for (const [key, group] of Object.entries(groupedFeeds)) {
115
115
  const [dataServiceId, signersStr] = key.split(":");
116
116
  const uniqueSignersCount = parseInt(signersStr, 10);
117
- const payloads = await this.#getPayloads(
117
+ const payloads = await this.#safeGetPayloads(
118
118
  dataServiceId,
119
119
  group,
120
120
  uniqueSignersCount
@@ -154,6 +154,41 @@ class RedstoneUpdater extends import_base.SDKConstruct {
154
154
  );
155
155
  return results;
156
156
  }
157
+ /**
158
+ * Gets payloads, retries once if it has expired while inflight
159
+ * @param dataServiceId
160
+ * @param dataFeedsIds
161
+ * @param uniqueSignersCount
162
+ * @returns
163
+ */
164
+ async #safeGetPayloads(dataServiceId, dataFeedsIds, uniqueSignersCount) {
165
+ let result = await this.#getPayloads(
166
+ dataServiceId,
167
+ dataFeedsIds,
168
+ uniqueSignersCount
169
+ );
170
+ if (this.#historicalTimestampMs) {
171
+ return result;
172
+ }
173
+ let expired = false;
174
+ for (const { timestamp, dataFeedId } of result) {
175
+ const delta = Number(this.sdk.timestamp) - timestamp;
176
+ if (delta >= 240 && delta < 255) {
177
+ this.#logger?.warn(
178
+ `payload for ${dataFeedId} has expired by ${delta} seconds`
179
+ );
180
+ expired = true;
181
+ }
182
+ }
183
+ if (expired) {
184
+ result = await this.#getPayloads(
185
+ dataServiceId,
186
+ dataFeedsIds,
187
+ uniqueSignersCount
188
+ );
189
+ }
190
+ return result;
191
+ }
157
192
  /**
158
193
  * Gets redstone payloads in one request for multiple feeds with the same dataServiceId and uniqueSignersCount
159
194
  * If historicalTimestamp is set, responses will be cached
@@ -392,7 +392,7 @@ class GearboxSDK {
392
392
  /**
393
393
  * Reloads markets states based on events from last processed block to new block (defaults to latest block)
394
394
  * @param opts
395
- * @returns
395
+ * @returns true if successful, false if was skipped or failed
396
396
  */
397
397
  async syncState(opts) {
398
398
  let {
@@ -413,17 +413,18 @@ class GearboxSDK {
413
413
  timestamp = block.timestamp;
414
414
  }
415
415
  if (blockNumber <= this.currentBlock) {
416
- return;
416
+ return false;
417
417
  }
418
418
  if (this.#syncing) {
419
419
  this.logger?.warn(
420
420
  `cannot sync to ${blockNumber}, already syncing at ${this.currentBlock}`
421
421
  );
422
- return;
422
+ return false;
423
423
  }
424
424
  this.#syncing = true;
425
425
  const prevBlock = this.currentBlock;
426
426
  const prevTimestamp = this.timestamp;
427
+ let success = false;
427
428
  try {
428
429
  let delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
429
430
  this.logger?.debug(
@@ -478,6 +479,7 @@ class GearboxSDK {
478
479
  this.logger?.debug(
479
480
  `synced state to block ${blockNumber} (delta ${delta}s)`
480
481
  );
482
+ success = true;
481
483
  } catch (e) {
482
484
  this.logger?.error(
483
485
  `sync state to block ${blockNumber} with ts ${timestamp} failed, reverting to old block ${prevBlock} with ts ${prevTimestamp}: ${e}`
@@ -487,6 +489,7 @@ class GearboxSDK {
487
489
  } finally {
488
490
  this.#syncing = false;
489
491
  }
492
+ return success;
490
493
  }
491
494
  get provider() {
492
495
  return this.#provider;
@@ -92,7 +92,7 @@ class RedstoneUpdater extends SDKConstruct {
92
92
  for (const [key, group] of Object.entries(groupedFeeds)) {
93
93
  const [dataServiceId, signersStr] = key.split(":");
94
94
  const uniqueSignersCount = parseInt(signersStr, 10);
95
- const payloads = await this.#getPayloads(
95
+ const payloads = await this.#safeGetPayloads(
96
96
  dataServiceId,
97
97
  group,
98
98
  uniqueSignersCount
@@ -132,6 +132,41 @@ class RedstoneUpdater extends SDKConstruct {
132
132
  );
133
133
  return results;
134
134
  }
135
+ /**
136
+ * Gets payloads, retries once if it has expired while inflight
137
+ * @param dataServiceId
138
+ * @param dataFeedsIds
139
+ * @param uniqueSignersCount
140
+ * @returns
141
+ */
142
+ async #safeGetPayloads(dataServiceId, dataFeedsIds, uniqueSignersCount) {
143
+ let result = await this.#getPayloads(
144
+ dataServiceId,
145
+ dataFeedsIds,
146
+ uniqueSignersCount
147
+ );
148
+ if (this.#historicalTimestampMs) {
149
+ return result;
150
+ }
151
+ let expired = false;
152
+ for (const { timestamp, dataFeedId } of result) {
153
+ const delta = Number(this.sdk.timestamp) - timestamp;
154
+ if (delta >= 240 && delta < 255) {
155
+ this.#logger?.warn(
156
+ `payload for ${dataFeedId} has expired by ${delta} seconds`
157
+ );
158
+ expired = true;
159
+ }
160
+ }
161
+ if (expired) {
162
+ result = await this.#getPayloads(
163
+ dataServiceId,
164
+ dataFeedsIds,
165
+ uniqueSignersCount
166
+ );
167
+ }
168
+ return result;
169
+ }
135
170
  /**
136
171
  * Gets redstone payloads in one request for multiple feeds with the same dataServiceId and uniqueSignersCount
137
172
  * If historicalTimestamp is set, responses will be cached
@@ -108,9 +108,9 @@ export declare class GearboxSDK<const Plugins extends PluginsMap = {}> {
108
108
  /**
109
109
  * Reloads markets states based on events from last processed block to new block (defaults to latest block)
110
110
  * @param opts
111
- * @returns
111
+ * @returns true if successful, false if was skipped or failed
112
112
  */
113
- syncState(opts?: SyncStateOptions): Promise<void>;
113
+ syncState(opts?: SyncStateOptions): Promise<boolean>;
114
114
  get provider(): Provider;
115
115
  get currentBlock(): bigint;
116
116
  get timestamp(): bigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "9.13.1",
3
+ "version": "9.14.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",