@gearbox-protocol/sdk 3.2.1-next.1 → 4.0.0-next.2

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.
@@ -193,7 +193,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
193
193
  });
194
194
  }
195
195
  logger?.debug("looking for open strategy");
196
- const strategy = await this.sdk.router.findOpenStrategyPath({
196
+ const strategy = await this.sdk.routerFor(cm).findOpenStrategyPath({
197
197
  creditManager: cm.creditManager,
198
198
  expectedBalances,
199
199
  leftoverBalances,
@@ -50,8 +50,8 @@ class GearboxSDK {
50
50
  #attachConfig;
51
51
  // Collection of markets
52
52
  #marketRegister;
53
- // Router contract
54
- #router;
53
+ // Routers by address
54
+ #routers = new import_utils.AddressMap();
55
55
  logger;
56
56
  /**
57
57
  * Interest rate models can be reused across chain (and SDK operates on chain level)
@@ -203,11 +203,6 @@ class GearboxSDK {
203
203
  marketConfigurators,
204
204
  ignoreUpdateablePrices
205
205
  );
206
- try {
207
- this.#router = (0, import_router.createRouter)(this);
208
- } catch (e) {
209
- this.logger?.warn("router not found", e);
210
- }
211
206
  const pluginsList = import_utils.TypedObjectUtils.entries(this.plugins);
212
207
  const pluginResponse = await Promise.allSettled(
213
208
  pluginsList.map(([name, plugin]) => {
@@ -435,11 +430,37 @@ class GearboxSDK {
435
430
  }
436
431
  return this.#marketRegister;
437
432
  }
438
- get router() {
439
- if (this.#router === void 0) {
440
- throw ERR_NOT_ATTACHED;
433
+ /**
434
+ * Returns router contract that will work for given credit manager or credit facade
435
+ * @param params
436
+ * @returns
437
+ */
438
+ routerFor(params) {
439
+ let facadeAddr;
440
+ if ("creditFacade" in params) {
441
+ facadeAddr = (0, import_utils.toAddress)(params.creditFacade);
442
+ } else {
443
+ const cm = this.marketRegister.findCreditManager(
444
+ (0, import_utils.toAddress)(params.creditManager)
445
+ );
446
+ facadeAddr = cm.creditFacade.address;
447
+ }
448
+ const facadeV = this.contracts.mustGet(facadeAddr).version;
449
+ const routerRange = facadeV >= 310 ? [310, 319] : [300, 309];
450
+ const routerEntry = this.addressProvider.getLatestInRange(
451
+ import_constants.AP_ROUTER,
452
+ routerRange
453
+ );
454
+ if (!routerEntry) {
455
+ throw new Error(
456
+ `router not found for facade v ${facadeV} at ${facadeAddr}`
457
+ );
458
+ }
459
+ const [routerAddr, routerV] = routerEntry;
460
+ if (!this.#routers.has(routerAddr)) {
461
+ this.#routers.upsert(routerAddr, (0, import_router.createRouter)(this, routerAddr, routerV));
441
462
  }
442
- return this.#router;
463
+ return this.#routers.mustGet(routerAddr);
443
464
  }
444
465
  }
445
466
  // Annotate the CommonJS export names for ESM import in node:
@@ -219,7 +219,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
219
219
  */
220
220
  async fullyLiquidate(account, to, slippage = 50n, force = false) {
221
221
  const cm = this.sdk.marketRegister.findCreditManager(account.creditManager);
222
- const routerCloseResult = await this.sdk.router.findBestClosePath({
222
+ const routerCloseResult = await this.sdk.routerFor(account).findBestClosePath({
223
223
  creditAccount: account,
224
224
  creditManager: cm.creditManager,
225
225
  slippage,
@@ -263,7 +263,7 @@ class CreditAccountsService extends import_base.SDKConstruct {
263
263
  closePath
264
264
  }) {
265
265
  const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
266
- const routerCloseResult = closePath || await this.sdk.router.findBestClosePath({
266
+ const routerCloseResult = closePath || await this.sdk.routerFor(ca).findBestClosePath({
267
267
  creditAccount: ca,
268
268
  creditManager: cm.creditManager,
269
269
  slippage
@@ -21,18 +21,16 @@ __export(createRouter_exports, {
21
21
  createRouter: () => createRouter
22
22
  });
23
23
  module.exports = __toCommonJS(createRouter_exports);
24
- var import_constants = require("../constants/index.js");
25
24
  var import_RouterV300Contract = require("./RouterV300Contract.js");
26
25
  var import_RouterV310Contract = require("./RouterV310Contract.js");
27
- function createRouter(sdk) {
28
- const [address, v] = sdk.addressProvider.getLatestVersion(import_constants.AP_ROUTER);
29
- if (v >= 300 && v < 310) {
26
+ function createRouter(sdk, address, version) {
27
+ if (version >= 300 && version < 310) {
30
28
  return new import_RouterV300Contract.RouterV300Contract(sdk, address);
31
29
  }
32
- if (v === 310) {
30
+ if (version === 310) {
33
31
  return new import_RouterV310Contract.RouterV310Contract(sdk, address);
34
32
  }
35
- throw new Error(`Unsupported router version ${v}`);
33
+ throw new Error(`Unsupported router version ${version}`);
36
34
  }
37
35
  // Annotate the CommonJS export names for ESM import in node:
38
36
  0 && (module.exports = {
@@ -26,6 +26,7 @@ __reExport(utils_exports, require("./hex.js"), module.exports);
26
26
  __reExport(utils_exports, require("./json.js"), module.exports);
27
27
  __reExport(utils_exports, require("./mappers.js"), module.exports);
28
28
  __reExport(utils_exports, require("./retry.js"), module.exports);
29
+ __reExport(utils_exports, require("./toAddress.js"), module.exports);
29
30
  // Annotate the CommonJS export names for ESM import in node:
30
31
  0 && (module.exports = {
31
32
  ...require("./AddressMap.js"),
@@ -38,5 +39,6 @@ __reExport(utils_exports, require("./retry.js"), module.exports);
38
39
  ...require("./hex.js"),
39
40
  ...require("./json.js"),
40
41
  ...require("./mappers.js"),
41
- ...require("./retry.js")
42
+ ...require("./retry.js"),
43
+ ...require("./toAddress.js")
42
44
  });
@@ -0,0 +1,36 @@
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 toAddress_exports = {};
20
+ __export(toAddress_exports, {
21
+ toAddress: () => toAddress
22
+ });
23
+ module.exports = __toCommonJS(toAddress_exports);
24
+ function toAddress(value) {
25
+ if (typeof value === "string") {
26
+ return value;
27
+ }
28
+ if ("baseParams" in value) {
29
+ return value.baseParams.addr;
30
+ }
31
+ return value.address;
32
+ }
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ toAddress
36
+ });
@@ -179,7 +179,7 @@ class AccountOpener extends SDKConstruct {
179
179
  });
180
180
  }
181
181
  logger?.debug("looking for open strategy");
182
- const strategy = await this.sdk.router.findOpenStrategyPath({
182
+ const strategy = await this.sdk.routerFor(cm).findOpenStrategyPath({
183
183
  creditManager: cm.creditManager,
184
184
  expectedBalances,
185
185
  leftoverBalances,
@@ -11,6 +11,7 @@ import {
11
11
  AP_BOT_LIST,
12
12
  AP_GEAR_STAKING,
13
13
  AP_GEAR_TOKEN,
14
+ AP_ROUTER,
14
15
  NO_VERSION
15
16
  } from "./constants/index.js";
16
17
  import {
@@ -24,7 +25,12 @@ import {
24
25
  defaultPlugins
25
26
  } from "./plugins/index.js";
26
27
  import { createRouter } from "./router/index.js";
27
- import { AddressMap, formatBN, TypedObjectUtils } from "./utils/index.js";
28
+ import {
29
+ AddressMap,
30
+ formatBN,
31
+ toAddress,
32
+ TypedObjectUtils
33
+ } from "./utils/index.js";
28
34
  import { Hooks } from "./utils/internal/index.js";
29
35
  import { getLogsSafe } from "./utils/viem/index.js";
30
36
  const ERR_NOT_ATTACHED = new Error("Gearbox SDK not attached");
@@ -44,8 +50,8 @@ class GearboxSDK {
44
50
  #attachConfig;
45
51
  // Collection of markets
46
52
  #marketRegister;
47
- // Router contract
48
- #router;
53
+ // Routers by address
54
+ #routers = new AddressMap();
49
55
  logger;
50
56
  /**
51
57
  * Interest rate models can be reused across chain (and SDK operates on chain level)
@@ -197,11 +203,6 @@ class GearboxSDK {
197
203
  marketConfigurators,
198
204
  ignoreUpdateablePrices
199
205
  );
200
- try {
201
- this.#router = createRouter(this);
202
- } catch (e) {
203
- this.logger?.warn("router not found", e);
204
- }
205
206
  const pluginsList = TypedObjectUtils.entries(this.plugins);
206
207
  const pluginResponse = await Promise.allSettled(
207
208
  pluginsList.map(([name, plugin]) => {
@@ -429,11 +430,37 @@ class GearboxSDK {
429
430
  }
430
431
  return this.#marketRegister;
431
432
  }
432
- get router() {
433
- if (this.#router === void 0) {
434
- throw ERR_NOT_ATTACHED;
433
+ /**
434
+ * Returns router contract that will work for given credit manager or credit facade
435
+ * @param params
436
+ * @returns
437
+ */
438
+ routerFor(params) {
439
+ let facadeAddr;
440
+ if ("creditFacade" in params) {
441
+ facadeAddr = toAddress(params.creditFacade);
442
+ } else {
443
+ const cm = this.marketRegister.findCreditManager(
444
+ toAddress(params.creditManager)
445
+ );
446
+ facadeAddr = cm.creditFacade.address;
447
+ }
448
+ const facadeV = this.contracts.mustGet(facadeAddr).version;
449
+ const routerRange = facadeV >= 310 ? [310, 319] : [300, 309];
450
+ const routerEntry = this.addressProvider.getLatestInRange(
451
+ AP_ROUTER,
452
+ routerRange
453
+ );
454
+ if (!routerEntry) {
455
+ throw new Error(
456
+ `router not found for facade v ${facadeV} at ${facadeAddr}`
457
+ );
458
+ }
459
+ const [routerAddr, routerV] = routerEntry;
460
+ if (!this.#routers.has(routerAddr)) {
461
+ this.#routers.upsert(routerAddr, createRouter(this, routerAddr, routerV));
435
462
  }
436
- return this.#router;
463
+ return this.#routers.mustGet(routerAddr);
437
464
  }
438
465
  }
439
466
  export {
@@ -223,7 +223,7 @@ class CreditAccountsService extends SDKConstruct {
223
223
  */
224
224
  async fullyLiquidate(account, to, slippage = 50n, force = false) {
225
225
  const cm = this.sdk.marketRegister.findCreditManager(account.creditManager);
226
- const routerCloseResult = await this.sdk.router.findBestClosePath({
226
+ const routerCloseResult = await this.sdk.routerFor(account).findBestClosePath({
227
227
  creditAccount: account,
228
228
  creditManager: cm.creditManager,
229
229
  slippage,
@@ -267,7 +267,7 @@ class CreditAccountsService extends SDKConstruct {
267
267
  closePath
268
268
  }) {
269
269
  const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
270
- const routerCloseResult = closePath || await this.sdk.router.findBestClosePath({
270
+ const routerCloseResult = closePath || await this.sdk.routerFor(ca).findBestClosePath({
271
271
  creditAccount: ca,
272
272
  creditManager: cm.creditManager,
273
273
  slippage
@@ -1,15 +1,13 @@
1
- import { AP_ROUTER } from "../constants/index.js";
2
1
  import { RouterV300Contract } from "./RouterV300Contract.js";
3
2
  import { RouterV310Contract } from "./RouterV310Contract.js";
4
- function createRouter(sdk) {
5
- const [address, v] = sdk.addressProvider.getLatestVersion(AP_ROUTER);
6
- if (v >= 300 && v < 310) {
3
+ function createRouter(sdk, address, version) {
4
+ if (version >= 300 && version < 310) {
7
5
  return new RouterV300Contract(sdk, address);
8
6
  }
9
- if (v === 310) {
7
+ if (version === 310) {
10
8
  return new RouterV310Contract(sdk, address);
11
9
  }
12
- throw new Error(`Unsupported router version ${v}`);
10
+ throw new Error(`Unsupported router version ${version}`);
13
11
  }
14
12
  export {
15
13
  createRouter
@@ -9,3 +9,4 @@ export * from "./hex.js";
9
9
  export * from "./json.js";
10
10
  export * from "./mappers.js";
11
11
  export * from "./retry.js";
12
+ export * from "./toAddress.js";
@@ -0,0 +1,12 @@
1
+ function toAddress(value) {
2
+ if (typeof value === "string") {
3
+ return value;
4
+ }
5
+ if ("baseParams" in value) {
6
+ return value.baseParams.addr;
7
+ }
8
+ return value.address;
9
+ }
10
+ export {
11
+ toAddress
12
+ };
@@ -1,5 +1,5 @@
1
1
  import type { Address, Hex } from "viem";
2
- import type { BaseContract } from "./base/index.js";
2
+ import type { BaseContract, BaseState, IBaseContract } from "./base/index.js";
3
3
  import { TokensMeta } from "./base/index.js";
4
4
  import type { ConnectionOptions, NetworkOptions, TransportOptions } from "./chain/index.js";
5
5
  import { Provider } from "./chain/index.js";
@@ -8,7 +8,7 @@ import { BotListContract, GearStakingContract } from "./core/index.js";
8
8
  import { MarketRegister } from "./market/MarketRegister.js";
9
9
  import { PriceFeedRegister } from "./market/pricefeeds/index.js";
10
10
  import { type PluginInstances, type PluginMap } from "./plugins/index.js";
11
- import type { IRouterContract } from "./router/index.js";
11
+ import { type IRouterContract } from "./router/index.js";
12
12
  import type { GearboxState, GearboxStateHuman, ILogger, MultiCall } from "./types/index.js";
13
13
  import { AddressMap } from "./utils/index.js";
14
14
  export interface SDKOptions<Plugins extends PluginMap = {}> {
@@ -150,5 +150,14 @@ export declare class GearboxSDK<Plugins extends PluginMap = {}> {
150
150
  get botListContract(): BotListContract;
151
151
  get gearStakingContract(): GearStakingContract;
152
152
  get marketRegister(): MarketRegister;
153
- get router(): IRouterContract;
153
+ /**
154
+ * Returns router contract that will work for given credit manager or credit facade
155
+ * @param params
156
+ * @returns
157
+ */
158
+ routerFor(params: {
159
+ creditManager: Address | BaseState | IBaseContract;
160
+ } | {
161
+ creditFacade: Address | BaseState | IBaseContract;
162
+ }): IRouterContract;
154
163
  }
@@ -1,3 +1,4 @@
1
+ import type { Address } from "viem";
1
2
  import type { GearboxSDK } from "../GearboxSDK.js";
2
3
  import type { IRouterContract } from "./types.js";
3
- export declare function createRouter(sdk: GearboxSDK): IRouterContract;
4
+ export declare function createRouter(sdk: GearboxSDK, address: Address, version: number): IRouterContract;
@@ -9,3 +9,4 @@ export * from "./hex.js";
9
9
  export * from "./json.js";
10
10
  export * from "./mappers.js";
11
11
  export * from "./retry.js";
12
+ export * from "./toAddress.js";
@@ -0,0 +1,3 @@
1
+ import type { Address } from "abitype";
2
+ import type { BaseState, IBaseContract } from "../base/index.js";
3
+ export declare function toAddress(value: Address | BaseState | IBaseContract): Address;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.2.1-next.1",
3
+ "version": "4.0.0-next.2",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",
@@ -65,8 +65,8 @@
65
65
  "typecheck:ci": "tsc --noEmit"
66
66
  },
67
67
  "dependencies": {
68
- "@redstone-finance/evm-connector": "^0.7.3",
69
- "@redstone-finance/protocol": "^0.7.3",
68
+ "@redstone-finance/evm-connector": "^0.7.5",
69
+ "@redstone-finance/protocol": "^0.7.5",
70
70
  "abitype": "^1.0.8",
71
71
  "date-fns": "^4.1.0",
72
72
  "decimal.js-light": "^2.5.1",
@@ -79,7 +79,7 @@
79
79
  "@gearbox-protocol/eslint-config": "^2.0.0-next.2",
80
80
  "@gearbox-protocol/prettier-config": "^2.1.0",
81
81
  "@types/cross-spawn": "^6.0.6",
82
- "axios": "^1.8.4",
82
+ "axios": "^1.9.0",
83
83
  "cross-spawn": "^7.0.6",
84
84
  "eslint": "^8.57.1",
85
85
  "eslint-plugin-require-extensions": "^0.1.3",
@@ -89,9 +89,9 @@
89
89
  "pino-pretty": "^13.0.0",
90
90
  "prettier": "^3.5.3",
91
91
  "tsup": "^8.4.0",
92
- "tsx": "^4.19.3",
92
+ "tsx": "^4.19.4",
93
93
  "typescript": "^5.8.3",
94
- "vitest": "^3.1.1"
94
+ "vitest": "^3.1.2"
95
95
  },
96
96
  "peerDependencies": {
97
97
  "axios": "^1.0.0"