updateStakePool static method

Future<(List<TransactionInstruction>, List<TransactionInstruction>)> updateStakePool(
  1. SolanaProvider connection,
  2. StakePoolAccount stakePool, {
  3. bool noMerge = false,
})

Creates instructions required to completely update a stake pool after epoch change.

Implementation

static Future<(List<TransactionInstruction>, List<TransactionInstruction>)>
updateStakePool(
  SolanaProvider connection,
  StakePoolAccount stakePool, {
  bool noMerge = false,
}) async {
  final stakePoolAddress = stakePool.address;

  final validatorList = await connection.request(
    SolanaRPCGetStakePoolValidatorListAccount(
      address: stakePool.validatorList.address,
    ),
  );
  if (validatorList == null) {
    throw const SolanaPluginException(
      'Validator list account does not found.',
    );
  }
  final withdrawAuthority =
      StakePoolProgramUtils.findWithdrawAuthorityProgramAddress(
        stakePoolAddress: stakePoolAddress,
      );

  final updateListInstructions = <TransactionInstruction>[];
  final instructions = <TransactionInstruction>[];

  int startIndex = 0;
  final List<List<ValidatorStakeInfo>> validatorChunks = _arrayChunk(
    validatorList.validators,
    5,
  );

  for (final validatorChunk in validatorChunks) {
    final List<SolAddress> validatorAndTransientStakePairs = [];

    for (final validator in validatorChunk) {
      final validatorStake = StakePoolProgramUtils.findStakeProgramAddress(
        voteAccountAddress: validator.voteAccountAddress,
        stakePoolAddress: stakePoolAddress,
      );
      validatorAndTransientStakePairs.add(validatorStake.address);
      final transientStake =
          StakePoolProgramUtils.findTransientStakeProgramAddress(
            voteAccountAddress: validator.voteAccountAddress,
            stakePoolAddress: stakePoolAddress,
            seed: validator.transientSeedSuffixStart,
          );
      validatorAndTransientStakePairs.add(transientStake.address);
    }
    updateListInstructions.add(
      StakePoolProgram.updateValidatorListBalance(
        stakePool: stakePoolAddress,
        validatorList: stakePool.validatorList,
        reserveStake: stakePool.reserveStake,
        validatorAndTransientStakePairs: validatorAndTransientStakePairs,
        withdrawAuthority: withdrawAuthority.address,
        layout: StakePoolUpdateValidatorListBalanceLayout(
          startIndex: startIndex,
          noMerge: noMerge,
        ),
      ),
    );
    startIndex += 5;
  }

  instructions.add(
    StakePoolProgram.updateStakePoolBalance(
      stakePool: stakePoolAddress,
      validatorList: stakePool.validatorList,
      reserveStake: stakePool.reserveStake,
      managerFeeAccount: stakePool.managerFeeAccount,
      poolMint: stakePool.poolMint,
      withdrawAuthority: withdrawAuthority.address,
    ),
  );

  instructions.add(
    StakePoolProgram.cleanupRemovedValidatorEntries(
      stakePool: stakePoolAddress,
      validatorList: stakePool.validatorList,
    ),
  );
  return (updateListInstructions, instructions);
}