use crate::error::{OneYoctoAttachError, PrivateCallError};
use crate::prelude::{NearGas, H256};
use aurora_engine_types::account_id::AccountId;
pub const DEFAULT_PREPAID_GAS: NearGas = NearGas::new(300_000_000_000_000);
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct Timestamp(u64);
impl Timestamp {
#[must_use]
pub const fn new(ns: u64) -> Self {
Self(ns)
}
#[must_use]
pub const fn nanos(&self) -> u64 {
self.0
}
#[must_use]
pub const fn millis(&self) -> u64 {
self.0 / 1_000_000
}
#[must_use]
pub const fn secs(&self) -> u64 {
self.0 / 1_000_000_000
}
}
pub trait Env {
fn signer_account_id(&self) -> AccountId;
fn current_account_id(&self) -> AccountId;
fn predecessor_account_id(&self) -> AccountId;
fn block_height(&self) -> u64;
fn block_timestamp(&self) -> Timestamp;
fn attached_deposit(&self) -> u128;
fn random_seed(&self) -> H256;
fn prepaid_gas(&self) -> NearGas;
fn assert_private_call(&self) -> Result<(), PrivateCallError> {
if self.predecessor_account_id() == self.current_account_id() {
Ok(())
} else {
Err(PrivateCallError)
}
}
fn assert_one_yocto(&self) -> Result<(), OneYoctoAttachError> {
if self.attached_deposit() == 1 {
Ok(())
} else {
Err(OneYoctoAttachError)
}
}
}
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Fixed {
pub signer_account_id: AccountId,
pub current_account_id: AccountId,
pub predecessor_account_id: AccountId,
pub block_height: u64,
pub block_timestamp: Timestamp,
pub attached_deposit: u128,
pub random_seed: H256,
pub prepaid_gas: NearGas,
}
impl Env for Fixed {
fn signer_account_id(&self) -> AccountId {
self.signer_account_id.clone()
}
fn current_account_id(&self) -> AccountId {
self.current_account_id.clone()
}
fn predecessor_account_id(&self) -> AccountId {
self.predecessor_account_id.clone()
}
fn block_height(&self) -> u64 {
self.block_height
}
fn block_timestamp(&self) -> Timestamp {
self.block_timestamp
}
fn attached_deposit(&self) -> u128 {
self.attached_deposit
}
fn random_seed(&self) -> H256 {
self.random_seed
}
fn prepaid_gas(&self) -> NearGas {
self.prepaid_gas
}
}