@reflectmoney/stable.ts 2.4.0 → 2.5.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.
@@ -220,6 +220,16 @@ export declare abstract class Stablecoin<T extends Controller> {
220
220
  * @returns Promise resolving to the USD exchange rate as a number
221
221
  */
222
222
  abstract getReceiptUsdExchangeRate(): Promise<number>;
223
+ /**
224
+ * Abstract method to get the Base->Collateral exchange rate.
225
+ * Returned as an integer scaled by EXCHANGE_RATE_PRECISION.
226
+ */
227
+ abstract getBaseToCollateralExchangeRate(): Promise<number>;
228
+ /**
229
+ * Abstract method to get the Receipt->Collateral exchange rate.
230
+ * Returned as an integer scaled by EXCHANGE_RATE_PRECISION.
231
+ */
232
+ abstract getReceiptToCollateralExchangeRate(): Promise<number>;
223
233
  /**
224
234
  * Abstract method to simulate mint math and get the quote for a mint operation.
225
235
  * Must be implemented by concrete stablecoin classes.
@@ -357,17 +357,12 @@ class Stablecoin {
357
357
  */
358
358
  stablecoinToCollateralWithSlippage(amount, collateralMint, slippageBips) {
359
359
  return __awaiter(this, void 0, void 0, function* () {
360
- const { data } = yield this.getCollateralPrice(collateralMint);
361
- const priceObj = data[0].price;
362
- const price = new bn_js_1.default(priceObj.price);
363
- const expo = priceObj.expo;
364
- const { decimals } = yield (0, spl_token_1.getMint)(this.connection, collateralMint);
360
+ // Use receipt-to-collateral exchange rate (already accounts for decimals)
361
+ const rate = new bn_js_1.default(yield this.getReceiptToCollateralExchangeRate()); // scaled by EXCHANGE_RATE_PRECISION
365
362
  const collateralAmount = new bn_js_1.default(amount)
366
- .mul(new bn_js_1.default(10).pow(new bn_js_1.default(decimals)))
363
+ .mul(rate)
367
364
  .mul(new bn_js_1.default(10000 - slippageBips))
368
- .div(new bn_js_1.default(10).pow(new bn_js_1.default(6))) // stablecoin decimals
369
- .div(price)
370
- .div(new bn_js_1.default(10).pow(new bn_js_1.default(expo)))
365
+ .div(new bn_js_1.default(constants_1.EXCHANGE_RATE_PRECISION))
371
366
  .div(new bn_js_1.default(10000));
372
367
  return collateralAmount;
373
368
  });
@@ -382,17 +377,12 @@ class Stablecoin {
382
377
  */
383
378
  collateralToStablecoinWithSlippage(amount, collateralMint, slippageBips) {
384
379
  return __awaiter(this, void 0, void 0, function* () {
385
- const { data } = yield this.getCollateralPrice(collateralMint);
386
- const priceObj = data[0].price;
387
- const price = new bn_js_1.default(priceObj.price);
388
- const expo = priceObj.expo;
389
- const { decimals } = yield (0, spl_token_1.getMint)(this.connection, collateralMint);
380
+ // Invert the receipt-to-collateral exchange rate
381
+ const receiptToCollateralRate = new bn_js_1.default(yield this.getReceiptToCollateralExchangeRate()); // scaled
390
382
  const stablecoinAmount = new bn_js_1.default(amount)
391
- .mul(price)
392
- .mul(new bn_js_1.default(10).pow(new bn_js_1.default(6))) // stablecoin decimals
383
+ .mul(new bn_js_1.default(constants_1.EXCHANGE_RATE_PRECISION))
393
384
  .mul(new bn_js_1.default(10000 - slippageBips))
394
- .div(new bn_js_1.default(10).pow(new bn_js_1.default(expo)))
395
- .div(new bn_js_1.default(10).pow(new bn_js_1.default(decimals)))
385
+ .div(receiptToCollateralRate)
396
386
  .div(new bn_js_1.default(10000));
397
387
  return stablecoinAmount;
398
388
  });
@@ -99,7 +99,9 @@ export declare class LstStablecoin extends Stablecoin<DriftLstController> {
99
99
  * @returns Promise resolving to the USD exchange rate as a number
100
100
  */
101
101
  getBaseUsdExchangeRate(): Promise<number>;
102
+ getBaseToCollateralExchangeRate(): Promise<number>;
102
103
  getReceiptUsdExchangeRate(): Promise<number>;
104
+ getReceiptToCollateralExchangeRate(): Promise<number>;
103
105
  simulateMintMath(): Promise<BN>;
104
106
  simulateRedeemMath(): Promise<BN>;
105
107
  }
@@ -312,6 +312,11 @@ class LstStablecoin extends Stablecoin_1.Stablecoin {
312
312
  return totalUsdValue;
313
313
  });
314
314
  }
315
+ getBaseToCollateralExchangeRate() {
316
+ return __awaiter(this, void 0, void 0, function* () {
317
+ return this.getBaseUsdExchangeRate();
318
+ });
319
+ }
315
320
  getReceiptUsdExchangeRate() {
316
321
  return __awaiter(this, void 0, void 0, function* () {
317
322
  const baseUsdExchangeRate = yield this.getBaseUsdExchangeRate();
@@ -325,6 +330,11 @@ class LstStablecoin extends Stablecoin_1.Stablecoin {
325
330
  return exchangeRate;
326
331
  });
327
332
  }
333
+ getReceiptToCollateralExchangeRate() {
334
+ return __awaiter(this, void 0, void 0, function* () {
335
+ return this.getReceiptUsdExchangeRate();
336
+ });
337
+ }
328
338
  simulateMintMath() {
329
339
  return __awaiter(this, void 0, void 0, function* () {
330
340
  return new bn_js_1.default(0);
@@ -83,6 +83,16 @@ export declare class UsdcPlusStablecoin extends Stablecoin<DriftUsdcController>
83
83
  */
84
84
  getBaseUsdExchangeRate(): Promise<number>;
85
85
  getReceiptUsdExchangeRate(): Promise<number>;
86
+ /**
87
+ * Base (USDC) to Collateral (USDC) exchange rate.
88
+ * For USDC+, base and collateral are the same asset/decimals.
89
+ */
90
+ getBaseToCollateralExchangeRate(): Promise<number>;
91
+ /**
92
+ * Receipt (USDC+) to Collateral (USDC) exchange rate.
93
+ * rate = deposited_vault_value / receipt_supply, scaled by EXCHANGE_RATE_PRECISION
94
+ */
95
+ getReceiptToCollateralExchangeRate(): Promise<number>;
86
96
  /**
87
97
  * Math helper function that calculates receipt tokens to issue for a given deposit amount.
88
98
  * Mirrors the compute_receipt_token function from Rust.
@@ -291,6 +291,35 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
291
291
  return this.getBaseUsdExchangeRate();
292
292
  });
293
293
  }
294
+ /**
295
+ * Base (USDC) to Collateral (USDC) exchange rate.
296
+ * For USDC+, base and collateral are the same asset/decimals.
297
+ */
298
+ getBaseToCollateralExchangeRate() {
299
+ return __awaiter(this, void 0, void 0, function* () {
300
+ const { scaledBalance, balanceType } = this
301
+ .driftClient
302
+ .getSpotPosition(0);
303
+ const spotMarketAccount = this
304
+ .driftClient
305
+ .getSpotMarketAccount(0);
306
+ const usdcAmount = (0, sdk_2.getTokenAmount)(scaledBalance, spotMarketAccount, balanceType);
307
+ const { supply, decimals } = yield (0, spl_token_1.getMint)(this.connection, this.stablecoinMint, "confirmed");
308
+ const exchangeRate = usdcAmount
309
+ .mul(new bn_js_1.default(constants_1.EXCHANGE_RATE_PRECISION)) // gives us exhcnage rate precision
310
+ .div(new bn_js_1.default(supply.toString())); // price per stablecoin
311
+ return exchangeRate.toNumber();
312
+ });
313
+ }
314
+ /**
315
+ * Receipt (USDC+) to Collateral (USDC) exchange rate.
316
+ * rate = deposited_vault_value / receipt_supply, scaled by EXCHANGE_RATE_PRECISION
317
+ */
318
+ getReceiptToCollateralExchangeRate() {
319
+ return __awaiter(this, void 0, void 0, function* () {
320
+ return this.getBaseToCollateralExchangeRate();
321
+ });
322
+ }
294
323
  /**
295
324
  * Math helper function that calculates receipt tokens to issue for a given deposit amount.
296
325
  * Mirrors the compute_receipt_token function from Rust.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reflectmoney/stable.ts",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "type": "commonjs",
5
5
  "author": "stablecoinjesus @ Palindrome Engineering",
6
6
  "repository": {
@@ -8,7 +8,8 @@
8
8
  "url": "https://github.com/palindrome-eng/reflect-delta-neutral.git"
9
9
  },
10
10
  "scripts": {
11
- "test": "mocha --require ts-node/register ./tests/index.test.ts"
11
+ "test": "mocha --require ts-node/register ./tests/index.test.ts",
12
+ "test:slippage": "mocha --require ts-node/register ./tests/slippage.test.ts"
12
13
  },
13
14
  "dependencies": {
14
15
  "@coral-xyz/anchor": "0.29.0",