@reflectmoney/stable.ts 1.1.8 → 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.
@@ -34,6 +34,7 @@ export declare abstract class Stablecoin<T extends Controller> {
34
34
  driftClient?: DriftClient;
35
35
  /** Lookup table with stablecoin-specific accounts. */
36
36
  lookupTable: PublicKey;
37
+ isPermissioned: boolean;
37
38
  /**
38
39
  * Loads the controller account for this stablecoin.
39
40
  *
@@ -216,4 +217,20 @@ export declare abstract class Stablecoin<T extends Controller> {
216
217
  * @returns Promise resolving to the USD exchange rate as a number
217
218
  */
218
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>;
219
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;
@@ -45,7 +45,7 @@ export declare class UsdcPlusStablecoin extends Stablecoin<DriftUsdcController>
45
45
  * @param signer - Public key of the signer
46
46
  * @returns Promise resolving to the constructed accounts object of type T
47
47
  */
48
- constructAccounts<T>(signer: PublicKey): Promise<T>;
48
+ constructAccounts<T>(signer: PublicKey, permissions?: boolean): Promise<T>;
49
49
  /**
50
50
  * Constructs remaining accounts for transactions.
51
51
  * Currently returns an empty array as no additional accounts are needed.
@@ -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
  }
@@ -49,6 +49,7 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
49
49
  wallet: new nodewallet_1.default(web3_js_1.Keypair.generate()),
50
50
  authority: this.controllerKey
51
51
  });
52
+ this.isPermissioned = true;
52
53
  // Otherwise has to be loaded first.
53
54
  if (stablecoinMintOverride)
54
55
  this.stablecoinMint = stablecoinMintOverride;
@@ -135,8 +136,8 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
135
136
  * @param signer - Public key of the signer
136
137
  * @returns Promise resolving to the constructed accounts object of type T
137
138
  */
138
- constructAccounts(signer) {
139
- return __awaiter(this, void 0, void 0, function* () {
139
+ constructAccounts(signer_1) {
140
+ return __awaiter(this, arguments, void 0, function* (signer, permissions = this.isPermissioned) {
140
141
  const userAccount = (0, sdk_1.getUserAccountPublicKeySync)(constants_1.DRIFT_PROGRAM_ID, this.controllerKey);
141
142
  const userStats = (0, sdk_1.getUserStatsAccountPublicKey)(constants_1.DRIFT_PROGRAM_ID, this.controllerKey);
142
143
  const anchorRemainingAccounts = yield this.constructRemainingAccounts();
@@ -165,7 +166,7 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
165
166
  userReceiptAta,
166
167
  userStats,
167
168
  userUsdcAta,
168
- adminPermissions: null,
169
+ adminPermissions: permissions ? classes_1.PdaClient.derivePermissions(signer) : null,
169
170
  anchorRemainingAccounts,
170
171
  systemProgram: web3_js_1.SystemProgram.programId,
171
172
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
@@ -208,7 +209,7 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
208
209
  */
209
210
  mint(signer, amount, minimumReceived) {
210
211
  return __awaiter(this, void 0, void 0, function* () {
211
- const accounts = yield this.constructAccounts(signer);
212
+ const accounts = yield this.constructAccounts(signer, true);
212
213
  const ix = (0, reflect_main_1.createMintDriftS1Instruction)(accounts, {
213
214
  minUsdcAmount: minimumReceived,
214
215
  usdcAmount: amount
@@ -224,7 +225,7 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
224
225
  */
225
226
  redeem(signer, amount, minimumReceived) {
226
227
  return __awaiter(this, void 0, void 0, function* () {
227
- const accounts = yield this.constructAccounts(signer);
228
+ const accounts = yield this.constructAccounts(signer, true);
228
229
  const ix = (0, reflect_main_1.createRedeemDriftS1Instruction)(accounts, {
229
230
  canChill: false,
230
231
  minLstRedeem: minimumReceived,
@@ -280,5 +281,91 @@ class UsdcPlusStablecoin extends Stablecoin_1.Stablecoin {
280
281
  return this.getBaseUsdExchangeRate();
281
282
  });
282
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
+ }
283
370
  }
284
371
  exports.UsdcPlusStablecoin = UsdcPlusStablecoin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reflectmoney/stable.ts",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "stablecoinjesus @ Palindrome Engineering",
6
6
  "repository": {