@gearbox-protocol/sdk 8.27.12 → 8.28.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.
@@ -0,0 +1,81 @@
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 EthCallSpy_exports = {};
20
+ __export(EthCallSpy_exports, {
21
+ default: () => EthCallSpy
22
+ });
23
+ module.exports = __toCommonJS(EthCallSpy_exports);
24
+ class EthCallSpy {
25
+ #logger;
26
+ #detectedCalls = [];
27
+ #detectedBlock = 0n;
28
+ #check;
29
+ enabled;
30
+ constructor(check, logger, enabled) {
31
+ this.#check = check;
32
+ this.#logger = logger;
33
+ this.enabled = !!enabled;
34
+ }
35
+ onFetchRequest = async (request) => {
36
+ if (!this.enabled) {
37
+ return;
38
+ }
39
+ const data = await request.json();
40
+ const blockNumber = this.#shouldStore(data);
41
+ if (blockNumber) {
42
+ this.#storeCall(blockNumber, data);
43
+ this.#logger?.debug(
44
+ `spy stored eth_call at block ${blockNumber}, total calls: ${this.#detectedCalls.length}`
45
+ );
46
+ }
47
+ };
48
+ onFetchResponse = async (response) => {
49
+ if (!this.enabled) {
50
+ return;
51
+ }
52
+ const copy = response.clone();
53
+ const resp = await copy.json();
54
+ const id = resp.id;
55
+ const call = this.#detectedCalls.find(
56
+ ({ request }) => "id" in request && request.id === id
57
+ );
58
+ if (call) {
59
+ call.response = resp;
60
+ call.responseHeaders = Object.fromEntries(response.headers.entries());
61
+ }
62
+ };
63
+ get detectedCalls() {
64
+ return this.#detectedCalls;
65
+ }
66
+ #shouldStore(data) {
67
+ if (data.method === "eth_call" && typeof data.params[1] === "string" && // block number is present
68
+ data.params[1]?.startsWith("0x") && // and it's a block number
69
+ this.#check(data)) {
70
+ return BigInt(data.params[1]);
71
+ }
72
+ return void 0;
73
+ }
74
+ #storeCall(blockNumber, data) {
75
+ if (blockNumber !== this.#detectedBlock) {
76
+ this.#detectedBlock = blockNumber;
77
+ this.#detectedCalls = [];
78
+ }
79
+ this.#detectedCalls.push({ request: data });
80
+ }
81
+ }
@@ -23,6 +23,7 @@ __reExport(dev_exports, require("./create2.js"), module.exports);
23
23
  __reExport(dev_exports, require("./createAnvilClient.js"), module.exports);
24
24
  __reExport(dev_exports, require("./createTransport.js"), module.exports);
25
25
  __reExport(dev_exports, require("./detectChain.js"), module.exports);
26
+ __reExport(dev_exports, require("./EthCallSpy.js"), module.exports);
26
27
  __reExport(dev_exports, require("./ltUtils.js"), module.exports);
27
28
  __reExport(dev_exports, require("./migrateFaucet.js"), module.exports);
28
29
  // Annotate the CommonJS export names for ESM import in node:
@@ -35,6 +36,7 @@ __reExport(dev_exports, require("./migrateFaucet.js"), module.exports);
35
36
  ...require("./createAnvilClient.js"),
36
37
  ...require("./createTransport.js"),
37
38
  ...require("./detectChain.js"),
39
+ ...require("./EthCallSpy.js"),
38
40
  ...require("./ltUtils.js"),
39
41
  ...require("./migrateFaucet.js")
40
42
  });
@@ -23,6 +23,7 @@ __export(networks_exports, {
23
23
  RAMP_DURATION_BY_NETWORK: () => RAMP_DURATION_BY_NETWORK
24
24
  });
25
25
  module.exports = __toCommonJS(networks_exports);
26
+ var import_chain = require("../chain/index.js");
26
27
  var import_mappers = require("../utils/mappers.js");
27
28
  const ADDRESS_PROVIDER_BLOCK = {
28
29
  Mainnet: 18433056n,
@@ -51,27 +52,19 @@ const ADDRESS_PROVIDER_BLOCK = {
51
52
  Lisk: 18934260n
52
53
  // arbitrary not deployed yet
53
54
  };
54
- const BLOCK_DURATION_BY_NETWORK = {
55
- Mainnet: 12.05,
56
- Arbitrum: 0.26,
57
- Optimism: 2,
58
- Base: 2.01,
59
- Sonic: 0.91,
60
- // New networks
61
- MegaETH: 0.01,
62
- // <10ms/block, on testnet
63
- Monad: 1,
64
- // on testnet
65
- Berachain: 1.9,
66
- Avalanche: 1.7,
67
- BNB: 3,
68
- WorldChain: 2,
69
- Etherlink: 1,
70
- Hemi: 12,
71
- Lisk: 2
72
- };
55
+ const BLOCK_DURATION_LOCAL = {};
56
+ const BLOCK_DURATION = Object.values(import_chain.chains).reduce(
57
+ (acc, chain) => {
58
+ const blockTime = chain.blockTime || BLOCK_DURATION_LOCAL[chain.network] || 0;
59
+ if (blockTime === 0)
60
+ console.error(`Block time for ${chain.name} is unknown`);
61
+ acc[chain.network] = blockTime / 1e3;
62
+ return acc;
63
+ },
64
+ {}
65
+ );
73
66
  const RAMP_TIME = 30 * 24 * 60 * 60 * 1.2;
74
- const RAMP_DURATION_BY_NETWORK = import_mappers.TypedObjectUtils.entries(BLOCK_DURATION_BY_NETWORK).reduce(
67
+ const RAMP_DURATION_BY_NETWORK = import_mappers.TypedObjectUtils.entries(BLOCK_DURATION).reduce(
75
68
  (acc, [n, duration]) => {
76
69
  acc[n] = BigInt(Math.floor(RAMP_TIME / duration));
77
70
  return acc;
@@ -79,7 +72,7 @@ const RAMP_DURATION_BY_NETWORK = import_mappers.TypedObjectUtils.entries(BLOCK_D
79
72
  {}
80
73
  );
81
74
  const WEEK = 7 * 24 * 60 * 60;
82
- const BLOCKS_PER_WEEK_BY_NETWORK = import_mappers.TypedObjectUtils.entries(BLOCK_DURATION_BY_NETWORK).reduce(
75
+ const BLOCKS_PER_WEEK_BY_NETWORK = import_mappers.TypedObjectUtils.entries(BLOCK_DURATION).reduce(
83
76
  (acc, [n, duration]) => {
84
77
  acc[n] = BigInt(Math.floor(WEEK / duration));
85
78
  return acc;
@@ -0,0 +1,61 @@
1
+ class EthCallSpy {
2
+ #logger;
3
+ #detectedCalls = [];
4
+ #detectedBlock = 0n;
5
+ #check;
6
+ enabled;
7
+ constructor(check, logger, enabled) {
8
+ this.#check = check;
9
+ this.#logger = logger;
10
+ this.enabled = !!enabled;
11
+ }
12
+ onFetchRequest = async (request) => {
13
+ if (!this.enabled) {
14
+ return;
15
+ }
16
+ const data = await request.json();
17
+ const blockNumber = this.#shouldStore(data);
18
+ if (blockNumber) {
19
+ this.#storeCall(blockNumber, data);
20
+ this.#logger?.debug(
21
+ `spy stored eth_call at block ${blockNumber}, total calls: ${this.#detectedCalls.length}`
22
+ );
23
+ }
24
+ };
25
+ onFetchResponse = async (response) => {
26
+ if (!this.enabled) {
27
+ return;
28
+ }
29
+ const copy = response.clone();
30
+ const resp = await copy.json();
31
+ const id = resp.id;
32
+ const call = this.#detectedCalls.find(
33
+ ({ request }) => "id" in request && request.id === id
34
+ );
35
+ if (call) {
36
+ call.response = resp;
37
+ call.responseHeaders = Object.fromEntries(response.headers.entries());
38
+ }
39
+ };
40
+ get detectedCalls() {
41
+ return this.#detectedCalls;
42
+ }
43
+ #shouldStore(data) {
44
+ if (data.method === "eth_call" && typeof data.params[1] === "string" && // block number is present
45
+ data.params[1]?.startsWith("0x") && // and it's a block number
46
+ this.#check(data)) {
47
+ return BigInt(data.params[1]);
48
+ }
49
+ return void 0;
50
+ }
51
+ #storeCall(blockNumber, data) {
52
+ if (blockNumber !== this.#detectedBlock) {
53
+ this.#detectedBlock = blockNumber;
54
+ this.#detectedCalls = [];
55
+ }
56
+ this.#detectedCalls.push({ request: data });
57
+ }
58
+ }
59
+ export {
60
+ EthCallSpy as default
61
+ };
@@ -6,5 +6,6 @@ export * from "./create2.js";
6
6
  export * from "./createAnvilClient.js";
7
7
  export * from "./createTransport.js";
8
8
  export * from "./detectChain.js";
9
+ export * from "./EthCallSpy.js";
9
10
  export * from "./ltUtils.js";
10
11
  export * from "./migrateFaucet.js";
@@ -1,3 +1,4 @@
1
+ import { chains as CHAINS } from "../chain/index.js";
1
2
  import { TypedObjectUtils } from "../utils/mappers.js";
2
3
  const ADDRESS_PROVIDER_BLOCK = {
3
4
  Mainnet: 18433056n,
@@ -26,27 +27,19 @@ const ADDRESS_PROVIDER_BLOCK = {
26
27
  Lisk: 18934260n
27
28
  // arbitrary not deployed yet
28
29
  };
29
- const BLOCK_DURATION_BY_NETWORK = {
30
- Mainnet: 12.05,
31
- Arbitrum: 0.26,
32
- Optimism: 2,
33
- Base: 2.01,
34
- Sonic: 0.91,
35
- // New networks
36
- MegaETH: 0.01,
37
- // <10ms/block, on testnet
38
- Monad: 1,
39
- // on testnet
40
- Berachain: 1.9,
41
- Avalanche: 1.7,
42
- BNB: 3,
43
- WorldChain: 2,
44
- Etherlink: 1,
45
- Hemi: 12,
46
- Lisk: 2
47
- };
30
+ const BLOCK_DURATION_LOCAL = {};
31
+ const BLOCK_DURATION = Object.values(CHAINS).reduce(
32
+ (acc, chain) => {
33
+ const blockTime = chain.blockTime || BLOCK_DURATION_LOCAL[chain.network] || 0;
34
+ if (blockTime === 0)
35
+ console.error(`Block time for ${chain.name} is unknown`);
36
+ acc[chain.network] = blockTime / 1e3;
37
+ return acc;
38
+ },
39
+ {}
40
+ );
48
41
  const RAMP_TIME = 30 * 24 * 60 * 60 * 1.2;
49
- const RAMP_DURATION_BY_NETWORK = TypedObjectUtils.entries(BLOCK_DURATION_BY_NETWORK).reduce(
42
+ const RAMP_DURATION_BY_NETWORK = TypedObjectUtils.entries(BLOCK_DURATION).reduce(
50
43
  (acc, [n, duration]) => {
51
44
  acc[n] = BigInt(Math.floor(RAMP_TIME / duration));
52
45
  return acc;
@@ -54,7 +47,7 @@ const RAMP_DURATION_BY_NETWORK = TypedObjectUtils.entries(BLOCK_DURATION_BY_NETW
54
47
  {}
55
48
  );
56
49
  const WEEK = 7 * 24 * 60 * 60;
57
- const BLOCKS_PER_WEEK_BY_NETWORK = TypedObjectUtils.entries(BLOCK_DURATION_BY_NETWORK).reduce(
50
+ const BLOCKS_PER_WEEK_BY_NETWORK = TypedObjectUtils.entries(BLOCK_DURATION).reduce(
58
51
  (acc, [n, duration]) => {
59
52
  acc[n] = BigInt(Math.floor(WEEK / duration));
60
53
  return acc;
@@ -0,0 +1,22 @@
1
+ import type { HttpTransportConfig, PublicRpcSchema } from "viem";
2
+ import type { ILogger } from "../sdk/index.js";
3
+ export type EthCallMethod = Extract<PublicRpcSchema[number], {
4
+ Method: "eth_call";
5
+ }>;
6
+ export interface DetectedCall {
7
+ request: EthCallMethod;
8
+ response?: EthCallMethod["ReturnType"];
9
+ responseHeaders?: Record<string, string>;
10
+ }
11
+ export type CheckMulticallFn = (data: EthCallMethod) => boolean;
12
+ /**
13
+ * Helper to spy on eth_call requests and responses in viem transport
14
+ */
15
+ export default class EthCallSpy {
16
+ #private;
17
+ enabled: boolean;
18
+ constructor(check: CheckMulticallFn, logger?: ILogger, enabled?: boolean);
19
+ onFetchRequest: Required<HttpTransportConfig>["onFetchRequest"];
20
+ onFetchResponse: Required<HttpTransportConfig>["onFetchResponse"];
21
+ get detectedCalls(): DetectedCall[];
22
+ }
@@ -6,5 +6,6 @@ export * from "./create2.js";
6
6
  export * from "./createAnvilClient.js";
7
7
  export * from "./createTransport.js";
8
8
  export * from "./detectChain.js";
9
+ export * from "./EthCallSpy.js";
9
10
  export * from "./ltUtils.js";
10
11
  export * from "./migrateFaucet.js";
@@ -1,4 +1,4 @@
1
- import type { NetworkType } from "../chain/index.js";
1
+ import { type NetworkType } from "../chain/index.js";
2
2
  /**
3
3
  * Block number when address provider was deployed
4
4
  * @deprecated use chain.firstBlock instead
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "8.27.12",
3
+ "version": "8.28.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",