darkfi_sdk/wasm/
entrypoint.rsuse core::{mem::size_of, slice::from_raw_parts};
use crate::crypto::ContractId;
pub const SUCCESS: i64 = 0;
#[macro_export]
macro_rules! define_contract {
(
init: $init_func:ident,
exec: $exec_func:ident,
apply: $apply_func:ident,
metadata: $metadata_func:ident
) => {
#[no_mangle]
pub unsafe extern "C" fn __initialize(input: *mut u8) -> i64 {
let (contract_id, instruction_data) = $crate::wasm::entrypoint::deserialize(input);
match $init_func(contract_id, &instruction_data) {
Ok(()) => $crate::wasm::entrypoint::SUCCESS,
Err(e) => e.into(),
}
}
#[no_mangle]
pub unsafe extern "C" fn __entrypoint(input: *mut u8) -> i64 {
let (contract_id, instruction_data) = $crate::wasm::entrypoint::deserialize(input);
match $exec_func(contract_id, &instruction_data) {
Ok(()) => $crate::wasm::entrypoint::SUCCESS,
Err(e) => e.into(),
}
}
#[no_mangle]
pub unsafe extern "C" fn __update(input: *mut u8) -> i64 {
let (contract_id, update_data) = $crate::wasm::entrypoint::deserialize(input);
match $apply_func(contract_id, &update_data) {
Ok(()) => $crate::wasm::entrypoint::SUCCESS,
Err(e) => e.into(),
}
}
#[no_mangle]
pub unsafe extern "C" fn __metadata(input: *mut u8) -> i64 {
let (contract_id, instruction_data) = $crate::wasm::entrypoint::deserialize(input);
match $metadata_func(contract_id, &instruction_data) {
Ok(()) => $crate::wasm::entrypoint::SUCCESS,
Err(e) => e.into(),
}
}
};
}
pub unsafe fn deserialize<'a>(input: *mut u8) -> (ContractId, &'a [u8]) {
let mut offset: usize = 0;
let contract_id_len = 32;
let contract_id_slice = { from_raw_parts(input.add(offset), contract_id_len) };
offset += contract_id_len;
let instruction_data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
let contract_id = ContractId::from_bytes(contract_id_slice.try_into().unwrap());
let contract_id = contract_id.unwrap();
(contract_id, instruction_data)
}