Crate seda_sdk_rs

Crate seda_sdk_rs 

Source
Expand description

§Rust SDK

SDK for creating Oracle Programs on the SEDA chain.

§Caveats

When writing Oracle Programs in Rust for SEDA, there are several important considerations to keep in mind:

§Logging and Debugging

The standard Rust logging macros like println!, eprintln!, and dbg! are not suitable for use in Oracle Programs as they consume large amounts of gas. Instead, we provide the Console module which offers gas-efficient alternatives:

  • debug! - dbg! alternative. Used for printing debug information to std err
  • log! - println! alternative. Used for logging to std out
  • elog! - eprintln! alternative. Used for logging to std err

§Stripped Panic Messages

By default when SDK is imported and used it turns on the hide-panic-paths feature. This feature sets a panic hook to remove location details as to not show the filepath of the person who compiled the Oracle Program. It attempts to retain the panic message details, but if it cannot it instead elogs!("<panic>");.

If you run into the above and wish to see the full message, note that it will print the path of where the panic occurred(downloaded crates or your oracle program), you can disable this feature. That can be done via your Cargo.toml and doing seda-sdk-rs = { git = "https://github.com/sedaprotocol/seda-sdk", tag/version/branch = "some_qualifier", default-features = false }.

§Determinism and the Tally Phase

While the execution phase of an Oracle Program has access to non-deterministic data, the tally phase of an Oracle Program must be deterministic. This means it produces the same output given the same input. When you or a library tries to access randomness the program will halt and exit with an error.

The standard HashMap from std::collections can’t be used without a custom hasher, as the default hasher relies on randomness for the initial seed. Instead try one of the following:

  • BTreeMap - For key-value storage with ordered keys
  • Vec with manual key lookups - For smaller datasets
  • Our provided seda_sdk_rs::HashMap - A wrapper around std::collections::HashMap with a deterministic hasher

§Example

Below is an example of an Oracle Program that retrieves the name of a planet in the SWAPI database.

use seda_sdk_rs::{bytes::ToBytes, http::http_fetch, process::Process};
use serde::Deserialize;

// The JSON schema of the response we're expecting.
#[derive(Deserialize)]
struct SwPlanet {
  name: String,
}
use seda_sdk_rs::{bytes::ToBytes, http::http_fetch, process::Process};

#[seda_sdk_rs::oracle_program]
impl SimplePriceFeed {
  fn execute() {
    let input = Process::get_inputs();

    // HTTP Fetch to the SWAPI
    let response = http_fetch(
      format!("https://swapi.dev/api/planets/{}", String::from_utf8(input).unwrap()),
      None,
    );

    if response.is_ok() {
      // We need to parse the response Bytes as the expected JSON.
      let data = serde_json::from_slice::<SwPlanet>(&response.bytes).unwrap();

      // Exits the program (with an exit code of 0) and sets the Data Request result to the planet name
      Process::success(&format!("{}", data.name).to_bytes());
    } else {
      Process::error(&"Error while fetching".to_bytes());
    }
  }

  fn tally() {
    Process::error(&"Tally was not implemented (yet)".to_bytes());
  }
}

Re-exports§

pub use http::http_fetch;
pub use http::HttpFetchMethod;
pub use http::HttpFetchOptions;
pub use http::HttpFetchResponse;
pub use keccak256::keccak256;
pub use process::Process;
pub use proxy_http_fetch::generate_proxy_http_signing_message;
pub use proxy_http_fetch::proxy_http_fetch;
pub use secp256k1::secp256k1_verify;

Modules§

bytes
Module for handling byte arrays in a oracle program compatible way.
errors
Error definitions and result type for the seda_runtime_sdk.
hashmap
A deterministic HashMap for tally oracle programs without random hashing.
http
HTTP fetch action and associated types for the seda_runtime_sdk.
keccak256
Keccak-256 hashing for the seda_runtime_sdk for oracle programs.
log
Logging and debugging macros for the seda_runtime_sdk.
process
Process management utilities for the SDK.
promise
This module defines the PromiseStatus enum, which represents the status of a promise in the Seda runtime SDK. It can either be fulfilled with a result or rejected with an error message. The enum provides methods to handle the promise status and convert it from a core::result::Result type.
proxy_http_fetch
Proxy HTTP fetch action and associated types for the seda_runtime_sdk. These are used to make HTTP requests through the proxy nodes.
secp256k1
This module provides functions for verifying secp256k1 ECDSA signatures using the Seda VM’s FFI interface.

Macros§

bytes_serde_json
Implements ToBytes and FromBytes via JSON serialization/deserialization for the given type.
debug
A debug macro that prints the expression and its value to stderr.
elog
A logging macro that prints the value to stderr, with a newline.
log
A logging macro that prints the value to stdout, with a newline.

Structs§

RevealBody
Represents the body of a reveal body of a data request report.
RevealResult
Represents a reveal body of a data request report.

Functions§

get_reveals
Retrieves the reveals that are in consensus from the command line arguments.
get_unfiltered_reveals
Retrieves the unfiltered reveals from the command line arguments.

Attribute Macros§

oracle_program