@gearbox-protocol/sdk 3.0.4 → 3.1.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.
@@ -211,6 +211,17 @@ class PriceOracleBaseContract extends import_base.BaseContract {
211
211
  const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
212
212
  return amount * price / scale;
213
213
  }
214
+ /**
215
+ * Tries to convert amount of USD to token, using latest known prices
216
+ * @param to
217
+ * @param amount
218
+ * @param reserve
219
+ */
220
+ convertFromUSD(to, amount, reserve = false) {
221
+ const price = reserve ? this.reservePrice(to) : this.mainPrice(to);
222
+ const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(to));
223
+ return amount * scale / price;
224
+ }
214
225
  /**
215
226
  * Loads new prices for this oracle from PriceFeedCompressor
216
227
  * Does not update price feeds, only updates prices
@@ -35,8 +35,8 @@ async function getLogsSafe(client, params = {}) {
35
35
  throw e;
36
36
  }
37
37
  const [left, right] = await Promise.all([
38
- (0, import_actions.getLogs)(client, { ...params, ...bisected[0] }),
39
- (0, import_actions.getLogs)(client, { ...params, ...bisected[1] })
38
+ getLogsSafe(client, { ...params, ...bisected[0] }),
39
+ getLogsSafe(client, { ...params, ...bisected[1] })
40
40
  ]);
41
41
  return [...left, ...right];
42
42
  }
@@ -178,6 +178,17 @@ class PriceOracleBaseContract extends BaseContract {
178
178
  const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
179
179
  return amount * price / scale;
180
180
  }
181
+ /**
182
+ * Tries to convert amount of USD to token, using latest known prices
183
+ * @param to
184
+ * @param amount
185
+ * @param reserve
186
+ */
187
+ convertFromUSD(to, amount, reserve = false) {
188
+ const price = reserve ? this.reservePrice(to) : this.mainPrice(to);
189
+ const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(to));
190
+ return amount * scale / price;
191
+ }
181
192
  /**
182
193
  * Loads new prices for this oracle from PriceFeedCompressor
183
194
  * Does not update price feeds, only updates prices
@@ -14,8 +14,8 @@ async function getLogsSafe(client, params = {}) {
14
14
  throw e;
15
15
  }
16
16
  const [left, right] = await Promise.all([
17
- getLogs(client, { ...params, ...bisected[0] }),
18
- getLogs(client, { ...params, ...bisected[1] })
17
+ getLogsSafe(client, { ...params, ...bisected[0] }),
18
+ getLogsSafe(client, { ...params, ...bisected[1] })
19
19
  ]);
20
20
  return [...left, ...right];
21
21
  }
@@ -95,6 +95,13 @@ export declare class PriceOracleBaseContract<abi extends Abi | readonly unknown[
95
95
  * @param reserve
96
96
  */
97
97
  convertToUSD(from: Address, amount: bigint, reserve?: boolean): bigint;
98
+ /**
99
+ * Tries to convert amount of USD to token, using latest known prices
100
+ * @param to
101
+ * @param amount
102
+ * @param reserve
103
+ */
104
+ convertFromUSD(to: Address, amount: bigint, reserve?: boolean): bigint;
98
105
  /**
99
106
  * Loads new prices for this oracle from PriceFeedCompressor
100
107
  * Does not update price feeds, only updates prices
@@ -24,4 +24,6 @@ export interface IPriceOracleContract extends IBaseContract {
24
24
  onDemandPriceUpdates: (updates?: UpdatePriceFeedsResult) => OnDemandPriceUpdate[];
25
25
  convertToUnderlying: (token: Address, amount: bigint, reserve?: boolean) => bigint;
26
26
  convert: (from: Address, to: Address, amount: bigint, reserve?: boolean) => bigint;
27
+ convertToUSD: (from: Address, amount: bigint, reserve?: boolean) => bigint;
28
+ convertFromUSD: (to: Address, amount: bigint, reserve?: boolean) => bigint;
27
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.4",
3
+ "version": "3.1.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",