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#[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
33pub trait Env {
38 fn signer_account_id(&self) -> AccountId;
40 fn current_account_id(&self) -> AccountId;
42 fn predecessor_account_id(&self) -> AccountId;
44 fn block_height(&self) -> u64;
46 fn block_timestamp(&self) -> Timestamp;
48 fn attached_deposit(&self) -> u128;
50 fn random_seed(&self) -> H256;
52 fn prepaid_gas(&self) -> NearGas;
54 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#[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}