aurora_engine_precompiles/
prepaid_gas.rs

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