@gearbox-protocol/sdk 9.9.0 → 9.9.1

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.
@@ -414,64 +414,77 @@ class GearboxSDK {
414
414
  return;
415
415
  }
416
416
  if (this.#syncing) {
417
- this.logger?.warn(`cannot sync to ${blockNumber}, already syncing`);
417
+ this.logger?.warn(
418
+ `cannot sync to ${blockNumber}, already syncing at ${this.currentBlock}`
419
+ );
418
420
  return;
419
421
  }
420
422
  this.#syncing = true;
421
- let delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
422
- this.logger?.debug(
423
- `syncing state to block ${blockNumber} (delta ${delta}s )...`
424
- );
425
- const watchAddresses = [
426
- ...Array.from(this.marketRegister.watchAddresses),
427
- this.addressProvider.address
428
- ];
429
- const fromBlock = this.currentBlock + 1n;
430
- this.logger?.debug(
431
- `getting logs from ${watchAddresses.length} addresses in [${fromBlock}:${blockNumber}]`
432
- );
433
- const logs = await (0, import_viem2.getLogsSafe)(this.provider.publicClient, {
434
- fromBlock,
435
- toBlock: blockNumber,
436
- address: watchAddresses
437
- });
438
- for (const log of logs) {
439
- const contract = this.contracts.get(log.address);
440
- if (contract) {
441
- const event = (0, import_viem.parseEventLogs)({
442
- abi: contract.abi,
443
- logs: [log]
444
- })[0];
445
- contract.processLog(event);
446
- }
447
- }
448
- this.#currentBlock = blockNumber;
449
- this.#timestamp = timestamp;
450
- await this.marketRegister.syncState(skipPriceUpdate);
451
- await this.#hooks.triggerHooks("syncState", { blockNumber, timestamp });
452
- const pluginsList = import_utils.TypedObjectUtils.entries(this.plugins);
453
- const pluginResponse = await Promise.allSettled(
454
- pluginsList.map(([name, plugin]) => {
455
- if (plugin.syncState) {
456
- this.logger?.debug(`syncing plugin ${name}`);
457
- return plugin.syncState();
423
+ const prevBlock = this.currentBlock;
424
+ const prevTimestamp = this.timestamp;
425
+ try {
426
+ let delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
427
+ this.logger?.debug(
428
+ `syncing state to block ${blockNumber} (delta ${delta}s )...`
429
+ );
430
+ const watchAddresses = [
431
+ ...Array.from(this.marketRegister.watchAddresses),
432
+ this.addressProvider.address
433
+ ];
434
+ const fromBlock = this.currentBlock + 1n;
435
+ this.logger?.debug(
436
+ `getting logs from ${watchAddresses.length} addresses in [${fromBlock}:${blockNumber}]`
437
+ );
438
+ const logs = await (0, import_viem2.getLogsSafe)(this.provider.publicClient, {
439
+ fromBlock,
440
+ toBlock: blockNumber,
441
+ address: watchAddresses
442
+ });
443
+ for (const log of logs) {
444
+ const contract = this.contracts.get(log.address);
445
+ if (contract) {
446
+ const event = (0, import_viem.parseEventLogs)({
447
+ abi: contract.abi,
448
+ logs: [log]
449
+ })[0];
450
+ contract.processLog(event);
458
451
  }
459
- return void 0;
460
- })
461
- );
462
- pluginResponse.forEach((r, i) => {
463
- const [name, plugin] = pluginsList[i];
464
- if (plugin.syncState && r.status === "fulfilled") {
465
- this.logger?.debug(`synced plugin ${name}`);
466
- } else if (plugin.syncState && r.status === "rejected") {
467
- this.logger?.error(r.reason, `failed to sync plugin ${name}`);
468
452
  }
469
- });
470
- this.#syncing = false;
471
- delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
472
- this.logger?.debug(
473
- `synced state to block ${blockNumber} (delta ${delta}s)`
474
- );
453
+ this.#currentBlock = blockNumber;
454
+ this.#timestamp = timestamp;
455
+ await this.marketRegister.syncState(skipPriceUpdate);
456
+ await this.#hooks.triggerHooks("syncState", { blockNumber, timestamp });
457
+ const pluginsList = import_utils.TypedObjectUtils.entries(this.plugins);
458
+ const pluginResponse = await Promise.allSettled(
459
+ pluginsList.map(([name, plugin]) => {
460
+ if (plugin.syncState) {
461
+ this.logger?.debug(`syncing plugin ${name}`);
462
+ return plugin.syncState();
463
+ }
464
+ return void 0;
465
+ })
466
+ );
467
+ pluginResponse.forEach((r, i) => {
468
+ const [name, plugin] = pluginsList[i];
469
+ if (plugin.syncState && r.status === "fulfilled") {
470
+ this.logger?.debug(`synced plugin ${name}`);
471
+ } else if (plugin.syncState && r.status === "rejected") {
472
+ this.logger?.error(r.reason, `failed to sync plugin ${name}`);
473
+ }
474
+ });
475
+ delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
476
+ this.logger?.debug(
477
+ `synced state to block ${blockNumber} (delta ${delta}s)`
478
+ );
479
+ } catch (e) {
480
+ this.logger?.error(
481
+ `sync state to block ${blockNumber} with ts ${timestamp} failed, reverting to old block ${prevBlock} with ts ${prevTimestamp}: ${e}`
482
+ );
483
+ this.#currentBlock = prevBlock;
484
+ this.#timestamp = prevTimestamp;
485
+ } finally {
486
+ this.#syncing = false;
487
+ }
475
488
  }
476
489
  get provider() {
477
490
  return this.#provider;
@@ -412,64 +412,77 @@ class GearboxSDK {
412
412
  return;
413
413
  }
414
414
  if (this.#syncing) {
415
- this.logger?.warn(`cannot sync to ${blockNumber}, already syncing`);
415
+ this.logger?.warn(
416
+ `cannot sync to ${blockNumber}, already syncing at ${this.currentBlock}`
417
+ );
416
418
  return;
417
419
  }
418
420
  this.#syncing = true;
419
- let delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
420
- this.logger?.debug(
421
- `syncing state to block ${blockNumber} (delta ${delta}s )...`
422
- );
423
- const watchAddresses = [
424
- ...Array.from(this.marketRegister.watchAddresses),
425
- this.addressProvider.address
426
- ];
427
- const fromBlock = this.currentBlock + 1n;
428
- this.logger?.debug(
429
- `getting logs from ${watchAddresses.length} addresses in [${fromBlock}:${blockNumber}]`
430
- );
431
- const logs = await getLogsSafe(this.provider.publicClient, {
432
- fromBlock,
433
- toBlock: blockNumber,
434
- address: watchAddresses
435
- });
436
- for (const log of logs) {
437
- const contract = this.contracts.get(log.address);
438
- if (contract) {
439
- const event = parseEventLogs({
440
- abi: contract.abi,
441
- logs: [log]
442
- })[0];
443
- contract.processLog(event);
444
- }
445
- }
446
- this.#currentBlock = blockNumber;
447
- this.#timestamp = timestamp;
448
- await this.marketRegister.syncState(skipPriceUpdate);
449
- await this.#hooks.triggerHooks("syncState", { blockNumber, timestamp });
450
- const pluginsList = TypedObjectUtils.entries(this.plugins);
451
- const pluginResponse = await Promise.allSettled(
452
- pluginsList.map(([name, plugin]) => {
453
- if (plugin.syncState) {
454
- this.logger?.debug(`syncing plugin ${name}`);
455
- return plugin.syncState();
421
+ const prevBlock = this.currentBlock;
422
+ const prevTimestamp = this.timestamp;
423
+ try {
424
+ let delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
425
+ this.logger?.debug(
426
+ `syncing state to block ${blockNumber} (delta ${delta}s )...`
427
+ );
428
+ const watchAddresses = [
429
+ ...Array.from(this.marketRegister.watchAddresses),
430
+ this.addressProvider.address
431
+ ];
432
+ const fromBlock = this.currentBlock + 1n;
433
+ this.logger?.debug(
434
+ `getting logs from ${watchAddresses.length} addresses in [${fromBlock}:${blockNumber}]`
435
+ );
436
+ const logs = await getLogsSafe(this.provider.publicClient, {
437
+ fromBlock,
438
+ toBlock: blockNumber,
439
+ address: watchAddresses
440
+ });
441
+ for (const log of logs) {
442
+ const contract = this.contracts.get(log.address);
443
+ if (contract) {
444
+ const event = parseEventLogs({
445
+ abi: contract.abi,
446
+ logs: [log]
447
+ })[0];
448
+ contract.processLog(event);
456
449
  }
457
- return void 0;
458
- })
459
- );
460
- pluginResponse.forEach((r, i) => {
461
- const [name, plugin] = pluginsList[i];
462
- if (plugin.syncState && r.status === "fulfilled") {
463
- this.logger?.debug(`synced plugin ${name}`);
464
- } else if (plugin.syncState && r.status === "rejected") {
465
- this.logger?.error(r.reason, `failed to sync plugin ${name}`);
466
450
  }
467
- });
468
- this.#syncing = false;
469
- delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
470
- this.logger?.debug(
471
- `synced state to block ${blockNumber} (delta ${delta}s)`
472
- );
451
+ this.#currentBlock = blockNumber;
452
+ this.#timestamp = timestamp;
453
+ await this.marketRegister.syncState(skipPriceUpdate);
454
+ await this.#hooks.triggerHooks("syncState", { blockNumber, timestamp });
455
+ const pluginsList = TypedObjectUtils.entries(this.plugins);
456
+ const pluginResponse = await Promise.allSettled(
457
+ pluginsList.map(([name, plugin]) => {
458
+ if (plugin.syncState) {
459
+ this.logger?.debug(`syncing plugin ${name}`);
460
+ return plugin.syncState();
461
+ }
462
+ return void 0;
463
+ })
464
+ );
465
+ pluginResponse.forEach((r, i) => {
466
+ const [name, plugin] = pluginsList[i];
467
+ if (plugin.syncState && r.status === "fulfilled") {
468
+ this.logger?.debug(`synced plugin ${name}`);
469
+ } else if (plugin.syncState && r.status === "rejected") {
470
+ this.logger?.error(r.reason, `failed to sync plugin ${name}`);
471
+ }
472
+ });
473
+ delta = Math.floor(Date.now() / 1e3) - Number(timestamp);
474
+ this.logger?.debug(
475
+ `synced state to block ${blockNumber} (delta ${delta}s)`
476
+ );
477
+ } catch (e) {
478
+ this.logger?.error(
479
+ `sync state to block ${blockNumber} with ts ${timestamp} failed, reverting to old block ${prevBlock} with ts ${prevTimestamp}: ${e}`
480
+ );
481
+ this.#currentBlock = prevBlock;
482
+ this.#timestamp = prevTimestamp;
483
+ } finally {
484
+ this.#syncing = false;
485
+ }
473
486
  }
474
487
  get provider() {
475
488
  return this.#provider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "9.9.0",
3
+ "version": "9.9.1",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",