@wireio/stake 0.7.1 → 0.7.2

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.
package/lib/stake.d.ts CHANGED
@@ -9162,7 +9162,7 @@ declare class SolanaStakingClient implements IStakingClient {
9162
9162
  * in the wallet so that a MAX deposit (balance - buffer) will succeed.
9163
9163
  *
9164
9164
  * It accounts for:
9165
- * - Runtime tx fees (via a dummy self-transfer)
9165
+ * - Runtime tx fees (via cached dummy self-transfer fee)
9166
9166
  * - Protocol deposit fee (via getDepositFee(amount))
9167
9167
  *
9168
9168
  * Intended UI usage:
@@ -9176,6 +9176,9 @@ declare class SolanaStakingClient implements IStakingClient {
9176
9176
  minBufferLamports?: bigint;
9177
9177
  balanceOverrideLamports?: bigint;
9178
9178
  }): Promise<bigint>;
9179
+ private cachedTxFee?;
9180
+ private static readonly FEE_CACHE_TTL_MS;
9181
+ private getSingleTxFeeLamports;
9179
9182
  /**
9180
9183
  * Send a signed transaction over HTTP RPC and wait for confirmation.
9181
9184
  * Throws if the transaction fails.
package/lib/stake.js CHANGED
@@ -9531,7 +9531,7 @@ const _SolanaStakingClient = class _SolanaStakingClient {
9531
9531
  }
9532
9532
  getDepositBuffer(options) {
9533
9533
  return __async$8(this, null, function* () {
9534
- var _a, _b, _c, _d, _e;
9534
+ var _a, _b, _c, _d;
9535
9535
  this.ensureUser();
9536
9536
  const payer = this.solPubKey;
9537
9537
  const balanceLamports = (_a = options == null ? void 0 : options.balanceOverrideLamports) != null ? _a : BigInt(yield this.connection.getBalance(payer, commitment));
@@ -9540,29 +9540,13 @@ const _SolanaStakingClient = class _SolanaStakingClient {
9540
9540
  }
9541
9541
  let gasBuffer = BigInt(0);
9542
9542
  try {
9543
- const dummyIx = web3_js.SystemProgram.transfer({
9544
- fromPubkey: payer,
9545
- toPubkey: payer,
9546
- lamports: 0
9547
- });
9548
- const tx = new web3_js.Transaction().add(dummyIx);
9549
- const { blockhash } = yield this.connection.getLatestBlockhash(
9550
- commitment
9551
- );
9552
- tx.recentBlockhash = blockhash;
9553
- tx.feePayer = payer;
9554
- const message = tx.compileMessage();
9555
- const feeInfo = yield this.connection.getFeeForMessage(
9556
- message,
9557
- commitment
9558
- );
9559
- const singleTxFeeLamports = BigInt((_b = feeInfo.value) != null ? _b : 5e3);
9560
- const txCount = BigInt((_c = options == null ? void 0 : options.txCount) != null ? _c : 2);
9561
- const safetyMultiplier = (_d = options == null ? void 0 : options.safetyMultiplier) != null ? _d : 3;
9543
+ const singleTxFeeLamports = yield this.getSingleTxFeeLamports();
9544
+ const txCount = BigInt((_b = options == null ? void 0 : options.txCount) != null ? _b : 2);
9545
+ const safetyMultiplier = (_c = options == null ? void 0 : options.safetyMultiplier) != null ? _c : 3;
9562
9546
  const safetyScaled = BigInt(Math.round(safetyMultiplier * 100));
9563
9547
  let buf = singleTxFeeLamports * txCount * safetyScaled / BigInt(100);
9564
9548
  const defaultMinBufferLamports = BigInt(1e7);
9565
- const minBufferLamports = (_e = options == null ? void 0 : options.minBufferLamports) != null ? _e : defaultMinBufferLamports;
9549
+ const minBufferLamports = (_d = options == null ? void 0 : options.minBufferLamports) != null ? _d : defaultMinBufferLamports;
9566
9550
  if (buf < minBufferLamports) {
9567
9551
  buf = minBufferLamports;
9568
9552
  }
@@ -9577,24 +9561,24 @@ const _SolanaStakingClient = class _SolanaStakingClient {
9577
9561
  if (typeof this.getDepositFee !== "function") {
9578
9562
  return gasBuffer;
9579
9563
  }
9580
- let lo = BigInt(0);
9581
- let hi = spendable;
9582
- for (let i = 0; i < 20 && hi - lo > BigInt(1); i++) {
9583
- const mid = (lo + hi) / BigInt(2);
9584
- let fee;
9585
- try {
9586
- fee = yield this.getDepositFee(mid);
9587
- } catch (e) {
9588
- return gasBuffer;
9589
- }
9590
- if (mid + fee <= spendable) {
9591
- lo = mid;
9592
- } else {
9593
- hi = mid;
9594
- }
9564
+ let feeAtSpendable;
9565
+ try {
9566
+ feeAtSpendable = yield this.getDepositFee(spendable);
9567
+ } catch (e) {
9568
+ return gasBuffer;
9569
+ }
9570
+ if (feeAtSpendable <= BigInt(0)) {
9571
+ return gasBuffer;
9595
9572
  }
9596
- const fudge = BigInt(1e3);
9597
- let effectivePrincipal = lo > fudge ? lo - fudge : lo;
9573
+ const s = spendable;
9574
+ const f = feeAtSpendable;
9575
+ const denom = s + f;
9576
+ if (denom === BigInt(0)) {
9577
+ return gasBuffer;
9578
+ }
9579
+ let a = s * s / denom;
9580
+ const fudge = BigInt(1e4);
9581
+ let effectivePrincipal = a > fudge ? a - fudge : a;
9598
9582
  if (effectivePrincipal < BigInt(0)) {
9599
9583
  effectivePrincipal = BigInt(0);
9600
9584
  }
@@ -9602,6 +9586,33 @@ const _SolanaStakingClient = class _SolanaStakingClient {
9602
9586
  return buffer < gasBuffer ? gasBuffer : buffer;
9603
9587
  });
9604
9588
  }
9589
+ getSingleTxFeeLamports() {
9590
+ return __async$8(this, null, function* () {
9591
+ var _a;
9592
+ const now = Date.now();
9593
+ if (this.cachedTxFee && now - this.cachedTxFee.fetchedAt < _SolanaStakingClient.FEE_CACHE_TTL_MS) {
9594
+ return this.cachedTxFee.value;
9595
+ }
9596
+ const payer = this.solPubKey;
9597
+ const dummyIx = web3_js.SystemProgram.transfer({
9598
+ fromPubkey: payer,
9599
+ toPubkey: payer,
9600
+ lamports: 0
9601
+ });
9602
+ const tx = new web3_js.Transaction().add(dummyIx);
9603
+ const { blockhash } = yield this.connection.getLatestBlockhash(commitment);
9604
+ tx.recentBlockhash = blockhash;
9605
+ tx.feePayer = payer;
9606
+ const message = tx.compileMessage();
9607
+ const feeInfo = yield this.connection.getFeeForMessage(message, commitment);
9608
+ const singleTxFeeLamports = BigInt((_a = feeInfo.value) != null ? _a : 5e3);
9609
+ this.cachedTxFee = {
9610
+ value: singleTxFeeLamports,
9611
+ fetchedAt: now
9612
+ };
9613
+ return singleTxFeeLamports;
9614
+ });
9615
+ }
9605
9616
  sendAndConfirmHttp(signed, ctx) {
9606
9617
  return __async$8(this, null, function* () {
9607
9618
  this.ensureUser();
@@ -9661,6 +9672,7 @@ const _SolanaStakingClient = class _SolanaStakingClient {
9661
9672
  }
9662
9673
  };
9663
9674
  _SolanaStakingClient.EPOCHS_PER_YEAR_TTL_MS = 10 * 60 * 1e3;
9675
+ _SolanaStakingClient.FEE_CACHE_TTL_MS = 6e4;
9664
9676
  let SolanaStakingClient = _SolanaStakingClient;
9665
9677
 
9666
9678
  var _format$q = "hh-sol-artifact-1";