use mantle_types::Address;
pub trait Storage = serde::Serialize + serde::de::DeserializeOwned;
pub trait Service {
fn coalesce() -> Self;
fn sunder(c: Self);
}
pub trait Event {
fn emit(&self);
}
#[derive(Default, Copy, Clone, Debug)]
pub struct Context {
#[doc(hidden)]
pub sender: Option<Address>,
#[doc(hidden)]
pub value: Option<u64>,
#[doc(hidden)]
pub gas: Option<u64>,
#[doc(hidden)]
pub call_type: CallType,
}
#[derive(Copy, Clone, Debug)]
pub enum CallType {
Default,
Delegated,
Constant,
}
impl Default for CallType {
fn default() -> Self {
CallType::Default
}
}
impl Context {
pub fn delegated() -> Self {
Self {
call_type: CallType::Delegated,
..Default::default()
}
}
pub fn with_gas(mut self, gas: u64) -> Self {
self.gas = Some(gas);
self
}
pub fn sender(&self) -> Address {
self.sender.unwrap_or_else(crate::backend::sender)
}
pub fn address(&self) -> Address {
crate::backend::address()
}
pub fn value(&self) -> u64 {
self.value.unwrap_or_else(crate::backend::value)
}
}
impl Context {
pub fn with_sender(mut self, sender: Address) -> Self {
self.sender = Some(sender);
self
}
pub fn with_value(mut self, value: u64) -> Self {
self.value = Some(value);
self
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum RpcError<T> {
NoAccount,
InsufficientFunds,
InsufficientGas,
InvalidInput,
InvalidOutput(Vec<u8>),
Exec(T),
}
impl<T: serde::de::DeserializeOwned> From<crate::backend::Error> for RpcError<T> {
fn from(err: crate::backend::Error) -> Self {
use crate::backend::Error as BackendError;
match err {
BackendError::Unknown => panic!("Unknown error occured."),
BackendError::InsufficientFunds => RpcError::InsufficientFunds,
BackendError::InvalidInput => RpcError::InvalidInput,
BackendError::NoAccount => RpcError::NoAccount,
BackendError::Execution { payload, .. } => {
RpcError::Exec(match serde_cbor::from_slice::<T>(&payload) {
Ok(t) => t,
Err(_) => return RpcError::InvalidOutput(payload),
})
}
}
}
}