Skip to content

Commit

Permalink
Fixed mutabilityState calculation for function fragments (#762).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Apr 4, 2020
1 parent da412f6 commit 6526de0
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/abi/src.ts/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,15 @@ function verifyState(value: any): { constant: boolean, payable: boolean, stateMu
if (value.stateMutability != null) {
result.stateMutability = value.stateMutability;

// Set (and check things are consistent) the constant property
result.constant = (result.stateMutability === "view" || result.stateMutability === "pure");
if (value.constant != null) {
if ((!!value.constant) !== result.constant) {
throw new Error("cannot have constant function with mutability " + result.stateMutability);
}
}

// Set (and check things are consistent) the payable property
result.payable = (result.stateMutability === "payable");
if (value.payable != null) {
if ((!!value.payable) !== result.payable) {
Expand All @@ -623,16 +625,31 @@ function verifyState(value: any): { constant: boolean, payable: boolean, stateMu

} else if (value.payable != null) {
result.payable = !!value.payable;
result.stateMutability = (result.payable ? "payable": "nonpayable");
result.constant = !result.payable;
if (value.constant != null && (value.constant !== result.constant)) {

// If payable we can assume non-constant; otherwise we can't assume
if (value.constant == null && !result.payable) {
throw new Error("unable to determine stateMutability");
}

result.constant = !!value.constant;

if (result.constant) {
result.stateMutability = "view";
} else {
result.stateMutability = (result.payable ? "payable": "nonpayable");
}

if (result.payable && result.constant) {
throw new Error("cannot have constant payable function");
}

} else if (value.constant != null) {
result.constant = !!value.constant;
result.payable = !result.constant;
result.stateMutability = (result.constant ? "view": "payable");

} else {
throw new Error("unable to determine stateMutability");
}

return result;
Expand Down

0 comments on commit 6526de0

Please sign in to comment.