1+ use crate :: consts:: BUFFER ;
2+ use crate :: types:: DelegateAccountArgs ;
3+ use crate :: utils:: { close_pda_with_system_transfer, create_pda, seeds_with_bump} ;
14use borsh:: BorshSerialize ;
25use solana_program:: account_info:: AccountInfo ;
36use solana_program:: entrypoint:: ProgramResult ;
47use solana_program:: instruction:: { AccountMeta , Instruction } ;
8+ use solana_program:: program:: invoke_signed;
59use solana_program:: program_error:: ProgramError ;
6- use solana_program:: program_memory:: { sol_memcpy , sol_memset} ;
10+ use solana_program:: program_memory:: sol_memset;
711use solana_program:: pubkey:: Pubkey ;
8-
9- // TODO: import from the delegation program crate once open-sourced
10- use crate :: consts:: BUFFER ;
11- use crate :: types:: DelegateAccountArgs ;
12- use crate :: utils:: { close_pda_with_system_transfer, create_pda, seeds_with_bump} ;
12+ use solana_program:: system_instruction;
1313
1414pub struct DelegateAccounts < ' a , ' info > {
1515 pub payer : & ' a AccountInfo < ' info > ,
@@ -76,7 +76,7 @@ pub fn delegate_account<'a, 'info>(
7676 {
7777 let pda_ro = accounts. pda . try_borrow_data ( ) ?;
7878 let mut buf = accounts. buffer . try_borrow_mut_data ( ) ?;
79- sol_memcpy ( & mut buf , & pda_ro, data_len ) ;
79+ buf . copy_from_slice ( & pda_ro) ;
8080 }
8181
8282 // Zero PDA (single RW borrow)
@@ -85,7 +85,17 @@ pub fn delegate_account<'a, 'info>(
8585 sol_memset ( & mut pda_mut, 0 , data_len) ;
8686 }
8787
88- accounts. pda . assign ( accounts. delegation_program . key ) ;
88+ // Assign the PDA to the delegation program if not already assigned
89+ if accounts. pda . owner != accounts. system_program . key {
90+ accounts. pda . assign ( accounts. system_program . key ) ;
91+ }
92+ if accounts. pda . owner != accounts. delegation_program . key {
93+ invoke_signed (
94+ & system_instruction:: assign ( accounts. pda . key , accounts. delegation_program . key ) ,
95+ & [ accounts. pda . clone ( ) , accounts. system_program . clone ( ) ] ,
96+ pda_signer_seeds,
97+ ) ?;
98+ }
8999
90100 let seeds_vec: Vec < Vec < u8 > > = pda_seeds. iter ( ) . map ( |& slice| slice. to_vec ( ) ) . collect ( ) ;
91101
0 commit comments