Skip to content

Commit

Permalink
Use Typed State Mutability (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyke-bot authored Sep 5, 2024
1 parent d8bbe48 commit 382c215
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/interface_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ pub fn function_state_mutability(
) -> Result<String, JsError> {
let c = decode_hex_code(code)?;
let s = decode_hex_selector(selector)?;
Ok(crate::state_mutability::function_state_mutability(&c, &s, gas_limit).to_string())
Ok(crate::state_mutability::function_state_mutability(&c, &s, gas_limit).as_json_str().to_string())
}
1 change: 1 addition & 0 deletions src/interface_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ fn function_state_mutability(

Ok(
crate::state_mutability::function_state_mutability(&code_bytes, &sel, gas_limit)
.as_json_str()
.to_string(),
)
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod utils;
pub mod selectors;

pub type Selector = [u8; 4];
pub type StateMutability = alloy_dyn_abi::parser::StateMutability;

#[cfg(feature = "python")]
mod interface_py;
Expand Down
19 changes: 9 additions & 10 deletions src/state_mutability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
Element, U256,
},
utils::execute_until_function_start,
StateMutability,
Selector,
};
use alloy_primitives::uint;
Expand Down Expand Up @@ -209,6 +210,7 @@ fn analyze_view_pure(vm: Vm<Label>, gas_limit: u32) -> ViewPureResult {
ret
}


/// Extracts function state mutability
///
/// # Arguments
Expand All @@ -217,23 +219,20 @@ fn analyze_view_pure(vm: Vm<Label>, gas_limit: u32) -> ViewPureResult {
/// * `selector` - A function selector
/// * `gas_limit` - Maximum allowed gas usage; set to `0` to use defaults
///
/// # Return value
/// `payable` | `nonpayable` | `view` | `pure`
///
/// # Examples
///
/// ```
/// use evmole::function_state_mutability;
/// use evmole::{function_state_mutability, StateMutability};
/// use alloy_primitives::hex;
///
/// let code = hex::decode("6080604052348015600e575f80fd5b50600436106030575f3560e01c80632125b65b146034578063b69ef8a8146044575b5f80fd5b6044603f3660046046565b505050565b005b5f805f606084860312156057575f80fd5b833563ffffffff811681146069575f80fd5b925060208401356001600160a01b03811681146083575f80fd5b915060408401356001600160e01b0381168114609d575f80fd5b80915050925092509256").unwrap();
/// let selector = [0x21, 0x25, 0xb6, 0x5b];
///
/// let state_mutability = function_state_mutability(&code, &selector, 0);
///
/// assert_eq!(state_mutability, "pure");
/// assert_eq!(state_mutability, StateMutability::Pure);
/// ```
pub fn function_state_mutability(code: &[u8], selector: &Selector, gas_limit: u32) -> &'static str {
pub fn function_state_mutability(code: &[u8], selector: &Selector, gas_limit: u32) -> StateMutability {
let mut cd: [u8; 32] = [0; 32];
cd[0..4].copy_from_slice(selector);
let vm = Vm::<Label>::new(
Expand All @@ -252,16 +251,16 @@ pub fn function_state_mutability(code: &[u8], selector: &Selector, gas_limit: u3

let (is_payable, gas_used) = analyze_payable(vm.clone(), real_gas_limit / 2, 1);
if is_payable {
"payable"
StateMutability::Payable
} else {
let gas_remaining = real_gas_limit - gas_used.min(real_gas_limit / 2);
let vpr = analyze_view_pure(vm, gas_remaining);
if vpr.pure {
"pure"
StateMutability::Pure
} else if vpr.view {
"view"
StateMutability::View
} else {
"nonpayable"
StateMutability::NonPayable
}
}
}

0 comments on commit 382c215

Please sign in to comment.