How to call System api in the Motoko

I read some system api in the DFINITY interface specification document,eg stable memory(The Internet Computer Interface Specification :: Internet Computer),but I don’t know how to use it.
At first, I create a management canister,

type SystemActor = actor {
        
        stable_size: () -> async Int32;
        raw_rand : () -> async Blob;
    };

    public shared(msg) func stable_size() : async Int32 {
        
        let ic0 : SystemActor = actor("aaaaa-aa");
        let res = await ic0.stable_size();
        
        res
    };

and call stable_size, then report error:

The Replica returned an error: code 4, message: "Management canister has no method 'stable_size'"

How to call the stable_size or other system api?

Thanks

Motoko does not allow direct access to the System API, as that would easily break all safety properties of Motoko. Instead, it wraps the Systems low-level features in (hopefully) more convenient and safe higher level abstractions.

For example, it manages stable memory automatically for you, via the stable variables.

There are plans to offer more manual access to stable memory, though.

2 Likes

Just to be clear, there is a distinction between functions provided by the System API and functions provided by the so-called ManagementCanister.

Your code for accessing the ManagementCanister and its functionality (like IC.raw_rand()) is correct, but much of the System API (including ic0.stable_size) is, by design, not accessible from the ManagementCanister but is instead made available to wasm programs as imported Wasm functions.

Sorry for the confusion.

4 Likes

@nomeata @claudio
Thanks, I see.