defuse/
simulation_output.rs

1use defuse_core::{
2    Deadline, Result, Salt,
3    accounts::{AccountEvent, NonceEvent},
4    engine::deltas::InvariantViolated,
5    fees::Pips,
6    intents::IntentEvent,
7};
8
9use near_sdk::near;
10
11#[near(serializers = [json])]
12#[derive(Debug, Clone)]
13pub struct SimulationReport {
14    pub intents_executed: Vec<IntentEvent<AccountEvent<'static, NonceEvent>>>,
15    pub logs: Vec<String>,
16    pub min_deadline: Deadline,
17}
18
19#[near(serializers = [json])]
20#[derive(Debug, Clone)]
21pub struct SimulationOutput {
22    #[serde(flatten)]
23    pub report: SimulationReport,
24
25    /// Unmatched token deltas needed to keep the invariant.
26    /// If not empty, can be used along with fee to calculate `token_diff` closure.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub invariant_violated: Option<InvariantViolated>,
29
30    /// Additional info about current state
31    pub state: StateOutput,
32}
33
34impl SimulationOutput {
35    pub fn into_result(self) -> Result<(), InvariantViolated> {
36        if let Some(unmatched_deltas) = self.invariant_violated {
37            return Err(unmatched_deltas);
38        }
39        Ok(())
40    }
41}
42
43#[near(serializers = [json])]
44#[derive(Debug, Clone)]
45pub struct StateOutput {
46    pub fee: Pips,
47
48    pub current_salt: Salt,
49}