@gearbox-protocol/sdk 5.0.1 → 6.0.0-next.1

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.
Files changed (34) hide show
  1. package/dist/cjs/dev/calcLiquidatableLTs.js +5 -1
  2. package/dist/cjs/sdk/base/BaseContract.js +0 -1
  3. package/dist/cjs/sdk/chain/chains.js +0 -3
  4. package/dist/cjs/sdk/constants/networks.js +15 -34
  5. package/dist/cjs/sdk/market/MarketSuite.js +1 -5
  6. package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +52 -47
  7. package/dist/cjs/sdk/market/oracle/PriceOracleV300Contract.js +2 -3
  8. package/dist/cjs/sdk/market/oracle/PriceOracleV310Contract.js +2 -3
  9. package/dist/cjs/sdk/market/oracle/createPriceOracle.js +28 -9
  10. package/dist/cjs/sdk/market/pricefeeds/AbstractLPPriceFeed.js +2 -1
  11. package/dist/cjs/sdk/market/pricefeeds/RedstonePriceFeed.js +1 -1
  12. package/dist/cjs/sdk/plugins/V300StalenessPeriodPlugin.js +5 -5
  13. package/dist/cjs/sdk/sdk-legacy/core/endpoint.js +4 -4
  14. package/dist/esm/dev/calcLiquidatableLTs.js +5 -1
  15. package/dist/esm/sdk/base/BaseContract.js +0 -1
  16. package/dist/esm/sdk/chain/chains.js +0 -3
  17. package/dist/esm/sdk/constants/networks.js +15 -34
  18. package/dist/esm/sdk/market/MarketSuite.js +2 -6
  19. package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +53 -47
  20. package/dist/esm/sdk/market/oracle/PriceOracleV300Contract.js +2 -3
  21. package/dist/esm/sdk/market/oracle/PriceOracleV310Contract.js +2 -3
  22. package/dist/esm/sdk/market/oracle/createPriceOracle.js +27 -8
  23. package/dist/esm/sdk/market/pricefeeds/AbstractLPPriceFeed.js +2 -1
  24. package/dist/esm/sdk/market/pricefeeds/RedstonePriceFeed.js +2 -2
  25. package/dist/esm/sdk/plugins/V300StalenessPeriodPlugin.js +6 -6
  26. package/dist/esm/sdk/sdk-legacy/core/endpoint.js +4 -4
  27. package/dist/types/sdk/market/MarketSuite.d.ts +2 -2
  28. package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +28 -684
  29. package/dist/types/sdk/market/oracle/PriceOracleV300Contract.d.ts +1 -1
  30. package/dist/types/sdk/market/oracle/PriceOracleV310Contract.d.ts +2 -2
  31. package/dist/types/sdk/market/oracle/createPriceOracle.d.ts +14 -3
  32. package/dist/types/sdk/market/oracle/types.d.ts +97 -6
  33. package/dist/types/sdk/plugins/V300StalenessPeriodPlugin.d.ts +3 -2
  34. package/package.json +1 -1
@@ -5,6 +5,7 @@ import { iUpdatablePriceFeedAbi } from "../../../abi/iUpdatablePriceFeed.js";
5
5
  import { BaseContract } from "../../base/index.js";
6
6
  import {
7
7
  AP_PRICE_FEED_COMPRESSOR,
8
+ isV300,
8
9
  VERSION_RANGE_310
9
10
  } from "../../constants/index.js";
10
11
  import { AddressMap, formatBN } from "../../utils/index.js";
@@ -12,10 +13,6 @@ import { PriceFeedRef } from "../pricefeeds/index.js";
12
13
  import PriceFeedAnswerMap from "./PriceFeedAnswerMap.js";
13
14
  const ZERO_PRICE_FEED = stringToHex("PRICE_FEED::ZERO", { size: 32 });
14
15
  class PriceOracleBaseContract extends BaseContract {
15
- /**
16
- * Underlying token of market to which this price oracle belongs
17
- */
18
- underlying;
19
16
  /**
20
17
  * Mapping Token => [PriceFeed Address, stalenessPeriod]
21
18
  */
@@ -41,12 +38,14 @@ class PriceOracleBaseContract extends BaseContract {
41
38
  void 0,
42
39
  "reservePrices"
43
40
  );
44
- #priceFeedTree = [];
45
- constructor(sdk, args, data, underlying) {
41
+ #priceFeedTree = new AddressMap(
42
+ void 0,
43
+ "priceFeedTree"
44
+ );
45
+ constructor(sdk, args, data) {
46
46
  super(sdk, args);
47
- this.underlying = underlying;
48
47
  const { priceFeedMap, priceFeedTree } = data;
49
- this.#loadState(priceFeedMap, priceFeedTree);
48
+ this.#loadState(priceFeedMap, priceFeedTree, true);
50
49
  }
51
50
  /**
52
51
  * Returns main and reserve price feeds for given tokens
@@ -68,7 +67,7 @@ class PriceOracleBaseContract extends BaseContract {
68
67
  */
69
68
  async updatePriceFeeds() {
70
69
  const updatables = [];
71
- for (const node of this.#priceFeedTree) {
70
+ for (const node of this.#priceFeedTree.values()) {
72
71
  if (node.updatable) {
73
72
  updatables.push(this.sdk.priceFeeds.mustGet(node.baseParams.addr));
74
73
  }
@@ -134,30 +133,19 @@ class PriceOracleBaseContract extends BaseContract {
134
133
  }
135
134
  /**
136
135
  * Returns true if oracle's price feed tree contains given price feed
136
+ * This feed is not necessary connected to token, but can be a component of composite feed for some token
137
137
  * @param priceFeed
138
138
  * @returns
139
139
  */
140
140
  usesPriceFeed(priceFeed) {
141
- return this.#priceFeedTree.some(
142
- (node) => node.baseParams.addr.toLowerCase() === priceFeed.toLowerCase()
143
- );
144
- }
145
- /**
146
- * Tries to convert amount of token into underlying of current market
147
- * @param token
148
- * @param amount
149
- * @param reserve
150
- * @returns
151
- */
152
- convertToUnderlying(token, amount, reserve = false) {
153
- return this.convert(token, this.underlying, amount, reserve);
141
+ return this.#priceFeedTree.has(priceFeed);
154
142
  }
155
143
  /**
156
144
  * Tries to convert amount of from one token to another, using latest known prices
157
145
  * @param from
158
146
  * @param to
159
147
  * @param amount
160
- * @param reserve
148
+ * @param reserve use reserve price feed instead of main
161
149
  */
162
150
  convert(from, to, amount, reserve = false) {
163
151
  if (from === to) {
@@ -172,9 +160,8 @@ class PriceOracleBaseContract extends BaseContract {
172
160
  /**
173
161
  * Tries to convert amount of token to USD, using latest known prices
174
162
  * @param from
175
- * @param to
176
163
  * @param amount
177
- * @param reserve
164
+ * @param reserve use reserve price feed instead of main
178
165
  */
179
166
  convertToUSD(from, amount, reserve = false) {
180
167
  const price = reserve ? this.reservePrice(from) : this.mainPrice(from);
@@ -185,7 +172,7 @@ class PriceOracleBaseContract extends BaseContract {
185
172
  * Tries to convert amount of USD to token, using latest known prices
186
173
  * @param to
187
174
  * @param amount
188
- * @param reserve
175
+ * @param reserve use reserve price feed instead of main
189
176
  */
190
177
  convertFromUSD(to, amount, reserve = false) {
191
178
  const price = reserve ? this.reservePrice(to) : this.mainPrice(to);
@@ -194,23 +181,26 @@ class PriceOracleBaseContract extends BaseContract {
194
181
  }
195
182
  /**
196
183
  * Loads new prices for this oracle from PriceFeedCompressor
197
- * Does not update price feeds, only updates prices
184
+ * Will (re)create price feeds if needed
198
185
  */
199
186
  async updatePrices() {
200
187
  await this.sdk.marketRegister.updatePrices([this.address]);
201
188
  }
189
+ /**
190
+ * Paired method to updatePrices, helps to update prices on all oracles in one multicall
191
+ */
202
192
  syncStateMulticall() {
203
- const args = [this.address];
204
- if (this.version === 300) {
205
- args.push(
193
+ let args = [this.address];
194
+ if (isV300(this.version)) {
195
+ args = [
196
+ args[0],
206
197
  Array.from(
207
198
  /* @__PURE__ */ new Set([
208
- this.underlying,
209
199
  ...this.mainPriceFeeds.keys(),
210
200
  ...this.reservePriceFeeds.keys()
211
201
  ])
212
202
  )
213
- );
203
+ ];
214
204
  }
215
205
  const [address] = this.sdk.addressProvider.mustGetLatest(
216
206
  AP_PRICE_FEED_COMPRESSOR,
@@ -225,25 +215,39 @@ class PriceOracleBaseContract extends BaseContract {
225
215
  },
226
216
  onResult: (resp) => {
227
217
  const { priceFeedMap, priceFeedTree } = resp;
228
- this.#loadState(priceFeedMap, priceFeedTree);
218
+ this.#loadState(priceFeedMap, priceFeedTree, true);
229
219
  }
230
220
  };
231
221
  }
232
- #loadState(entries, tree) {
233
- this.#priceFeedTree = tree;
234
- this.mainPriceFeeds.clear();
235
- this.reservePriceFeeds.clear();
236
- this.mainPrices.clear();
237
- this.reservePrices.clear();
222
+ /**
223
+ * Helper function to handle situation when we have multiple different compressor data entries for same oracle
224
+ * This happens in v300
225
+ *
226
+ * @deprecated should be unnecessary after full v310 migration (oracles will be unique)
227
+ * @param data
228
+ * @returns
229
+ */
230
+ merge(data) {
231
+ const { priceFeedMap, priceFeedTree } = data;
232
+ this.#loadState(priceFeedMap, priceFeedTree, false);
233
+ return this;
234
+ }
235
+ #loadState(entries, tree, reset) {
236
+ if (reset) {
237
+ this.#priceFeedTree.clear();
238
+ this.mainPriceFeeds.clear();
239
+ this.reservePriceFeeds.clear();
240
+ this.mainPrices.clear();
241
+ this.reservePrices.clear();
242
+ }
238
243
  for (const node of tree) {
244
+ this.#priceFeedTree.upsert(node.baseParams.addr, node);
239
245
  this.sdk.priceFeeds.getOrCreate(node);
240
246
  }
241
- entries.forEach((entry) => {
247
+ for (const entry of entries) {
242
248
  const { token, priceFeed, reserve, stalenessPeriod } = entry;
243
249
  const ref = new PriceFeedRef(this.sdk, priceFeed, stalenessPeriod);
244
- const node = this.#priceFeedTree.find(
245
- (n) => n.baseParams.addr === priceFeed
246
- );
250
+ const node = this.#priceFeedTree.get(priceFeed);
247
251
  const price = node?.answer?.price;
248
252
  const priceFeedType = node?.baseParams.contractType;
249
253
  if (reserve) {
@@ -260,7 +264,7 @@ class PriceOracleBaseContract extends BaseContract {
260
264
  }
261
265
  }
262
266
  this.#labelPriceFeed(priceFeed, reserve ? "Reserve" : "Main", token);
263
- });
267
+ }
264
268
  this.logger?.debug(
265
269
  `Got ${this.mainPriceFeeds.size} main and ${this.reservePriceFeeds.size} reserve price feeds`
266
270
  );
@@ -281,6 +285,8 @@ class PriceOracleBaseContract extends BaseContract {
281
285
  * Helper method to find "attachment point" of price feed (makes sense for updatable price feeds only) -
282
286
  * returns token (in v3.0 can be ticker) and main/reserve flag
283
287
  *
288
+ * @deprecated Should be gone after v310 migration
289
+ *
284
290
  * @param priceFeed
285
291
  * @returns
286
292
  */
@@ -297,6 +303,9 @@ class PriceOracleBaseContract extends BaseContract {
297
303
  }
298
304
  return [void 0, false];
299
305
  }
306
+ /**
307
+ * Returns list of addresses that should be watched for events to sync state
308
+ */
300
309
  get watchAddresses() {
301
310
  return /* @__PURE__ */ new Set([this.address]);
302
311
  }
@@ -323,9 +332,6 @@ class PriceOracleBaseContract extends BaseContract {
323
332
  )
324
333
  };
325
334
  }
326
- get priceFeedTree() {
327
- return this.#priceFeedTree;
328
- }
329
335
  #noAnswerWarn(priceFeed, node) {
330
336
  let label = this.labelAddress(priceFeed);
331
337
  if (!node) {
@@ -4,7 +4,7 @@ import { tickerInfoTokensByNetwork } from "../../sdk-gov-legacy/index.js";
4
4
  import { PriceOracleBaseContract } from "./PriceOracleBaseContract.js";
5
5
  const abi = [...iPriceOracleV300Abi, ...iPausableAbi];
6
6
  class PriceOracleV300Contract extends PriceOracleBaseContract {
7
- constructor(sdk, data, underlying) {
7
+ constructor(sdk, data) {
8
8
  super(
9
9
  sdk,
10
10
  {
@@ -12,8 +12,7 @@ class PriceOracleV300Contract extends PriceOracleBaseContract {
12
12
  name: "PriceOracleV3",
13
13
  abi
14
14
  },
15
- data,
16
- underlying
15
+ data
17
16
  );
18
17
  }
19
18
  processLog(log) {
@@ -2,7 +2,7 @@ import { iPriceOracleV310Abi } from "../../../abi/v310.js";
2
2
  import { PriceOracleBaseContract } from "./PriceOracleBaseContract.js";
3
3
  const abi = iPriceOracleV310Abi;
4
4
  class PriceOracleV310Contract extends PriceOracleBaseContract {
5
- constructor(sdk, data, underlying) {
5
+ constructor(sdk, data) {
6
6
  super(
7
7
  sdk,
8
8
  {
@@ -10,8 +10,7 @@ class PriceOracleV310Contract extends PriceOracleBaseContract {
10
10
  name: "PriceOracleV3",
11
11
  abi
12
12
  },
13
- data,
14
- underlying
13
+ data
15
14
  );
16
15
  }
17
16
  processLog(log) {
@@ -1,16 +1,35 @@
1
1
  import { isV300, isV310 } from "../../constants/index.js";
2
+ import { PriceOracleBaseContract } from "./PriceOracleBaseContract.js";
2
3
  import { PriceOracleV300Contract } from "./PriceOracleV300Contract.js";
3
4
  import { PriceOracleV310Contract } from "./PriceOracleV310Contract.js";
4
- function createPriceOracle(sdk, data, underlying) {
5
- const v = data.baseParams.version;
6
- if (isV300(v)) {
7
- return new PriceOracleV300Contract(sdk, data, underlying);
5
+ function getOrCreatePriceOracle(sdk, data) {
6
+ const { version, addr } = data.baseParams;
7
+ const existing = sdk.contracts.get(addr);
8
+ if (existing) {
9
+ return tryExtendExistingOracle(existing, data);
8
10
  }
9
- if (isV310(v)) {
10
- return new PriceOracleV310Contract(sdk, data, underlying);
11
+ if (isV300(version)) {
12
+ return new PriceOracleV300Contract(sdk, data);
11
13
  }
12
- throw new Error(`Unsupported oracle version ${v}`);
14
+ if (isV310(version)) {
15
+ return new PriceOracleV310Contract(sdk, data);
16
+ }
17
+ throw new Error(`Unsupported oracle version ${version}`);
18
+ }
19
+ function tryExtendExistingOracle(existing, data) {
20
+ const { version, addr } = data.baseParams;
21
+ if (!(existing instanceof PriceOracleBaseContract)) {
22
+ throw new Error(
23
+ `expected oracle contract at ${addr}, found existing ${existing.contractType}`
24
+ );
25
+ }
26
+ if (Number(existing.version) !== Number(version)) {
27
+ throw new Error(
28
+ `expected oracle contract at ${addr} to have version ${version}, found ${existing.version}`
29
+ );
30
+ }
31
+ return existing.merge(data);
13
32
  }
14
33
  export {
15
- createPriceOracle
34
+ getOrCreatePriceOracle
16
35
  };
@@ -1,4 +1,5 @@
1
1
  import { decodeAbiParameters, hexToBytes } from "viem";
2
+ import { isV310 } from "../../constants/versions.js";
2
3
  import {
3
4
  AbstractPriceFeedContract
4
5
  } from "./AbstractPriceFeed.js";
@@ -15,7 +16,7 @@ class AbstractLPPriceFeedContract extends AbstractPriceFeedContract {
15
16
  constructor(sdk, args) {
16
17
  super(sdk, { ...args, decimals: 8 });
17
18
  this.hasLowerBoundCap = true;
18
- if (args.baseParams.version === 310n) {
19
+ if (isV310(args.baseParams.version)) {
19
20
  const decoder = decodeAbiParameters(
20
21
  [
21
22
  { type: "address", name: "lpToken" },
@@ -1,6 +1,6 @@
1
1
  import { bytesToString, decodeAbiParameters, toBytes } from "viem";
2
2
  import { redstonePriceFeedAbi } from "../../abi/index.js";
3
- import { ADDRESS_0X0 } from "../../constants/index.js";
3
+ import { ADDRESS_0X0, isV310 } from "../../constants/index.js";
4
4
  import { AbstractPriceFeedContract } from "./AbstractPriceFeed.js";
5
5
  class RedstonePriceFeedContract extends AbstractPriceFeedContract {
6
6
  token;
@@ -16,7 +16,7 @@ class RedstonePriceFeedContract extends AbstractPriceFeedContract {
16
16
  name: `RedstonePriceFeed`,
17
17
  abi: redstonePriceFeedAbi
18
18
  });
19
- if (args.baseParams.version === 310n) {
19
+ if (isV310(args.baseParams.version)) {
20
20
  const decoder = decodeAbiParameters(
21
21
  [
22
22
  { type: "address", name: "token" },
@@ -1,7 +1,7 @@
1
1
  import { getAbiItem, getAddress } from "viem";
2
2
  import { iPriceOracleV300Abi } from "../../abi/v300.js";
3
3
  import { SDKConstruct } from "../base/index.js";
4
- import { ADDRESS_PROVIDER_BLOCK } from "../constants/index.js";
4
+ import { ADDRESS_PROVIDER_BLOCK, isV300 } from "../constants/index.js";
5
5
  import { PriceFeedRef } from "../market/index.js";
6
6
  import { AddressMap, formatDuration, hexEq } from "../utils/index.js";
7
7
  import { getLogsSafe } from "../utils/viem/index.js";
@@ -16,12 +16,12 @@ class V300StalenessPeriodPlugin extends SDKConstruct {
16
16
  this.#logger = sdk.logger?.child?.({ name: "V300StalenessPeriodPlugin" }) ?? sdk.logger;
17
17
  }
18
18
  async attach() {
19
- await this.#syncReservePriceFeeds();
19
+ await this.#syncPriceFeeds();
20
20
  }
21
21
  async syncState() {
22
- await this.#syncReservePriceFeeds();
22
+ await this.#syncPriceFeeds();
23
23
  }
24
- async #syncReservePriceFeeds() {
24
+ async #syncPriceFeeds() {
25
25
  const oracles = this.#getOraclesMap();
26
26
  const [fromBlock, toBlock] = [this.#syncedTo + 1n, this.sdk.currentBlock];
27
27
  if (oracles.size === 0 || fromBlock > toBlock) {
@@ -44,7 +44,7 @@ class V300StalenessPeriodPlugin extends SDKConstruct {
44
44
  strict: true
45
45
  });
46
46
  this.#logger?.info(
47
- `loaded ${events.length} SetReservePriceFeed events in range [${fromBlock}; ${toBlock}]`
47
+ `loaded ${events.length} price feed events in range [${fromBlock}; ${toBlock}]`
48
48
  );
49
49
  for (const e of events) {
50
50
  const oracle = oracles.mustGet(e.address);
@@ -97,7 +97,7 @@ class V300StalenessPeriodPlugin extends SDKConstruct {
97
97
  }
98
98
  #getOraclesMap() {
99
99
  return new AddressMap(
100
- this.sdk.marketRegister.markets.filter((m) => m.priceOracle.version === 300).map((m) => [m.priceOracle.address, m.priceOracle])
100
+ this.sdk.marketRegister.markets.filter((m) => isV300(m.priceOracle.version)).map((m) => [m.priceOracle.address, m.priceOracle])
101
101
  );
102
102
  }
103
103
  }
@@ -41,11 +41,11 @@ const CHARTS_BACKEND_ADDRESSES = {
41
41
  };
42
42
  const STATIC_TOKEN = "https://static.gearbox.fi/tokens/";
43
43
  const LEADERBOARD_APIS = {
44
- [CHAINS.Mainnet.id]: "https://gpointbot.fly.dev",
45
- [CHAINS.Optimism.id]: "https://gpointbot.fly.dev",
46
- [CHAINS.Arbitrum.id]: "https://gpointbot.fly.dev",
44
+ ...Object.values(CHAINS).reduce((acc, chain) => {
45
+ acc[chain.id] = "https://gpointbot.fly.dev";
46
+ return acc;
47
+ }, {}),
47
48
  [HARDHAT]: "https://gpointbot.fly.dev",
48
- // !& new chains
49
49
  [TESTNET_CHAINS.Mainnet]: "https://testnet.gearbox.foundation/gpointbot",
50
50
  [TESTNET_CHAINS.Optimism]: "https://testnet.gearbox.foundation/gpointbot",
51
51
  [TESTNET_CHAINS.Arbitrum]: "https://testnet.gearbox.foundation/gpointbot"
@@ -5,13 +5,13 @@ import type { GearboxSDK } from "../GearboxSDK.js";
5
5
  import type { MarketStateHuman } from "../types/index.js";
6
6
  import { CreditSuite } from "./credit/index.js";
7
7
  import { MarketConfiguratorContract } from "./MarketConfiguratorContract.js";
8
- import type { PriceOracleContract } from "./oracle/index.js";
8
+ import type { IPriceOracleContract } from "./oracle/index.js";
9
9
  import { PoolSuite } from "./pool/index.js";
10
10
  export declare class MarketSuite extends SDKConstruct {
11
11
  readonly acl: Address;
12
12
  readonly configurator: MarketConfiguratorContract;
13
13
  readonly pool: PoolSuite;
14
- readonly priceOracle: PriceOracleContract;
14
+ readonly priceOracle: IPriceOracleContract;
15
15
  readonly creditManagers: CreditSuite[];
16
16
  /**
17
17
  * Original data received from compressor