anchor_instruction

Function anchor_instruction 

Source
pub fn anchor_instruction(
    program_id: Pubkey,
    accounts: impl ToAccountMetas,
    data: impl InstructionData,
) -> Instruction
Expand description

Creates a new Anchor instruction from the generated declare_program! client structs

This function simplifies the process of creating Solana instructions from Anchor’s generated types, providing a clean interface that handles the conversion automatically.

§Arguments

  • program_id - The on-chain program’s public key
  • accounts - Account struct that implements ToAccountMetas (generated by Anchor)
  • data - Instruction data struct that implements InstructionData (generated by Anchor)

§Returns

A solana_program::instruction::Instruction ready to be added to a transaction.

§Example

use anchor_utils::anchor_instruction;
use anchor_lang::prelude::*;
use solana_program::instruction::Instruction;

// Using Anchor's generated types from declare_program!
declare_program!(quarry_mine);


let instruction: Instruction = anchor_instruction(
    quarry_mine::ID,
    quarry_mine::client::accounts::StakeTokens {
        authority: user_authority,
        miner: miner_account,
        quarry: quarry_account,
        rewarder: rewarder_account,
        token_account: token_account,
        miner_vault: miner_vault_account,
        token_program: token_program_id,
    },
    quarry_mine::client::args::StakeTokens {
        amount: 1_000_000,
    },
);