clockwork_crank/state/
instruction.rs

1use {
2    anchor_lang::{
3        prelude::borsh::BorshSchema, prelude::*, solana_program::instruction::Instruction,
4        AnchorDeserialize,
5    },
6    std::{convert::TryFrom, hash::Hash},
7};
8
9/**
10 * InstructionData
11 */
12
13#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, Hash, PartialEq)]
14pub struct InstructionData {
15    /// Pubkey of the instruction processor that executes this instruction
16    pub program_id: Pubkey,
17    /// Metadata for what accounts should be passed to the instruction processor
18    pub accounts: Vec<AccountMetaData>,
19    /// Opaque data passed to the instruction processor
20    pub data: Vec<u8>,
21}
22
23impl From<Instruction> for InstructionData {
24    fn from(instruction: Instruction) -> Self {
25        InstructionData {
26            program_id: instruction.program_id,
27            accounts: instruction
28                .accounts
29                .iter()
30                .map(|a| AccountMetaData {
31                    pubkey: a.pubkey,
32                    is_signer: a.is_signer,
33                    is_writable: a.is_writable,
34                })
35                .collect(),
36            data: instruction.data,
37        }
38    }
39}
40
41impl From<&InstructionData> for Instruction {
42    fn from(instruction: &InstructionData) -> Self {
43        Instruction {
44            program_id: instruction.program_id,
45            accounts: instruction
46                .accounts
47                .iter()
48                .map(|a| AccountMeta {
49                    pubkey: a.pubkey,
50                    is_signer: a.is_signer,
51                    is_writable: a.is_writable,
52                })
53                .collect(),
54            data: instruction.data.clone(),
55        }
56    }
57}
58
59impl TryFrom<Vec<u8>> for InstructionData {
60    type Error = Error;
61    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
62        Ok(
63            borsh::try_from_slice_with_schema::<InstructionData>(data.as_slice())
64                .map_err(|_err| ErrorCode::AccountDidNotDeserialize)?,
65        )
66    }
67}
68
69/**
70 * AccountMetaData
71 */
72
73#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, Hash, PartialEq)]
74pub struct AccountMetaData {
75    /// An account's public key
76    pub pubkey: Pubkey,
77    /// True if an Instruction requires a Transaction signature matching `pubkey`.
78    pub is_signer: bool,
79    /// True if the `pubkey` can be loaded as a read-write account.
80    pub is_writable: bool,
81}