Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

invoke

Invokes an external oracle process and returns its result. Avoid calling this function directly in user code. Instead, write oracle interface modules, which group all single oracle features together. To use an oracle, call invoke with:

  1. connection_string: A string describing how to connect to the oracle. The execution runtime handles oracle process management transparently under the hood. Consult your runtime documentation for details what protocols and options are supported. For stdio-based oracles, this can be a path to an executable (e.g., "stdio:./my_oracle"), a command with arguments (e.g., "stdio:python3 ./my_oracle.py"), or package manager invocations (e.g., "stdio:npx -y my_oracle").
  2. selector: The name or identifier of the method to invoke on the oracle (as short string). It acts as a function name or command within the oracle process.
  3. calldata: The arguments to pass to the oracle method, as a serializable Cairo type. To pass multiple arguments, use a tuple or struct that implements Serde.

The function returns a Result<R, oracle::Error>, where R is the expected return type, or an error if the invocation fails or the oracle returns an error.

mod math_oracle {
    pub type Result<T> = oracle::Result<T>;

    pub fn pow(x: u64, n: u32) -> Result<u128> {
        oracle::invoke("stdio:python3 ./my_math_oracle.py", 'pow', (x, n))
    }

    pub fn sqrt(x: u64) -> Result<u64> {
        oracle::invoke("stdio:python3 ./my_math_oracle.py", 'sqrt', x)
    }
}

mod fs_oracle {
    pub type Result<T> = oracle::Result<T>;

    pub fn fs_read(path: ByteArray) -> Result<ByteArray> {
        oracle::invoke("builtin:fs", 'read', path)
    }

    pub fn fs_exists(path: ByteArray) -> Result<bool> {
        oracle::invoke("builtin:fs", 'exists', path)
    }
}

Fully qualified path: oracle::invoke

pub fn invoke<T, +Destruct<T>, +Drop<T>, +Serde<T>, R, +Serde<R>>(
    connection_string: ByteArray, selector: felt252, calldata: T,
) -> Result<R, Error>