Skip to content

Commit 5377ce2

Browse files
committed
updated the code
1 parent d00ef93 commit 5377ce2

File tree

2 files changed

+57
-11
lines changed
  • basics/pda-rent-payer/poseidon/pda-rent-payer

2 files changed

+57
-11
lines changed
Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
11
use anchor_lang::prelude::*;
2-
2+
use anchor_lang::system_program::{Transfer, transfer};
33
declare_id!("8R1pBZKFyvBdR7LDa4R45JWSdUFnJdRSo9P1MPr571LC");
4-
54
#[program]
65
pub mod pda_rent_payer {
76
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)?;
1137
Ok(())
1238
}
1339
}
14-
1540
#[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+
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { Pubkey, type Result } from '@solanaturbine/poseidon';
1+
import { LAMPORTS_PER_SOL } from '@solana/web3.js';
2+
import { Pubkey, type Result, Signer, SystemAccount, SystemProgram, u64 } from '@solanaturbine/poseidon';
23

34
export default class PdaRentPayer {
4-
static PROGRAM_ID = new Pubkey('11111111111111111111111111111111');
5+
static PROGRAM_ID = new Pubkey('8R1pBZKFyvBdR7LDa4R45JWSdUFnJdRSo9P1MPr571LC');
56

6-
initialize(): Result {
7-
// Write your program here
7+
initRentVault(payer: Signer, rentVault: SystemAccount, fundLamports: u64): Result {
8+
rentVault.derive(['rent_vault']);
9+
SystemProgram.transfer(payer, rentVault, fundLamports);
10+
}
11+
12+
createNewAccount(newAccount: Signer, rentVault: SystemAccount, amount: u64): Result {
13+
rentVault.derive(['rent_vault']);
14+
SystemProgram.transfer(rentVault, newAccount, amount, ['rent_vault', rentVault.getBump()]);
815
}
916
}

0 commit comments

Comments
 (0)