1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use serde::{Deserialize, Serialize};

use crate::{
    idl_error_code::IdlErrorCode, idl_instruction::IdlInstruction, idl_metadata::IdlMetadata,
};

use super::{idl_type::IdlType, idl_type_definition::IdlTypeDefinition};

/// IDL that is compatible with what anchor and shank extract from a solana program.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Idl {
    /// Version of the Solana contract at the time the IDL was generated.
    pub version: String,

    /// The name of the program.
    pub name: String,

    /// Program constants.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub constants: Vec<IdlConst>,

    /// Instructions that are handled by the program.
    pub instructions: Vec<IdlInstruction>,

    /// Program state.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub state: Option<IdlState>,

    /// Accounts holding data that are used by the program.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub accounts: Vec<IdlTypeDefinition>,

    /// Types defined in the program that are used by account structs.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub types: Vec<IdlTypeDefinition>,

    /// Events emitted by the program.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub events: Option<Vec<IdlEvent>>,

    /// Errors used inside the program.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub errors: Option<Vec<IdlErrorCode>>,

    /// Metadata about the program (only available for programs generated by Shank).
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub metadata: Option<IdlMetadata>,
}

/// A constant defined in the program.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IdlConst {
    pub name: String,
    #[serde(rename = "type")]
    pub ty: IdlType,
    pub value: String,
}

/// Program state found in the program..
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IdlState {
    #[serde(rename = "struct")]
    pub strct: IdlTypeDefinition,
    pub methods: Vec<IdlInstruction>,
}

/// A type of event emitted by the program.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IdlEvent {
    pub name: String,
    pub fields: Vec<IdlEventField>,
}

/// A field that is part of a struct of an [IdlTypeDefinition].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IdlEventField {
    pub name: String,
    #[serde(rename = "type")]
    pub ty: IdlType,
    pub index: bool,
}