1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
mod external;
mod mocked_blockchain;
mod receipt;

pub(crate) use self::external::SdkExternal;
pub use self::mocked_blockchain::MockedBlockchain;
pub use self::receipt::{Receipt, VmAction};
use crate::AccountId;
use core::cell::RefCell;
use near_primitives_core::account::id::ParseAccountError;

thread_local! {
    /// Low-level blockchain interface wrapped by the environment. Prefer using `env::*` and
    /// `testing_env` for interacting with the real and fake blockchains.
    static BLOCKCHAIN_INTERFACE: RefCell<MockedBlockchain>
         = RefCell::new(MockedBlockchain::default());
}

/// Perform function on a mutable reference to the [`MockedBlockchain`]. This can only be used
/// inside tests.
pub fn with_mocked_blockchain<F, R>(f: F) -> R
where
    F: FnOnce(&mut MockedBlockchain) -> R,
{
    BLOCKCHAIN_INTERFACE.with(|b| f(&mut b.borrow_mut()))
}

impl From<near_vm_logic::types::AccountId> for AccountId {
    fn from(id: near_vm_logic::types::AccountId) -> Self {
        Self::new_unchecked(String::from(id))
    }
}

impl std::convert::TryFrom<AccountId> for near_vm_logic::types::AccountId {
    type Error = ParseAccountError;

    fn try_from(value: AccountId) -> Result<Self, Self::Error> {
        value.as_str().parse()
    }
}