Trait gstd::ReservationIdExt

source ·
pub trait ReservationIdExt: Sealed + Sized {
    // Required methods
    fn reserve(amount: u64, duration: u32) -> Result<Self>;
    fn unreserve(self) -> Result<u64>;
}
Expand description

Reservation identifier extension.

The identifier is used to reserve and unreserve gas amount for program execution later.

§Examples

use gstd::{prelude::*, ReservationId};

static mut RESERVED: Option<ReservationId> = None;

#[no_mangle]
extern "C" fn init() {
    let reservation_id = ReservationId::reserve(50_000_000, 7).expect("Unable to reserve");
    unsafe { RESERVED = Some(reservation_id) };
}

#[no_mangle]
extern "C" fn handle() {
    let reservation_id = unsafe { RESERVED.take().expect("Empty `RESERVED`") };
    reservation_id.unreserve().expect("Unable to unreserve");
}

Required Methods§

source

fn reserve(amount: u64, duration: u32) -> Result<Self>

Reserve the amount of gas for further usage.

duration is the block count within which the reserve must be used.

This function returns ReservationId, which one can use for gas unreserving.

§Examples

Reserve 50 million of gas for one block, send a reply, then unreserve gas back:

use gstd::{msg, prelude::*, ReservationId};

#[no_mangle]
extern "C" fn handle() {
    let reservation_id = ReservationId::reserve(50_000_000, 1).expect("Unable to reserve");
    msg::reply_bytes_from_reservation(reservation_id.clone(), b"PONG", 0)
        .expect("Unable to reply");
    let reservation_left = reservation_id.unreserve().expect("Unable to unreserve");
}
source

fn unreserve(self) -> Result<u64>

Unreserve unused gas from the reservation.

If successful, it returns the reserved amount of gas.

Object Safety§

This trait is not object safe.

Implementors§