we_cdk/macros/
utils.rs

1/// Base58 string to bytes conversion
2///
3/// # Result
4/// The result of execution is `&[u8]` bytes
5///
6/// # Usage
7/// ```
8/// use we_cdk::*;
9///
10/// #[action]
11/// fn _constructor() {
12///     let address: Binary = base58!("3NzkzibVRkKUzaRzjUxndpTPvoBzQ3iLng3");
13/// }
14/// ```
15#[macro_export]
16macro_rules! base58 {
17    ($value:expr) => {{
18        let (error, ptr, len) = wevm::v0::bindings::base_58($value.as_ptr(), $value.len());
19        error!(error);
20        core::slice::from_raw_parts(ptr, len)
21    }};
22}
23
24/// Bytes to Base58 string conversion
25///
26/// # Result
27/// The result of execution is `&[u8]` bytes
28///
29/// # Usage
30/// ```
31/// use we_cdk::*;
32///
33/// #[action]
34/// fn _constructor() {
35///     let address: Binary = base58!("3NzkzibVRkKUzaRzjUxndpTPvoBzQ3iLng3");
36///     let address_string: String = to_base58_string!(address);
37/// }
38/// ```
39#[macro_export]
40macro_rules! to_base58_string {
41    ($value:expr) => {{
42        let (error, ptr, len) =
43            wevm::v0::bindings::to_base_58_string($value.as_ptr(), $value.len());
44        error!(error);
45        let bytes = core::slice::from_raw_parts(ptr, len);
46        core::str::from_utf8_unchecked(bytes)
47    }};
48}
49
50/// Get the ContractId of the calling contract
51///
52/// # Result
53/// The result of execution is `&[u8]` ContractId
54/// If the returned slice is empty, then the contract was called from a transaction
55///
56/// # Usage
57/// ```
58/// use we_cdk::*;
59///
60/// #[action]
61/// fn _constructor() {
62///     let caller: Binary = caller!();
63/// }
64/// ```
65#[macro_export]
66macro_rules! caller {
67    () => {{
68        let (error, ptr, len) = wevm::v0::bindings::caller();
69        error!(error);
70        core::slice::from_raw_parts(ptr, len)
71    }};
72}