solana_idl_classic/
idl.rs

1use serde::{Deserialize, Serialize};
2
3use super::{idl_type::IdlType, idl_type_definition::IdlTypeDefinition};
4use crate::{
5    idl_error_code::IdlErrorCode, idl_instruction::IdlInstruction,
6    idl_metadata::IdlMetadata,
7};
8
9/// IDL that is compatible with what anchor and shank extract from a solana program.
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11pub struct Idl {
12    /// Version of the Solana contract at the time the IDL was generated.
13    pub version: String,
14
15    /// The name of the program.
16    pub name: String,
17
18    /// Program constants.
19    #[serde(skip_serializing_if = "Vec::is_empty", default)]
20    pub constants: Vec<IdlConst>,
21
22    /// Instructions that are handled by the program.
23    pub instructions: Vec<IdlInstruction>,
24
25    /// Program state.
26    #[serde(skip_serializing_if = "Option::is_none", default)]
27    pub state: Option<IdlState>,
28
29    /// Accounts holding data that are used by the program.
30    #[serde(skip_serializing_if = "Vec::is_empty", default)]
31    pub accounts: Vec<IdlTypeDefinition>,
32
33    /// Types defined in the program that are used by account structs.
34    #[serde(skip_serializing_if = "Vec::is_empty", default)]
35    pub types: Vec<IdlTypeDefinition>,
36
37    /// Events emitted by the program.
38    #[serde(skip_serializing_if = "Option::is_none", default)]
39    pub events: Option<Vec<IdlEvent>>,
40
41    /// Errors used inside the program.
42    #[serde(skip_serializing_if = "Option::is_none", default)]
43    pub errors: Option<Vec<IdlErrorCode>>,
44
45    /// Metadata about the program (only available for programs generated by Shank).
46    #[serde(skip_serializing_if = "Option::is_none", default)]
47    pub metadata: Option<IdlMetadata>,
48}
49
50/// A constant defined in the program.
51#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
52pub struct IdlConst {
53    pub name: String,
54    #[serde(rename = "type")]
55    pub ty: IdlType,
56    pub value: String,
57}
58
59/// Program state found in the program..
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
61pub struct IdlState {
62    #[serde(rename = "struct")]
63    pub strct: IdlTypeDefinition,
64    pub methods: Vec<IdlInstruction>,
65}
66
67/// A type of event emitted by the program.
68#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
69pub struct IdlEvent {
70    pub name: String,
71    pub fields: Vec<IdlEventField>,
72}
73
74/// A field that is part of a struct of an [IdlTypeDefinition].
75#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
76pub struct IdlEventField {
77    pub name: String,
78    #[serde(rename = "type")]
79    pub ty: IdlType,
80    pub index: bool,
81}