@gearbox-protocol/sdk 7.0.0 → 7.2.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,101 @@
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 AccountsCounterPlugin_exports = {};
20
+ __export(AccountsCounterPlugin_exports, {
21
+ AccountsCounterPlugin: () => AccountsCounterPlugin
22
+ });
23
+ module.exports = __toCommonJS(AccountsCounterPlugin_exports);
24
+ var import_compressors = require("../abi/compressors.js");
25
+ var import_sdk = require("../sdk/index.js");
26
+ class AccountsCounterPlugin extends import_sdk.SDKConstruct {
27
+ #accounts = new import_sdk.AddressMap();
28
+ version = 1;
29
+ async attach() {
30
+ await this.#load();
31
+ }
32
+ async syncState() {
33
+ await this.#load();
34
+ }
35
+ async #load() {
36
+ const [compressor] = this.sdk.addressProvider.mustGetLatest(
37
+ import_sdk.AP_CREDIT_ACCOUNT_COMPRESSOR,
38
+ import_sdk.VERSION_RANGE_310
39
+ );
40
+ const cms = this.sdk.marketRegister.creditManagers;
41
+ const count = await this.sdk.provider.publicClient.multicall({
42
+ contracts: cms.flatMap(
43
+ (cm) => [
44
+ {
45
+ abi: import_compressors.iCreditAccountCompressorAbi,
46
+ address: compressor,
47
+ functionName: "countCreditAccounts",
48
+ args: [
49
+ cm.creditManager.address,
50
+ {
51
+ owner: import_sdk.ADDRESS_0X0,
52
+ includeZeroDebt: false,
53
+ maxHealthFactor: import_sdk.MAX_UINT256,
54
+ minHealthFactor: 0n,
55
+ reverting: false
56
+ }
57
+ ]
58
+ },
59
+ {
60
+ abi: import_compressors.iCreditAccountCompressorAbi,
61
+ address: compressor,
62
+ functionName: "countCreditAccounts",
63
+ args: [
64
+ cm.creditManager.address,
65
+ {
66
+ owner: import_sdk.ADDRESS_0X0,
67
+ includeZeroDebt: false,
68
+ maxHealthFactor: import_sdk.MAX_UINT256,
69
+ minHealthFactor: 0n,
70
+ reverting: true
71
+ }
72
+ ]
73
+ }
74
+ ]
75
+ ),
76
+ allowFailure: false
77
+ });
78
+ this.#accounts.clear();
79
+ for (let i = 0; i < cms.length; i++) {
80
+ const cm = cms[i];
81
+ const [reverting, nonReverting] = [count[2 * i], count[2 * i + 1]];
82
+ this.#accounts.upsert(cm.creditManager.address, reverting + nonReverting);
83
+ }
84
+ }
85
+ get state() {
86
+ return {
87
+ version: this.version,
88
+ accounts: this.#accounts.asRecord()
89
+ };
90
+ }
91
+ hydrate(state) {
92
+ this.#accounts.clear();
93
+ for (const [addr, count] of import_sdk.TypedObjectUtils.entries(state.accounts)) {
94
+ this.#accounts.upsert(addr, count);
95
+ }
96
+ }
97
+ }
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ AccountsCounterPlugin
101
+ });
@@ -19,23 +19,26 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var createTransport_exports = {};
20
20
  __export(createTransport_exports, {
21
21
  createTransport: () => createTransport,
22
- getAlchemyHttpUrl: () => getAlchemyHttpUrl,
23
22
  getAlchemyUrl: () => getAlchemyUrl,
24
- getAlchemyWsUrl: () => getAlchemyWsUrl
23
+ getDrpcUrl: () => getDrpcUrl,
24
+ getProviderUrl: () => getProviderUrl
25
25
  });
26
26
  module.exports = __toCommonJS(createTransport_exports);
27
27
  var import_viem = require("viem");
28
28
  var import_sdk = require("../sdk/index.js");
29
29
  function createTransport(config) {
30
- const { alchemyKeys, protocol, network, ...rest } = config;
30
+ const { rpcProviders = [], protocol, network, ...rest } = config;
31
+ const allKeys = rpcProviders.flatMap((provider) => provider.keys) ?? [];
31
32
  const rpcUrls = config.rpcUrls.filter((url) => {
32
- return url.startsWith(protocol) && !alchemyKeys.some((key) => url.includes(key));
33
+ return url.startsWith(protocol) && !allKeys.some((key) => url.includes(key));
33
34
  });
34
- const { alchemyDomain } = (0, import_sdk.getChain)(network);
35
- if (alchemyDomain) {
36
- rpcUrls.unshift(
37
- ...alchemyKeys.map((key) => getAlchemyUrl(alchemyDomain, key, protocol))
38
- );
35
+ for (const { provider, keys } of rpcProviders) {
36
+ for (const key of keys) {
37
+ const url = getProviderUrl(provider, network, key, protocol);
38
+ if (url) {
39
+ rpcUrls.push(url);
40
+ }
41
+ }
39
42
  }
40
43
  const transports = rpcUrls.map(
41
44
  (url) => protocol === "http" ? (0, import_viem.http)(url, rest) : (0, import_viem.webSocket)(url, rest)
@@ -45,19 +48,42 @@ function createTransport(config) {
45
48
  }
46
49
  return transports.length > 1 ? (0, import_viem.fallback)(transports) : transports[0];
47
50
  }
48
- function getAlchemyUrl(domain, apiKey, protocol) {
49
- return protocol === "http" ? getAlchemyHttpUrl(domain, apiKey) : getAlchemyWsUrl(domain, apiKey);
51
+ function getProviderUrl(provider, network, apiKey, protocol) {
52
+ switch (provider) {
53
+ case "alchemy":
54
+ return getAlchemyUrl(network, apiKey, protocol);
55
+ case "drpc":
56
+ return getDrpcUrl(network, apiKey, protocol);
57
+ }
50
58
  }
51
- function getAlchemyHttpUrl(domain, apiKey) {
52
- return `https://${domain}.g.alchemy.com/v2/${apiKey}`;
59
+ function getAlchemyUrl(network, apiKey, protocol) {
60
+ const { alchemyDomain } = (0, import_sdk.getChain)(network);
61
+ if (!alchemyDomain) {
62
+ return void 0;
63
+ }
64
+ return `${protocol}s://${alchemyDomain}.g.alchemy.com/v2/${apiKey}`;
53
65
  }
54
- function getAlchemyWsUrl(domain, apiKey) {
55
- return `wss://${domain}.g.alchemy.com/v2/${apiKey}`;
66
+ const DRPC_NETS = {
67
+ Arbitrum: "arbitrum",
68
+ Base: "base",
69
+ BNB: "bsc",
70
+ Mainnet: "ethereum",
71
+ Optimism: "optimism",
72
+ Sonic: "sonic",
73
+ WorldChain: "worldchain",
74
+ Berachain: "berachain",
75
+ Avalanche: "avalanche",
76
+ Monad: "monad-testnet",
77
+ MegaETH: ""
78
+ };
79
+ function getDrpcUrl(network, apiKey, protocol) {
80
+ const net = DRPC_NETS[network];
81
+ return net ? `${protocol}s://lb.drpc.org/ogws?network=${net}&dkey=${apiKey}` : void 0;
56
82
  }
57
83
  // Annotate the CommonJS export names for ESM import in node:
58
84
  0 && (module.exports = {
59
85
  createTransport,
60
- getAlchemyHttpUrl,
61
86
  getAlchemyUrl,
62
- getAlchemyWsUrl
87
+ getDrpcUrl,
88
+ getProviderUrl
63
89
  });
@@ -16,6 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
16
16
  var dev_exports = {};
17
17
  module.exports = __toCommonJS(dev_exports);
18
18
  __reExport(dev_exports, require("./AccountOpener.js"), module.exports);
19
+ __reExport(dev_exports, require("./AccountsCounterPlugin.js"), module.exports);
19
20
  __reExport(dev_exports, require("./calcLiquidatableLTs.js"), module.exports);
20
21
  __reExport(dev_exports, require("./create2.js"), module.exports);
21
22
  __reExport(dev_exports, require("./createAnvilClient.js"), module.exports);
@@ -26,6 +27,7 @@ __reExport(dev_exports, require("./migrateFaucet.js"), module.exports);
26
27
  // Annotate the CommonJS export names for ESM import in node:
27
28
  0 && (module.exports = {
28
29
  ...require("./AccountOpener.js"),
30
+ ...require("./AccountsCounterPlugin.js"),
29
31
  ...require("./calcLiquidatableLTs.js"),
30
32
  ...require("./create2.js"),
31
33
  ...require("./createAnvilClient.js"),
@@ -0,0 +1,85 @@
1
+ import { iCreditAccountCompressorAbi } from "../abi/compressors.js";
2
+ import {
3
+ ADDRESS_0X0,
4
+ AddressMap,
5
+ AP_CREDIT_ACCOUNT_COMPRESSOR,
6
+ MAX_UINT256,
7
+ SDKConstruct,
8
+ TypedObjectUtils,
9
+ VERSION_RANGE_310
10
+ } from "../sdk/index.js";
11
+ class AccountsCounterPlugin extends SDKConstruct {
12
+ #accounts = new AddressMap();
13
+ version = 1;
14
+ async attach() {
15
+ await this.#load();
16
+ }
17
+ async syncState() {
18
+ await this.#load();
19
+ }
20
+ async #load() {
21
+ const [compressor] = this.sdk.addressProvider.mustGetLatest(
22
+ AP_CREDIT_ACCOUNT_COMPRESSOR,
23
+ VERSION_RANGE_310
24
+ );
25
+ const cms = this.sdk.marketRegister.creditManagers;
26
+ const count = await this.sdk.provider.publicClient.multicall({
27
+ contracts: cms.flatMap(
28
+ (cm) => [
29
+ {
30
+ abi: iCreditAccountCompressorAbi,
31
+ address: compressor,
32
+ functionName: "countCreditAccounts",
33
+ args: [
34
+ cm.creditManager.address,
35
+ {
36
+ owner: ADDRESS_0X0,
37
+ includeZeroDebt: false,
38
+ maxHealthFactor: MAX_UINT256,
39
+ minHealthFactor: 0n,
40
+ reverting: false
41
+ }
42
+ ]
43
+ },
44
+ {
45
+ abi: iCreditAccountCompressorAbi,
46
+ address: compressor,
47
+ functionName: "countCreditAccounts",
48
+ args: [
49
+ cm.creditManager.address,
50
+ {
51
+ owner: ADDRESS_0X0,
52
+ includeZeroDebt: false,
53
+ maxHealthFactor: MAX_UINT256,
54
+ minHealthFactor: 0n,
55
+ reverting: true
56
+ }
57
+ ]
58
+ }
59
+ ]
60
+ ),
61
+ allowFailure: false
62
+ });
63
+ this.#accounts.clear();
64
+ for (let i = 0; i < cms.length; i++) {
65
+ const cm = cms[i];
66
+ const [reverting, nonReverting] = [count[2 * i], count[2 * i + 1]];
67
+ this.#accounts.upsert(cm.creditManager.address, reverting + nonReverting);
68
+ }
69
+ }
70
+ get state() {
71
+ return {
72
+ version: this.version,
73
+ accounts: this.#accounts.asRecord()
74
+ };
75
+ }
76
+ hydrate(state) {
77
+ this.#accounts.clear();
78
+ for (const [addr, count] of TypedObjectUtils.entries(state.accounts)) {
79
+ this.#accounts.upsert(addr, count);
80
+ }
81
+ }
82
+ }
83
+ export {
84
+ AccountsCounterPlugin
85
+ };
@@ -1,15 +1,18 @@
1
1
  import { fallback, http, webSocket } from "viem";
2
2
  import { getChain } from "../sdk/index.js";
3
3
  function createTransport(config) {
4
- const { alchemyKeys, protocol, network, ...rest } = config;
4
+ const { rpcProviders = [], protocol, network, ...rest } = config;
5
+ const allKeys = rpcProviders.flatMap((provider) => provider.keys) ?? [];
5
6
  const rpcUrls = config.rpcUrls.filter((url) => {
6
- return url.startsWith(protocol) && !alchemyKeys.some((key) => url.includes(key));
7
+ return url.startsWith(protocol) && !allKeys.some((key) => url.includes(key));
7
8
  });
8
- const { alchemyDomain } = getChain(network);
9
- if (alchemyDomain) {
10
- rpcUrls.unshift(
11
- ...alchemyKeys.map((key) => getAlchemyUrl(alchemyDomain, key, protocol))
12
- );
9
+ for (const { provider, keys } of rpcProviders) {
10
+ for (const key of keys) {
11
+ const url = getProviderUrl(provider, network, key, protocol);
12
+ if (url) {
13
+ rpcUrls.push(url);
14
+ }
15
+ }
13
16
  }
14
17
  const transports = rpcUrls.map(
15
18
  (url) => protocol === "http" ? http(url, rest) : webSocket(url, rest)
@@ -19,18 +22,41 @@ function createTransport(config) {
19
22
  }
20
23
  return transports.length > 1 ? fallback(transports) : transports[0];
21
24
  }
22
- function getAlchemyUrl(domain, apiKey, protocol) {
23
- return protocol === "http" ? getAlchemyHttpUrl(domain, apiKey) : getAlchemyWsUrl(domain, apiKey);
25
+ function getProviderUrl(provider, network, apiKey, protocol) {
26
+ switch (provider) {
27
+ case "alchemy":
28
+ return getAlchemyUrl(network, apiKey, protocol);
29
+ case "drpc":
30
+ return getDrpcUrl(network, apiKey, protocol);
31
+ }
24
32
  }
25
- function getAlchemyHttpUrl(domain, apiKey) {
26
- return `https://${domain}.g.alchemy.com/v2/${apiKey}`;
33
+ function getAlchemyUrl(network, apiKey, protocol) {
34
+ const { alchemyDomain } = getChain(network);
35
+ if (!alchemyDomain) {
36
+ return void 0;
37
+ }
38
+ return `${protocol}s://${alchemyDomain}.g.alchemy.com/v2/${apiKey}`;
27
39
  }
28
- function getAlchemyWsUrl(domain, apiKey) {
29
- return `wss://${domain}.g.alchemy.com/v2/${apiKey}`;
40
+ const DRPC_NETS = {
41
+ Arbitrum: "arbitrum",
42
+ Base: "base",
43
+ BNB: "bsc",
44
+ Mainnet: "ethereum",
45
+ Optimism: "optimism",
46
+ Sonic: "sonic",
47
+ WorldChain: "worldchain",
48
+ Berachain: "berachain",
49
+ Avalanche: "avalanche",
50
+ Monad: "monad-testnet",
51
+ MegaETH: ""
52
+ };
53
+ function getDrpcUrl(network, apiKey, protocol) {
54
+ const net = DRPC_NETS[network];
55
+ return net ? `${protocol}s://lb.drpc.org/ogws?network=${net}&dkey=${apiKey}` : void 0;
30
56
  }
31
57
  export {
32
58
  createTransport,
33
- getAlchemyHttpUrl,
34
59
  getAlchemyUrl,
35
- getAlchemyWsUrl
60
+ getDrpcUrl,
61
+ getProviderUrl
36
62
  };
@@ -1,4 +1,5 @@
1
1
  export * from "./AccountOpener.js";
2
+ export * from "./AccountsCounterPlugin.js";
2
3
  export * from "./calcLiquidatableLTs.js";
3
4
  export * from "./create2.js";
4
5
  export * from "./createAnvilClient.js";
@@ -0,0 +1,17 @@
1
+ import type { Address } from "viem";
2
+ import type { IGearboxSDKPlugin, IPluginState } from "../sdk/index.js";
3
+ import { SDKConstruct } from "../sdk/index.js";
4
+ export interface AccountsCounterPluginState extends IPluginState {
5
+ /**
6
+ * Mapping of credit manager addresses to the number of accounts
7
+ */
8
+ accounts: Record<Address, bigint>;
9
+ }
10
+ export declare class AccountsCounterPlugin extends SDKConstruct implements IGearboxSDKPlugin<AccountsCounterPluginState> {
11
+ #private;
12
+ readonly version = 1;
13
+ attach(): Promise<void>;
14
+ syncState(): Promise<void>;
15
+ get state(): AccountsCounterPluginState;
16
+ hydrate(state: AccountsCounterPluginState): void;
17
+ }
@@ -1,8 +1,20 @@
1
1
  import type { HttpTransportConfig, Transport, WebSocketTransportConfig } from "viem";
2
2
  import type { NetworkType } from "../sdk/index.js";
3
+ export type RpcProvider = "alchemy" | "drpc";
4
+ export interface ProviderConfig {
5
+ provider: RpcProvider;
6
+ keys: string[];
7
+ }
3
8
  export interface CreateTransportURLOptions {
9
+ /**
10
+ * Explicitly provided RPC URLs, like anvil
11
+ * Have highest priority
12
+ */
4
13
  rpcUrls: string[];
5
- alchemyKeys: string[];
14
+ /**
15
+ * Known providers, first has highest priority
16
+ */
17
+ rpcProviders?: ProviderConfig[];
6
18
  network: NetworkType;
7
19
  }
8
20
  export type CreateHTTPTransportConfig = {
@@ -19,6 +31,6 @@ export type CreateTransportConfig = CreateHTTPTransportConfig | CreateWSTranspor
19
31
  * @returns
20
32
  */
21
33
  export declare function createTransport(config: CreateTransportConfig): Transport;
22
- export declare function getAlchemyUrl(domain: string, apiKey: string, protocol: "http" | "ws"): string;
23
- export declare function getAlchemyHttpUrl(domain: string, apiKey: string): string;
24
- export declare function getAlchemyWsUrl(domain: string, apiKey: string): string;
34
+ export declare function getProviderUrl(provider: RpcProvider, network: NetworkType, apiKey: string, protocol: "http" | "ws"): string | undefined;
35
+ export declare function getAlchemyUrl(network: NetworkType, apiKey: string, protocol: "http" | "ws"): string | undefined;
36
+ export declare function getDrpcUrl(network: NetworkType, apiKey: string, protocol: "http" | "ws"): string | undefined;
@@ -1,4 +1,5 @@
1
1
  export * from "./AccountOpener.js";
2
+ export * from "./AccountsCounterPlugin.js";
2
3
  export * from "./calcLiquidatableLTs.js";
3
4
  export * from "./create2.js";
4
5
  export * from "./createAnvilClient.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "7.0.0",
3
+ "version": "7.2.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",