depositSol static method

Future<List<TransactionInstruction>> depositSol({
  1. required SolanaRPC connection,
  2. required SolAddress stakePoolAddress,
  3. required SolAddress from,
  4. required SolAddress userSolTransfer,
  5. required BigInt lamports,
  6. SolAddress? destinationTokenAccount,
  7. SolAddress? referrerTokenAccount,
  8. required SolAddress depositAuthority,
})

Creates instructions required to deposit sol to stake pool.

Implementation

static Future<List<TransactionInstruction>> depositSol({
  required SolanaRPC connection,
  required SolAddress stakePoolAddress,
  required SolAddress from,
  required SolAddress userSolTransfer,
  required BigInt lamports,
  SolAddress? destinationTokenAccount,
  SolAddress? referrerTokenAccount,
  required SolAddress depositAuthority,
}) async {
  final fromBalance =
      await connection.request(SolanaRPCGetBalance(account: from));
  if (fromBalance < lamports) {
    throw const SolanaPluginException('Not enough SOL to deposit into pool.');
  }

  final stakePool = await connection
      .request(SolanaRPCGetStakePoolAccount(address: stakePoolAddress));
  if (stakePool == null) {
    throw const SolanaPluginException("Stake pool account does not found.");
  }

  final instructions = <TransactionInstruction>[];

  // Create the ephemeral SOL account
  instructions.add(SystemProgram.transfer(
    from: from,
    to: userSolTransfer,
    layout: SystemTransferLayout(lamports: lamports),
  ));

  // Create token account if not specified
  if (destinationTokenAccount == null) {
    final associatedAddress =
        AssociatedTokenAccountProgramUtils.associatedTokenAccount(
            mint: stakePool.poolMint, owner: from);
    instructions
        .add(AssociatedTokenAccountProgram.associatedTokenAccountIdempotent(
      payer: from,
      associatedToken: associatedAddress.address,
      owner: from,
      mint: stakePool.poolMint,
    ));
    destinationTokenAccount = associatedAddress.address;
  }

  final withdrawAuthority =
      StakePoolProgramUtils.findWithdrawAuthorityProgramAddress(
          stakePoolAddress: stakePoolAddress);
  instructions.add(StakePoolProgram.depositSol(
    stakePool: stakePoolAddress,
    reserveStake: stakePool.reserveStake,
    fundingAccount: userSolTransfer,
    destinationPoolAccount: destinationTokenAccount,
    managerFeeAccount: stakePool.managerFeeAccount,
    referralPoolAccount: referrerTokenAccount ?? destinationTokenAccount,
    poolMint: stakePool.poolMint,
    layout: StakePoolDepositSolLayout(lamports: lamports),
    withdrawAuthority: withdrawAuthority.address,
    depositAuthority: depositAuthority,
  ));

  return instructions;
}