xpx_supercontracts_sdk/
utils.rs

1//! Basic utils functions for communication with `WasmVM`.
2
3use crate::external;
4use crate::statuses::FunctionResult;
5use crate::transactions_type::FUNCTION_RETURN_SUCCESS;
6
7/// Constructor is function for one time call that can
8/// can invoke only once for all lifetime of SuperContract.
9///
10/// Most useful case is run some specific functionality and
11/// functions once for SuperContract life. Example: create Mosaic
12///
13/// Difference from `init` fucntion that, function can call
14/// every time when execute some SuperContract function. And
15/// for that concrete function it can call only once.
16///
17/// # Examples
18/// ```rust,no_run
19/// use xpx_supercontracts_sdk::utils::{constructor, ping};
20/// let res = constructor(|| -> i64 {
21///     let respond = ping(10);
22/// 	let res = respond.unwrap();
23///     assert_eq!(res, 11);
24/// 	res
25/// });
26/// ```
27///
28pub fn constructor(constructor_handler: fn() -> i64) -> i64 {
29    unsafe {
30        let status = external::__constructor();
31        if status != FUNCTION_RETURN_SUCCESS {
32            return status;
33        }
34    };
35    constructor_handler()
36}
37
38/// Init is function constructor that can can invoked only one time.
39///
40/// Most useful case is run some specifuc functionality and
41/// functions to tune-up and prepare some state for SuperContract.
42///
43/// It's impossible run that function twice.
44///
45/// # Examples
46/// ```rust,no_run
47/// use xpx_supercontracts_sdk::utils::{init, ping};
48/// init(|| {
49///     let respond = ping(10);
50///     assert_eq!(respond.unwrap(), 11);
51/// });
52/// ```
53///
54pub fn init(init_handler: fn() -> ()) {
55    unsafe {
56        let status = external::__init();
57        if status != 0 {
58            return;
59        }
60    };
61    init_handler();
62}
63
64/// Send ping message to `WasmVM`. Successful result should be
65/// incremented value. Useful for most simple request/response
66/// message tests for  `WasmVM`.
67///
68/// # Examples
69/// ```rust,no_run
70/// use xpx_supercontracts_sdk::utils::ping;
71/// let respond = ping(10);
72/// assert_eq!(respond.unwrap(), 11);
73/// ```
74pub fn ping(msg: usize) -> FunctionResult {
75    return unsafe { Ok(external::__ping(msg)) };
76}
77
78/// Return incremented result from all previous invoke that functions.
79///
80/// Useful for calculating some incremented state.
81///
82/// # Examples
83/// ```rust,no_run
84/// use xpx_supercontracts_sdk::utils::inc;
85/// let respond = inc();
86/// assert_eq!(respond.unwrap(), 11);
87/// ```
88pub fn inc() -> FunctionResult {
89    return unsafe { Ok(external::__inc()) };
90}
91
92/// Send debug message to `WasmVM`. It's convenient
93/// basic function for development debugging.
94/// Message that was sent will display in `WasmVM`
95/// stdout as information log message. It not affect
96/// basic Supercontract execution but should be
97/// removed from `release` version, because it
98/// will spend `Gas` (unit ticks).
99///
100/// # Examples
101/// ```rust,no_run
102/// use xpx_supercontracts_sdk::utils::debug_message;
103/// debug_message(&"Debug message from Supercontract".to_string());
104/// ```
105pub fn debug_message(msg: &String) {
106    let raw_msg = msg.as_bytes();
107    unsafe {
108        external::__write_log(raw_msg.as_ptr(), raw_msg.len());
109    };
110}