Skip to content

Commit 199e307

Browse files
authored
constant -> option (rust-ethereum#256)
As noted in the deprecation warning, when parsing compiler output, ethabi fills in a `false`. This causes trouble in `ethers` and `foundry` by making it so compiler outputted ABIs aren't parseable by `ethers.js` due to bad `constant` settings (i.e. `false` for a `pure`). This PR fixes that by keeping it concrete for derive, but making it optional in the main implementation. This allows us to parse json from the solc compiler correctly and pipe that out to a valid ABI json. This could be an incorrect implementation, but it seems to work
1 parent 7f4bb3d commit 199e307

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

derive/src/function.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ pub struct Function {
6161
/// Function output params.
6262
outputs: Outputs,
6363
#[deprecated(note = "The constant attribute was removed in Solidity 0.5.0 and has been \
64-
replaced with stateMutability. If parsing a JSON AST created with \
65-
this version or later this value will always be false, which may be wrong.")]
64+
replaced with stateMutability.")]
6665
/// Constant function.
6766
constant: bool,
6867
/// Whether the function reads or modifies blockchain state
@@ -139,7 +138,7 @@ impl<'a> From<&'a ethabi::Function> for Function {
139138
result: output_result,
140139
recreate_quote: to_ethabi_param_vec(&f.outputs),
141140
},
142-
constant: f.constant,
141+
constant: f.constant.unwrap_or_default(),
143142
state_mutability: f.state_mutability,
144143
}
145144
}
@@ -176,7 +175,7 @@ impl Function {
176175
name: #name.into(),
177176
inputs: #recreate_inputs,
178177
outputs: #recreate_outputs,
179-
constant: #constant,
178+
constant: Some(#constant),
180179
state_mutability: #state_mutability
181180
}
182181
}
@@ -227,7 +226,7 @@ mod tests {
227226
name: "empty".into(),
228227
inputs: vec![],
229228
outputs: vec![],
230-
constant: false,
229+
constant: None,
231230
state_mutability: ethabi::StateMutability::Payable,
232231
};
233232

@@ -243,7 +242,7 @@ mod tests {
243242
name: "empty".into(),
244243
inputs: vec![],
245244
outputs: vec![],
246-
constant: false,
245+
constant: Some(false),
247246
state_mutability: ::ethabi::StateMutability::Payable
248247
}
249248
}
@@ -295,7 +294,7 @@ mod tests {
295294
kind: ethabi::ParamType::Uint(256),
296295
internal_type: None,
297296
}],
298-
constant: false,
297+
constant: None,
299298
state_mutability: ethabi::StateMutability::Payable,
300299
};
301300

@@ -319,7 +318,7 @@ mod tests {
319318
kind: ethabi::ParamType::Uint(256usize),
320319
internal_type: None
321320
}],
322-
constant: false,
321+
constant: Some(false),
323322
state_mutability: ::ethabi::StateMutability::Payable
324323
}
325324
}
@@ -381,7 +380,7 @@ mod tests {
381380
ethabi::Param { name: "".into(), kind: ethabi::ParamType::Uint(256), internal_type: None },
382381
ethabi::Param { name: "".into(), kind: ethabi::ParamType::String, internal_type: None },
383382
],
384-
constant: false,
383+
constant: None,
385384
state_mutability: ethabi::StateMutability::Payable,
386385
};
387386

@@ -413,7 +412,7 @@ mod tests {
413412
kind: ethabi::ParamType::String,
414413
internal_type: None
415414
}],
416-
constant: false,
415+
constant: Some(false),
417416
state_mutability: ::ethabi::StateMutability::Payable
418417
}
419418
}

ethabi/src/contract.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ mod test {
366366
kind: ParamType::Address,
367367
internal_type: None,
368368
}],
369-
constant: false,
369+
constant: None,
370370
state_mutability: Default::default(),
371371
}]
372372
),
@@ -376,7 +376,7 @@ mod test {
376376
name: "bar".to_string(),
377377
inputs: vec![],
378378
outputs: vec![],
379-
constant: false,
379+
constant: None,
380380
state_mutability: Default::default(),
381381
}]
382382
),
@@ -441,14 +441,14 @@ mod test {
441441
kind: ParamType::Address,
442442
internal_type: None,
443443
}],
444-
constant: false,
444+
constant: None,
445445
state_mutability: Default::default(),
446446
},
447447
Function {
448448
name: "foo".to_string(),
449449
inputs: vec![],
450450
outputs: vec![],
451-
constant: false,
451+
constant: None,
452452
state_mutability: Default::default(),
453453
},
454454
]

ethabi/src/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
633633
},
634634
],
635635
outputs: vec![],
636-
constant: false,
636+
constant: None,
637637
state_mutability: crate::StateMutability::default(),
638638
}
639639
};
@@ -670,7 +670,7 @@ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
670670
},
671671
],
672672
outputs: vec![],
673-
constant: false,
673+
constant: None,
674674
state_mutability: crate::StateMutability::default(),
675675
}
676676
};

ethabi/src/function.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ pub struct Function {
2929
/// Function output.
3030
pub outputs: Vec<Param>,
3131
#[deprecated(note = "The constant attribute was removed in Solidity 0.5.0 and has been \
32-
replaced with stateMutability. If parsing a JSON AST created with \
33-
this version or later this value will always be false, which may be wrong.")]
32+
replaced with stateMutability.")]
3433
/// Constant function.
3534
#[cfg_attr(feature = "full-serde", serde(default))]
36-
pub constant: bool,
35+
pub constant: Option<bool>,
3736
/// Whether the function reads or modifies blockchain state
3837
#[cfg_attr(feature = "full-serde", serde(rename = "stateMutability", default))]
3938
pub state_mutability: StateMutability,
@@ -116,7 +115,7 @@ mod tests {
116115
Param { name: "b".to_owned(), kind: ParamType::Bool, internal_type: None },
117116
],
118117
outputs: vec![],
119-
constant: false,
118+
constant: None,
120119
state_mutability: StateMutability::Payable,
121120
};
122121

ethabi/src/operation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod tests {
5959
name: "foo".to_owned(),
6060
inputs: vec![Param { name: "a".to_owned(), kind: ParamType::Address, internal_type: None }],
6161
outputs: vec![],
62-
constant: false,
62+
constant: None,
6363
state_mutability: StateMutability::NonPayable,
6464
};
6565
assert_eq!(deserialized, Operation::Function(function));

0 commit comments

Comments
 (0)