use solana_instruction::AccountMeta;
use solana_pubkey::Pubkey;
#[derive(Debug, Clone)]
pub struct DecodedField {
pub name: String,
pub value: String,
pub children: Vec<DecodedField>,
}
impl DecodedField {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
children: Vec::new(),
}
}
pub fn with_children(name: impl Into<String>, children: Vec<DecodedField>) -> Self {
Self {
name: name.into(),
value: String::new(),
children,
}
}
}
#[derive(Debug, Clone)]
pub struct DecodedInstruction {
pub name: String,
pub fields: Vec<DecodedField>,
pub account_names: Vec<String>,
}
impl DecodedInstruction {
pub fn with_fields_and_accounts(
name: impl Into<String>,
fields: Vec<DecodedField>,
account_names: Vec<String>,
) -> Self {
Self {
name: name.into(),
fields,
account_names,
}
}
}
pub trait InstructionDecoder: Send + Sync {
fn program_id(&self) -> Pubkey;
fn program_name(&self) -> &'static str;
fn decode(&self, data: &[u8], accounts: &[AccountMeta]) -> Option<DecodedInstruction>;
}