aurora_engine_precompiles/
prepaid_gas.rs

1use super::{EvmPrecompileResult, Precompile};
2use crate::prelude::types::{make_address, Address, EthGas};
3use crate::{utils, PrecompileOutput};
4use aurora_engine_sdk::env::Env;
5use aurora_engine_types::U256;
6use aurora_evm::{Context, ExitError};
7
8/// `prepaid_gas` precompile address
9///
10/// Address: `0x536822d27de53629ef1f84c60555689e9488609f`
11/// This address is computed as: `&keccak("prepaidGas")[12..]`
12pub const ADDRESS: Address = make_address(0x536822d2, 0x7de53629ef1f84c60555689e9488609f);
13
14mod costs {
15    use crate::prelude::types::EthGas;
16
17    // TODO(#483): Determine the correct amount of gas
18    pub(super) const PREPAID_GAS_COST: EthGas = EthGas::new(0);
19}
20
21pub struct PrepaidGas<'a, E> {
22    env: &'a E,
23}
24
25impl<'a, E> PrepaidGas<'a, E> {
26    pub const fn new(env: &'a E) -> Self {
27        Self { env }
28    }
29}
30
31impl<E: Env> Precompile for PrepaidGas<'_, E> {
32    fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError> {
33        Ok(costs::PREPAID_GAS_COST)
34    }
35
36    fn run(
37        &self,
38        input: &[u8],
39        target_gas: Option<EthGas>,
40        context: &Context,
41        _is_static: bool,
42    ) -> EvmPrecompileResult {
43        utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
44        let cost = Self::required_gas(input)?;
45        if let Some(target_gas) = target_gas {
46            if cost > target_gas {
47                return Err(ExitError::OutOfGas);
48            }
49        }
50
51        let prepaid_gas = self.env.prepaid_gas();
52        let bytes = U256::from(prepaid_gas.as_u64()).to_big_endian();
53        Ok(PrecompileOutput::without_logs(cost, bytes.to_vec()))
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::prelude::sdk::types::near_account_to_evm_address;
60    use crate::prepaid_gas;
61
62    #[test]
63    fn test_prepaid_gas_precompile_id() {
64        assert_eq!(
65            prepaid_gas::ADDRESS,
66            near_account_to_evm_address(b"prepaidGas")
67        );
68    }
69}