11use anchor_lang:: prelude:: * ;
2- use anchor_lang:: system_program:: { transfer , Transfer } ;
2+ use anchor_lang:: system_program:: { Transfer , transfer } ;
33declare_id ! ( "BYj8GpV9hpv9PAVdwoWFCTMkysJkk5jstYjuCrw4pxem" ) ;
44#[ program]
55pub mod pda_rent_payer {
66 use super :: * ;
7- pub fn init_rent_vault (
8- ctx : Context < InitRentVaultContext > ,
9- fund_lamports : u64 ,
7+ pub fn init_rent_vault ( ctx : Context < InitRentVaultContext > ) -> Result < ( ) > {
8+ ctx. accounts . state . owner = ctx. accounts . owner . key ( ) ;
9+ ctx. accounts . state . state_bump = ctx. bumps . state ;
10+ ctx. accounts . state . auth_bump = ctx. bumps . auth ;
11+ ctx. accounts . state . vault_bump = ctx. bumps . vault ;
12+ Ok ( ( ) )
13+ }
14+ pub fn deposit_to_rent_vault (
15+ ctx : Context < DepositToRentVaultContext > ,
16+ amount : u64 ,
1017 ) -> Result < ( ) > {
11- ctx. accounts . rent_vault . rent_vault_bump = ctx. bumps . rent_vault ;
1218 let transfer_accounts = Transfer {
1319 from : ctx. accounts . owner . to_account_info ( ) ,
14- to : ctx. accounts . rent_vault . to_account_info ( ) ,
20+ to : ctx. accounts . vault . to_account_info ( ) ,
1521 } ;
1622 let cpi_ctx = CpiContext :: new (
1723 ctx. accounts . system_program . to_account_info ( ) ,
1824 transfer_accounts,
1925 ) ;
20- transfer ( cpi_ctx, fund_lamports ) ?;
26+ transfer ( cpi_ctx, amount ) ?;
2127 Ok ( ( ) )
2228 }
2329 pub fn create_new_account (
2430 ctx : Context < CreateNewAccountContext > ,
25- transfer_amount : u64 ,
31+ amount : u64 ,
2632 ) -> Result < ( ) > {
27- ctx. accounts . new_account . owner = ctx. accounts . owner . key ( ) ;
28- ctx. accounts . new_account . account_bump = ctx. bumps . new_account ;
33+ ctx. accounts . new_account_state . owner = ctx. accounts . owner . key ( ) ;
2934 let transfer_accounts = Transfer {
30- from : ctx. accounts . rent_vault . to_account_info ( ) ,
31- to : ctx. accounts . new_account . to_account_info ( ) ,
35+ from : ctx. accounts . vault . to_account_info ( ) ,
36+ to : ctx. accounts . new_account_state . to_account_info ( ) ,
3237 } ;
33- let cpi_ctx = CpiContext :: new (
38+ let seeds = & [
39+ b"vault" ,
40+ ctx. accounts . auth . to_account_info ( ) . key . as_ref ( ) ,
41+ & [ ctx. accounts . state . vault_bump ] ,
42+ ] ;
43+ let pda_signer = & [ & seeds[ ..] ] ;
44+ let cpi_ctx = CpiContext :: new_with_signer (
3445 ctx. accounts . system_program . to_account_info ( ) ,
3546 transfer_accounts,
47+ pda_signer,
3648 ) ;
37- transfer ( cpi_ctx, transfer_amount ) ?;
49+ transfer ( cpi_ctx, amount ) ?;
3850 Ok ( ( ) )
3951 }
4052}
@@ -43,39 +55,74 @@ pub struct InitRentVaultContext<'info> {
4355 #[ account(
4456 init,
4557 payer = owner,
46- space = 9 ,
47- seeds = [ b"rent_vault" ,
58+ space = 8 ,
59+ seeds = [ b"vault" ,
60+ auth. key( ) . as_ref( ) ] ,
61+ bump,
62+ ) ]
63+ pub vault : Account < ' info , RentVault > ,
64+ #[ account( mut ) ]
65+ pub owner : Signer < ' info > ,
66+ #[ account(
67+ init,
68+ payer = owner,
69+ space = 43 ,
70+ seeds = [ b"state" ,
4871 owner. key( ) . as_ref( ) ] ,
4972 bump,
5073 ) ]
51- pub rent_vault : Account < ' info , RentState > ,
74+ pub state : Account < ' info , RentAccountState > ,
75+ #[ account( seeds = [ b"auth" , state. key( ) . as_ref( ) ] , bump) ]
76+ /// CHECK: This acc is safe
77+ pub auth : UncheckedAccount < ' info > ,
78+ pub system_program : Program < ' info , System > ,
79+ }
80+ #[ derive( Accounts ) ]
81+ pub struct DepositToRentVaultContext < ' info > {
5282 #[ account( mut ) ]
5383 pub owner : Signer < ' info > ,
84+ #[ account( seeds = [ b"auth" , state. key( ) . as_ref( ) ] , bump = state. auth_bump) ]
85+ /// CHECK: This acc is safe
86+ pub auth : UncheckedAccount < ' info > ,
87+ #[ account( seeds = [ b"vault" , auth. key( ) . as_ref( ) ] , bump = state. vault_bump) ]
88+ pub vault : Account < ' info , RentVault > ,
89+ #[ account( seeds = [ b"state" , owner. key( ) . as_ref( ) ] , bump = state. state_bump) ]
90+ pub state : Account < ' info , RentAccountState > ,
5491 pub system_program : Program < ' info , System > ,
5592}
5693#[ derive( Accounts ) ]
5794pub struct CreateNewAccountContext < ' info > {
58- #[ account( ) ]
59- pub rent_vault : Account < ' info , RentState > ,
6095 #[ account(
6196 init,
6297 payer = owner,
6398 space = 41 ,
64- seeds = [ b"account " ,
99+ seeds = [ b"new_account " ,
65100 owner. key( ) . as_ref( ) ] ,
66101 bump,
67102 ) ]
68- pub new_account : Account < ' info , AccountState > ,
103+ pub new_account_state : Account < ' info , NewAccountState > ,
69104 #[ account( mut ) ]
70105 pub owner : Signer < ' info > ,
106+ #[ account( seeds = [ b"auth" , state. key( ) . as_ref( ) ] , bump = state. auth_bump) ]
107+ /// CHECK: This acc is safe
108+ pub auth : UncheckedAccount < ' info > ,
109+ #[ account( mut , seeds = [ b"vault" , auth. key( ) . as_ref( ) ] , bump = state. vault_bump) ]
110+ pub vault : SystemAccount < ' info > ,
111+ #[ account( seeds = [ b"state" , owner. key( ) . as_ref( ) ] , bump = state. state_bump) ]
112+ pub state : Account < ' info , RentAccountState > ,
71113 pub system_program : Program < ' info , System > ,
72114}
73115#[ account]
74- pub struct AccountState {
116+ pub struct RentAccountState {
75117 pub owner : Pubkey ,
76- pub account_bump : u8 ,
118+ pub state_bump : u8 ,
119+ pub auth_bump : u8 ,
120+ pub vault_bump : u8 ,
77121}
78122#[ account]
79- pub struct RentState {
80- pub rent_vault_bump : u8 ,
123+ pub struct NewAccountState {
124+ pub owner : Pubkey ,
125+ pub new_account_bump : u8 ,
81126}
127+ #[ account]
128+ pub struct RentVault { }
0 commit comments