Skip to main content

check_init_data

Function check_init_data 

Source
pub fn check_init_data<State, F>(state_ptr: i32, callback: F) -> u32
where State: for<'a> Deserialize<'a> + Serialize + Clone, F: Fn(&State, &mut ContractInitCheck),
Expand description

Validates the initial state of a contract before subject creation.

The runtime passes the proposed state through state_ptr. The callback decides whether that state is valid and writes the outcome into ContractInitCheck.

§Arguments

  • state_ptr - Pointer to the proposed initial state in host memory.
  • callback - Validation function with signature fn(&State, &mut ContractInitCheck).

§Returns

Pointer to a serialized ContractInitCheckBorsh.

§Example

#[unsafe(no_mangle)]
pub unsafe fn init_check_function(state_ptr: i32) -> u32 {
    sdk::check_init_data(state_ptr, |state: &MyState, result| {
        if state.value > 100 {
            result.success = false;
            result.error = "Value too high".to_string();
        } else {
            result.success = true;
        }
    })
}