Skip to content

Commit

Permalink
Default stateMutability to payable in ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed May 9, 2022
1 parent c6edbe8 commit 1ad7e31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion crates/abi/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::elements::{
Component, Contract, Event, EventField, FuncInput, FuncOutput, FuncType, Function, JsonAbi,
ModuleAbis,
ModuleAbis, StateMutability,
};
use crate::AbiError;
use fe_analyzer::namespace::items::{ContractId, FunctionId, ModuleId};
Expand Down Expand Up @@ -97,6 +97,7 @@ fn function_def(db: &dyn AnalyzerDb, name: &str, fn_id: FunctionId, typ: FuncTyp
typ,
inputs,
outputs,
state_mutability: StateMutability::Payable,
}
}

Expand Down
12 changes: 9 additions & 3 deletions crates/abi/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub struct EventField {

/// A function interface.
#[derive(Serialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Function {
/// The function's name.
pub name: String,
Expand All @@ -183,6 +184,8 @@ pub struct Function {
pub inputs: Vec<FuncInput>,
/// All function outputs.
pub outputs: Vec<FuncOutput>,
/// Mutability of the function
pub state_mutability: StateMutability,
}

/// Component of an ABI tuple.
Expand Down Expand Up @@ -250,7 +253,6 @@ pub enum FuncType {
}

/// The mutability of a public function.
#[allow(dead_code)]
#[derive(Serialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "lowercase")]
pub enum StateMutability {
Expand All @@ -262,7 +264,9 @@ pub enum StateMutability {

#[cfg(test)]
mod tests {
use crate::elements::{Contract, Event, EventField, FuncInput, FuncOutput, FuncType, Function};
use crate::elements::{
Contract, Event, EventField, FuncInput, FuncOutput, FuncType, Function, StateMutability,
};

#[test]
fn contract_json() {
Expand Down Expand Up @@ -291,6 +295,7 @@ mod tests {
typ: "uint256".to_string(),
components: vec![],
}],
state_mutability: StateMutability::Payable,
}],
};

Expand All @@ -313,7 +318,8 @@ mod tests {
"name":"function_name",
"type":"function",
"inputs":[{"name":"input_name","type":"address"}],
"outputs":[{"name":"output_name","type":"uint256"}]
"outputs":[{"name":"output_name","type":"uint256"}],
"stateMutability":"payable"
}
]"#
.split_whitespace()
Expand Down
5 changes: 5 additions & 0 deletions newsfragments/705.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Temporary default stateMutability to payable in ABI

The ABI metadata that the compiler currently generates does not include the `stateMutability` field. This piece of information is important for tooling such as hardhat because it determines whether a function needs to be called with or without sending a transaction.

As soon as we have support for `mut self` and `mut ctx` we will be able to derive that information from the function signature. In the meantime we now default to `payable`.

0 comments on commit 1ad7e31

Please sign in to comment.