switchboard-evm

A Rust library to interact with Switchboard on EVM based chains

59 releases (4 breaking)

0.5.18 Nov 16, 2023
0.5.13 Oct 31, 2023
0.3.5 Jul 31, 2023
Download history 35/week @ 2024-06-29 1/week @ 2024-07-27 5/week @ 2024-09-21 218/week @ 2024-09-28

223 downloads per month

MIT license

1MB
21K SLoC

Switchboard Logo

switchboard-evm

A Rust library to interact with Switchboard on EVM based chains.

Crates.io Badge

Discord Badge

Twitter Badge

Typedocs: docs.rs/switchboard-evm

EVM SDK: github.com/switchboard-xyz/evm-sdk

Switchboard Documentation: docs.switchboard.xyz

Install

Run the following Cargo command in your project directory:

cargo add switchboard-evm

Or add the following line to your Cargo.toml:

[dependencies]
switchboard-evm = "0.5.15"

Usage

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

/// Required
static CLIENT_URL: &str = "https://goerli-rollup.arbitrum.io/rpc";
// Generate your contract's ABI
abigen!(Receiver, r#"[ function callback(uint256) ]"#,);

#[derive(EthAbiType, EthAbiCodec, Default, Debug, Clone)]
pub struct Params {
    callback: Address,
}

#[sb_function(expiration_seconds = 120, gas_limit = 5_500_000)]
async fn sb_function(client: SbMiddleware, _call_id: Address, params: Params) -> SbResult {
    let receiver_contract = Receiver::new(params.callback, client.into());
    let mut random = [0u8; 32];
    Gramine::read_rand(random.as_mut_slice()).map_err(|_| SbError::SgxRandReadFail)?;
    Ok(vec![receiver_contract.callback(U256::from_little_endian(&random))])
}

#[sb_error]
pub enum SbError {
    SgxRandReadFail
}

Dependencies

~33–53MB
~1M SLoC