pub trait OngoingOperationModule: ContractBase + Sized {
    // Required method
    fn current_ongoing_operation(
&self
) -> SingleValueMapper<Self::Api, ManagedBuffer<Self::Api>>; // Provided methods fn run_while_it_has_gas<Process>(
&self,
min_gas_to_save_progress: u64,
process: Process
) -> OperationCompletionStatus
where Process: FnMut() -> LoopOp { ... } fn can_continue_operation(
&self,
operation_cost: u64,
min_gas_to_save_progress: u64
) -> bool { ... } fn load_operation<T: TopDecode + Default>(&self) -> T { ... } fn save_progress<T: TopEncode>(&self, op: &T) { ... } fn clear_operation(&self) { ... } }

Required Methods§

Provided Methods§

source

fn run_while_it_has_gas<Process>(
&self,
min_gas_to_save_progress: u64,
process: Process
) -> OperationCompletionStatuswhere
Process: FnMut() -> LoopOp,

Run the given lambda function until it’s either completed or it runs out of gas. min_gas_to_save_progress should be a reasonable value to save gas. This can vary a lot based on the given ongoing operation data structures.

Usage example: Counting to 100
fn count_to_100(&self) -> OperationCompletionStatus {
    let mut current_number = self.load_operation::<usize>();
    let run_result = self.run_while_it_has_gas(DEFAULT_MIN_GAS_TO_SAVE_PROGRESS, || {
        if current_number == 100 {
            return STOP_OP;
        }

        current_number += 1;
         
        CONTINUE_OP
    });
     
    if run_result == OperationCompletionStatus::InterruptedBeforeOutOfGas {
        self.save_progress(&current_number);
    }

    run_result
}
source

fn can_continue_operation(
&self,
operation_cost: u64,
min_gas_to_save_progress: u64
) -> bool

source

fn load_operation<T: TopDecode + Default>(&self) -> T

Load the current ongoing operation. Will return the default value if no operation is saved.

source

fn save_progress<T: TopEncode>(&self, op: &T)

Save progress for the current operation. The given value can be any serializable type.

source

fn clear_operation(&self)

Clears the currently stored operation. This is for internal use.

Implementors§