oasis_contract_sdk_types/
message.rs

1//! Messages that can be emitted by contracts.
2
3/// Messages can be emitted by contracts and are processed after the contract execution completes.
4#[non_exhaustive]
5#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
6pub enum Message {
7    /// Calls an arbitrary runtime method handler in a child context with an optional gas limit.
8    ///
9    /// The call is executed in the context of the smart contract as the caller within the same
10    /// transaction.
11    ///
12    /// This can be used to call other smart contracts.
13    #[cbor(rename = "call")]
14    Call {
15        #[cbor(optional)]
16        id: u64,
17        reply: NotifyReply,
18        method: String,
19        body: cbor::Value,
20        #[cbor(optional)]
21        max_gas: Option<u64>,
22        #[cbor(optional)]
23        data: Option<cbor::Value>,
24    },
25}
26
27/// Specifies when the caller (smart contract) wants to be notified of a reply.
28#[derive(Clone, Copy, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
29#[repr(u8)]
30pub enum NotifyReply {
31    Never = 0,
32    OnError = 1,
33    OnSuccess = 2,
34    Always = 3,
35}
36
37/// Replies to delivered messages.
38#[non_exhaustive]
39#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
40pub enum Reply {
41    /// Reply from a call message.
42    #[cbor(rename = "call")]
43    Call {
44        #[cbor(optional)]
45        id: u64,
46        result: CallResult,
47        #[cbor(optional)]
48        data: Option<cbor::Value>,
49    },
50}
51
52/// Call result.
53#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
54pub enum CallResult {
55    #[cbor(rename = "ok")]
56    Ok(cbor::Value),
57
58    #[cbor(rename = "fail")]
59    Failed { module: String, code: u32 },
60}
61
62impl CallResult {
63    /// Check whether the call result indicates a successful operation or not.
64    pub fn is_success(&self) -> bool {
65        match self {
66            CallResult::Ok(_) => true,
67            CallResult::Failed { .. } => false,
68        }
69    }
70}
71
72#[cfg(feature = "oasis-runtime-sdk")]
73impl From<oasis_runtime_sdk::module::CallResult> for CallResult {
74    fn from(r: oasis_runtime_sdk::module::CallResult) -> Self {
75        match r {
76            oasis_runtime_sdk::module::CallResult::Ok(value) => Self::Ok(value),
77            oasis_runtime_sdk::module::CallResult::Failed { module, code, .. } => {
78                Self::Failed { module, code }
79            }
80            oasis_runtime_sdk::module::CallResult::Aborted(err) => {
81                use oasis_runtime_sdk::error::Error;
82
83                Self::Failed {
84                    module: err.module_name().to_string(),
85                    code: err.code(),
86                }
87            }
88        }
89    }
90}