@reflectmoney/stable.ts 1.1.9 → 1.2.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.
@@ -217,4 +217,20 @@ export declare abstract class Stablecoin<T extends Controller> {
217
217
  * @returns Promise resolving to the USD exchange rate as a number
218
218
  */
219
219
  abstract getReceiptUsdExchangeRate(): Promise<number>;
220
+ /**
221
+ * Abstract method to simulate mint math and get the quote for a mint operation.
222
+ * Must be implemented by concrete stablecoin classes.
223
+ *
224
+ * @param amount - Amount of base tokens (e.g., USDC) to deposit
225
+ * @returns Promise resolving to the amount of receipt tokens that would be minted
226
+ */
227
+ abstract simulateMintMath(amount: BN): Promise<BN>;
228
+ /**
229
+ * Abstract method to simulate redeem math and get the quote for a redeem operation.
230
+ * Must be implemented by concrete stablecoin classes.
231
+ *
232
+ * @param amount - Amount of receipt tokens to redeem
233
+ * @returns Promise resolving to the amount of base tokens that would be returned
234
+ */
235
+ abstract simulateRedeemMath(amount: BN): Promise<BN>;
220
236
  }
@@ -100,4 +100,6 @@ export declare class LstStablecoin extends Stablecoin<DriftLstController> {
100
100
  */
101
101
  getBaseUsdExchangeRate(): Promise<number>;
102
102
  getReceiptUsdExchangeRate(): Promise<number>;
103
+ simulateMintMath(): Promise<BN>;
104
+ simulateRedeemMath(): Promise<BN>;
103
105
  }
@@ -325,5 +325,15 @@ class LstStablecoin extends Stablecoin_1.Stablecoin {
325
325
  return exchangeRate;
326
326
  });
327
327
  }
328
+ simulateMintMath() {
329
+ return __awaiter(this, void 0, void 0, function* () {
330
+ return new bn_js_1.default(0);
331
+ });
332
+ }
333
+ simulateRedeemMath() {
334
+ return __awaiter(this, void 0, void 0, function* () {
335
+ return new bn_js_1.default(0);
336
+ });
337
+ }
328
338
  }
329
339
  exports.LstStablecoin = LstStablecoin;
@@ -83,4 +83,44 @@ export declare class UsdcPlusStablecoin extends Stablecoin<DriftUsdcController>
83
83
  */
84
84
  getBaseUsdExchangeRate(): Promise<number>;
85
85
  getReceiptUsdExchangeRate(): Promise<number>;
86
+ /**
87
+ * Math helper function that calculates receipt tokens to issue for a given deposit amount.
88
+ * Mirrors the compute_receipt_token function from Rust.
89
+ *
90
+ * @param deposit - Amount of USDC being deposited
91
+ * @param depositedVault - Current deposited vault value
92
+ * @param receiptTokenSupply - Current effective supply of receipt tokens
93
+ * @returns Amount of receipt tokens to issue
94
+ */
95
+ private computeReceiptToken;
96
+ /**
97
+ * Math helper function that calculates base token amount to return for a given receipt token burn.
98
+ * Mirrors the compute_base_token function from Rust.
99
+ *
100
+ * @param receipt - Amount of receipt tokens being burned
101
+ * @param depositedVault - Current deposited vault value
102
+ * @param receiptTokenSupply - Current effective supply of receipt tokens
103
+ * @returns Amount of base tokens (USDC) to return
104
+ */
105
+ private computeBaseToken;
106
+ /**
107
+ * Processes a user deposit: calculates receipt tokens to issue and updates internal accounting.
108
+ * Mirrors the process_deposit function from Rust AutoCompound.
109
+ *
110
+ * @param usdcDeposited - Amount of USDC being deposited
111
+ * @returns Amount of receipt tokens to issue
112
+ * @throws Error if USDC input is zero
113
+ */
114
+ private processDeposit;
115
+ /**
116
+ * Processes a user redemption: calculates USDC to return and updates internal accounting.
117
+ * Mirrors the process_redemption function from Rust AutoCompound.
118
+ *
119
+ * @param receiptTokensBurned - Amount of receipt tokens being burned
120
+ * @returns Amount of USDC to return to user
121
+ * @throws Error if receipt token input is zero
122
+ */
123
+ private processRedemption;
124
+ simulateMintMath(usdcAmount: BN): Promise<BN>;
125
+ simulateRedeemMath(receiptTokens: BN): Promise<BN>;
86
126
  }
@@ -281,5 +281,91 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
281
281
  return this.getBaseUsdExchangeRate();
282
282
  });
283
283
  }
284
+ /**
285
+ * Math helper function that calculates receipt tokens to issue for a given deposit amount.
286
+ * Mirrors the compute_receipt_token function from Rust.
287
+ *
288
+ * @param deposit - Amount of USDC being deposited
289
+ * @param depositedVault - Current deposited vault value
290
+ * @param receiptTokenSupply - Current effective supply of receipt tokens
291
+ * @returns Amount of receipt tokens to issue
292
+ */
293
+ computeReceiptToken(deposit, depositedVault, receiptTokenSupply) {
294
+ // If supply is 0, return deposit amount (1:1 ratio for first deposit)
295
+ if (receiptTokenSupply.isZero()) {
296
+ return deposit;
297
+ }
298
+ // receipt_tokens = (deposit * receipt_token_supply) / deposited_vault
299
+ return deposit.mul(receiptTokenSupply).div(depositedVault);
300
+ }
301
+ /**
302
+ * Math helper function that calculates base token amount to return for a given receipt token burn.
303
+ * Mirrors the compute_base_token function from Rust.
304
+ *
305
+ * @param receipt - Amount of receipt tokens being burned
306
+ * @param depositedVault - Current deposited vault value
307
+ * @param receiptTokenSupply - Current effective supply of receipt tokens
308
+ * @returns Amount of base tokens (USDC) to return
309
+ */
310
+ computeBaseToken(receipt, depositedVault, receiptTokenSupply) {
311
+ // usdc_amount = (receipt * deposited_vault) / receipt_token_supply
312
+ return receipt.mul(depositedVault).div(receiptTokenSupply);
313
+ }
314
+ /**
315
+ * Processes a user deposit: calculates receipt tokens to issue and updates internal accounting.
316
+ * Mirrors the process_deposit function from Rust AutoCompound.
317
+ *
318
+ * @param usdcDeposited - Amount of USDC being deposited
319
+ * @returns Amount of receipt tokens to issue
320
+ * @throws Error if USDC input is zero
321
+ */
322
+ processDeposit(usdcDeposited) {
323
+ if (usdcDeposited.isZero()) {
324
+ throw new Error("USDC input can not be zero");
325
+ }
326
+ if (!this.controller || !this.controller.compounder) {
327
+ throw new Error("Controller not loaded. Call load() first.");
328
+ }
329
+ const autocompound = this.controller.compounder;
330
+ // Calculate receipt tokens to issue
331
+ const receiptTokens = this.computeReceiptToken(usdcDeposited, new bn_js_1.default(autocompound.depositedVaultValue), new bn_js_1.default(autocompound.effectiveSupply));
332
+ return receiptTokens;
333
+ }
334
+ /**
335
+ * Processes a user redemption: calculates USDC to return and updates internal accounting.
336
+ * Mirrors the process_redemption function from Rust AutoCompound.
337
+ *
338
+ * @param receiptTokensBurned - Amount of receipt tokens being burned
339
+ * @returns Amount of USDC to return to user
340
+ * @throws Error if receipt token input is zero
341
+ */
342
+ processRedemption(receiptTokensBurned) {
343
+ if (receiptTokensBurned.isZero()) {
344
+ throw new Error("LP input can not be zero");
345
+ }
346
+ if (!this.controller || !this.controller.compounder) {
347
+ throw new Error("Controller not loaded. Call load() first.");
348
+ }
349
+ const autocompound = this.controller.compounder;
350
+ // Calculate USDC to return
351
+ const usdcAmount = this.computeBaseToken(receiptTokensBurned, new bn_js_1.default(autocompound.depositedVaultValue), new bn_js_1.default(autocompound.effectiveSupply));
352
+ return usdcAmount;
353
+ }
354
+ simulateMintMath(usdcAmount) {
355
+ return __awaiter(this, void 0, void 0, function* () {
356
+ if (!this.controller) {
357
+ yield this.load();
358
+ }
359
+ return this.processDeposit(usdcAmount);
360
+ });
361
+ }
362
+ simulateRedeemMath(receiptTokens) {
363
+ return __awaiter(this, void 0, void 0, function* () {
364
+ if (!this.controller) {
365
+ yield this.load();
366
+ }
367
+ return this.processRedemption(receiptTokens);
368
+ });
369
+ }
284
370
  }
285
371
  exports.UsdcPlusStablecoin = UsdcPlusStablecoin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reflectmoney/stable.ts",
3
- "version": "1.1.9",
3
+ "version": "1.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "stablecoinjesus @ Palindrome Engineering",
6
6
  "repository": {