clockwork_crank/state/
instruction.rs1use {
2 anchor_lang::{
3 prelude::borsh::BorshSchema, prelude::*, solana_program::instruction::Instruction,
4 AnchorDeserialize,
5 },
6 std::{convert::TryFrom, hash::Hash},
7};
8
9#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, Hash, PartialEq)]
14pub struct InstructionData {
15 pub program_id: Pubkey,
17 pub accounts: Vec<AccountMetaData>,
19 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#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, Hash, PartialEq)]
74pub struct AccountMetaData {
75 pub pubkey: Pubkey,
77 pub is_signer: bool,
79 pub is_writable: bool,
81}