Skip to main content

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, Default, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
72#[repr(u8)]
73#[cbor(with_default)]
74pub enum CallFormat {
75    /// Plain text call data.
76    #[default]
77    Plain = 0,
78    /// Encrypted call data using X25519 for key exchange and Deoxys-II for symmetric encryption.
79    EncryptedX25519DeoxysII = 1,
80}
81
82impl CallFormat {
83    fn is_plain(&self) -> bool {
84        matches!(self, CallFormat::Plain)
85    }
86}
87
88#[cfg(feature = "oasis-runtime-sdk")]
89impl From<oasis_runtime_sdk::types::transaction::CallFormat> for CallFormat {
90    fn from(a: oasis_runtime_sdk::types::transaction::CallFormat) -> Self {
91        use oasis_runtime_sdk::types::transaction::CallFormat as RuntimeCallFormat;
92
93        match a {
94            RuntimeCallFormat::Plain => Self::Plain,
95            RuntimeCallFormat::EncryptedX25519DeoxysII => Self::EncryptedX25519DeoxysII,
96        }
97    }
98}
99
100/// Execution context.
101///
102/// Contains information that is useful on most invocations as it is always
103/// included without requiring any explicit queries.
104#[derive(Debug, Default, cbor::Decode, cbor::Encode)]
105pub struct ExecutionContext {
106    /// Contract instance identifier.
107    pub instance_id: InstanceId,
108    /// Contract instance address.
109    pub instance_address: address::Address,
110    /// Caller address.
111    pub caller_address: address::Address,
112    /// Tokens deposited by the caller.
113    #[cbor(optional)]
114    pub deposited_tokens: Vec<token::BaseUnits>,
115    /// Read-only flag.
116    ///
117    /// A read-only call cannot make any changes to runtime state. Any attempt at modifying state
118    /// will result in the call failing.
119    #[cbor(optional)]
120    pub read_only: bool,
121    /// Transaction's call format.
122    #[cbor(optional, skip_serializing_if = "CallFormat::is_plain")]
123    pub call_format: CallFormat,
124}
125
126/// Contract execution result.
127#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
128pub enum ExecutionResult {
129    #[cbor(rename = "ok")]
130    Ok(ExecutionOk),
131
132    #[cbor(rename = "fail")]
133    Failed {
134        #[cbor(optional)]
135        module: String,
136
137        code: u32,
138
139        #[cbor(optional)]
140        message: String,
141    },
142}
143
144/// Result of a successful contract execution.
145#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
146pub struct ExecutionOk {
147    /// Raw data returned from the contract.
148    pub data: Vec<u8>,
149    /// Messages emitted from the contract.
150    #[cbor(optional)]
151    pub messages: Vec<message::Message>,
152    /// Events emitted from the contract.
153    #[cbor(optional)]
154    pub events: Vec<event::Event>,
155}