Skip to main content

huff_neo_utils/ast/
abi.rs

1use crate::ast::span::AstSpan;
2use crate::token::Literal;
3use serde::{Deserialize, Serialize};
4
5/// An argument's location
6#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
7pub enum ArgumentLocation {
8    /// Memory location
9    #[default]
10    Memory,
11    /// Storage location
12    Storage,
13    /// Calldata location
14    Calldata,
15}
16
17/// A function or event argument
18#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
19pub struct Argument {
20    /// Type of the argument
21    pub arg_type: Option<String>,
22    /// Optional Argument Location
23    pub arg_location: Option<ArgumentLocation>,
24    /// The name of the argument
25    pub name: Option<String>,
26    /// Is the argument indexed? TODO: should be valid for event arguments ONLY
27    pub indexed: bool,
28    /// The argument span
29    pub span: AstSpan,
30}
31
32/// A Function Signature
33#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
34pub struct FunctionDefinition {
35    /// The name of the function
36    pub name: String,
37    /// The function signature
38    pub signature: [u8; 4],
39    /// The parameters of the function
40    pub inputs: Vec<Argument>,
41    /// The function type
42    pub fn_type: FunctionType,
43    /// The return values of the function
44    pub outputs: Vec<Argument>,
45    /// The span of the function
46    pub span: AstSpan,
47}
48
49/// Function Types
50#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
51#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
52#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
53pub enum FunctionType {
54    /// Viewable Function
55    View,
56    /// Payable Function
57    Payable,
58    /// Non Payable Function
59    NonPayable,
60    /// Pure Function
61    Pure,
62}
63
64impl FunctionType {
65    /// Get the string representation of the function type for usage in Solidity interface
66    /// generation.
67    pub fn interface_mutability(&self) -> &str {
68        match self {
69            FunctionType::View => " view",
70            FunctionType::Pure => " pure",
71            _ => "", // payable / nonpayable types not valid in Solidity interfaces
72        }
73    }
74}
75
76/// An Event Signature
77#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
78pub struct EventDefinition {
79    /// The name of the event
80    pub name: String,
81    /// The parameters of the event
82    pub parameters: Vec<Argument>,
83    /// The event span
84    pub span: AstSpan,
85    /// The event hash
86    pub hash: Literal,
87}