요약
필요한 모든 서명자가 이미 원래 트랜잭션에 서명한 경우 CPI에 _rsinvoke_를
사용합니다. PDA 서명이 필요하지 않습니다. 서명자 및 쓰기 가능 권한은
호출자에서 피호출자로 확장됩니다.
PDA 서명자가 없는 CPI
CPI에 PDA 서명자가 필요하지 않은 경우
invoke
함수가 사용됩니다. invoke 함수는 빈 signers_seeds 배열로
invoke_signed 함수를 호출합니다. 빈 서명자 배열은 서명에 PDA가 필요하지
않음을 나타냅니다.
Invoke function
pub fn invoke(instruction: &Instruction, account_infos: &[AccountInfo]) -> ProgramResult {invoke_signed(instruction, account_infos, &[])}
아래 예제는 Anchor와 네이티브 Rust를 사용하여 CPI를 수행합니다. 한 계정에서 다른 계정으로 SOL을 전송하는 단일 명령어를 포함합니다.
Anchor
다음 예제는 Anchor 프로그램에서 CPI를 구현하는 두 가지 접근 방식을 보여줍니다. 예제는 기능적으로 동일하지만 서로 다른 수준의 추상화를 보여줍니다.
- 예제 1: Anchor의 *rs
CpiContext*와 헬퍼 함수를 사용합니다. - 예제 2:
solana_program크레이트의system_instruction::transfer함수를 사용합니다. - 예제 3: CPI 명령어를 수동으로 구성합니다. 이 접근 방식은 호출하려는 명령어를 빌드하는 데 도움이 되는 크레이트가 없을 때 유용합니다.
use anchor_lang::prelude::*;use anchor_lang::system_program::{transfer, Transfer};declare_id!("9AvUNHjxscdkiKQ8tUn12QCMXtcnbR9BVGq3ULNzFMRi");#[program]pub mod cpi {use super::*;pub fn sol_transfer(ctx: Context<SolTransfer>, amount: u64) -> Result<()> {let from_pubkey = ctx.accounts.sender.to_account_info();let to_pubkey = ctx.accounts.recipient.to_account_info();let program_id = ctx.accounts.system_program.to_account_info();let cpi_context = CpiContext::new(program_id,Transfer {from: from_pubkey,to: to_pubkey,},);transfer(cpi_context, amount)?;Ok(())}}#[derive(Accounts)]pub struct SolTransfer<'info> {#[account(mut)]sender: Signer<'info>,#[account(mut)]recipient: SystemAccount<'info>,system_program: Program<'info, System>,}
Rust
다음 예제는 네이티브 Rust로 작성된 프로그램에서 CPI를 수행하는 방법을 보여줍니다. 한 계정에서 다른 계정으로 SOL을 전송하는 단일 명령어를 포함합니다. (테스트 파일은 프로그램을 테스트하기 위해 LiteSVM을 사용합니다.)
use borsh::BorshDeserialize;use solana_program::{account_info::AccountInfo,entrypoint,entrypoint::ProgramResult,program::invoke,program_error::ProgramError,pubkey::Pubkey,system_instruction,};// Declare program entrypointentrypoint!(process_instruction);// Define program instructions#[derive(BorshDeserialize)]enum ProgramInstruction {SolTransfer { amount: u64 },}impl ProgramInstruction {fn unpack(input: &[u8]) -> Result<Self, ProgramError> {Self::try_from_slice(input).map_err(|_| ProgramError::InvalidInstructionData)}}pub fn process_instruction(_program_id: &Pubkey,accounts: &[AccountInfo],instruction_data: &[u8],) -> ProgramResult {// Deserialize instruction datalet instruction = ProgramInstruction::unpack(instruction_data)?;// Process instructionmatch instruction {ProgramInstruction::SolTransfer { amount } => {// Parse accountslet [sender_info, recipient_info, system_program_info] = accounts else {return Err(ProgramError::NotEnoughAccountKeys);};// Verify the sender is a signerif !sender_info.is_signer {return Err(ProgramError::MissingRequiredSignature);}// Create and invoke the transfer instructionlet transfer_ix = system_instruction::transfer(sender_info.key,recipient_info.key,amount,);invoke(&transfer_ix,&[sender_info.clone(),recipient_info.clone(),system_program_info.clone(),],)?;Ok(())}}}
Is this page helpful?