aurora_engine_sdk/
env.rs

1use crate::error::{OneYoctoAttachError, PrivateCallError};
2use crate::prelude::{NearGas, H256};
3use aurora_engine_types::account_id::AccountId;
4
5pub const DEFAULT_PREPAID_GAS: NearGas = NearGas::new(300_000_000_000_000);
6
7/// Timestamp represented by the number of nanoseconds since the Unix Epoch.
8#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
9pub struct Timestamp(u64);
10
11impl Timestamp {
12    #[must_use]
13    pub const fn new(ns: u64) -> Self {
14        Self(ns)
15    }
16
17    #[must_use]
18    pub const fn nanos(&self) -> u64 {
19        self.0
20    }
21
22    #[must_use]
23    pub const fn millis(&self) -> u64 {
24        self.0 / 1_000_000
25    }
26
27    #[must_use]
28    pub const fn secs(&self) -> u64 {
29        self.0 / 1_000_000_000
30    }
31}
32
33/// Returns information about the NEAR context in which the
34/// transaction is executing. In the case of a standalone binary,
35/// independent of NEAR these values would need to be mocked or otherwise
36/// passed in from an external source.
37pub trait Env {
38    /// Account ID that signed the transaction.
39    fn signer_account_id(&self) -> AccountId;
40    /// Account ID of the currently executing contract.
41    fn current_account_id(&self) -> AccountId;
42    /// Account ID which called the current contract.
43    fn predecessor_account_id(&self) -> AccountId;
44    /// Height of the current block.
45    fn block_height(&self) -> u64;
46    /// Timestamp (in ns) of the current block.
47    fn block_timestamp(&self) -> Timestamp;
48    /// Amount of NEAR attached to current call
49    fn attached_deposit(&self) -> u128;
50    /// Random seed generated for the current block
51    fn random_seed(&self) -> H256;
52    /// Prepaid NEAR Gas
53    fn prepaid_gas(&self) -> NearGas;
54    /// Used NEAR Gas
55    fn used_gas(&self) -> NearGas;
56
57    fn assert_private_call(&self) -> Result<(), PrivateCallError> {
58        if self.predecessor_account_id() == self.current_account_id() {
59            Ok(())
60        } else {
61            Err(PrivateCallError)
62        }
63    }
64
65    fn assert_one_yocto(&self) -> Result<(), OneYoctoAttachError> {
66        if self.attached_deposit() == 1 {
67            Ok(())
68        } else {
69            Err(OneYoctoAttachError)
70        }
71    }
72}
73
74/// Fully in-memory implementation of the blockchain environment with
75/// fixed values for all the fields.
76#[derive(Default, Debug, Clone, PartialEq, Eq)]
77pub struct Fixed {
78    pub signer_account_id: AccountId,
79    pub current_account_id: AccountId,
80    pub predecessor_account_id: AccountId,
81    pub block_height: u64,
82    pub block_timestamp: Timestamp,
83    pub attached_deposit: u128,
84    pub random_seed: H256,
85    pub prepaid_gas: NearGas,
86    pub used_gas: NearGas,
87}
88
89impl Env for Fixed {
90    fn signer_account_id(&self) -> AccountId {
91        self.signer_account_id.clone()
92    }
93
94    fn current_account_id(&self) -> AccountId {
95        self.current_account_id.clone()
96    }
97
98    fn predecessor_account_id(&self) -> AccountId {
99        self.predecessor_account_id.clone()
100    }
101
102    fn block_height(&self) -> u64 {
103        self.block_height
104    }
105
106    fn block_timestamp(&self) -> Timestamp {
107        self.block_timestamp
108    }
109
110    fn attached_deposit(&self) -> u128 {
111        self.attached_deposit
112    }
113
114    fn random_seed(&self) -> H256 {
115        self.random_seed
116    }
117
118    fn prepaid_gas(&self) -> NearGas {
119        self.prepaid_gas
120    }
121
122    fn used_gas(&self) -> NearGas {
123        self.used_gas
124    }
125}