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(
77 instruction: Instruction,
78) -> Result<CompiledInstructionV0> {
79 let mut pubkeys_to_metadata: HashMap<Pubkey, AccountMeta> = HashMap::new();
80
81 pubkeys_to_metadata.insert(
83 instruction.program_id,
84 AccountMeta {
85 pubkey: instruction.program_id,
86 is_signer: false,
87 is_writable: false,
88 },
89 );
90
91 for acc in &instruction.accounts {
93 let entry = pubkeys_to_metadata
94 .entry(acc.pubkey)
95 .or_insert(AccountMeta {
96 pubkey: acc.pubkey,
97 is_signer: false,
98 is_writable: false,
99 });
100 entry.is_signer |= acc.is_signer;
101 entry.is_writable |= acc.is_writable;
102 }
103
104 let mut sorted_accounts: Vec<Pubkey> = pubkeys_to_metadata.keys().cloned().collect();
106 sorted_accounts.sort_by(|a, b| {
107 let a_meta = &pubkeys_to_metadata[a];
108 let b_meta = &pubkeys_to_metadata[b];
109
110 fn get_priority(meta: &AccountMeta) -> u8 {
111 match (meta.is_signer, meta.is_writable) {
112 (true, true) => 0,
113 (true, false) => 1,
114 (false, true) => 2,
115 (false, false) => 3,
116 }
117 }
118
119 get_priority(a_meta).cmp(&get_priority(b_meta))
120 });
121
122 let mut num_rw_signers = 0u8;
124 let mut num_ro_signers = 0u8;
125 let mut num_rw = 0u8;
126
127 for pubkey in &sorted_accounts {
128 let meta = &pubkeys_to_metadata[pubkey];
129 if meta.is_signer && meta.is_writable {
130 num_rw_signers += 1;
131 } else if meta.is_signer && !meta.is_writable {
132 num_ro_signers += 1;
133 } else if meta.is_writable {
134 num_rw += 1;
135 }
136 }
137
138 let accounts_to_index: HashMap<Pubkey, u8> = sorted_accounts
140 .iter()
141 .enumerate()
142 .map(|(i, k)| (*k, i as u8))
143 .collect();
144
145 let compiled_instruction = CompiledInstructionData {
147 program_id_index: *accounts_to_index.get(&instruction.program_id).unwrap(),
148 accounts: instruction
149 .accounts
150 .iter()
151 .map(|acc| *accounts_to_index.get(&acc.pubkey).unwrap())
152 .collect(),
153 data: instruction.data,
154 };
155
156 Ok(CompiledInstructionV0 {
157 num_ro_signers,
158 num_rw_signers,
159 num_rw,
160 instructions: vec![compiled_instruction],
161 accounts: sorted_accounts,
162 })
163}
164
165pub fn decompile_instruction(compiled: &CompiledInstructionV0) -> Result<Instruction> {
167 if compiled.instructions.is_empty() {
168 return Err(ProgramError::InvalidInstructionData.into());
169 }
170
171 let ix = &compiled.instructions[0];
172 let program_id = compiled.accounts[ix.program_id_index as usize];
173
174 let accounts: Vec<AccountMeta> = ix
175 .accounts
176 .iter()
177 .enumerate()
178 .map(|(_i, &idx)| {
179 let pubkey = compiled.accounts[idx as usize];
180 let is_writable = if idx < compiled.num_rw_signers {
181 true
182 } else if idx < compiled.num_rw_signers + compiled.num_ro_signers {
183 false
184 } else if idx < compiled.num_rw_signers + compiled.num_ro_signers + compiled.num_rw {
185 true
186 } else {
187 false
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}