1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Exchange mechanic: Asset and value exchange system.
//!
//! This module provides a policy-based exchange system that can model
//! various types of trades, from simple barter to complex market exchanges.
//!
//! # Architecture
//!
//! The exchange mechanic follows a **Policy-Based Design**:
//! - The core `ExchangeMechanic<V, E>` is generic over two policies
//! - `V: ValuationPolicy` determines how to calculate fair value
//! - `E: ExecutionPolicy` determines when to execute trades
//! - All logic is resolved at compile time via static dispatch
//!
//! # Quick Start
//!
//! ```
//! use issun_core::mechanics::exchange::prelude::*;
//! use issun_core::mechanics::{Mechanic, EventEmitter};
//!
//! // Define your exchange type
//! type SimpleExchange = ExchangeMechanic<SimpleValuation, FairTradeExecution>;
//!
//! // Create configuration
//! let config = ExchangeConfig::default();
//! let mut state = ExchangeState::default();
//!
//! // Prepare input for this frame
//! let input = ExchangeInput {
//! offered_value: 100.0,
//! requested_value: 95.0,
//! market_liquidity: 0.7,
//! urgency: 0.3,
//! };
//!
//! // Simple event collector
//! # struct TestEmitter { events: Vec<ExchangeEvent> }
//! # impl EventEmitter<ExchangeEvent> for TestEmitter {
//! # fn emit(&mut self, event: ExchangeEvent) { self.events.push(event); }
//! # }
//! let mut emitter = TestEmitter { events: vec![] };
//!
//! // Execute one step
//! SimpleExchange::step(&config, &mut state, input, &mut emitter);
//! ```
//!
//! # Module Organization
//!
//! - `types`: Basic data structures (Config, Input, Event, State)
//! - `policies`: Policy traits (ValuationPolicy, ExecutionPolicy)
//! - `strategies`: Concrete implementations of policies
//! - `mechanic`: The core `ExchangeMechanic<V, E>` implementation
//! - `prelude`: Convenient re-exports for common use
//!
//! # Policy Combinations
//!
//! ## Valuation Policies
//! - `SimpleValuation`: Direct value comparison with fairness threshold
//! - `MarketAdjustedValuation`: Adjusts value based on liquidity and reputation
//!
//! ## Execution Policies
//! - `FairTradeExecution`: Strict fairness enforcement
//! - `UrgentExecution`: Relaxes fairness based on urgency
//!
//! # Examples
//!
//! ## Example 1: Simple Fair Trade
//!
//! ```
//! use issun_core::mechanics::exchange::prelude::*;
//! use issun_core::mechanics::{Mechanic, EventEmitter};
//!
//! type FairMarket = ExchangeMechanic<SimpleValuation, FairTradeExecution>;
//!
//! let config = ExchangeConfig {
//! transaction_fee_rate: 0.02, // 2% fee
//! minimum_value_threshold: 10.0,
//! fairness_threshold: 0.8, // 0.8x to 1.25x is fair
//! };
//!
//! let mut state = ExchangeState::default();
//! let input = ExchangeInput {
//! offered_value: 100.0,
//! requested_value: 105.0, // Slightly unfavorable but within threshold
//! market_liquidity: 0.5,
//! urgency: 0.0,
//! };
//!
//! # struct TestEmitter { events: Vec<ExchangeEvent> }
//! # impl EventEmitter<ExchangeEvent> for TestEmitter {
//! # fn emit(&mut self, event: ExchangeEvent) { self.events.push(event); }
//! # }
//! let mut emitter = TestEmitter { events: vec![] };
//!
//! FairMarket::step(&config, &mut state, input, &mut emitter);
//! // Trade succeeds, fair_value = 100.0, fee = 2.0, final = 98.0
//! ```
//!
//! ## Example 2: Urgent Market Trade
//!
//! ```
//! use issun_core::mechanics::exchange::prelude::*;
//! use issun_core::mechanics::{Mechanic, EventEmitter};
//!
//! type UrgentMarket = ExchangeMechanic<MarketAdjustedValuation, UrgentExecution>;
//!
//! let config = ExchangeConfig::default();
//! let mut state = ExchangeState::new(0.8); // High reputation
//!
//! let input = ExchangeInput {
//! offered_value: 70.0,
//! requested_value: 100.0, // Unfavorable ratio
//! market_liquidity: 0.9, // High liquidity helps
//! urgency: 0.8, // High urgency allows unfair trade
//! };
//!
//! # struct TestEmitter { events: Vec<ExchangeEvent> }
//! # impl EventEmitter<ExchangeEvent> for TestEmitter {
//! # fn emit(&mut self, event: ExchangeEvent) { self.events.push(event); }
//! # }
//! let mut emitter = TestEmitter { events: vec![] };
//!
//! UrgentMarket::step(&config, &mut state, input, &mut emitter);
//! // Trade succeeds due to urgency, but reputation decreases
//! ```
// Convenience
// Re-export core types
pub use ExchangeMechanic;
pub use ;
pub use ;