1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! # Anchor Utils
//!
//! Utility functions for simplifying Anchor framework development on Solana.
//!
//! This crate provides helper functions that make it easier to work with Anchor programs,
//! particularly when creating instructions from Anchor's generated client code.
//!
//! ## Features
//!
//! - **Simplified instruction creation**: Convert Anchor's generated structs directly to Solana instructions
//! - **Type-safe interface**: Leverages Anchor's type system for compile-time safety
//! - **Zero overhead**: Minimal abstraction layer with no runtime cost
use *;
use ;
use Instruction;
/// 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
///
/// ```rust
/// 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 user_authority = Pubkey::new_unique();
/// # let miner_account = Pubkey::new_unique();
/// # let quarry_account = Pubkey::new_unique();
/// # let rewarder_account = Pubkey::new_unique();
/// # let token_account = Pubkey::new_unique();
/// # let miner_vault_account = Pubkey::new_unique();
/// # let token_program_id = Pubkey::new_unique();
///
/// 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,
/// },
/// );
/// ```