@gearbox-protocol/sdk 13.3.3 → 13.4.0-beta.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.
- package/dist/cjs/permissionless/utils/price-update/get-price-update-tx.js +3 -15
- package/dist/cjs/sdk/GearboxSDK.js +135 -20
- package/dist/cjs/sdk/accounts/AbstractCreditAccountsService.js +196 -340
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +30 -35
- package/dist/cjs/sdk/accounts/multicall-utils.js +91 -0
- package/dist/cjs/sdk/base/BaseContract.js +26 -6
- package/dist/cjs/sdk/base/ChainContractsRegister.js +39 -2
- package/dist/cjs/sdk/base/Construct.js +15 -3
- package/dist/cjs/sdk/base/TokensMeta.js +23 -0
- package/dist/cjs/sdk/constants/address-provider.js +0 -22
- package/dist/cjs/sdk/core/AbstractAddressProviderContract.js +15 -0
- package/dist/cjs/sdk/market/MarketRegister.js +74 -3
- package/dist/cjs/sdk/market/credit/CreditFacadeV310Contract.js +6 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
- package/dist/cjs/sdk/options.js +30 -24
- package/dist/cjs/sdk/plugins/BasePlugin.js +24 -0
- package/dist/cjs/sdk/pools/AbstractPoolService.js +6 -0
- package/dist/cjs/sdk/router/AbstractRouterContract.js +4 -1
- package/dist/cjs/sdk/router/RouterV310Contract.js +20 -15
- package/dist/cjs/sdk/utils/AddressMap.js +53 -17
- package/dist/cjs/sdk/utils/AddressSet.js +9 -0
- package/dist/esm/permissionless/utils/price-update/get-price-update-tx.js +6 -20
- package/dist/esm/sdk/GearboxSDK.js +135 -20
- package/dist/esm/sdk/accounts/AbstractCreditAccountsService.js +204 -342
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +30 -35
- package/dist/esm/sdk/accounts/multicall-utils.js +69 -0
- package/dist/esm/sdk/base/BaseContract.js +26 -6
- package/dist/esm/sdk/base/ChainContractsRegister.js +39 -2
- package/dist/esm/sdk/base/Construct.js +15 -3
- package/dist/esm/sdk/base/TokensMeta.js +23 -0
- package/dist/esm/sdk/constants/address-provider.js +0 -21
- package/dist/esm/sdk/core/AbstractAddressProviderContract.js +15 -0
- package/dist/esm/sdk/market/MarketRegister.js +74 -3
- package/dist/esm/sdk/market/credit/CreditFacadeV310Contract.js +6 -0
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
- package/dist/esm/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
- package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
- package/dist/esm/sdk/options.js +30 -24
- package/dist/esm/sdk/plugins/BasePlugin.js +24 -0
- package/dist/esm/sdk/pools/AbstractPoolService.js +6 -0
- package/dist/esm/sdk/router/AbstractRouterContract.js +4 -1
- package/dist/esm/sdk/router/RouterV310Contract.js +20 -15
- package/dist/esm/sdk/utils/AddressMap.js +53 -17
- package/dist/esm/sdk/utils/AddressSet.js +9 -0
- package/dist/types/permissionless/bindings/price-feed-store.d.ts +1 -2
- package/dist/types/permissionless/bindings/types.d.ts +0 -4
- package/dist/types/permissionless/utils/price-update/get-price-update-tx.d.ts +1 -3
- package/dist/types/sdk/GearboxSDK.d.ts +236 -34
- package/dist/types/sdk/accounts/AbstractCreditAccountsService.d.ts +92 -147
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +14 -5
- package/dist/types/sdk/accounts/multicall-utils.d.ts +39 -0
- package/dist/types/sdk/accounts/types.d.ts +237 -40
- package/dist/types/sdk/base/BaseContract.d.ts +67 -6
- package/dist/types/sdk/base/ChainContractsRegister.d.ts +51 -2
- package/dist/types/sdk/base/Construct.d.ts +31 -0
- package/dist/types/sdk/base/PlaceholderContract.d.ts +3 -0
- package/dist/types/sdk/base/SDKConstruct.d.ts +10 -0
- package/dist/types/sdk/base/TokensMeta.d.ts +59 -2
- package/dist/types/sdk/base/types.d.ts +185 -25
- package/dist/types/sdk/chain/chains.d.ts +78 -18
- package/dist/types/sdk/chain/detectNetwork.d.ts +7 -0
- package/dist/types/sdk/constants/address-provider.d.ts +4 -3
- package/dist/types/sdk/core/AbstractAddressProviderContract.d.ts +23 -0
- package/dist/types/sdk/core/types.d.ts +46 -0
- package/dist/types/sdk/market/MarketRegister.d.ts +81 -0
- package/dist/types/sdk/market/adapters/PlaceholderAdapterContracts.d.ts +3 -0
- package/dist/types/sdk/market/credit/CreditFacadeV310Contract.d.ts +1 -0
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +40 -50
- package/dist/types/sdk/market/oracle/types.d.ts +78 -59
- package/dist/types/sdk/market/pricefeeds/AbstractLPPriceFeed.d.ts +3 -0
- package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +3 -0
- package/dist/types/sdk/market/pricefeeds/PriceFeedRef.d.ts +22 -2
- package/dist/types/sdk/market/pricefeeds/PriceFeedsRegister.d.ts +80 -16
- package/dist/types/sdk/market/pricefeeds/getRawPriceUpdates.d.ts +2 -2
- package/dist/types/sdk/market/pricefeeds/types.d.ts +75 -11
- package/dist/types/sdk/options.d.ts +13 -4
- package/dist/types/sdk/plugins/BasePlugin.d.ts +39 -0
- package/dist/types/sdk/plugins/types.d.ts +73 -43
- package/dist/types/sdk/pools/AbstractPoolService.d.ts +12 -0
- package/dist/types/sdk/pools/types.d.ts +75 -6
- package/dist/types/sdk/router/AbstractRouterContract.d.ts +21 -2
- package/dist/types/sdk/router/RouterV310Contract.d.ts +27 -15
- package/dist/types/sdk/router/types.d.ts +47 -70
- package/dist/types/sdk/types/state.d.ts +32 -3
- package/dist/types/sdk/utils/AddressMap.d.ts +61 -17
- package/dist/types/sdk/utils/AddressSet.d.ts +15 -0
- package/package.json +4 -2
|
@@ -26,6 +26,9 @@ var import_math = require("../constants/math.js");
|
|
|
26
26
|
var import_utils = require("../utils/index.js");
|
|
27
27
|
var import_helpers = require("./helpers.js");
|
|
28
28
|
class AbstractRouterContract extends import_base.BaseContract {
|
|
29
|
+
/**
|
|
30
|
+
* Reference to the parent SDK instance.
|
|
31
|
+
**/
|
|
29
32
|
sdk;
|
|
30
33
|
constructor(sdk, args) {
|
|
31
34
|
super(sdk, args);
|
|
@@ -88,7 +91,7 @@ class AbstractRouterContract extends import_base.BaseContract {
|
|
|
88
91
|
};
|
|
89
92
|
}
|
|
90
93
|
/**
|
|
91
|
-
* Tries to sell just
|
|
94
|
+
* Tries to sell just enough of the most valuable token to cover debt.
|
|
92
95
|
* @param ca
|
|
93
96
|
* @param keepAssets
|
|
94
97
|
* @returns
|
|
@@ -41,8 +41,8 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
|
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
45
|
-
|
|
44
|
+
* {@inheritDoc IRouterContract.findOneTokenPath}
|
|
45
|
+
**/
|
|
46
46
|
async findOneTokenPath(props) {
|
|
47
47
|
const {
|
|
48
48
|
creditAccount,
|
|
@@ -83,8 +83,8 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
87
|
-
|
|
86
|
+
* {@inheritDoc IRouterContract.findOpenStrategyPath}
|
|
87
|
+
**/
|
|
88
88
|
async findOpenStrategyPath(props) {
|
|
89
89
|
const {
|
|
90
90
|
creditManager: cm,
|
|
@@ -141,8 +141,8 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
|
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
143
|
/**
|
|
144
|
-
*
|
|
145
|
-
|
|
144
|
+
* {@inheritDoc IRouterContract.findClaimAllRewards}
|
|
145
|
+
**/
|
|
146
146
|
async findClaimAllRewards(props) {
|
|
147
147
|
const record = props.tokensToClaim.reduce(
|
|
148
148
|
(acc, a) => {
|
|
@@ -167,8 +167,8 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
|
|
|
167
167
|
};
|
|
168
168
|
}
|
|
169
169
|
/**
|
|
170
|
-
*
|
|
171
|
-
|
|
170
|
+
* {@inheritDoc IRouterContract.findBestClosePath}
|
|
171
|
+
**/
|
|
172
172
|
async findBestClosePath(props) {
|
|
173
173
|
const {
|
|
174
174
|
creditAccount: ca,
|
|
@@ -233,17 +233,22 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
|
|
|
233
233
|
};
|
|
234
234
|
}
|
|
235
235
|
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* @param
|
|
239
|
-
|
|
236
|
+
* Override the number of route splits used when swapping a specific token.
|
|
237
|
+
*
|
|
238
|
+
* @param token - Token address to configure.
|
|
239
|
+
* @param numSplits - Number of parallel route splits.
|
|
240
|
+
* @internal
|
|
241
|
+
**/
|
|
240
242
|
setNumSplits(token, numSplits) {
|
|
241
243
|
this.#numSplits.upsert(token, numSplits);
|
|
242
244
|
}
|
|
243
245
|
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
|
|
246
|
+
* Set the default number of route splits applied to the highest-value
|
|
247
|
+
* token in each swap.
|
|
248
|
+
*
|
|
249
|
+
* @param numSplits - Default number of parallel route splits.
|
|
250
|
+
* @internal
|
|
251
|
+
**/
|
|
247
252
|
setDefaultNumSplits(numSplits) {
|
|
248
253
|
this.#defaultNumSplits = numSplits;
|
|
249
254
|
}
|
|
@@ -26,6 +26,10 @@ class AddressMap {
|
|
|
26
26
|
#map;
|
|
27
27
|
#frozen = false;
|
|
28
28
|
#name;
|
|
29
|
+
/**
|
|
30
|
+
* @param entries - Optional initial key-value pairs. Address strings are checksummed automatically.
|
|
31
|
+
* @param name - Optional label used in error messages when a lookup fails.
|
|
32
|
+
*/
|
|
29
33
|
constructor(entries, name) {
|
|
30
34
|
this.#map = /* @__PURE__ */ new Map();
|
|
31
35
|
if (entries) {
|
|
@@ -37,9 +41,10 @@ class AddressMap {
|
|
|
37
41
|
this.#name = name;
|
|
38
42
|
}
|
|
39
43
|
/**
|
|
40
|
-
* Adds or updates value
|
|
41
|
-
* @param address
|
|
42
|
-
* @param value
|
|
44
|
+
* Adds or updates a value. Passing `undefined` removes the entry.
|
|
45
|
+
* @param address - EVM address (checksummed automatically).
|
|
46
|
+
* @param value - Value to store, or `undefined` to delete the entry.
|
|
47
|
+
* @throws If the map has been {@link freeze | frozen}.
|
|
43
48
|
*/
|
|
44
49
|
upsert(address, value) {
|
|
45
50
|
if (this.#frozen) {
|
|
@@ -53,9 +58,10 @@ class AddressMap {
|
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @param address
|
|
58
|
-
* @param value
|
|
61
|
+
* Inserts a value, throwing if the address is already present.
|
|
62
|
+
* @param address - EVM address (checksummed automatically).
|
|
63
|
+
* @param value - Value to store.
|
|
64
|
+
* @throws If the map has been {@link freeze | frozen} or if `address` already exists.
|
|
59
65
|
*/
|
|
60
66
|
insert(address, value) {
|
|
61
67
|
if (this.#frozen) {
|
|
@@ -70,27 +76,26 @@ class AddressMap {
|
|
|
70
76
|
this.#map.set(key, value);
|
|
71
77
|
}
|
|
72
78
|
/**
|
|
73
|
-
* Checks
|
|
74
|
-
* @param address
|
|
75
|
-
* @returns
|
|
79
|
+
* Checks whether an address is present in the map.
|
|
80
|
+
* @param address - EVM address (case-insensitive).
|
|
76
81
|
*/
|
|
77
82
|
has(address) {
|
|
78
83
|
const key = (0, import_viem.getAddress)(address);
|
|
79
84
|
return this.#map.has(key);
|
|
80
85
|
}
|
|
81
86
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @param address
|
|
84
|
-
* @returns
|
|
87
|
+
* Looks up a value by EVM address (case-insensitive).
|
|
88
|
+
* @param address - EVM address to look up.
|
|
89
|
+
* @returns The stored value, or `undefined` if not present.
|
|
85
90
|
*/
|
|
86
91
|
get(address) {
|
|
87
92
|
const key = (0, import_viem.getAddress)(address);
|
|
88
93
|
return this.#map.get(key);
|
|
89
94
|
}
|
|
90
95
|
/**
|
|
91
|
-
*
|
|
92
|
-
* @param address
|
|
93
|
-
* @
|
|
96
|
+
* Looks up a value by EVM address, throwing if the address is absent.
|
|
97
|
+
* @param address - EVM address to look up.
|
|
98
|
+
* @throws If `address` is not in the map.
|
|
94
99
|
*/
|
|
95
100
|
mustGet(address) {
|
|
96
101
|
const key = (0, import_viem.getAddress)(address);
|
|
@@ -100,40 +105,71 @@ class AddressMap {
|
|
|
100
105
|
return this.#map.get(key);
|
|
101
106
|
}
|
|
102
107
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @param address
|
|
108
|
+
* Removes an entry by address. No-op if the address is absent.
|
|
109
|
+
* @param address - EVM address to remove.
|
|
105
110
|
*/
|
|
106
111
|
delete(address) {
|
|
107
112
|
const key = (0, import_viem.getAddress)(address);
|
|
108
113
|
this.#map.delete(key);
|
|
109
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Removes all entries from the map.
|
|
117
|
+
**/
|
|
110
118
|
clear() {
|
|
111
119
|
this.#map.clear();
|
|
112
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Returns all entries as an array of `[checksummedAddress, value]` tuples.
|
|
123
|
+
**/
|
|
113
124
|
entries() {
|
|
114
125
|
return Array.from(this.#map.entries());
|
|
115
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns all values in insertion order.
|
|
129
|
+
**/
|
|
116
130
|
values() {
|
|
117
131
|
return Array.from(this.#map.values());
|
|
118
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns all checksummed addresses in insertion order.
|
|
135
|
+
**/
|
|
119
136
|
keys() {
|
|
120
137
|
return Array.from(this.#map.keys());
|
|
121
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Converts the map to a plain `Record<Address, T>` object.
|
|
141
|
+
**/
|
|
122
142
|
asRecord() {
|
|
123
143
|
return Object.fromEntries(this.#map.entries());
|
|
124
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Number of entries in the map.
|
|
147
|
+
**/
|
|
125
148
|
get size() {
|
|
126
149
|
return this.#map.size;
|
|
127
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Prevents further mutations. Any subsequent call to {@link upsert},
|
|
153
|
+
* {@link insert}, or {@link delete} will throw.
|
|
154
|
+
*/
|
|
128
155
|
freeze() {
|
|
129
156
|
this.#frozen = true;
|
|
130
157
|
}
|
|
131
158
|
get name() {
|
|
132
159
|
return this.#name;
|
|
133
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Creates an `AddressMap` from a plain record object.
|
|
163
|
+
* @param record - Object whose keys are EVM addresses.
|
|
164
|
+
*/
|
|
134
165
|
static fromRecord(record) {
|
|
135
166
|
return new AddressMap(Object.entries(record));
|
|
136
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Creates an `AddressMap` by extracting an address from each array element.
|
|
170
|
+
* @param array - Source items.
|
|
171
|
+
* @param mapFn - Function that returns the address key for a given item.
|
|
172
|
+
*/
|
|
137
173
|
static fromMappedArray(array, mapFn) {
|
|
138
174
|
return new AddressMap(array.map((item) => [mapFn(item), item]));
|
|
139
175
|
}
|
|
@@ -23,6 +23,9 @@ __export(AddressSet_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(AddressSet_exports);
|
|
24
24
|
var import_viem = require("viem");
|
|
25
25
|
class AddressSet extends Set {
|
|
26
|
+
/**
|
|
27
|
+
* @param entries - Optional initial addresses. Each is checksummed automatically.
|
|
28
|
+
*/
|
|
26
29
|
constructor(entries) {
|
|
27
30
|
super(Array.from(entries ?? []).map((a) => (0, import_viem.getAddress)(a)));
|
|
28
31
|
}
|
|
@@ -35,9 +38,15 @@ class AddressSet extends Set {
|
|
|
35
38
|
has(value) {
|
|
36
39
|
return super.has((0, import_viem.getAddress)(value));
|
|
37
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns all addresses as an array.
|
|
43
|
+
**/
|
|
38
44
|
asArray() {
|
|
39
45
|
return Array.from(this);
|
|
40
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Maps each address through `fn` and returns the resulting array.
|
|
49
|
+
**/
|
|
41
50
|
map(fn) {
|
|
42
51
|
return this.asArray().map(fn);
|
|
43
52
|
}
|
|
@@ -1,26 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
multicall3Abi,
|
|
4
|
-
parseAbi
|
|
2
|
+
multicall3Abi
|
|
5
3
|
} from "viem";
|
|
6
4
|
import {
|
|
7
5
|
createRawTx,
|
|
8
|
-
GearboxSDK
|
|
6
|
+
GearboxSDK,
|
|
7
|
+
getRawPriceUpdates
|
|
9
8
|
} from "../../../sdk/index.js";
|
|
10
|
-
import {
|
|
11
|
-
PriceFeedStoreContract
|
|
12
|
-
} from "../../bindings/index.js";
|
|
9
|
+
import { PriceFeedStoreContract } from "../../bindings/index.js";
|
|
13
10
|
import { Addresses } from "../../deployment/addresses.js";
|
|
14
|
-
function getUpdateCalldata(tx) {
|
|
15
|
-
const data = decodeFunctionData({
|
|
16
|
-
abi: parseAbi(["function updatePrice(bytes calldata data) external"]),
|
|
17
|
-
data: tx.raw.callData
|
|
18
|
-
});
|
|
19
|
-
return {
|
|
20
|
-
priceFeed: tx.raw.to,
|
|
21
|
-
data: data.args[0]
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
11
|
async function getPriceUpdateTx({
|
|
25
12
|
client,
|
|
26
13
|
priceFeeds,
|
|
@@ -57,13 +44,12 @@ async function getPriceUpdateTx({
|
|
|
57
44
|
});
|
|
58
45
|
return multicallTx;
|
|
59
46
|
}
|
|
60
|
-
const priceUpdates = updateTxs
|
|
47
|
+
const priceUpdates = getRawPriceUpdates(updateTxs);
|
|
61
48
|
if (priceUpdates.length === 0) {
|
|
62
49
|
return void 0;
|
|
63
50
|
}
|
|
64
51
|
return pfStore.updatePrices(priceUpdates);
|
|
65
52
|
}
|
|
66
53
|
export {
|
|
67
|
-
getPriceUpdateTx
|
|
68
|
-
getUpdateCalldata
|
|
54
|
+
getPriceUpdateTx
|
|
69
55
|
};
|
|
@@ -67,24 +67,50 @@ async function attachClient(options, network) {
|
|
|
67
67
|
}
|
|
68
68
|
class GearboxSDK extends ChainContractsRegister {
|
|
69
69
|
#hooks = new Hooks();
|
|
70
|
+
/**
|
|
71
|
+
* Registered plugin instances, keyed by plugin name.
|
|
72
|
+
**/
|
|
70
73
|
plugins;
|
|
71
|
-
// Block which was use for data query
|
|
72
74
|
#currentBlock;
|
|
73
75
|
#timestamp;
|
|
74
76
|
#syncing = false;
|
|
75
|
-
// Collection of core singleton contracts
|
|
76
77
|
#addressProvider;
|
|
77
78
|
#attachConfig;
|
|
78
|
-
// Collection of markets
|
|
79
79
|
#marketRegister;
|
|
80
80
|
#priceFeeds;
|
|
81
|
+
/**
|
|
82
|
+
* Gas limit applied to read-only `eth_call` requests.
|
|
83
|
+
* `undefined` means that gas limit will not be set on read-only calls,
|
|
84
|
+
* leaving it to rpc provider to decide.
|
|
85
|
+
**/
|
|
81
86
|
gasLimit;
|
|
82
87
|
/**
|
|
83
|
-
*
|
|
84
|
-
|
|
88
|
+
* When `true`, the SDK throws on unrecognised contract types instead of
|
|
89
|
+
* falling back to a generic contract wrapper.
|
|
90
|
+
**/
|
|
85
91
|
strictContractTypes;
|
|
92
|
+
/**
|
|
93
|
+
* Registers a callback for an SDK lifecycle event.
|
|
94
|
+
*
|
|
95
|
+
* @see {@link SDKHooks} for available event names.
|
|
96
|
+
**/
|
|
86
97
|
addHook = this.#hooks.addHook.bind(this.#hooks);
|
|
98
|
+
/**
|
|
99
|
+
* Removes a previously registered lifecycle callback.
|
|
100
|
+
*
|
|
101
|
+
* @see {@link SDKHooks} for available event names.
|
|
102
|
+
**/
|
|
87
103
|
removeHook = this.#hooks.removeHook.bind(this.#hooks);
|
|
104
|
+
/**
|
|
105
|
+
* Creates and initialises a new SDK instance by reading live on-chain state.
|
|
106
|
+
*
|
|
107
|
+
* This is the primary way to bootstrap the SDK. The method connects to the
|
|
108
|
+
* chain, discovers the address provider and all configured markets, and
|
|
109
|
+
* attaches any supplied plugins.
|
|
110
|
+
*
|
|
111
|
+
* @param options - Combined SDK, client, and (optional) network options.
|
|
112
|
+
* @returns A fully initialised SDK instance.
|
|
113
|
+
**/
|
|
88
114
|
static async attach(options) {
|
|
89
115
|
const {
|
|
90
116
|
logger,
|
|
@@ -120,6 +146,19 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
120
146
|
pyth
|
|
121
147
|
});
|
|
122
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a new SDK instance from a previously serialised {@link GearboxState}
|
|
151
|
+
* snapshot, without making any on-chain calls.
|
|
152
|
+
*
|
|
153
|
+
* Use this for fast startup when a recent state snapshot is available
|
|
154
|
+
* (e.g. loaded from a file or received from a backend service).
|
|
155
|
+
*
|
|
156
|
+
* @param options - SDK and client options (block number and address provider
|
|
157
|
+
* are taken from the snapshot).
|
|
158
|
+
* @param state - Serialised state obtained from {@link GearboxSDK.state}.
|
|
159
|
+
* @returns A fully initialised SDK instance.
|
|
160
|
+
* @throws If the snapshot's {@link STATE_VERSION} does not match.
|
|
161
|
+
**/
|
|
123
162
|
static hydrate(options, state) {
|
|
124
163
|
const { logger, plugins, strictContractTypes, gasLimit, ...rest } = options;
|
|
125
164
|
const client = createClient(options, {
|
|
@@ -272,9 +311,15 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
272
311
|
return this;
|
|
273
312
|
}
|
|
274
313
|
/**
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
|
|
314
|
+
* Re-attaches the SDK using the same configuration, discarding all cached
|
|
315
|
+
* state and re-reading everything from the chain.
|
|
316
|
+
*
|
|
317
|
+
* Useful when the SDK needs a full refresh (e.g. after a protocol upgrade).
|
|
318
|
+
* Note that if the original `blockNumber` was pinned, the same block is
|
|
319
|
+
* re-used — call {@link syncState} instead if you want to advance.
|
|
320
|
+
*
|
|
321
|
+
* @throws If the SDK has not been attached yet.
|
|
322
|
+
**/
|
|
278
323
|
async reattach() {
|
|
279
324
|
if (!this.#attachConfig) {
|
|
280
325
|
throw new Error("cannot reattach, attach config is not set");
|
|
@@ -282,8 +327,15 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
282
327
|
await this.#attach(this.#attachConfig);
|
|
283
328
|
}
|
|
284
329
|
/**
|
|
285
|
-
*
|
|
286
|
-
|
|
330
|
+
* Replaces the SDK's in-memory state with a new serialised snapshot
|
|
331
|
+
* without re-creating the instance.
|
|
332
|
+
*
|
|
333
|
+
* After hydration the `rehydrate` hook is triggered so that listeners
|
|
334
|
+
* can react to the state change.
|
|
335
|
+
*
|
|
336
|
+
* @param state - Serialised state obtained from {@link GearboxSDK.state}.
|
|
337
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
338
|
+
**/
|
|
287
339
|
async rehydrate(state) {
|
|
288
340
|
if (!this.#attachConfig) {
|
|
289
341
|
throw new Error("cannot rehydrate, attach config is not set");
|
|
@@ -299,9 +351,20 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
299
351
|
timestamp: state.timestamp
|
|
300
352
|
});
|
|
301
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* Gearbox network type the SDK is connected to (e.g. `"Mainnet"`, `"Arbitrum"`).
|
|
356
|
+
**/
|
|
302
357
|
get networkType() {
|
|
303
358
|
return this.client.chain.network;
|
|
304
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Returns a human-readable snapshot of the entire SDK state, suitable
|
|
362
|
+
* for logging or diagnostic inspection.
|
|
363
|
+
*
|
|
364
|
+
* @param raw - When `true`, include raw numeric values alongside
|
|
365
|
+
* formatted ones.
|
|
366
|
+
* @default true
|
|
367
|
+
**/
|
|
305
368
|
stateHuman(raw = true) {
|
|
306
369
|
return {
|
|
307
370
|
block: Number(this.currentBlock),
|
|
@@ -319,6 +382,13 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
319
382
|
...this.marketRegister.stateHuman(raw)
|
|
320
383
|
};
|
|
321
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* Serialisable snapshot of the current SDK state.
|
|
387
|
+
*
|
|
388
|
+
* The returned object can be persisted (e.g. written to a file) and later
|
|
389
|
+
* passed to {@link GearboxSDK.hydrate} for instant restoration without
|
|
390
|
+
* on-chain reads.
|
|
391
|
+
**/
|
|
322
392
|
get state() {
|
|
323
393
|
return {
|
|
324
394
|
version: STATE_VERSION,
|
|
@@ -337,10 +407,20 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
337
407
|
};
|
|
338
408
|
}
|
|
339
409
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
|
|
410
|
+
* Incrementally updates the SDK state by replaying on-chain events from the
|
|
411
|
+
* last processed block up to the target block (defaults to `latest`).
|
|
412
|
+
*
|
|
413
|
+
* Use the to periodically update sdk state on cron, or subscribe to new blocks
|
|
414
|
+
* using viem's `watchBlocks`
|
|
415
|
+
*
|
|
416
|
+
* On failure the SDK reverts to the previous block/timestamp so that
|
|
417
|
+
* subsequent calls can retry.
|
|
418
|
+
*
|
|
419
|
+
* @param opts - Target block and sync behaviour. When omitted the latest
|
|
420
|
+
* block is fetched automatically.
|
|
421
|
+
* @returns `true` if the sync completed successfully, `false` if it was
|
|
422
|
+
* skipped or failed.
|
|
423
|
+
**/
|
|
344
424
|
async syncState(opts) {
|
|
345
425
|
let {
|
|
346
426
|
blockNumber,
|
|
@@ -442,12 +522,22 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
442
522
|
}
|
|
443
523
|
return success;
|
|
444
524
|
}
|
|
525
|
+
/**
|
|
526
|
+
* Block number that the SDK state corresponds to.
|
|
527
|
+
*
|
|
528
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
529
|
+
**/
|
|
445
530
|
get currentBlock() {
|
|
446
531
|
if (this.#currentBlock === void 0) {
|
|
447
532
|
throw ERR_NOT_ATTACHED;
|
|
448
533
|
}
|
|
449
534
|
return this.#currentBlock;
|
|
450
535
|
}
|
|
536
|
+
/**
|
|
537
|
+
* Block timestamp (Unix epoch seconds) corresponding to {@link currentBlock}.
|
|
538
|
+
*
|
|
539
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
540
|
+
**/
|
|
451
541
|
get timestamp() {
|
|
452
542
|
if (this.#timestamp === void 0) {
|
|
453
543
|
throw ERR_NOT_ATTACHED;
|
|
@@ -455,14 +545,23 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
455
545
|
return this.#timestamp;
|
|
456
546
|
}
|
|
457
547
|
/**
|
|
458
|
-
*
|
|
459
|
-
|
|
548
|
+
* Global registry of all price feeds known to the SDK (on all markets).
|
|
549
|
+
*
|
|
550
|
+
* Unlike per-oracle price feed references, this register does not carry
|
|
551
|
+
* oracle-specific metadata (staleness period, main/reserve designation, etc.).
|
|
552
|
+
*
|
|
553
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
554
|
+
**/
|
|
460
555
|
get priceFeeds() {
|
|
461
556
|
if (this.#priceFeeds === void 0) {
|
|
462
557
|
throw ERR_NOT_ATTACHED;
|
|
463
558
|
}
|
|
464
559
|
return this.#priceFeeds;
|
|
465
560
|
}
|
|
561
|
+
/**
|
|
562
|
+
* Address of the GEAR governance token on this chain, or `undefined`
|
|
563
|
+
* if the address provider does not list it.
|
|
564
|
+
**/
|
|
466
565
|
get gear() {
|
|
467
566
|
try {
|
|
468
567
|
const g = this.addressProvider.getAddress(AP_GEAR_TOKEN, NO_VERSION);
|
|
@@ -472,12 +571,23 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
472
571
|
return void 0;
|
|
473
572
|
}
|
|
474
573
|
}
|
|
574
|
+
/**
|
|
575
|
+
* The chain's address provider contract, the central directory for all
|
|
576
|
+
* protocol-wide Gearbox protocol addresses.
|
|
577
|
+
*
|
|
578
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
579
|
+
**/
|
|
475
580
|
get addressProvider() {
|
|
476
581
|
if (this.#addressProvider === void 0) {
|
|
477
582
|
throw ERR_NOT_ATTACHED;
|
|
478
583
|
}
|
|
479
584
|
return this.#addressProvider;
|
|
480
585
|
}
|
|
586
|
+
/**
|
|
587
|
+
* Registry of all loaded markets (pools, credit managers, oracles, etc.).
|
|
588
|
+
*
|
|
589
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
590
|
+
**/
|
|
481
591
|
get marketRegister() {
|
|
482
592
|
if (this.#marketRegister === void 0) {
|
|
483
593
|
throw ERR_NOT_ATTACHED;
|
|
@@ -485,10 +595,15 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
485
595
|
return this.#marketRegister;
|
|
486
596
|
}
|
|
487
597
|
/**
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
|
|
598
|
+
* Resolves the appropriate router contract for a given credit manager,
|
|
599
|
+
* credit facade, or explicit version range.
|
|
600
|
+
*
|
|
601
|
+
* @param params - Identifies the context: a credit manager address/state,
|
|
602
|
+
* a credit facade address/state, or a {@link VersionRange}.
|
|
603
|
+
* @returns The matching router contract instance.
|
|
604
|
+
* @throws If the credit facade version is unsupported or no router is
|
|
605
|
+
* registered for the resolved version range.
|
|
606
|
+
**/
|
|
492
607
|
routerFor(params) {
|
|
493
608
|
let routerRange;
|
|
494
609
|
if (Array.isArray(params)) {
|