precompile_utils/solidity/modifier.rs
1// This file is part of Frontier.
2
3// Copyright (c) Moonsong Labs.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0
6
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18
19//! Provide checks related to function modifiers (view/payable).
20
21use crate::solidity::revert::{MayRevert, RevertReason};
22use fp_evm::Context;
23use sp_core::U256;
24
25/// Represents modifiers a Solidity function can be annotated with.
26#[derive(Copy, Clone, PartialEq, Eq)]
27pub enum FunctionModifier {
28 /// Function that doesn't modify the state.
29 View,
30 /// Function that modifies the state but refuse receiving funds.
31 /// Correspond to a Solidity function with no modifiers.
32 NonPayable,
33 /// Function that modifies the state and accept funds.
34 Payable,
35}
36
37/// Check that a function call is compatible with the context it is
38/// called into.
39pub fn check_function_modifier(
40 context: &Context,
41 is_static: bool,
42 modifier: FunctionModifier,
43) -> MayRevert {
44 if is_static && modifier != FunctionModifier::View {
45 return Err(
46 RevertReason::custom("Can't call non-static function in static context").into(),
47 );
48 }
49
50 if modifier != FunctionModifier::Payable && context.apparent_value > U256::zero() {
51 return Err(RevertReason::custom("Function is not payable").into());
52 }
53
54 Ok(())
55}