switchboard_common/env/
evm.rs

1use crate::SbError;
2use serde::Deserialize;
3
4/// EVM specific environment used during a Switchboard function execution
5#[derive(Deserialize, Debug, Default)]
6pub struct EvmFunctionEnvironment {
7    /// `CHAIN_ID`: The chain ID of the chain this evm function is executing on
8    #[serde(default)]
9    pub chain_id: u64,
10    /// `VERIFYING_CONTRACT`: An environmnet variable denoting the signoff
11    /// callback program ID. On evm chains this is equivalent to the Switchboard
12    /// program address.
13    #[serde(default)]
14    pub verifying_contract: String,
15    /// `FUNCTION_KEY`: environemnt variable passed in that denoted what function
16    /// is executing
17    #[serde(default)]
18    pub function_key: String,
19    /// A list of function parameter based calls to attempt to handle this run.
20    /// Parsing these is up to the function.
21    #[serde(default)]
22    pub function_params: String,
23    /// `FUNCTION_CALL_IDS`: A list of the UUIDs of all the calls the function
24    /// will be attempting to resolve.
25    #[serde(default)]
26    pub function_call_ids: Vec<String>,
27}
28impl EvmFunctionEnvironment {
29    pub fn parse() -> Result<Self, SbError> {
30        match envy::from_env::<EvmFunctionEnvironment>() {
31            Ok(env) => Ok(env),
32            Err(error) => Err(SbError::CustomMessage(format!(
33                "failed to decode environment variables: {}",
34                error
35            ))),
36        }
37    }
38
39    /**
40     * Returns the vec! of environment variable key-value pairs used by bollard
41     */
42    pub fn to_env(&self) -> Vec<String> {
43        let mut env = vec![];
44        env.push(format!("CHAIN_ID={}", self.chain_id));
45        env.push(format!("VERIFYING_CONTRACT={}", self.verifying_contract));
46        env.push(format!("FUNCTION_KEY={}", self.function_key));
47        env.push(format!("FUNCTION_PARAMS={}", self.function_params));
48        env.push(format!("function_call_ids={:?}", self.function_call_ids));
49        env
50    }
51}