use oasis_types::{Address, Balance};
pub trait Service {
fn coalesce() -> Self;
fn sunder(c: Self);
fn address(&self) -> Address {
crate::backend::address()
}
}
pub trait Event: crate::abi::Serialize {
fn emit(&self);
}
const TOPIC_LEN: usize = 32;
#[doc(hidden)]
pub fn encode_event_topic<T: crate::abi::Serialize>(topic: &T) -> [u8; TOPIC_LEN] {
let repr = crate::abi_encode!(topic).unwrap();
if repr.len() <= TOPIC_LEN {
let mut topic = [0u8; TOPIC_LEN];
topic[..repr.len()].copy_from_slice(&repr);
topic
} else {
tiny_keccak::keccak256(&repr)
}
}
#[derive(Default, Copy, Clone, Debug)]
pub struct Context {
#[doc(hidden)]
pub sender: Option<Address>,
#[doc(hidden)]
pub value: Option<Balance>,
#[doc(hidden)]
pub gas: Option<u64>,
}
impl Context {
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 aad(&self) -> Vec<u8> {
crate::backend::aad()
}
pub fn value(&self) -> Balance {
self.value.unwrap_or_else(crate::backend::value)
}
}
impl Context {
#[cfg(any(test, not(target_os = "wasi")))]
pub fn with_sender(mut self, sender: Address) -> Self {
self.sender = Some(sender);
self
}
pub fn with_value<B: Into<Balance>>(mut self, value: B) -> Self {
self.value = Some(value.into());
self
}
}