@gearbox-protocol/sdk 3.0.0-vfour.348 → 3.0.0-vfour.349

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,37 @@
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 PriceFeedAnswerMap_exports = {};
20
+ __export(PriceFeedAnswerMap_exports, {
21
+ default: () => PriceFeedAnswerMap
22
+ });
23
+ module.exports = __toCommonJS(PriceFeedAnswerMap_exports);
24
+ var import_utils = require("../../utils/index.js");
25
+ class PriceFeedAnswerMap extends import_utils.AddressMap {
26
+ price(token) {
27
+ const answer = this.get(token);
28
+ if (!answer) {
29
+ throw new Error(`no answer found`);
30
+ }
31
+ const { success, price } = answer;
32
+ if (!success) {
33
+ throw new Error(`answer is not successful`);
34
+ }
35
+ return price;
36
+ }
37
+ }
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var PriceOracleBaseContract_exports = {};
20
30
  __export(PriceOracleBaseContract_exports, {
@@ -29,6 +39,7 @@ var import_base = require("../../base/index.js");
29
39
  var import_constants = require("../../constants/index.js");
30
40
  var import_utils = require("../../utils/index.js");
31
41
  var import_pricefeeds = require("../pricefeeds/index.js");
42
+ var import_PriceFeedAnswerMap = __toESM(require("./PriceFeedAnswerMap.js"));
32
43
  const ZERO_PRICE_FEED = (0, import_viem.stringToHex)("PRICE_FEED::ZERO", { size: 32 });
33
44
  class PriceOracleBaseContract extends import_base.BaseContract {
34
45
  /**
@@ -50,24 +61,16 @@ class PriceOracleBaseContract extends import_base.BaseContract {
50
61
  "reservePriceFeeds"
51
62
  );
52
63
  /**
53
- * Mapping Token => Price in underlying
64
+ * Mapping Token => Price in USD
54
65
  */
55
- mainPrices = new import_utils.AddressMap(void 0, "mainPrices");
66
+ mainPrices = new import_PriceFeedAnswerMap.default(void 0, "mainPrices");
56
67
  /**
57
- * Same, but with details
68
+ * Mapping Token => Price in USD
58
69
  */
59
- #mainPrices = new import_utils.AddressMap();
60
- /**
61
- * Mapping Token => Price in underlying
62
- */
63
- reservePrices = new import_utils.AddressMap(
70
+ reservePrices = new import_PriceFeedAnswerMap.default(
64
71
  void 0,
65
72
  "reservePrices"
66
73
  );
67
- /**
68
- * Same, but with debug details
69
- */
70
- #reservePrices = new import_utils.AddressMap();
71
74
  #priceFeedTree = [];
72
75
  constructor(sdk, args, data, underlying) {
73
76
  super(sdk, args);
@@ -141,6 +144,24 @@ class PriceOracleBaseContract extends import_base.BaseContract {
141
144
  );
142
145
  return result;
143
146
  }
147
+ /**
148
+ * Gets main price for given token
149
+ * Throws if token price feed is not found or answer is not successful
150
+ * @param token
151
+ * @returns
152
+ */
153
+ mainPrice(token) {
154
+ return this.mainPrices.price(token);
155
+ }
156
+ /**
157
+ * Gets reserve price for given token
158
+ * Throws if token price feed is not found or answer is not successful
159
+ * @param token
160
+ * @returns
161
+ */
162
+ reservePrice(token) {
163
+ return this.reservePrices.price(token);
164
+ }
144
165
  /**
145
166
  * Returns true if oracle's price feed tree contains given price feed
146
167
  * @param priceFeed
@@ -172,9 +193,9 @@ class PriceOracleBaseContract extends import_base.BaseContract {
172
193
  if (from === to) {
173
194
  return amount;
174
195
  }
175
- const fromPrice = reserve ? this.reservePrices.mustGet(from) : this.mainPrices.mustGet(from);
196
+ const fromPrice = reserve ? this.reservePrice(from) : this.mainPrice(from);
176
197
  const fromScale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
177
- const toPrice = reserve ? this.reservePrices.mustGet(to) : this.mainPrices.mustGet(to);
198
+ const toPrice = reserve ? this.reservePrice(to) : this.mainPrice(to);
178
199
  const toScale = 10n ** BigInt(this.sdk.tokensMeta.decimals(to));
179
200
  return amount * fromPrice * toScale / (toPrice * fromScale);
180
201
  }
@@ -186,7 +207,7 @@ class PriceOracleBaseContract extends import_base.BaseContract {
186
207
  * @param reserve
187
208
  */
188
209
  convertToUSD(from, amount, reserve = false) {
189
- const price = reserve ? this.reservePrices.mustGet(from) : this.mainPrices.mustGet(from);
210
+ const price = reserve ? this.reservePrice(from) : this.mainPrice(from);
190
211
  const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
191
212
  return amount * price / scale;
192
213
  }
@@ -244,22 +265,16 @@ class PriceOracleBaseContract extends import_base.BaseContract {
244
265
  const priceFeedType = node?.baseParams.contractType;
245
266
  if (reserve) {
246
267
  this.reservePriceFeeds.upsert(token, ref);
247
- if (price !== void 0) {
248
- this.reservePrices.upsert(token, price);
249
- }
268
+ this.reservePrices.upsert(token, node?.answer);
250
269
  if (!price && priceFeedType !== ZERO_PRICE_FEED) {
251
270
  this.#noAnswerWarn(priceFeed, node);
252
271
  }
253
- this.#reservePrices.upsert(token, node?.answer);
254
272
  } else {
255
273
  this.mainPriceFeeds.upsert(token, ref);
256
- if (price !== void 0) {
257
- this.mainPrices.upsert(token, price);
258
- }
274
+ this.mainPrices.upsert(token, node?.answer);
259
275
  if (!price && priceFeedType !== ZERO_PRICE_FEED) {
260
276
  this.#noAnswerWarn(priceFeed, node);
261
277
  }
262
- this.#mainPrices.upsert(token, node?.answer);
263
278
  }
264
279
  this.#labelPriceFeed(priceFeed, reserve ? "Reserve" : "Main", token);
265
280
  });
@@ -309,13 +324,13 @@ class PriceOracleBaseContract extends import_base.BaseContract {
309
324
  this.reservePriceFeeds.entries().map(([token, v]) => [this.labelAddress(token), v.stateHuman(raw)])
310
325
  ),
311
326
  mainPrices: Object.fromEntries(
312
- this.#mainPrices.entries().map(([token, answer]) => [
327
+ this.mainPrices.entries().map(([token, answer]) => [
313
328
  this.labelAddress(token),
314
329
  formatAnswer(answer, raw)
315
330
  ])
316
331
  ),
317
332
  reservePrices: Object.fromEntries(
318
- this.#reservePrices.entries().map(([token, answer]) => [
333
+ this.reservePrices.entries().map(([token, answer]) => [
319
334
  this.labelAddress(token),
320
335
  formatAnswer(answer, raw)
321
336
  ])
@@ -120,6 +120,9 @@ class AddressMap {
120
120
  freeze() {
121
121
  this.#frozen = true;
122
122
  }
123
+ get name() {
124
+ return this.#name;
125
+ }
123
126
  }
124
127
  // Annotate the CommonJS export names for ESM import in node:
125
128
  0 && (module.exports = {
@@ -0,0 +1,17 @@
1
+ import { AddressMap } from "../../utils/index.js";
2
+ class PriceFeedAnswerMap extends AddressMap {
3
+ price(token) {
4
+ const answer = this.get(token);
5
+ if (!answer) {
6
+ throw new Error(`no answer found`);
7
+ }
8
+ const { success, price } = answer;
9
+ if (!success) {
10
+ throw new Error(`answer is not successful`);
11
+ }
12
+ return price;
13
+ }
14
+ }
15
+ export {
16
+ PriceFeedAnswerMap as default
17
+ };
@@ -6,6 +6,7 @@ import { BaseContract } from "../../base/index.js";
6
6
  import { AP_PRICE_FEED_COMPRESSOR } from "../../constants/index.js";
7
7
  import { AddressMap, formatBN } from "../../utils/index.js";
8
8
  import { PriceFeedRef } from "../pricefeeds/index.js";
9
+ import PriceFeedAnswerMap from "./PriceFeedAnswerMap.js";
9
10
  const ZERO_PRICE_FEED = stringToHex("PRICE_FEED::ZERO", { size: 32 });
10
11
  class PriceOracleBaseContract extends BaseContract {
11
12
  /**
@@ -27,24 +28,16 @@ class PriceOracleBaseContract extends BaseContract {
27
28
  "reservePriceFeeds"
28
29
  );
29
30
  /**
30
- * Mapping Token => Price in underlying
31
+ * Mapping Token => Price in USD
31
32
  */
32
- mainPrices = new AddressMap(void 0, "mainPrices");
33
+ mainPrices = new PriceFeedAnswerMap(void 0, "mainPrices");
33
34
  /**
34
- * Same, but with details
35
+ * Mapping Token => Price in USD
35
36
  */
36
- #mainPrices = new AddressMap();
37
- /**
38
- * Mapping Token => Price in underlying
39
- */
40
- reservePrices = new AddressMap(
37
+ reservePrices = new PriceFeedAnswerMap(
41
38
  void 0,
42
39
  "reservePrices"
43
40
  );
44
- /**
45
- * Same, but with debug details
46
- */
47
- #reservePrices = new AddressMap();
48
41
  #priceFeedTree = [];
49
42
  constructor(sdk, args, data, underlying) {
50
43
  super(sdk, args);
@@ -118,6 +111,24 @@ class PriceOracleBaseContract extends BaseContract {
118
111
  );
119
112
  return result;
120
113
  }
114
+ /**
115
+ * Gets main price for given token
116
+ * Throws if token price feed is not found or answer is not successful
117
+ * @param token
118
+ * @returns
119
+ */
120
+ mainPrice(token) {
121
+ return this.mainPrices.price(token);
122
+ }
123
+ /**
124
+ * Gets reserve price for given token
125
+ * Throws if token price feed is not found or answer is not successful
126
+ * @param token
127
+ * @returns
128
+ */
129
+ reservePrice(token) {
130
+ return this.reservePrices.price(token);
131
+ }
121
132
  /**
122
133
  * Returns true if oracle's price feed tree contains given price feed
123
134
  * @param priceFeed
@@ -149,9 +160,9 @@ class PriceOracleBaseContract extends BaseContract {
149
160
  if (from === to) {
150
161
  return amount;
151
162
  }
152
- const fromPrice = reserve ? this.reservePrices.mustGet(from) : this.mainPrices.mustGet(from);
163
+ const fromPrice = reserve ? this.reservePrice(from) : this.mainPrice(from);
153
164
  const fromScale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
154
- const toPrice = reserve ? this.reservePrices.mustGet(to) : this.mainPrices.mustGet(to);
165
+ const toPrice = reserve ? this.reservePrice(to) : this.mainPrice(to);
155
166
  const toScale = 10n ** BigInt(this.sdk.tokensMeta.decimals(to));
156
167
  return amount * fromPrice * toScale / (toPrice * fromScale);
157
168
  }
@@ -163,7 +174,7 @@ class PriceOracleBaseContract extends BaseContract {
163
174
  * @param reserve
164
175
  */
165
176
  convertToUSD(from, amount, reserve = false) {
166
- const price = reserve ? this.reservePrices.mustGet(from) : this.mainPrices.mustGet(from);
177
+ const price = reserve ? this.reservePrice(from) : this.mainPrice(from);
167
178
  const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
168
179
  return amount * price / scale;
169
180
  }
@@ -221,22 +232,16 @@ class PriceOracleBaseContract extends BaseContract {
221
232
  const priceFeedType = node?.baseParams.contractType;
222
233
  if (reserve) {
223
234
  this.reservePriceFeeds.upsert(token, ref);
224
- if (price !== void 0) {
225
- this.reservePrices.upsert(token, price);
226
- }
235
+ this.reservePrices.upsert(token, node?.answer);
227
236
  if (!price && priceFeedType !== ZERO_PRICE_FEED) {
228
237
  this.#noAnswerWarn(priceFeed, node);
229
238
  }
230
- this.#reservePrices.upsert(token, node?.answer);
231
239
  } else {
232
240
  this.mainPriceFeeds.upsert(token, ref);
233
- if (price !== void 0) {
234
- this.mainPrices.upsert(token, price);
235
- }
241
+ this.mainPrices.upsert(token, node?.answer);
236
242
  if (!price && priceFeedType !== ZERO_PRICE_FEED) {
237
243
  this.#noAnswerWarn(priceFeed, node);
238
244
  }
239
- this.#mainPrices.upsert(token, node?.answer);
240
245
  }
241
246
  this.#labelPriceFeed(priceFeed, reserve ? "Reserve" : "Main", token);
242
247
  });
@@ -286,13 +291,13 @@ class PriceOracleBaseContract extends BaseContract {
286
291
  this.reservePriceFeeds.entries().map(([token, v]) => [this.labelAddress(token), v.stateHuman(raw)])
287
292
  ),
288
293
  mainPrices: Object.fromEntries(
289
- this.#mainPrices.entries().map(([token, answer]) => [
294
+ this.mainPrices.entries().map(([token, answer]) => [
290
295
  this.labelAddress(token),
291
296
  formatAnswer(answer, raw)
292
297
  ])
293
298
  ),
294
299
  reservePrices: Object.fromEntries(
295
- this.#reservePrices.entries().map(([token, answer]) => [
300
+ this.reservePrices.entries().map(([token, answer]) => [
296
301
  this.labelAddress(token),
297
302
  formatAnswer(answer, raw)
298
303
  ])
@@ -97,6 +97,9 @@ class AddressMap {
97
97
  freeze() {
98
98
  this.#frozen = true;
99
99
  }
100
+ get name() {
101
+ return this.#name;
102
+ }
100
103
  }
101
104
  export {
102
105
  AddressMap
@@ -0,0 +1,6 @@
1
+ import type { Address } from "viem";
2
+ import type { PriceFeedAnswer } from "../../base/index.js";
3
+ import { AddressMap } from "../../utils/index.js";
4
+ export default class PriceFeedAnswerMap extends AddressMap<PriceFeedAnswer> {
5
+ price(token: Address): bigint;
6
+ }
@@ -7,6 +7,7 @@ import type { PriceOracleStateHuman } from "../../types/index.js";
7
7
  import { AddressMap } from "../../utils/index.js";
8
8
  import type { IPriceFeedContract, UpdatePriceFeedsResult } from "../pricefeeds/index.js";
9
9
  import { PriceFeedRef } from "../pricefeeds/index.js";
10
+ import PriceFeedAnswerMap from "./PriceFeedAnswerMap.js";
10
11
  import type { IPriceOracleContract, OnDemandPriceUpdate, PriceFeedsForTokensOptions } from "./types.js";
11
12
  export declare class PriceOracleBaseContract<abi extends Abi | readonly unknown[]> extends BaseContract<abi> implements IPriceOracleContract {
12
13
  #private;
@@ -23,13 +24,13 @@ export declare class PriceOracleBaseContract<abi extends Abi | readonly unknown[
23
24
  */
24
25
  readonly reservePriceFeeds: AddressMap<PriceFeedRef>;
25
26
  /**
26
- * Mapping Token => Price in underlying
27
+ * Mapping Token => Price in USD
27
28
  */
28
- readonly mainPrices: AddressMap<bigint>;
29
+ readonly mainPrices: PriceFeedAnswerMap;
29
30
  /**
30
- * Mapping Token => Price in underlying
31
+ * Mapping Token => Price in USD
31
32
  */
32
- readonly reservePrices: AddressMap<bigint>;
33
+ readonly reservePrices: PriceFeedAnswerMap;
33
34
  constructor(sdk: GearboxSDK, args: BaseContractOptions<abi>, data: PriceOracleData, underlying: Address);
34
35
  /**
35
36
  * Returns main and reserve price feeds for given tokens
@@ -50,6 +51,20 @@ export declare class PriceOracleBaseContract<abi extends Abi | readonly unknown[
50
51
  * @returns
51
52
  */
52
53
  onDemandPriceUpdates(updates?: UpdatePriceFeedsResult): OnDemandPriceUpdate[];
54
+ /**
55
+ * Gets main price for given token
56
+ * Throws if token price feed is not found or answer is not successful
57
+ * @param token
58
+ * @returns
59
+ */
60
+ mainPrice(token: Address): bigint;
61
+ /**
62
+ * Gets reserve price for given token
63
+ * Throws if token price feed is not found or answer is not successful
64
+ * @param token
65
+ * @returns
66
+ */
67
+ reservePrice(token: Address): bigint;
53
68
  /**
54
69
  * Returns true if oracle's price feed tree contains given price feed
55
70
  * @param priceFeed
@@ -39,4 +39,5 @@ export declare class AddressMap<T> {
39
39
  asRecord(): Record<Address, T>;
40
40
  get size(): number;
41
41
  freeze(): void;
42
+ protected get name(): string | undefined;
42
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.0-vfour.348",
3
+ "version": "3.0.0-vfour.349",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",