depositStake static method

Future<List<TransactionInstruction>> depositStake({
  1. required SolanaRPC rpc,
  2. required SolAddress stakePoolAddress,
  3. required SolAddress authorizedPubkey,
  4. required SolAddress validatorVote,
  5. required SolAddress depositStake,
  6. required SolAddress? poolTokenReceiverAccount,
})

Creates instructions required to deposit stake to stake pool.

Implementation

static Future<List<TransactionInstruction>> depositStake({
  required SolanaRPC rpc,
  required SolAddress stakePoolAddress,
  required SolAddress authorizedPubkey,
  required SolAddress validatorVote,
  required SolAddress depositStake,
  required SolAddress? poolTokenReceiverAccount,
}) async {
  final stakePool = await rpc
      .request(SolanaRPCGetStakePoolAccount(address: stakePoolAddress));
  if (stakePool == null) {
    throw const MessageException("Stake pool account does not found.");
  }
  final withdrawAuthority =
      StakePoolProgramUtils.findWithdrawAuthorityProgramAddress(
          stakePoolAddress: stakePoolAddress);
  final validatorStake = StakePoolProgramUtils.findStakeProgramAddress(
      voteAccountAddress: validatorVote, stakePoolAddress: stakePoolAddress);
  final instructions = <TransactionInstruction>[];
  final poolMint = stakePool.poolMint;
  // Create token account if not specified
  if (poolTokenReceiverAccount == null) {
    final associatedAddress =
        AssociatedTokenAccountProgramUtils.associatedTokenAccount(
            mint: poolMint, owner: authorizedPubkey);
    instructions.add(
        AssociatedTokenAccountProgram.associatedTokenAccountIdempotent(
            payer: authorizedPubkey,
            associatedToken: associatedAddress.address,
            owner: authorizedPubkey,
            mint: poolMint));
    poolTokenReceiverAccount = associatedAddress.address;
  }
  instructions.add(StakeProgram.authorize(
    stakePubkey: depositStake,
    authorizedPubkey: authorizedPubkey,
    layout: StakeAuthorizeLayout(
        newAuthorized: stakePool.stakeDepositAuthority,
        stakeAuthorizationType: 0),
  ));

  instructions.add(StakeProgram.authorize(
    stakePubkey: depositStake,
    authorizedPubkey: authorizedPubkey,
    layout: StakeAuthorizeLayout(
        newAuthorized: stakePool.stakeDepositAuthority,
        stakeAuthorizationType: 1),
  ));

  instructions.add(StakePoolProgram.depositStake(
    stakePool: stakePoolAddress,
    validatorList: stakePool.validatorList,
    depositAuthority: stakePool.stakeDepositAuthority,
    reserveStake: stakePool.reserveStake,
    managerFeeAccount: stakePool.managerFeeAccount,
    referralPoolAccount: poolTokenReceiverAccount,
    destinationPoolAccount: poolTokenReceiverAccount,
    withdrawAuthority: withdrawAuthority.address,
    depositStake: depositStake,
    validatorStake: validatorStake.address,
    poolMint: poolMint,
  ));

  return instructions;
}