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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Exposes the public API to communicate with the host.

use odra_mock_vm_types::{Address, Balance, BlockTime, MockVMType, OdraType};
use odra_types::{event::OdraEvent, ExecutionError};

use crate::{borrow_env, native_token::NativeTokenMetadata};

/// Returns the current block time.
pub fn get_block_time() -> BlockTime {
    borrow_env().get_block_time()
}

/// Gets the address of the currently executing contract.
pub fn caller() -> Address {
    borrow_env().caller()
}

/// Returns the address of currently executing contract.
pub fn self_address() -> Address {
    borrow_env().callee()
}

/// Stores the `value` under `key`.
pub fn set_var<T: MockVMType>(key: &str, value: T) {
    borrow_env().set_var(key, value)
}

/// Gets a value stored under `key`.
pub fn get_var<T: OdraType>(key: &str) -> Option<T> {
    borrow_env().get_var(key)
}

/// Puts a key-value into a collection.
pub fn set_dict_value<K: MockVMType, V: MockVMType>(dict: &str, key: &K, value: V) {
    borrow_env().set_dict_value(dict, key.ser().unwrap().as_slice(), value)
}

/// Gets the value from the `dict` collection under `key`.
pub fn get_dict_value<K: MockVMType, T: MockVMType>(dict: &str, key: &K) -> Option<T> {
    let key = key.ser().unwrap();
    let key = key.as_slice();
    borrow_env().get_dict_value(dict, key)
}

/// Stops execution of a contract and reverts execution effects with a given [`ExecutionError`].
pub fn revert<E>(error: E) -> !
where
    E: Into<ExecutionError>
{
    let execution_error: ExecutionError = error.into();
    borrow_env().revert(execution_error.into());
    panic!("OdraRevert")
}

/// Sends an event to the execution environment.
pub fn emit_event<T: OdraType + OdraEvent>(event: T) {
    let event_data = event.ser().unwrap();
    borrow_env().emit_event(&event_data);
}

/// Returns amount of native token attached to the call.
pub fn attached_value() -> Balance {
    borrow_env().attached_value()
}

/// Returns the value that represents one native token.
pub fn one_token() -> Balance {
    Balance::one()
}

/// Transfers native token from the contract caller to the given address.
pub fn transfer_tokens<B: Into<Balance>>(to: Address, amount: B) -> bool {
    let callee = borrow_env().callee();
    borrow_env().transfer_tokens(callee, to, amount.into())
}

/// Returns the balance of the account associated with the current contract.
pub fn self_balance() -> Balance {
    borrow_env().self_balance()
}

/// Returns the platform native token metadata
pub fn native_token_metadata() -> NativeTokenMetadata {
    NativeTokenMetadata::new()
}