Ref - https://www.anchor-lang.com/docs/basics/cpi
Initialize a new anchor contract
anchor init cpi-contract
anchor_lang::system_program:use anchor_lang::prelude::*;
use anchor_lang::system_program::{transfer, Transfer};
declare_id!("C6SrAByDmBm4u5dcZLYJErYirVn4o6tf6329SpFoVbW7");
#[program]
pub mod cpi_contract {
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>,
}
Tests
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { CpiContract } from "../target/types/cpi_contract";
import { assert } from "chai";
describe("cpi-contract", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const provider = anchor.getProvider();
const recipient = anchor.web3.Keypair.generate();
const program = anchor.workspace.cpiContract as Program<CpiContract>;
console.log("Program", program);
it("Is initialized!", async () => {
// Add your test here.
const tx = await program.methods.solTransfer(new anchor.BN(1000000000)).accounts({
sender: provider.publicKey,
recipient: recipient.publicKey,
}).rpc();
console.log("Your transaction signature", tx);
const account = await provider.connection.getAccountInfo(recipient.publicKey);
assert.equal(account?.lamports, 1000000000);
});
});
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();
// Prepare instruction AccountMetas
let account_metas = vec![
AccountMeta::new(from_pubkey.key(), true),
AccountMeta::new(to_pubkey.key(), false),
];
// SOL transfer instruction discriminator
let instruction_discriminator: u32 = 2;
// Prepare instruction data
let mut instruction_data = Vec::with_capacity(4 + 8);
instruction_data.extend_from_slice(&instruction_discriminator.to_le_bytes());
instruction_data.extend_from_slice(&amount.to_le_bytes());
// Create instruction
let instruction = Instruction {
program_id: program_id.key(),
accounts: account_metas,
data: instruction_data,
};
// Invoke instruction
invoke(&instruction, &[from_pubkey, to_pubkey, program_id])?;
Ok(())
}
#[derive(Accounts)]
pub struct SolTransfer<'info> {
#[account(mut)]
sender: Signer<'info>,
#[account(mut)]
recipient: SystemAccount<'info>,
system_program: Program<'info, System>,
}