|
| 1 | +//! ABI JSON types and extraction for Edge contracts. |
| 2 | +//! |
| 3 | +//! Produces Ethereum-compatible ABI descriptors from type-checked contract info. |
| 4 | +
|
| 5 | +use edge_ast::item::EventDecl; |
| 6 | +use serde::Serialize; |
| 7 | + |
| 8 | +use crate::checker::{ContractInfo, TypeChecker}; |
| 9 | + |
| 10 | +/// State mutability of a function in the ABI. |
| 11 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] |
| 12 | +#[serde(rename_all = "camelCase")] |
| 13 | +pub enum StateMutability { |
| 14 | + /// Function does not read or write state |
| 15 | + Pure, |
| 16 | + /// Function reads but does not write state |
| 17 | + View, |
| 18 | + /// Function may write state (no ETH value accepted) |
| 19 | + NonPayable, |
| 20 | + /// Function accepts ETH value |
| 21 | + Payable, |
| 22 | +} |
| 23 | + |
| 24 | +/// A single parameter in an ABI function or event. |
| 25 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] |
| 26 | +#[serde(rename_all = "camelCase")] |
| 27 | +pub struct AbiParam { |
| 28 | + /// Parameter name (empty string for unnamed return values) |
| 29 | + pub name: String, |
| 30 | + /// Solidity ABI type string (e.g. "uint256", "address") |
| 31 | + #[serde(rename = "type")] |
| 32 | + pub ty: String, |
| 33 | +} |
| 34 | + |
| 35 | +/// A single parameter in an ABI event. |
| 36 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] |
| 37 | +#[serde(rename_all = "camelCase")] |
| 38 | +pub struct AbiEventParam { |
| 39 | + /// Parameter name |
| 40 | + pub name: String, |
| 41 | + /// Solidity ABI type string |
| 42 | + #[serde(rename = "type")] |
| 43 | + pub ty: String, |
| 44 | + /// Whether this parameter is indexed |
| 45 | + pub indexed: bool, |
| 46 | +} |
| 47 | + |
| 48 | +/// A top-level ABI entry (function or event). |
| 49 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] |
| 50 | +#[serde(tag = "type", rename_all = "camelCase")] |
| 51 | +pub enum AbiEntry { |
| 52 | + /// A function entry |
| 53 | + Function(AbiFunctionEntry), |
| 54 | + /// An event entry |
| 55 | + Event(AbiEventEntry), |
| 56 | +} |
| 57 | + |
| 58 | +/// ABI descriptor for a function. |
| 59 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] |
| 60 | +#[serde(rename_all = "camelCase")] |
| 61 | +pub struct AbiFunctionEntry { |
| 62 | + /// Function name |
| 63 | + pub name: String, |
| 64 | + /// Input parameters |
| 65 | + pub inputs: Vec<AbiParam>, |
| 66 | + /// Output parameters |
| 67 | + pub outputs: Vec<AbiParam>, |
| 68 | + /// State mutability |
| 69 | + pub state_mutability: StateMutability, |
| 70 | +} |
| 71 | + |
| 72 | +/// ABI descriptor for an event. |
| 73 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] |
| 74 | +#[serde(rename_all = "camelCase")] |
| 75 | +pub struct AbiEventEntry { |
| 76 | + /// Event name |
| 77 | + pub name: String, |
| 78 | + /// Event parameters |
| 79 | + pub inputs: Vec<AbiEventParam>, |
| 80 | + /// Whether the event is anonymous |
| 81 | + pub anonymous: bool, |
| 82 | +} |
| 83 | + |
| 84 | +/// Extract an Ethereum-compatible ABI from a type-checked contract and top-level events. |
| 85 | +pub fn extract_abi(contract: &ContractInfo, events: &[EventDecl]) -> Vec<AbiEntry> { |
| 86 | + let mut entries = Vec::new(); |
| 87 | + |
| 88 | + // Functions: only include public functions |
| 89 | + for func in &contract.functions { |
| 90 | + if !func.is_pub { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + let inputs = func |
| 95 | + .params |
| 96 | + .iter() |
| 97 | + .map(|(name, ty)| AbiParam { |
| 98 | + name: name.clone(), |
| 99 | + ty: TypeChecker::type_to_abi_string(ty), |
| 100 | + }) |
| 101 | + .collect(); |
| 102 | + |
| 103 | + let outputs = func |
| 104 | + .returns |
| 105 | + .iter() |
| 106 | + .map(|ty| AbiParam { |
| 107 | + name: String::new(), |
| 108 | + ty: TypeChecker::type_to_abi_string(ty), |
| 109 | + }) |
| 110 | + .collect(); |
| 111 | + |
| 112 | + let state_mutability = if func.is_mut { |
| 113 | + StateMutability::NonPayable |
| 114 | + } else { |
| 115 | + StateMutability::View |
| 116 | + }; |
| 117 | + |
| 118 | + entries.push(AbiEntry::Function(AbiFunctionEntry { |
| 119 | + name: func.name.clone(), |
| 120 | + inputs, |
| 121 | + outputs, |
| 122 | + state_mutability, |
| 123 | + })); |
| 124 | + } |
| 125 | + |
| 126 | + // Events |
| 127 | + for event in events { |
| 128 | + let inputs = event |
| 129 | + .fields |
| 130 | + .iter() |
| 131 | + .map(|field| AbiEventParam { |
| 132 | + name: field.name.name.clone(), |
| 133 | + ty: TypeChecker::type_to_abi_string(&field.ty), |
| 134 | + indexed: field.indexed, |
| 135 | + }) |
| 136 | + .collect(); |
| 137 | + |
| 138 | + entries.push(AbiEntry::Event(AbiEventEntry { |
| 139 | + name: event.name.name.clone(), |
| 140 | + inputs, |
| 141 | + anonymous: event.is_anon, |
| 142 | + })); |
| 143 | + } |
| 144 | + |
| 145 | + entries |
| 146 | +} |
0 commit comments