aurora_engine_precompiles/
account_ids.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::account_id::AccountId;
6use aurora_evm::{Context, ExitError};
7
8mod costs {
9    use crate::prelude::types::EthGas;
10
11    // TODO(#483): Determine the correct amount of gas
12    pub(super) const PREDECESSOR_ACCOUNT_GAS: EthGas = EthGas::new(0);
13    // TODO(#483): Determine the correct amount of gas
14    #[allow(dead_code)]
15    pub(super) const CURRENT_ACCOUNT_GAS: EthGas = EthGas::new(0);
16}
17
18pub struct PredecessorAccount<'a, E> {
19    env: &'a E,
20}
21
22pub mod predecessor_account {
23    use aurora_engine_types::types::{make_address, Address};
24
25    /// `predecessor_account_id` precompile address
26    ///
27    /// Address: `0x723ffbaba940e75e7bf5f6d61dcbf8d9a4de0fd7`
28    /// This address is computed as: `&keccak("predecessorAccountId")[12..]`
29    pub const ADDRESS: Address = make_address(0x723ffbab, 0xa940e75e7bf5f6d61dcbf8d9a4de0fd7);
30}
31
32impl<'a, E> PredecessorAccount<'a, E> {
33    pub const fn new(env: &'a E) -> Self {
34        Self { env }
35    }
36}
37
38impl<E: Env> Precompile for PredecessorAccount<'_, E> {
39    fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError> {
40        Ok(costs::PREDECESSOR_ACCOUNT_GAS)
41    }
42
43    fn run(
44        &self,
45        input: &[u8],
46        target_gas: Option<EthGas>,
47        context: &Context,
48        _is_static: bool,
49    ) -> EvmPrecompileResult {
50        utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
51        let cost = Self::required_gas(input)?;
52        if let Some(target_gas) = target_gas {
53            if cost > target_gas {
54                return Err(ExitError::OutOfGas);
55            }
56        }
57
58        let predecessor_account_id = self.env.predecessor_account_id();
59        Ok(PrecompileOutput::without_logs(
60            cost,
61            predecessor_account_id.as_bytes().to_vec(),
62        ))
63    }
64}
65
66pub struct CurrentAccount {
67    current_account_id: AccountId,
68}
69
70impl CurrentAccount {
71    /// `current_account_id` precompile address
72    ///
73    /// Address: `0xfefae79e4180eb0284f261205e3f8cea737aff56`
74    /// This address is computed as: `&keccak("currentAccountId")[12..]`
75    pub const ADDRESS: Address = make_address(0xfefae79e, 0x4180eb0284f261205e3f8cea737aff56);
76
77    #[must_use]
78    pub const fn new(current_account_id: AccountId) -> Self {
79        Self { current_account_id }
80    }
81}
82
83impl Precompile for CurrentAccount {
84    fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError> {
85        Ok(costs::PREDECESSOR_ACCOUNT_GAS)
86    }
87
88    fn run(
89        &self,
90        input: &[u8],
91        target_gas: Option<EthGas>,
92        context: &Context,
93        _is_static: bool,
94    ) -> EvmPrecompileResult {
95        utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
96        let cost = Self::required_gas(input)?;
97        if let Some(target_gas) = target_gas {
98            if cost > target_gas {
99                return Err(ExitError::OutOfGas);
100            }
101        }
102
103        Ok(PrecompileOutput::without_logs(
104            cost,
105            self.current_account_id.as_bytes().to_vec(),
106        ))
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use crate::account_ids::{predecessor_account, CurrentAccount};
113    use crate::prelude::sdk::types::near_account_to_evm_address;
114
115    #[test]
116    fn test_predecessor_account_precompile_id() {
117        assert_eq!(
118            predecessor_account::ADDRESS,
119            near_account_to_evm_address(b"predecessorAccountId")
120        );
121    }
122
123    #[test]
124    fn test_current_account_precompile_id() {
125        assert_eq!(
126            CurrentAccount::ADDRESS,
127            near_account_to_evm_address(b"currentAccountId")
128        );
129    }
130}