zescrow_core/
interface.rs1#[cfg(feature = "json")]
2use std::fs::File;
3#[cfg(feature = "json")]
4use std::path::Path;
5
6#[cfg(feature = "json")]
7use anyhow::Context;
8use bincode::{Decode, Encode};
9#[cfg(feature = "json")]
10use serde::de::DeserializeOwned;
11#[cfg(feature = "json")]
12use serde::{Deserialize, Serialize};
13
14use crate::{Asset, EscrowError, Party};
15
16pub const ESCROW_PARAMS_PATH: &str = concat!(
18    env!("CARGO_MANIFEST_DIR"),
19    "/../templates/escrow_params.json"
20);
21
22pub const ESCROW_METADATA_PATH: &str = concat!(
24    env!("CARGO_MANIFEST_DIR"),
25    "/../templates/escrow_metadata.json"
26);
27
28pub const ESCROW_CONDITIONS_PATH: &str = concat!(
30    env!("CARGO_MANIFEST_DIR"),
31    "/../templates/escrow_conditions.json"
32);
33
34pub const PROOF_DATA_PATH: &str =
36    concat!(env!("CARGO_MANIFEST_DIR"), "/../templates/proof_data.json");
37
38#[cfg(feature = "json")]
55pub fn load_escrow_data<P, T>(path: P) -> anyhow::Result<T>
56where
57    P: AsRef<Path>,
58    T: DeserializeOwned,
59{
60    let path = path.as_ref();
61    let content =
62        std::fs::read_to_string(path).with_context(|| format!("loading escrow data: {path:?}"))?;
63    serde_json::from_str(&content).with_context(|| format!("parsing JSON from {path:?}"))
64}
65
66#[cfg(feature = "json")]
85pub fn save_escrow_data<P, T>(path: P, data: &T) -> anyhow::Result<()>
86where
87    P: AsRef<Path>,
88    T: Serialize,
89{
90    let path = path.as_ref();
91    let file = File::create(path).with_context(|| format!("creating file {path:?}"))?;
92    serde_json::to_writer_pretty(file, data)
93        .with_context(|| format!("serializing to JSON to {path:?}"))
94}
95
96#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
98#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)]
99pub enum ExecutionState {
100    Initialized,
102
103    Funded,
105
106    ConditionsMet,
109}
110
111#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
113#[derive(Debug, Clone, Encode, Decode)]
114pub enum ExecutionResult {
115    Ok(ExecutionState),
117    Err(String),
119}
120
121#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
123#[derive(Debug, Clone, Encode, Decode)]
124pub struct EscrowMetadata {
125    pub params: EscrowParams,
127    pub state: ExecutionState,
129    pub escrow_id: Option<u64>,
131}
132
133#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
135#[derive(Debug, Clone, Encode, Decode)]
136pub struct EscrowParams {
137    pub chain_config: ChainConfig,
139
140    pub asset: Asset,
142
143    pub sender: Party,
145
146    pub recipient: Party,
148
149    pub finish_after: Option<u64>,
152
153    pub cancel_after: Option<u64>,
156
157    pub has_conditions: bool,
159}
160
161#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
163#[derive(Debug, Clone, Encode, Decode)]
164pub struct ChainConfig {
165    pub chain: Chain,
167    pub rpc_url: String,
169    pub sender_private_id: String,
174    pub agent_id: String,
176}
177
178#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
180#[cfg_attr(feature = "json", serde(rename_all = "lowercase"))]
181#[derive(Debug, Copy, Clone, Encode, Decode)]
182pub enum Chain {
183    Ethereum,
185    Solana,
187}
188
189impl AsRef<str> for Chain {
190    fn as_ref(&self) -> &str {
191        match self {
192            Chain::Ethereum => "ethereum",
193            Chain::Solana => "solana",
194        }
195    }
196}
197
198impl std::str::FromStr for Chain {
199    type Err = EscrowError;
200
201    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
207        match s.to_lowercase().as_str() {
208            "ethereum" | "eth" => Ok(Self::Ethereum),
209            "solana" | "sol" => Ok(Self::Solana),
210            _ => Err(EscrowError::UnsupportedChain),
211        }
212    }
213}