antegen_fiber_program/state/
instruction.rs1use anchor_lang::prelude::*;
2use anchor_lang::solana_program::instruction::{AccountMeta, Instruction};
3use std::collections::HashMap;
4
5#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
7pub struct SerializableInstruction {
8 pub program_id: Pubkey,
9 pub accounts: Vec<SerializableAccountMeta>,
10 pub data: Vec<u8>,
11}
12
13#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
15pub struct SerializableAccountMeta {
16 pub pubkey: Pubkey,
17 pub is_signer: bool,
18 pub is_writable: bool,
19}
20
21impl From<Instruction> for SerializableInstruction {
22 fn from(ix: Instruction) -> Self {
23 SerializableInstruction {
24 program_id: ix.program_id,
25 accounts: ix
26 .accounts
27 .into_iter()
28 .map(|acc| SerializableAccountMeta {
29 pubkey: acc.pubkey,
30 is_signer: acc.is_signer,
31 is_writable: acc.is_writable,
32 })
33 .collect(),
34 data: ix.data,
35 }
36 }
37}
38
39impl From<SerializableInstruction> for Instruction {
40 fn from(ix: SerializableInstruction) -> Self {
41 Instruction {
42 program_id: ix.program_id,
43 accounts: ix
44 .accounts
45 .into_iter()
46 .map(|acc| AccountMeta {
47 pubkey: acc.pubkey,
48 is_signer: acc.is_signer,
49 is_writable: acc.is_writable,
50 })
51 .collect(),
52 data: ix.data,
53 }
54 }
55}
56
57#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
59pub struct CompiledInstructionData {
60 pub program_id_index: u8,
61 pub accounts: Vec<u8>,
62 pub data: Vec<u8>,
63}
64
65#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
67pub struct CompiledInstructionV0 {
68 pub num_ro_signers: u8,
69 pub num_rw_signers: u8,
70 pub num_rw: u8,
71 pub instructions: Vec<CompiledInstructionData>,
72 pub accounts: Vec<Pubkey>,
73}
74
75pub fn compile_instruction(instruction: Instruction) -> Result<CompiledInstructionV0> {
77 let mut pubkeys_to_metadata: HashMap<Pubkey, AccountMeta> = HashMap::new();
78
79 pubkeys_to_metadata.insert(
81 instruction.program_id,
82 AccountMeta {
83 pubkey: instruction.program_id,
84 is_signer: false,
85 is_writable: false,
86 },
87 );
88
89 for acc in &instruction.accounts {
91 let entry = pubkeys_to_metadata
92 .entry(acc.pubkey)
93 .or_insert(AccountMeta {
94 pubkey: acc.pubkey,
95 is_signer: false,
96 is_writable: false,
97 });
98 if acc.pubkey != instruction.program_id {
102 entry.is_signer |= acc.is_signer;
103 entry.is_writable |= acc.is_writable;
104 }
105 }
106
107 let mut sorted_accounts: Vec<Pubkey> = pubkeys_to_metadata.keys().cloned().collect();
109 sorted_accounts.sort_by(|a, b| {
110 let a_meta = &pubkeys_to_metadata[a];
111 let b_meta = &pubkeys_to_metadata[b];
112
113 fn get_priority(meta: &AccountMeta) -> u8 {
114 match (meta.is_signer, meta.is_writable) {
115 (true, true) => 0,
116 (true, false) => 1,
117 (false, true) => 2,
118 (false, false) => 3,
119 }
120 }
121
122 get_priority(a_meta).cmp(&get_priority(b_meta))
123 });
124
125 let mut num_rw_signers = 0u8;
127 let mut num_ro_signers = 0u8;
128 let mut num_rw = 0u8;
129
130 for pubkey in &sorted_accounts {
131 let meta = &pubkeys_to_metadata[pubkey];
132 if meta.is_signer && meta.is_writable {
133 num_rw_signers += 1;
134 } else if meta.is_signer && !meta.is_writable {
135 num_ro_signers += 1;
136 } else if meta.is_writable {
137 num_rw += 1;
138 }
139 }
140
141 let accounts_to_index: HashMap<Pubkey, u8> = sorted_accounts
143 .iter()
144 .enumerate()
145 .map(|(i, k)| (*k, i as u8))
146 .collect();
147
148 let compiled_instruction = CompiledInstructionData {
150 program_id_index: *accounts_to_index.get(&instruction.program_id).unwrap(),
151 accounts: instruction
152 .accounts
153 .iter()
154 .map(|acc| *accounts_to_index.get(&acc.pubkey).unwrap())
155 .collect(),
156 data: instruction.data,
157 };
158
159 Ok(CompiledInstructionV0 {
160 num_ro_signers,
161 num_rw_signers,
162 num_rw,
163 instructions: vec![compiled_instruction],
164 accounts: sorted_accounts,
165 })
166}
167
168pub fn decompile_instruction(compiled: &CompiledInstructionV0) -> Result<Instruction> {
170 if compiled.instructions.is_empty() {
171 return Err(ProgramError::InvalidInstructionData.into());
172 }
173
174 let ix = &compiled.instructions[0];
175 let program_id = compiled.accounts[ix.program_id_index as usize];
176
177 let accounts: Vec<AccountMeta> = ix
178 .accounts
179 .iter()
180 .map(|&idx| {
181 let pubkey = compiled.accounts[idx as usize];
182 let is_writable = if idx < compiled.num_rw_signers {
183 true
184 } else if idx < compiled.num_rw_signers + compiled.num_ro_signers {
185 false
186 } else {
187 idx < compiled.num_rw_signers + compiled.num_ro_signers + compiled.num_rw
188 };
189 let is_signer = idx < compiled.num_rw_signers + compiled.num_ro_signers;
190
191 AccountMeta {
192 pubkey,
193 is_signer,
194 is_writable,
195 }
196 })
197 .collect();
198
199 Ok(Instruction {
200 program_id,
201 accounts,
202 data: ix.data.clone(),
203 })
204}