@ignitionfi/spl-stake-pool 1.1.20 → 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.
package/dist/index.cjs.js CHANGED
@@ -1083,7 +1083,8 @@ const STAKE_POOL_INSTRUCTION_LAYOUTS = Object.freeze({
1083
1083
  index: 24,
1084
1084
  layout: BufferLayout__namespace.struct([
1085
1085
  BufferLayout__namespace.u8('instruction'),
1086
- BufferLayout__namespace.ns64('lamports'),
1086
+ BufferLayout__namespace.ns64('poolTokensIn'),
1087
+ BufferLayout__namespace.ns64('minimumLamportsOut'),
1087
1088
  ]),
1088
1089
  },
1089
1090
  DepositSolWithSlippage: {
@@ -1116,6 +1117,14 @@ const STAKE_POOL_INSTRUCTION_LAYOUTS = Object.freeze({
1116
1117
  BufferLayout__namespace.ns64('minimumLamportsOut'),
1117
1118
  ]),
1118
1119
  },
1120
+ WithdrawStakeWithSession: {
1121
+ index: 29,
1122
+ layout: BufferLayout__namespace.struct([
1123
+ BufferLayout__namespace.u8('instruction'),
1124
+ BufferLayout__namespace.ns64('poolTokensIn'),
1125
+ BufferLayout__namespace.ns64('minimumLamportsOut'),
1126
+ ]),
1127
+ },
1119
1128
  });
1120
1129
  /**
1121
1130
  * Stake Pool Instruction class
@@ -1591,6 +1600,37 @@ class StakePoolInstruction {
1591
1600
  data,
1592
1601
  });
1593
1602
  }
1603
+ /**
1604
+ * Creates a transaction instruction to withdraw stake from a stake pool using a Fogo session.
1605
+ */
1606
+ static withdrawStakeWithSession(params) {
1607
+ const type = STAKE_POOL_INSTRUCTION_LAYOUTS.WithdrawStakeWithSession;
1608
+ const data = encodeData(type, {
1609
+ poolTokensIn: params.poolTokensIn,
1610
+ minimumLamportsOut: params.minimumLamportsOut,
1611
+ });
1612
+ const keys = [
1613
+ { pubkey: params.stakePool, isSigner: false, isWritable: true },
1614
+ { pubkey: params.validatorList, isSigner: false, isWritable: true },
1615
+ { pubkey: params.withdrawAuthority, isSigner: false, isWritable: false },
1616
+ { pubkey: params.stakeToSplit, isSigner: false, isWritable: true },
1617
+ { pubkey: params.stakeToReceive, isSigner: false, isWritable: true },
1618
+ { pubkey: params.sessionSigner, isSigner: true, isWritable: false }, // user_stake_authority_info (signer_or_session)
1619
+ { pubkey: params.sessionSigner, isSigner: false, isWritable: false }, // user_transfer_authority_info (not used in session path)
1620
+ { pubkey: params.burnFromPool, isSigner: false, isWritable: true },
1621
+ { pubkey: params.managerFeeAccount, isSigner: false, isWritable: true },
1622
+ { pubkey: params.poolMint, isSigner: false, isWritable: true },
1623
+ { pubkey: web3_js.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
1624
+ { pubkey: params.tokenProgramId, isSigner: false, isWritable: false },
1625
+ { pubkey: web3_js.StakeProgram.programId, isSigner: false, isWritable: false },
1626
+ { pubkey: params.programSigner, isSigner: false, isWritable: false },
1627
+ ];
1628
+ return new web3_js.TransactionInstruction({
1629
+ programId: params.programId,
1630
+ keys,
1631
+ data,
1632
+ });
1633
+ }
1594
1634
  /**
1595
1635
  * Creates an instruction to create metadata
1596
1636
  * using the mpl token metadata program for the pool token
@@ -2200,6 +2240,106 @@ async function withdrawWsolWithSession(connection, stakePoolAddress, signerOrSes
2200
2240
  signers,
2201
2241
  };
2202
2242
  }
2243
+ /**
2244
+ * Creates instructions required to withdraw stake from a stake pool using a Fogo session.
2245
+ * The withdrawn stake account will be authorized to the user's wallet.
2246
+ */
2247
+ async function withdrawStakeWithSession(connection, stakePoolAddress, signerOrSession, userPubkey, amount, useReserve = false, voteAccountAddress, minimumLamportsOut = 0, payer, validatorComparator) {
2248
+ const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
2249
+ const stakePoolProgramId = getStakePoolProgramId(connection.rpcEndpoint);
2250
+ const stakePool = stakePoolAccount.account.data;
2251
+ const poolTokens = solToLamports(amount);
2252
+ const poolAmount = new BN(poolTokens);
2253
+ const poolTokenAccount = splToken.getAssociatedTokenAddressSync(stakePool.poolMint, userPubkey);
2254
+ const tokenAccount = await splToken.getAccount(connection, poolTokenAccount);
2255
+ if (tokenAccount.amount < poolTokens) {
2256
+ throw new Error(`Not enough token balance to withdraw ${amount} pool tokens.
2257
+ Maximum withdraw amount is ${lamportsToSol(tokenAccount.amount)} pool tokens.`);
2258
+ }
2259
+ const [programSigner] = web3_js.PublicKey.findProgramAddressSync([Buffer.from('fogo_session_program_signer')], stakePoolProgramId);
2260
+ const withdrawAuthority = await findWithdrawAuthorityProgramAddress(stakePoolProgramId, stakePoolAddress);
2261
+ const stakeAccountRentExemption = await connection.getMinimumBalanceForRentExemption(web3_js.StakeProgram.space);
2262
+ // Determine which stake accounts to withdraw from
2263
+ const withdrawAccounts = [];
2264
+ if (useReserve) {
2265
+ withdrawAccounts.push({
2266
+ stakeAddress: stakePool.reserveStake,
2267
+ voteAddress: undefined,
2268
+ poolAmount,
2269
+ });
2270
+ }
2271
+ else if (voteAccountAddress) {
2272
+ const stakeAccountAddress = await findStakeProgramAddress(stakePoolProgramId, voteAccountAddress, stakePoolAddress);
2273
+ const stakeAccount = await connection.getAccountInfo(stakeAccountAddress);
2274
+ if (!stakeAccount) {
2275
+ throw new Error(`Validator stake account not found for vote address ${voteAccountAddress.toBase58()}`);
2276
+ }
2277
+ const availableLamports = new BN(stakeAccount.lamports - MINIMUM_ACTIVE_STAKE - stakeAccountRentExemption);
2278
+ if (availableLamports.lt(new BN(0))) {
2279
+ throw new Error('Invalid Stake Account');
2280
+ }
2281
+ const availableForWithdrawal = calcLamportsWithdrawAmount(stakePool, availableLamports);
2282
+ if (availableForWithdrawal.lt(poolAmount)) {
2283
+ throw new Error(`Not enough lamports available for withdrawal from ${stakeAccountAddress},
2284
+ ${poolAmount} asked, ${availableForWithdrawal} available.`);
2285
+ }
2286
+ withdrawAccounts.push({
2287
+ stakeAddress: stakeAccountAddress,
2288
+ voteAddress: voteAccountAddress,
2289
+ poolAmount,
2290
+ });
2291
+ }
2292
+ else {
2293
+ // Get the list of accounts to withdraw from automatically
2294
+ withdrawAccounts.push(...(await prepareWithdrawAccounts(connection, stakePool, stakePoolAddress, poolAmount, validatorComparator, poolTokenAccount.equals(stakePool.managerFeeAccount))));
2295
+ }
2296
+ const instructions = [];
2297
+ const signers = [];
2298
+ const stakeAccountPubkeys = [];
2299
+ // Max 5 accounts to prevent an error: "Transaction too large"
2300
+ const maxWithdrawAccounts = 5;
2301
+ let i = 0;
2302
+ for (const withdrawAccount of withdrawAccounts) {
2303
+ if (i >= maxWithdrawAccounts) {
2304
+ break;
2305
+ }
2306
+ // Create a new stake account to receive the withdrawal
2307
+ const stakeReceiver = web3_js.Keypair.generate();
2308
+ signers.push(stakeReceiver);
2309
+ stakeAccountPubkeys.push(stakeReceiver.publicKey);
2310
+ // Create the stake account that will receive the split stake
2311
+ instructions.push(web3_js.SystemProgram.createAccount({
2312
+ fromPubkey: payer !== null && payer !== void 0 ? payer : userPubkey,
2313
+ newAccountPubkey: stakeReceiver.publicKey,
2314
+ lamports: stakeAccountRentExemption,
2315
+ space: web3_js.StakeProgram.space,
2316
+ programId: web3_js.StakeProgram.programId,
2317
+ }));
2318
+ // Add the withdraw stake with session instruction
2319
+ instructions.push(StakePoolInstruction.withdrawStakeWithSession({
2320
+ programId: stakePoolProgramId,
2321
+ stakePool: stakePoolAddress,
2322
+ validatorList: stakePool.validatorList,
2323
+ withdrawAuthority,
2324
+ stakeToSplit: withdrawAccount.stakeAddress,
2325
+ stakeToReceive: stakeReceiver.publicKey,
2326
+ sessionSigner: signerOrSession,
2327
+ burnFromPool: poolTokenAccount,
2328
+ managerFeeAccount: stakePool.managerFeeAccount,
2329
+ poolMint: stakePool.poolMint,
2330
+ tokenProgramId: stakePool.tokenProgramId,
2331
+ programSigner,
2332
+ poolTokensIn: withdrawAccount.poolAmount.toNumber(),
2333
+ minimumLamportsOut: solToLamports(minimumLamportsOut),
2334
+ }));
2335
+ i++;
2336
+ }
2337
+ return {
2338
+ instructions,
2339
+ signers,
2340
+ stakeAccountPubkeys,
2341
+ };
2342
+ }
2203
2343
  async function addValidatorToPool(connection, stakePoolAddress, validatorVote, seed) {
2204
2344
  const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
2205
2345
  const stakePoolProgramId = getStakePoolProgramId(connection.rpcEndpoint);
@@ -2587,5 +2727,6 @@ exports.updatePoolTokenMetadata = updatePoolTokenMetadata;
2587
2727
  exports.updateStakePool = updateStakePool;
2588
2728
  exports.withdrawSol = withdrawSol;
2589
2729
  exports.withdrawStake = withdrawStake;
2730
+ exports.withdrawStakeWithSession = withdrawStakeWithSession;
2590
2731
  exports.withdrawWsolWithSession = withdrawWsolWithSession;
2591
2732
  //# sourceMappingURL=index.cjs.js.map