|
1 | 1 | use anchor_lang::prelude::*; |
2 | | - |
| 2 | +use anchor_lang::system_program::{Transfer, transfer}; |
3 | 3 | declare_id!("8R1pBZKFyvBdR7LDa4R45JWSdUFnJdRSo9P1MPr571LC"); |
4 | | - |
5 | 4 | #[program] |
6 | 5 | pub mod pda_rent_payer { |
7 | 6 | use super::*; |
8 | | - |
9 | | - pub fn initialize(ctx: Context<Initialize>) -> Result<()> { |
10 | | - msg!("Greetings from: {:?}", ctx.program_id); |
| 7 | + pub fn init_rent_vault( |
| 8 | + ctx: Context<InitRentVaultContext>, |
| 9 | + fund_lamports: u64, |
| 10 | + ) -> Result<()> { |
| 11 | + let transfer_accounts = Transfer { |
| 12 | + from: ctx.accounts.payer.to_account_info(), |
| 13 | + to: ctx.accounts.rentVault.to_account_info(), |
| 14 | + }; |
| 15 | + let cpi_ctx = CpiContext::new( |
| 16 | + ctx.accounts.system_program.to_account_info(), |
| 17 | + transfer_accounts, |
| 18 | + ); |
| 19 | + transfer(cpi_ctx, fund_lamports)?; |
| 20 | + Ok(()) |
| 21 | + } |
| 22 | + pub fn create_new_account( |
| 23 | + ctx: Context<CreateNewAccountContext>, |
| 24 | + amount: u64, |
| 25 | + ) -> Result<()> { |
| 26 | + let transfer_accounts = Transfer { |
| 27 | + from: ctx.accounts.rentVault.to_account_info(), |
| 28 | + to: ctx.accounts.newAccount.to_account_info(), |
| 29 | + }; |
| 30 | + let signer_seeds: &[&[&[u8]]; 1] = &[&[b"rent_vault", &[ctx.bumps.rent_vault]]]; |
| 31 | + let cpi_ctx = CpiContext::new_with_signer( |
| 32 | + ctx.accounts.system_program.to_account_info(), |
| 33 | + transfer_accounts, |
| 34 | + signer_seeds, |
| 35 | + ); |
| 36 | + transfer(cpi_ctx, amount)?; |
11 | 37 | Ok(()) |
12 | 38 | } |
13 | 39 | } |
14 | | - |
15 | 40 | #[derive(Accounts)] |
16 | | -pub struct Initialize {} |
| 41 | +pub struct InitRentVaultContext<'info> { |
| 42 | + #[account(mut)] |
| 43 | + pub payer: Signer<'info>, |
| 44 | + #[account(mut, seeds = [b"rent_vault"], bump)] |
| 45 | + pub rent_vault: SystemAccount<'info>, |
| 46 | + pub system_program: Program<'info, System>, |
| 47 | +} |
| 48 | +#[derive(Accounts)] |
| 49 | +pub struct CreateNewAccountContext<'info> { |
| 50 | + #[account(mut, seeds = [b"rent_vault"], bump)] |
| 51 | + pub rent_vault: SystemAccount<'info>, |
| 52 | + #[account(mut)] |
| 53 | + pub new_account: Signer<'info>, |
| 54 | + pub system_program: Program<'info, System>, |
| 55 | +} |
0 commit comments