forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.rs
More file actions
146 lines (129 loc) · 4.13 KB
/
Copy pathstate.rs
File metadata and controls
146 lines (129 loc) · 4.13 KB
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
138
139
140
141
142
143
144
145
146
use soroban_sdk::{contracterror, panic_with_error, symbol_short, BytesN, Env, Symbol};
use crate::event_struct::{MOD_CORE, ACT_TRANSITION};
use crate::event_utils::publish_event;
const KEY_STATE: Symbol = symbol_short!("PROTO_ST");
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum StateError {
InvalidTransition = 1,
AlreadyInState = 2,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum ProtocolState {
Pending = 0,
Active = 1,
Settled = 2,
Closed = 3,
}
impl ProtocolState {
fn from_u32(v: u32) -> Self {
match v {
0 => ProtocolState::Pending,
1 => ProtocolState::Active,
2 => ProtocolState::Settled,
3 => ProtocolState::Closed,
_ => ProtocolState::Pending,
}
}
}
fn allowed_transition(from: ProtocolState, to: ProtocolState) -> bool {
match (from, to) {
(ProtocolState::Pending, ProtocolState::Active) => true,
(ProtocolState::Pending, ProtocolState::Closed) => true,
(ProtocolState::Active, ProtocolState::Settled) => true,
(ProtocolState::Active, ProtocolState::Closed) => true,
(ProtocolState::Settled, ProtocolState::Closed) => true,
_ => false,
}
}
pub fn init(env: &Env) {
env.storage()
.instance()
.set(&KEY_STATE, &(ProtocolState::Pending as u32));
}
pub fn get_state(env: &Env) -> ProtocolState {
let raw: u32 = env.storage().instance().get(&KEY_STATE).unwrap_or(0);
ProtocolState::from_u32(raw)
}
pub fn transition_to(env: &Env, next: ProtocolState) {
let current = get_state(env);
if current == next {
panic_with_error!(env, StateError::AlreadyInState);
}
if !allowed_transition(current, next) {
panic_with_error!(env, StateError::InvalidTransition);
}
env.storage().instance().set(&KEY_STATE, &(next as u32));
publish_event(
env,
MOD_CORE | ACT_TRANSITION,
current as u64,
BytesN::from_array(env, &[0u8; 32]),
);
}
pub fn require_state(env: &Env, expected: ProtocolState) {
let current = get_state(env);
if current != expected {
panic_with_error!(env, StateError::InvalidTransition);
}
}
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::Env;
#[soroban_sdk::contract]
pub struct TestContract;
#[soroban_sdk::contractimpl]
impl TestContract {}
#[test]
fn initial_state_is_pending() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
env.as_contract(&contract_id, || {
init(&env);
assert_eq!(get_state(&env), ProtocolState::Pending);
});
}
#[test]
fn transition_pending_to_active() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
env.as_contract(&contract_id, || {
init(&env);
transition_to(&env, ProtocolState::Active);
assert_eq!(get_state(&env), ProtocolState::Active);
});
}
#[test]
fn full_lifecycle() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
env.as_contract(&contract_id, || {
init(&env);
transition_to(&env, ProtocolState::Active);
transition_to(&env, ProtocolState::Settled);
transition_to(&env, ProtocolState::Closed);
assert_eq!(get_state(&env), ProtocolState::Closed);
});
}
#[test]
#[should_panic]
fn invalid_pending_to_settled_rejected() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
env.as_contract(&contract_id, || {
init(&env);
transition_to(&env, ProtocolState::Settled);
});
}
#[test]
#[should_panic]
fn require_state_panics_on_mismatch() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
env.as_contract(&contract_id, || {
init(&env);
require_state(&env, ProtocolState::Active);
});
}
}