@gearbox-protocol/sdk 3.0.3 → 3.0.4

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.
@@ -32,6 +32,7 @@ var import_plugins = require("./plugins/index.js");
32
32
  var import_router = require("./router/index.js");
33
33
  var import_utils = require("./utils/index.js");
34
34
  var import_internal = require("./utils/internal/index.js");
35
+ var import_viem2 = require("./utils/viem/index.js");
35
36
  const ERR_NOT_ATTACHED = new Error("Gearbox SDK not attached");
36
37
  class GearboxSDK {
37
38
  #hooks = new import_internal.Hooks();
@@ -354,7 +355,7 @@ class GearboxSDK {
354
355
  this.logger?.debug(
355
356
  `getting logs from ${watchAddresses.length} addresses in [${this.currentBlock}:${blockNumber}]`
356
357
  );
357
- const logs = await this.provider.publicClient.getLogs({
358
+ const logs = await (0, import_viem2.getLogsSafe)(this.provider.publicClient, {
358
359
  fromBlock: this.currentBlock,
359
360
  toBlock: blockNumber,
360
361
  address: watchAddresses
@@ -34,6 +34,7 @@ module.exports = __toCommonJS(AddressProviderV3Contract_exports);
34
34
  var import_viem = require("viem");
35
35
  var import_v300 = require("../../../abi/v300.js");
36
36
  var import_constants = require("../../constants/index.js");
37
+ var import_viem2 = require("../../utils/viem/index.js");
37
38
  var import_AbstractAddressProviderContract = __toESM(require("./AbstractAddressProviderContract.js"));
38
39
  const abi = import_v300.iAddressProviderV300Abi;
39
40
  class AddressProviderContractV3 extends import_AbstractAddressProviderContract.default {
@@ -89,7 +90,7 @@ class AddressProviderContractV3 extends import_AbstractAddressProviderContract.d
89
90
  this.logger?.debug(
90
91
  `loading events from block ${fromBlock} to ${blockNumber}`
91
92
  );
92
- const events = await this.sdk.provider.publicClient.getLogs({
93
+ const events = await (0, import_viem2.getLogsSafe)(this.sdk.provider.publicClient, {
93
94
  address: this.address,
94
95
  event: (0, import_viem.getAbiItem)({ abi: this.abi, name: "SetAddress" }),
95
96
  fromBlock,
@@ -27,6 +27,7 @@ var import_base = require("../base/index.js");
27
27
  var import_constants = require("../constants/index.js");
28
28
  var import_market = require("../market/index.js");
29
29
  var import_utils = require("../utils/index.js");
30
+ var import_viem2 = require("../utils/viem/index.js");
30
31
  class V300StalenessPeriodPlugin extends import_base.SDKConstruct {
31
32
  #syncedTo;
32
33
  #logger;
@@ -49,7 +50,7 @@ class V300StalenessPeriodPlugin extends import_base.SDKConstruct {
49
50
  if (addresses.length === 0 || fromBlock > toBlock) {
50
51
  return;
51
52
  }
52
- const events = await this.client.getLogs({
53
+ const events = await (0, import_viem2.getLogsSafe)(this.client, {
53
54
  address: addresses,
54
55
  events: [
55
56
  (0, import_viem.getAbiItem)({
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var getLogsSafe_exports = {};
20
+ __export(getLogsSafe_exports, {
21
+ getLogsSafe: () => getLogsSafe
22
+ });
23
+ module.exports = __toCommonJS(getLogsSafe_exports);
24
+ var import_viem = require("viem");
25
+ var import_actions = require("viem/actions");
26
+ async function getLogsSafe(client, params = {}) {
27
+ try {
28
+ const events = await (0, import_actions.getLogs)(client, params);
29
+ return events;
30
+ } catch (e) {
31
+ const fromBlock = params.fromBlock;
32
+ const toBlock = params.toBlock;
33
+ const bisected = tryBisectBlockRange({ fromBlock, toBlock }, e);
34
+ if (!bisected) {
35
+ throw e;
36
+ }
37
+ const [left, right] = await Promise.all([
38
+ (0, import_actions.getLogs)(client, { ...params, ...bisected[0] }),
39
+ (0, import_actions.getLogs)(client, { ...params, ...bisected[1] })
40
+ ]);
41
+ return [...left, ...right];
42
+ }
43
+ }
44
+ function tryBisectBlockRange({ fromBlock, toBlock }, e) {
45
+ const alchemyMid = checkForAlchemyBlockRange(e);
46
+ if (alchemyMid && alchemyMid > fromBlock && alchemyMid < toBlock) {
47
+ return [
48
+ { fromBlock, toBlock: alchemyMid },
49
+ { fromBlock: alchemyMid + 1n, toBlock }
50
+ ];
51
+ }
52
+ const blockRangeErrors = [
53
+ "query exceeds max block",
54
+ "range is too large",
55
+ "eth_getLogs is limited to",
56
+ "eth_getLogs requests with up to"
57
+ ];
58
+ if (e instanceof Error && blockRangeErrors.some((errorText) => e.message.includes(errorText))) {
59
+ const middle = (fromBlock + toBlock) / 2n;
60
+ return [
61
+ { fromBlock, toBlock: middle },
62
+ { fromBlock: middle + 1n, toBlock }
63
+ ];
64
+ }
65
+ return void 0;
66
+ }
67
+ const ALCHEMY_BLOCK_RANGE_REGEX = /this block range should work: \[(0x[0-9a-fA-F]+),\s*(0x[0-9a-fA-F]+)\]/;
68
+ function checkForAlchemyBlockRange(e) {
69
+ if (e instanceof import_viem.HttpRequestError) {
70
+ try {
71
+ const err = JSON.parse(e.details);
72
+ if (typeof err.message === "string") {
73
+ const match = err.message.match(ALCHEMY_BLOCK_RANGE_REGEX);
74
+ if (match) {
75
+ return BigInt(match[2]);
76
+ }
77
+ }
78
+ } catch {
79
+ }
80
+ }
81
+ return void 0;
82
+ }
83
+ // Annotate the CommonJS export names for ESM import in node:
84
+ 0 && (module.exports = {
85
+ getLogsSafe
86
+ });
@@ -16,12 +16,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
16
16
  var viem_exports = {};
17
17
  module.exports = __toCommonJS(viem_exports);
18
18
  __reExport(viem_exports, require("./cast.js"), module.exports);
19
+ __reExport(viem_exports, require("./getLogsSafe.js"), module.exports);
19
20
  __reExport(viem_exports, require("./sendRawTx.js"), module.exports);
20
21
  __reExport(viem_exports, require("./simulateMulticall.js"), module.exports);
21
22
  __reExport(viem_exports, require("./simulateWithPriceUpdates.js"), module.exports);
22
23
  // Annotate the CommonJS export names for ESM import in node:
23
24
  0 && (module.exports = {
24
25
  ...require("./cast.js"),
26
+ ...require("./getLogsSafe.js"),
25
27
  ...require("./sendRawTx.js"),
26
28
  ...require("./simulateMulticall.js"),
27
29
  ...require("./simulateWithPriceUpdates.js")
@@ -26,6 +26,7 @@ import {
26
26
  import { createRouter } from "./router/index.js";
27
27
  import { AddressMap, formatBN, TypedObjectUtils } from "./utils/index.js";
28
28
  import { Hooks } from "./utils/internal/index.js";
29
+ import { getLogsSafe } from "./utils/viem/index.js";
29
30
  const ERR_NOT_ATTACHED = new Error("Gearbox SDK not attached");
30
31
  class GearboxSDK {
31
32
  #hooks = new Hooks();
@@ -348,7 +349,7 @@ class GearboxSDK {
348
349
  this.logger?.debug(
349
350
  `getting logs from ${watchAddresses.length} addresses in [${this.currentBlock}:${blockNumber}]`
350
351
  );
351
- const logs = await this.provider.publicClient.getLogs({
352
+ const logs = await getLogsSafe(this.provider.publicClient, {
352
353
  fromBlock: this.currentBlock,
353
354
  toBlock: blockNumber,
354
355
  address: watchAddresses
@@ -1,6 +1,7 @@
1
1
  import { bytesToString, getAbiItem, parseEventLogs, toBytes } from "viem";
2
2
  import { iAddressProviderV300Abi } from "../../../abi/v300.js";
3
3
  import { ADDRESS_PROVIDER_BLOCK } from "../../constants/index.js";
4
+ import { getLogsSafe } from "../../utils/viem/index.js";
4
5
  import AbstractAddressProviderContract from "./AbstractAddressProviderContract.js";
5
6
  const abi = iAddressProviderV300Abi;
6
7
  class AddressProviderContractV3 extends AbstractAddressProviderContract {
@@ -56,7 +57,7 @@ class AddressProviderContractV3 extends AbstractAddressProviderContract {
56
57
  this.logger?.debug(
57
58
  `loading events from block ${fromBlock} to ${blockNumber}`
58
59
  );
59
- const events = await this.sdk.provider.publicClient.getLogs({
60
+ const events = await getLogsSafe(this.sdk.provider.publicClient, {
60
61
  address: this.address,
61
62
  event: getAbiItem({ abi: this.abi, name: "SetAddress" }),
62
63
  fromBlock,
@@ -4,6 +4,7 @@ import { SDKConstruct } from "../base/index.js";
4
4
  import { ADDRESS_PROVIDER_BLOCK } from "../constants/index.js";
5
5
  import { PriceFeedRef } from "../market/index.js";
6
6
  import { formatDuration, hexEq } from "../utils/index.js";
7
+ import { getLogsSafe } from "../utils/viem/index.js";
7
8
  class V300StalenessPeriodPlugin extends SDKConstruct {
8
9
  #syncedTo;
9
10
  #logger;
@@ -26,7 +27,7 @@ class V300StalenessPeriodPlugin extends SDKConstruct {
26
27
  if (addresses.length === 0 || fromBlock > toBlock) {
27
28
  return;
28
29
  }
29
- const events = await this.client.getLogs({
30
+ const events = await getLogsSafe(this.client, {
30
31
  address: addresses,
31
32
  events: [
32
33
  getAbiItem({
@@ -0,0 +1,64 @@
1
+ import {
2
+ HttpRequestError
3
+ } from "viem";
4
+ import { getLogs } from "viem/actions";
5
+ async function getLogsSafe(client, params = {}) {
6
+ try {
7
+ const events = await getLogs(client, params);
8
+ return events;
9
+ } catch (e) {
10
+ const fromBlock = params.fromBlock;
11
+ const toBlock = params.toBlock;
12
+ const bisected = tryBisectBlockRange({ fromBlock, toBlock }, e);
13
+ if (!bisected) {
14
+ throw e;
15
+ }
16
+ const [left, right] = await Promise.all([
17
+ getLogs(client, { ...params, ...bisected[0] }),
18
+ getLogs(client, { ...params, ...bisected[1] })
19
+ ]);
20
+ return [...left, ...right];
21
+ }
22
+ }
23
+ function tryBisectBlockRange({ fromBlock, toBlock }, e) {
24
+ const alchemyMid = checkForAlchemyBlockRange(e);
25
+ if (alchemyMid && alchemyMid > fromBlock && alchemyMid < toBlock) {
26
+ return [
27
+ { fromBlock, toBlock: alchemyMid },
28
+ { fromBlock: alchemyMid + 1n, toBlock }
29
+ ];
30
+ }
31
+ const blockRangeErrors = [
32
+ "query exceeds max block",
33
+ "range is too large",
34
+ "eth_getLogs is limited to",
35
+ "eth_getLogs requests with up to"
36
+ ];
37
+ if (e instanceof Error && blockRangeErrors.some((errorText) => e.message.includes(errorText))) {
38
+ const middle = (fromBlock + toBlock) / 2n;
39
+ return [
40
+ { fromBlock, toBlock: middle },
41
+ { fromBlock: middle + 1n, toBlock }
42
+ ];
43
+ }
44
+ return void 0;
45
+ }
46
+ const ALCHEMY_BLOCK_RANGE_REGEX = /this block range should work: \[(0x[0-9a-fA-F]+),\s*(0x[0-9a-fA-F]+)\]/;
47
+ function checkForAlchemyBlockRange(e) {
48
+ if (e instanceof HttpRequestError) {
49
+ try {
50
+ const err = JSON.parse(e.details);
51
+ if (typeof err.message === "string") {
52
+ const match = err.message.match(ALCHEMY_BLOCK_RANGE_REGEX);
53
+ if (match) {
54
+ return BigInt(match[2]);
55
+ }
56
+ }
57
+ } catch {
58
+ }
59
+ }
60
+ return void 0;
61
+ }
62
+ export {
63
+ getLogsSafe
64
+ };
@@ -1,4 +1,5 @@
1
1
  export * from "./cast.js";
2
+ export * from "./getLogsSafe.js";
2
3
  export * from "./sendRawTx.js";
3
4
  export * from "./simulateMulticall.js";
4
5
  export * from "./simulateWithPriceUpdates.js";
@@ -0,0 +1,3 @@
1
+ import type { AbiEvent } from "abitype";
2
+ import { type BlockNumber, type Chain, type Client, type GetLogsParameters, type GetLogsReturnType, type Transport } from "viem";
3
+ export declare function getLogsSafe<chain extends Chain | undefined, const abiEvent extends AbiEvent | undefined = undefined, const abiEvents extends readonly AbiEvent[] | readonly unknown[] | undefined = abiEvent extends AbiEvent ? [abiEvent] : undefined, strict extends boolean | undefined = undefined>(client: Client<Transport, chain>, params?: GetLogsParameters<abiEvent, abiEvents, strict, BlockNumber, BlockNumber>): Promise<GetLogsReturnType<abiEvent, abiEvents, strict, BlockNumber, BlockNumber>>;
@@ -1,4 +1,5 @@
1
1
  export * from "./cast.js";
2
+ export * from "./getLogsSafe.js";
2
3
  export * from "./sendRawTx.js";
3
4
  export * from "./simulateMulticall.js";
4
5
  export * from "./simulateWithPriceUpdates.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",