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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
//! Basic utils functions for communication with `WasmVM`.

use crate::external;
use crate::statuses::FunctionResult;
use crate::transactions_type::FUNCTION_RETURN_SUCCESS;

/// Constructor is function for one time call that can
/// can invoke only once for all lifetime of SuperContract.
///
/// Most useful case is run some specific functionality and
/// functions once for SuperContract life. Example: create Mosaic
///
/// Difference from `init` fucntion that, function can call
/// every time when execute some SuperContract function. And
/// for that concrete function it can call only once.
///
/// # Examples
/// ```rust,no_run
/// use xpx_supercontracts_sdk::utils::{constructor, ping};
/// let res = constructor(|| -> i64 {
///     let respond = ping(10);
/// 	let res = respond.unwrap();
///     assert_eq!(res, 11);
/// 	res
/// });
/// ```
///
pub fn constructor(constructor_handler: fn() -> i64) -> i64 {
    unsafe {
        let status = external::__constructor();
        if status != FUNCTION_RETURN_SUCCESS {
            return status;
        }
    };
    constructor_handler()
}

/// Init is function constructor that can can invoked only one time.
///
/// Most useful case is run some specifuc functionality and
/// functions to tune-up and prepare some state for SuperContract.
///
/// It's impossible run that function twice.
///
/// # Examples
/// ```rust,no_run
/// use xpx_supercontracts_sdk::utils::{init, ping};
/// init(|| {
///     let respond = ping(10);
///     assert_eq!(respond.unwrap(), 11);
/// });
/// ```
///
pub fn init(init_handler: fn() -> ()) {
    unsafe {
        let status = external::__init();
        if status != 0 {
            return;
        }
    };
    init_handler();
}

/// Send ping message to `WasmVM`. Successful result should be
/// incremented value. Useful for most simple request/response
/// message tests for  `WasmVM`.
///
/// # Examples
/// ```rust,no_run
/// use xpx_supercontracts_sdk::utils::ping;
/// let respond = ping(10);
/// assert_eq!(respond.unwrap(), 11);
/// ```
pub fn ping(msg: usize) -> FunctionResult {
    return unsafe { Ok(external::__ping(msg)) };
}

/// Return incremented result from all previous invoke that functions.
///
/// Useful for calculating some incremented state.
///
/// # Examples
/// ```rust,no_run
/// use xpx_supercontracts_sdk::utils::inc;
/// let respond = inc();
/// assert_eq!(respond.unwrap(), 11);
/// ```
pub fn inc() -> FunctionResult {
    return unsafe { Ok(external::__inc()) };
}

/// Send debug message to `WasmVM`. It's convenient
/// basic function for development debugging.
/// Message that was sent will display in `WasmVM`
/// stdout as information log message. It not affect
/// basic Supercontract execution but should be
/// removed from `release` version, because it
/// will spend `Gas` (unit ticks).
///
/// # Examples
/// ```rust,no_run
/// use xpx_supercontracts_sdk::utils::debug_message;
/// debug_message(&"Debug message from Supercontract".to_string());
/// ```
pub fn debug_message(msg: &String) {
    let raw_msg = msg.as_bytes();
    unsafe {
        external::__write_log(raw_msg.as_ptr(), raw_msg.len());
    };
}