oasis_contract_sdk_types/
lib.rs

1//! A collection of common types used by the Oasis Contract SDK.
2
3pub mod address;
4pub mod crypto;
5pub mod env;
6pub mod event;
7pub mod message;
8pub mod modules;
9pub mod storage;
10pub mod token;
11
12pub mod testing;
13
14/// Unique stored code identifier.
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, cbor::Decode, cbor::Encode)]
16#[cbor(transparent)]
17pub struct CodeId(u64);
18
19impl CodeId {
20    /// Convert identifier to u64.
21    pub fn as_u64(&self) -> u64 {
22        self.0
23    }
24
25    /// Return the next identifier in sequence.
26    pub fn increment(&self) -> Self {
27        CodeId(self.0 + 1)
28    }
29
30    /// Convert identifier to storage key representation.
31    pub fn to_storage_key(self) -> [u8; 8] {
32        self.0.to_be_bytes()
33    }
34}
35
36impl From<u64> for CodeId {
37    fn from(v: u64) -> Self {
38        CodeId(v)
39    }
40}
41
42/// Unique deployed code instance identifier.
43#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, cbor::Decode, cbor::Encode)]
44#[cbor(transparent)]
45pub struct InstanceId(u64);
46
47impl InstanceId {
48    /// Convert identifier to u64.
49    pub fn as_u64(&self) -> u64 {
50        self.0
51    }
52
53    /// Return the next identifier in sequence.
54    pub fn increment(&self) -> Self {
55        InstanceId(self.0 + 1)
56    }
57
58    /// Convert identifier to storage key representation.
59    pub fn to_storage_key(self) -> [u8; 8] {
60        self.0.to_be_bytes()
61    }
62}
63
64impl From<u64> for InstanceId {
65    fn from(v: u64) -> Self {
66        InstanceId(v)
67    }
68}
69
70/// Format used for encoding the call (and output) information.
71#[derive(Clone, Copy, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
72#[repr(u8)]
73#[cbor(with_default)]
74pub enum CallFormat {
75    /// Plain text call data.
76    Plain = 0,
77    /// Encrypted call data using X25519 for key exchange and Deoxys-II for symmetric encryption.
78    EncryptedX25519DeoxysII = 1,
79}
80
81impl CallFormat {
82    fn is_plain(&self) -> bool {
83        matches!(self, CallFormat::Plain)
84    }
85}
86
87impl Default for CallFormat {
88    fn default() -> Self {
89        Self::Plain
90    }
91}
92
93#[cfg(feature = "oasis-runtime-sdk")]
94impl From<oasis_runtime_sdk::types::transaction::CallFormat> for CallFormat {
95    fn from(a: oasis_runtime_sdk::types::transaction::CallFormat) -> Self {
96        use oasis_runtime_sdk::types::transaction::CallFormat as RuntimeCallFormat;
97
98        match a {
99            RuntimeCallFormat::Plain => Self::Plain,
100            RuntimeCallFormat::EncryptedX25519DeoxysII => Self::EncryptedX25519DeoxysII,
101        }
102    }
103}
104
105/// Execution context.
106///
107/// Contains information that is useful on most invocations as it is always
108/// included without requiring any explicit queries.
109#[derive(Debug, Default, cbor::Decode, cbor::Encode)]
110pub struct ExecutionContext {
111    /// Contract instance identifier.
112    pub instance_id: InstanceId,
113    /// Contract instance address.
114    pub instance_address: address::Address,
115    /// Caller address.
116    pub caller_address: address::Address,
117    /// Tokens deposited by the caller.
118    #[cbor(optional)]
119    pub deposited_tokens: Vec<token::BaseUnits>,
120    /// Read-only flag.
121    ///
122    /// A read-only call cannot make any changes to runtime state. Any attempt at modifying state
123    /// will result in the call failing.
124    #[cbor(optional)]
125    pub read_only: bool,
126    /// Transaction's call format.
127    #[cbor(optional, skip_serializing_if = "CallFormat::is_plain")]
128    pub call_format: CallFormat,
129}
130
131/// Contract execution result.
132#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
133pub enum ExecutionResult {
134    #[cbor(rename = "ok")]
135    Ok(ExecutionOk),
136
137    #[cbor(rename = "fail")]
138    Failed {
139        #[cbor(optional)]
140        module: String,
141
142        code: u32,
143
144        #[cbor(optional)]
145        message: String,
146    },
147}
148
149/// Result of a successful contract execution.
150#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
151pub struct ExecutionOk {
152    /// Raw data returned from the contract.
153    pub data: Vec<u8>,
154    /// Messages emitted from the contract.
155    #[cbor(optional)]
156    pub messages: Vec<message::Message>,
157    /// Events emitted from the contract.
158    #[cbor(optional)]
159    pub events: Vec<event::Event>,
160}