| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  | use {
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |     solana_program_test::{processor, ProgramTest},
 | 
					
						
							|  |  |  |     solana_sdk::{
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |         account_info::{next_account_info, AccountInfo},
 | 
					
						
							|  |  |  |         entrypoint::ProgramResult,
 | 
					
						
							|  |  |  |         instruction::{AccountMeta, Instruction},
 | 
					
						
							|  |  |  |         msg,
 | 
					
						
							|  |  |  |         program::invoke,
 | 
					
						
							|  |  |  |         pubkey::Pubkey,
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |         signature::Signer,
 | 
					
						
							|  |  |  |         transaction::Transaction,
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |     },
 | 
					
						
							|  |  |  | };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Process instruction to invoke into another program
 | 
					
						
							|  |  |  | fn invoker_process_instruction(
 | 
					
						
							|  |  |  |     _program_id: &Pubkey,
 | 
					
						
							|  |  |  |     accounts: &[AccountInfo],
 | 
					
						
							|  |  |  |     _input: &[u8],
 | 
					
						
							|  |  |  | ) -> ProgramResult {
 | 
					
						
							|  |  |  |     // if we can call `msg!` successfully, then InvokeContext exists as required
 | 
					
						
							|  |  |  |     msg!("Processing invoker instruction before CPI");
 | 
					
						
							|  |  |  |     let account_info_iter = &mut accounts.iter();
 | 
					
						
							|  |  |  |     let invoked_program_info = next_account_info(account_info_iter)?;
 | 
					
						
							|  |  |  |     invoke(
 | 
					
						
							| 
									
										
										
										
											2021-03-03 21:46:48 -08:00
										 |  |  |         &Instruction::new_with_bincode(*invoked_program_info.key, &[0], vec![]),
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |         &[invoked_program_info.clone()],
 | 
					
						
							|  |  |  |     )?;
 | 
					
						
							|  |  |  |     msg!("Processing invoker instruction after CPI");
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Process instruction to be invoked by another program
 | 
					
						
							|  |  |  | #[allow(clippy::unnecessary_wraps)]
 | 
					
						
							|  |  |  | fn invoked_process_instruction(
 | 
					
						
							|  |  |  |     _program_id: &Pubkey,
 | 
					
						
							|  |  |  |     _accounts: &[AccountInfo],
 | 
					
						
							|  |  |  |     _input: &[u8],
 | 
					
						
							|  |  |  | ) -> ProgramResult {
 | 
					
						
							|  |  |  |     // if we can call `msg!` successfully, then InvokeContext exists as required
 | 
					
						
							|  |  |  |     msg!("Processing invoked instruction");
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn cpi() {
 | 
					
						
							|  |  |  |     let invoker_program_id = Pubkey::new_unique();
 | 
					
						
							|  |  |  |     let mut program_test = ProgramTest::new(
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |         "invoker",
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |         invoker_program_id,
 | 
					
						
							|  |  |  |         processor!(invoker_process_instruction),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     let invoked_program_id = Pubkey::new_unique();
 | 
					
						
							|  |  |  |     program_test.add_program(
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |         "invoked",
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |         invoked_program_id,
 | 
					
						
							|  |  |  |         processor!(invoked_process_instruction),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |     let mut context = program_test.start_with_context().await;
 | 
					
						
							| 
									
										
										
										
											2021-03-03 21:46:48 -08:00
										 |  |  |     let instructions = vec![Instruction::new_with_bincode(
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |         invoker_program_id,
 | 
					
						
							|  |  |  |         &[0],
 | 
					
						
							|  |  |  |         vec![AccountMeta::new_readonly(invoked_program_id, false)],
 | 
					
						
							|  |  |  |     )];
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let transaction = Transaction::new_signed_with_payer(
 | 
					
						
							|  |  |  |         &instructions,
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |         Some(&context.payer.pubkey()),
 | 
					
						
							|  |  |  |         &[&context.payer],
 | 
					
						
							|  |  |  |         context.last_blockhash,
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 08:54:09 -07:00
										 |  |  |     context
 | 
					
						
							| 
									
										
										
										
											2021-02-03 13:45:29 +01:00
										 |  |  |         .banks_client
 | 
					
						
							|  |  |  |         .process_transaction(transaction)
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .unwrap();
 | 
					
						
							|  |  |  | }
 |