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
use chrono::{DateTime, Utc};
use ic_cdk::api::time;
use ic_cdk::export::candid::{CandidType, Deserialize, Principal};
use ic_cdk::{caller, print};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub mod management_canister_client;

#[derive(Clone, PartialEq, Eq, Hash, CandidType, Deserialize)]
pub struct RemoteCallEndpoint {
    pub canister_id: Principal,
    pub method_name: String,
}

#[derive(Clone, CandidType, Deserialize)]
pub struct RemoteCallPayload {
    pub endpoint: RemoteCallEndpoint,
    pub args_raw: Vec<u8>,
    pub cycles: u64,
}

#[inline(always)]
pub fn log(msg: &str) {
    print(format!(
        "{}: [caller: {}]: {}",
        make_time(time()),
        caller(),
        msg
    ))
}

fn make_time(nanos: u64) -> String {
    let d = UNIX_EPOCH + Duration::from_nanos(nanos);
    let datetime = DateTime::<Utc>::from(d);

    datetime.format("%Y-%m-%d %H:%M:%S.%f").to_string()
}

#[inline(always)]
pub fn random_principal_test() -> Principal {
    Principal::from_slice(
        &SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos()
            .to_be_bytes(),
    )
}