init_rent_vault.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use borsh::BorshDeserialize;
  2. use pda_rent_payer_api::prelude::*;
  3. use solana_program::msg;
  4. use steel::*;
  5. pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
  6. // Parse args
  7. let args = InitializeRentVault::try_from_slice(data)?;
  8. // Load and validate accounts.
  9. let [payer_info, rent_vault_info, system_program] = accounts else {
  10. return Err(ProgramError::NotEnoughAccountKeys);
  11. };
  12. payer_info.is_signer()?;
  13. rent_vault_info
  14. .is_empty()?
  15. .is_writable()?
  16. .has_seeds(&[RENT_VAULT], &pda_rent_payer_api::ID)?;
  17. system_program.is_program(&system_program::ID)?;
  18. // Initialize vault.
  19. create_account::<RentVault>(
  20. rent_vault_info,
  21. system_program,
  22. payer_info,
  23. &pda_rent_payer_api::ID,
  24. &[RENT_VAULT],
  25. )?;
  26. let (_, bump) = rent_vault_pda();
  27. // Get account to see if it's created
  28. let _vault = rent_vault_info.as_account_mut::<RentVault>(&pda_rent_payer_api::ID)?;
  29. let transfer = solana_program::program::invoke_signed(
  30. &solana_program::system_instruction::transfer(
  31. payer_info.key,
  32. rent_vault_info.key,
  33. args.amount,
  34. ),
  35. &[
  36. payer_info.clone(),
  37. rent_vault_info.clone(),
  38. system_program.clone(),
  39. ],
  40. &[&[RENT_VAULT, &[bump]]],
  41. );
  42. let vault_balance = rent_vault_info.lamports();
  43. msg!("Updated vault balance: {}", vault_balance);
  44. match transfer {
  45. Ok(_) => (),
  46. Err(e) => return Err(e),
  47. }
  48. msg!("Initialized rent vault.");
  49. msg!("PDA: {:?}", rent_vault_info.key);
  50. Ok(())
  51. }