@gearbox-protocol/sdk 11.1.2 → 11.1.4

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.
@@ -26,11 +26,11 @@ class PriceFeedAnswerMap extends import_utils.AddressMap {
26
26
  price(token) {
27
27
  const answer = this.get(token);
28
28
  if (!answer) {
29
- throw new Error(`no answer found`);
29
+ throw new Error(`no answer found for token ${token}`);
30
30
  }
31
31
  const { success, price } = answer;
32
32
  if (!success) {
33
- throw new Error(`answer is not successful`);
33
+ throw new Error(`answer (${price}) is not successful for token ${token}`);
34
34
  }
35
35
  return price;
36
36
  }
@@ -261,7 +261,7 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
261
261
  }).map(({ token, balance }) => {
262
262
  return {
263
263
  token,
264
- balance: priceOracle.convertToUSD(token, balance)
264
+ balance: this.#convertToUSDForSort(priceOracle, token, balance)
265
265
  };
266
266
  }).sort((a, b) => {
267
267
  return a.balance > b.balance ? -1 : 1;
@@ -284,6 +284,18 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
284
284
  }
285
285
  return (token) => map.get(token) ?? 1n;
286
286
  }
287
+ /**
288
+ * Tries to get some value even when prices are broken, since it's only needed for sorting
289
+ * @param oracle
290
+ * @param token
291
+ * @param amount
292
+ * @returns
293
+ */
294
+ #convertToUSDForSort(oracle, token, amount) {
295
+ const scale = 10n ** BigInt(this.sdk.tokensMeta.get(token)?.decimals ?? 0);
296
+ const price = oracle.mainPrices.get(token)?.price || oracle.reservePrices.get(token)?.price || scale;
297
+ return amount * price / scale;
298
+ }
287
299
  #debugTokenData(tData) {
288
300
  return tData.map((t) => ({
289
301
  token: this.labelAddress(t.token),
@@ -33,7 +33,11 @@ function isDust(opts) {
33
33
  try {
34
34
  balanceUSD = priceOracle.convertToUSD(token, balance);
35
35
  } catch {
36
- balanceUSD = priceOracle.convertToUSD(token, balance, true);
36
+ try {
37
+ balanceUSD = priceOracle.convertToUSD(token, balance, true);
38
+ } catch {
39
+ return false;
40
+ }
37
41
  }
38
42
  return balanceUSD < minBalanceUSD;
39
43
  }
@@ -3,11 +3,11 @@ class PriceFeedAnswerMap extends AddressMap {
3
3
  price(token) {
4
4
  const answer = this.get(token);
5
5
  if (!answer) {
6
- throw new Error(`no answer found`);
6
+ throw new Error(`no answer found for token ${token}`);
7
7
  }
8
8
  const { success, price } = answer;
9
9
  if (!success) {
10
- throw new Error(`answer is not successful`);
10
+ throw new Error(`answer (${price}) is not successful for token ${token}`);
11
11
  }
12
12
  return price;
13
13
  }
@@ -238,7 +238,7 @@ class RouterV310Contract extends AbstractRouterContract {
238
238
  }).map(({ token, balance }) => {
239
239
  return {
240
240
  token,
241
- balance: priceOracle.convertToUSD(token, balance)
241
+ balance: this.#convertToUSDForSort(priceOracle, token, balance)
242
242
  };
243
243
  }).sort((a, b) => {
244
244
  return a.balance > b.balance ? -1 : 1;
@@ -261,6 +261,18 @@ class RouterV310Contract extends AbstractRouterContract {
261
261
  }
262
262
  return (token) => map.get(token) ?? 1n;
263
263
  }
264
+ /**
265
+ * Tries to get some value even when prices are broken, since it's only needed for sorting
266
+ * @param oracle
267
+ * @param token
268
+ * @param amount
269
+ * @returns
270
+ */
271
+ #convertToUSDForSort(oracle, token, amount) {
272
+ const scale = 10n ** BigInt(this.sdk.tokensMeta.get(token)?.decimals ?? 0);
273
+ const price = oracle.mainPrices.get(token)?.price || oracle.reservePrices.get(token)?.price || scale;
274
+ return amount * price / scale;
275
+ }
264
276
  #debugTokenData(tData) {
265
277
  return tData.map((t) => ({
266
278
  token: this.labelAddress(t.token),
@@ -10,7 +10,11 @@ function isDust(opts) {
10
10
  try {
11
11
  balanceUSD = priceOracle.convertToUSD(token, balance);
12
12
  } catch {
13
- balanceUSD = priceOracle.convertToUSD(token, balance, true);
13
+ try {
14
+ balanceUSD = priceOracle.convertToUSD(token, balance, true);
15
+ } catch {
16
+ return false;
17
+ }
14
18
  }
15
19
  return balanceUSD < minBalanceUSD;
16
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "11.1.2",
3
+ "version": "11.1.4",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",
@@ -76,7 +76,7 @@
76
76
  "lint-staged": "^16.2.6",
77
77
  "pino": "^10.1.0",
78
78
  "pino-pretty": "^13.1.2",
79
- "tsup": "^8.5.0",
79
+ "tsup": "^8.5.1",
80
80
  "tsx": "^4.20.6",
81
81
  "typescript": "^5.9.3",
82
82
  "viem-deal": "^2.0.4",