@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.
- package/dist/cjs/sdk/GearboxSDK.js +66 -53
- package/dist/esm/sdk/GearboxSDK.js +66 -53
- package/package.json +1 -1
|
@@ -414,64 +414,77 @@ class GearboxSDK {
|
|
|
414
414
|
return;
|
|
415
415
|
}
|
|
416
416
|
if (this.#syncing) {
|
|
417
|
-
this.logger?.warn(
|
|
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
|
-
|
|
422
|
-
this.
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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(
|
|
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
|
-
|
|
420
|
-
this.
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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;
|