Crate switchboard_evm

Source
Expand description

Switchboard is a multi-chain, permissionless oracle protocol providing verifiable off-chain compute for smart contracts.

This library provides the ethers-rs bindings for interacting and operating Switchboard, along side the [EvmFunctionRunner] to write custom Switchboard Functions.

Here’s an example of using the EvmFunctionRunner inside a Switchboard Function:

// Generate your contract's ABI
abigen!(Receiver, r#"[ function callback(int256, uint256) ]"#,);

#[derive(Debug, Deserialize)]
pub struct DeribitRespnseInner {
    pub mark_iv: f64,
    pub timestamp: u64,
}

#[derive(Debug, Deserialize)]
pub struct DeribitResponse {
    pub result: DeribitRespnseInner,
}

#[tokio::main(worker_threads = 12)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // --- Initialize clients ---
    let function_runner = EvmFunctionRunner::new()?;
    let client = function_runner.get_client(None).await?;

    let receiver: Address = env!("EXAMPLE_PROGRAM").parse()?;
    let receiver_contract = Receiver::new(receiver, client.into());
    // --- Logic Below ---
    let url = "https://www.deribit.com/api/v2/public/get_order_book?instrument_name=ETH-29SEP23-2000-C";
    let derebit_response: DeribitResponse = reqwest::get(url).await?.json().await?;
    let timestamp = derebit_response.result.timestamp.into();
    let mut mark_iv = Decimal::from_f64(derebit_response.result.mark_iv).unwrap();
    mark_iv.rescale(8);
    // --- Send the callback to the contract with Switchboard verification ---
    let callback = receiver_contract.callback(mark_iv.mantissa().into(), timestamp);
    let expiration = (Utc::now().timestamp() + 120).into();
    let gas_limit = 5_500_000.into();
    function_runner.emit(receiver, expiration, gas_limit, vec![callback])?;
    Ok(())
}

Re-exports§

pub use secrets::*;
pub use test::*;

Modules§

async_trait
githubcrates-iodocs-rs
bindings
env
error
function_result
gramine
ipfs
sdk
secrets
test
utils

Macros§

cfg_client
Macro used to wrap client-only modules
cfg_not_client
Macro used to wrap exclusively non-client modules

Structs§

EvmFunctionEnvironment
EVM specific environment used during a Switchboard function execution
EvmFunctionResultV0
EvmFunctionResultV1
EvmTransaction
EVM Represents an Ethereum Virtual Machine (EVM) transaction.
FunctionManagerEnvironment
FunctionResultV0
The schema of the output data that will be sent to the quote verification sidecar.
FunctionResultV1
The schema of the output data that will be sent to the quote verification sidecar.
Gramine
Gramine: Gramine is a virtualized runtime used to manage vanilla binaries to execute in an SGX execution environment. This struct allows access to specific overrides that come out-of-the-box with Gramine.
IpfsManager
LegacyEvmFunctionResult
LegacyFunctionResult
The schema of the output data that will be sent to the quote verification sidecar. This implementation has been deprecated in favor of FunctionResult.
LegacySolanaFunctionResult
NoParams
QvnEnvironment
SolanaFunctionEnvironment
The expected environment variables when a solana function is being executed
SolanaFunctionResultV0
Solana Represents the result of a Solana function call.
SolanaFunctionResultV1
Represents the result of a Solana function call.

Enums§

ChainResultInfo
Function result info
EvmFunctionResult
Enum representing the result of an EVM function call.
FunctionResult
LegacyChainResultInfo
SbError
Switchboard Functions error suite
SbFunctionError
SolanaFunctionRequestType
SolanaFunctionResult
SwitchboardClientError
Switchboard Functions error suite

Constants§

SWITCHBOARD_ABI
The Switchboard contract ABI with the included facets.
SWITCHBOARD_PUSH_RECEIVER_ABI
The Switchboard Push Receiver contract ABI with the included facets.

Statics§

FUNCTION_RESULT_PREFIX

Traits§

SbFunctionParameters

Functions§

read_and_trim_file
Read a file to a string and trim any leading or trailing whitespace.
unix_timestamp
Return the unix timestamp in seconds.

Type Aliases§

FnCall
SbMiddleware

Attribute Macros§

sb_error
sb_function