@ignitionfi/spl-stake-pool 1.1.19 → 1.1.21

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.
@@ -22264,7 +22264,8 @@ var solanaStakePool = (function (exports, node_buffer) {
22264
22264
  index: 24,
22265
22265
  layout: LayoutExports$1.struct([
22266
22266
  LayoutExports$1.u8('instruction'),
22267
- LayoutExports$1.ns64('lamports'),
22267
+ LayoutExports$1.ns64('poolTokensIn'),
22268
+ LayoutExports$1.ns64('minimumLamportsOut'),
22268
22269
  ]),
22269
22270
  },
22270
22271
  DepositSolWithSlippage: {
@@ -22297,6 +22298,14 @@ var solanaStakePool = (function (exports, node_buffer) {
22297
22298
  LayoutExports$1.ns64('minimumLamportsOut'),
22298
22299
  ]),
22299
22300
  },
22301
+ WithdrawStakeWithSession: {
22302
+ index: 29,
22303
+ layout: LayoutExports$1.struct([
22304
+ LayoutExports$1.u8('instruction'),
22305
+ LayoutExports$1.ns64('poolTokensIn'),
22306
+ LayoutExports$1.ns64('minimumLamportsOut'),
22307
+ ]),
22308
+ },
22300
22309
  });
22301
22310
  /**
22302
22311
  * Stake Pool Instruction class
@@ -22732,6 +22741,7 @@ var solanaStakePool = (function (exports, node_buffer) {
22732
22741
  * Creates a transaction instruction to withdraw WSOL from a stake pool using a session.
22733
22742
  */
22734
22743
  static withdrawWsolWithSession(params) {
22744
+ var _a;
22735
22745
  const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawWsolWithSession;
22736
22746
  const data = encodeData(type, {
22737
22747
  poolTokensIn: params.poolTokensIn,
@@ -22752,6 +22762,9 @@ var solanaStakePool = (function (exports, node_buffer) {
22752
22762
  { pubkey: params.tokenProgramId, isSigner: false, isWritable: false },
22753
22763
  { pubkey: params.wsolMint, isSigner: false, isWritable: false },
22754
22764
  { pubkey: params.programSigner, isSigner: false, isWritable: true },
22765
+ { pubkey: (_a = params.payer) !== null && _a !== void 0 ? _a : params.userTransferAuthority, isSigner: true, isWritable: true },
22766
+ { pubkey: params.userWallet, isSigner: false, isWritable: false },
22767
+ { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
22755
22768
  ];
22756
22769
  if (params.solWithdrawAuthority) {
22757
22770
  keys.push({
@@ -22760,6 +22773,39 @@ var solanaStakePool = (function (exports, node_buffer) {
22760
22773
  isWritable: false,
22761
22774
  });
22762
22775
  }
22776
+ // Associated Token Program must be last - only needed in transaction for CPI routing
22777
+ keys.push({ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false });
22778
+ return new TransactionInstruction({
22779
+ programId: params.programId,
22780
+ keys,
22781
+ data,
22782
+ });
22783
+ }
22784
+ /**
22785
+ * Creates a transaction instruction to withdraw stake from a stake pool using a Fogo session.
22786
+ */
22787
+ static withdrawStakeWithSession(params) {
22788
+ const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawStakeWithSession;
22789
+ const data = encodeData(type, {
22790
+ poolTokensIn: params.poolTokensIn,
22791
+ minimumLamportsOut: params.minimumLamportsOut,
22792
+ });
22793
+ const keys = [
22794
+ { pubkey: params.stakePool, isSigner: false, isWritable: true },
22795
+ { pubkey: params.validatorList, isSigner: false, isWritable: true },
22796
+ { pubkey: params.withdrawAuthority, isSigner: false, isWritable: false },
22797
+ { pubkey: params.stakeToSplit, isSigner: false, isWritable: true },
22798
+ { pubkey: params.stakeToReceive, isSigner: false, isWritable: true },
22799
+ { pubkey: params.sessionSigner, isSigner: true, isWritable: false }, // user_stake_authority_info (signer_or_session)
22800
+ { pubkey: params.sessionSigner, isSigner: false, isWritable: false }, // user_transfer_authority_info (not used in session path)
22801
+ { pubkey: params.burnFromPool, isSigner: false, isWritable: true },
22802
+ { pubkey: params.managerFeeAccount, isSigner: false, isWritable: true },
22803
+ { pubkey: params.poolMint, isSigner: false, isWritable: true },
22804
+ { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
22805
+ { pubkey: params.tokenProgramId, isSigner: false, isWritable: false },
22806
+ { pubkey: StakeProgram.programId, isSigner: false, isWritable: false },
22807
+ { pubkey: params.programSigner, isSigner: false, isWritable: false },
22808
+ ];
22763
22809
  return new TransactionInstruction({
22764
22810
  programId: params.programId,
22765
22811
  keys,
@@ -23333,7 +23379,7 @@ var solanaStakePool = (function (exports, node_buffer) {
23333
23379
  /**
23334
23380
  * Creates instructions required to withdraw wSOL from a stake pool.
23335
23381
  */
23336
- async function withdrawWsolWithSession(connection, stakePoolAddress, signerOrSession, userPubkey, amount, minimumLamportsOut = 0, solWithdrawAuthority) {
23382
+ async function withdrawWsolWithSession(connection, stakePoolAddress, signerOrSession, userPubkey, amount, minimumLamportsOut = 0, solWithdrawAuthority, payer) {
23337
23383
  const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
23338
23384
  const stakePoolProgramId = getStakePoolProgramId(connection.rpcEndpoint);
23339
23385
  const stakePool = stakePoolAccount.account.data;
@@ -23347,7 +23393,8 @@ var solanaStakePool = (function (exports, node_buffer) {
23347
23393
  const userWsolAccount = getAssociatedTokenAddressSync(NATIVE_MINT, userPubkey);
23348
23394
  const instructions = [];
23349
23395
  const signers = [];
23350
- instructions.push(createAssociatedTokenAccountIdempotentInstruction(signerOrSession, userWsolAccount, userPubkey, NATIVE_MINT));
23396
+ // The program handles wSOL ATA creation internally
23397
+ // This prevents rent drain attacks where paymaster pays for ATA and user reclaims rent
23351
23398
  const [programSigner] = PublicKey.findProgramAddressSync([Buffer.from('fogo_session_program_signer')], stakePoolProgramId);
23352
23399
  const withdrawAuthority = await findWithdrawAuthorityProgramAddress(stakePoolProgramId, stakePoolAddress);
23353
23400
  instructions.push(StakePoolInstruction.withdrawWsolWithSession({
@@ -23364,6 +23411,8 @@ var solanaStakePool = (function (exports, node_buffer) {
23364
23411
  solWithdrawAuthority,
23365
23412
  wsolMint: NATIVE_MINT,
23366
23413
  programSigner,
23414
+ userWallet: userPubkey,
23415
+ payer,
23367
23416
  poolTokensIn: poolTokens,
23368
23417
  minimumLamportsOut,
23369
23418
  }));
@@ -23372,6 +23421,106 @@ var solanaStakePool = (function (exports, node_buffer) {
23372
23421
  signers,
23373
23422
  };
23374
23423
  }
23424
+ /**
23425
+ * Creates instructions required to withdraw stake from a stake pool using a Fogo session.
23426
+ * The withdrawn stake account will be authorized to the user's wallet.
23427
+ */
23428
+ async function withdrawStakeWithSession(connection, stakePoolAddress, signerOrSession, userPubkey, amount, useReserve = false, voteAccountAddress, minimumLamportsOut = 0, payer, validatorComparator) {
23429
+ const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
23430
+ const stakePoolProgramId = getStakePoolProgramId(connection.rpcEndpoint);
23431
+ const stakePool = stakePoolAccount.account.data;
23432
+ const poolTokens = solToLamports(amount);
23433
+ const poolAmount = new BN(poolTokens);
23434
+ const poolTokenAccount = getAssociatedTokenAddressSync(stakePool.poolMint, userPubkey);
23435
+ const tokenAccount = await getAccount(connection, poolTokenAccount);
23436
+ if (tokenAccount.amount < poolTokens) {
23437
+ throw new Error(`Not enough token balance to withdraw ${amount} pool tokens.
23438
+ Maximum withdraw amount is ${lamportsToSol(tokenAccount.amount)} pool tokens.`);
23439
+ }
23440
+ const [programSigner] = PublicKey.findProgramAddressSync([Buffer.from('fogo_session_program_signer')], stakePoolProgramId);
23441
+ const withdrawAuthority = await findWithdrawAuthorityProgramAddress(stakePoolProgramId, stakePoolAddress);
23442
+ const stakeAccountRentExemption = await connection.getMinimumBalanceForRentExemption(StakeProgram.space);
23443
+ // Determine which stake accounts to withdraw from
23444
+ const withdrawAccounts = [];
23445
+ if (useReserve) {
23446
+ withdrawAccounts.push({
23447
+ stakeAddress: stakePool.reserveStake,
23448
+ voteAddress: undefined,
23449
+ poolAmount,
23450
+ });
23451
+ }
23452
+ else if (voteAccountAddress) {
23453
+ const stakeAccountAddress = await findStakeProgramAddress(stakePoolProgramId, voteAccountAddress, stakePoolAddress);
23454
+ const stakeAccount = await connection.getAccountInfo(stakeAccountAddress);
23455
+ if (!stakeAccount) {
23456
+ throw new Error(`Validator stake account not found for vote address ${voteAccountAddress.toBase58()}`);
23457
+ }
23458
+ const availableLamports = new BN(stakeAccount.lamports - MINIMUM_ACTIVE_STAKE - stakeAccountRentExemption);
23459
+ if (availableLamports.lt(new BN(0))) {
23460
+ throw new Error('Invalid Stake Account');
23461
+ }
23462
+ const availableForWithdrawal = calcLamportsWithdrawAmount(stakePool, availableLamports);
23463
+ if (availableForWithdrawal.lt(poolAmount)) {
23464
+ throw new Error(`Not enough lamports available for withdrawal from ${stakeAccountAddress},
23465
+ ${poolAmount} asked, ${availableForWithdrawal} available.`);
23466
+ }
23467
+ withdrawAccounts.push({
23468
+ stakeAddress: stakeAccountAddress,
23469
+ voteAddress: voteAccountAddress,
23470
+ poolAmount,
23471
+ });
23472
+ }
23473
+ else {
23474
+ // Get the list of accounts to withdraw from automatically
23475
+ withdrawAccounts.push(...(await prepareWithdrawAccounts(connection, stakePool, stakePoolAddress, poolAmount, validatorComparator, poolTokenAccount.equals(stakePool.managerFeeAccount))));
23476
+ }
23477
+ const instructions = [];
23478
+ const signers = [];
23479
+ const stakeAccountPubkeys = [];
23480
+ // Max 5 accounts to prevent an error: "Transaction too large"
23481
+ const maxWithdrawAccounts = 5;
23482
+ let i = 0;
23483
+ for (const withdrawAccount of withdrawAccounts) {
23484
+ if (i >= maxWithdrawAccounts) {
23485
+ break;
23486
+ }
23487
+ // Create a new stake account to receive the withdrawal
23488
+ const stakeReceiver = Keypair.generate();
23489
+ signers.push(stakeReceiver);
23490
+ stakeAccountPubkeys.push(stakeReceiver.publicKey);
23491
+ // Create the stake account that will receive the split stake
23492
+ instructions.push(SystemProgram.createAccount({
23493
+ fromPubkey: payer !== null && payer !== void 0 ? payer : userPubkey,
23494
+ newAccountPubkey: stakeReceiver.publicKey,
23495
+ lamports: stakeAccountRentExemption,
23496
+ space: StakeProgram.space,
23497
+ programId: StakeProgram.programId,
23498
+ }));
23499
+ // Add the withdraw stake with session instruction
23500
+ instructions.push(StakePoolInstruction.withdrawStakeWithSession({
23501
+ programId: stakePoolProgramId,
23502
+ stakePool: stakePoolAddress,
23503
+ validatorList: stakePool.validatorList,
23504
+ withdrawAuthority,
23505
+ stakeToSplit: withdrawAccount.stakeAddress,
23506
+ stakeToReceive: stakeReceiver.publicKey,
23507
+ sessionSigner: signerOrSession,
23508
+ burnFromPool: poolTokenAccount,
23509
+ managerFeeAccount: stakePool.managerFeeAccount,
23510
+ poolMint: stakePool.poolMint,
23511
+ tokenProgramId: stakePool.tokenProgramId,
23512
+ programSigner,
23513
+ poolTokensIn: withdrawAccount.poolAmount.toNumber(),
23514
+ minimumLamportsOut: solToLamports(minimumLamportsOut),
23515
+ }));
23516
+ i++;
23517
+ }
23518
+ return {
23519
+ instructions,
23520
+ signers,
23521
+ stakeAccountPubkeys,
23522
+ };
23523
+ }
23375
23524
  async function addValidatorToPool(connection, stakePoolAddress, validatorVote, seed) {
23376
23525
  const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
23377
23526
  const stakePoolProgramId = getStakePoolProgramId(connection.rpcEndpoint);
@@ -23759,6 +23908,7 @@ var solanaStakePool = (function (exports, node_buffer) {
23759
23908
  exports.updateStakePool = updateStakePool;
23760
23909
  exports.withdrawSol = withdrawSol;
23761
23910
  exports.withdrawStake = withdrawStake;
23911
+ exports.withdrawStakeWithSession = withdrawStakeWithSession;
23762
23912
  exports.withdrawWsolWithSession = withdrawWsolWithSession;
23763
23913
 
23764
23914
  return exports;