forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
224 lines (179 loc) · 6.64 KB
/
Copy pathlib.rs
File metadata and controls
224 lines (179 loc) · 6.64 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#![no_std]
mod action_adapter;
mod errors;
mod events;
mod storage;
mod types;
#[cfg(test)]
mod test;
use action_adapter::{adapt_outcome, execute_action};
use errors::Error;
use soroban_sdk::{contract, contractimpl, vec, Address, Bytes, Env, IntoVal, String, Symbol, Val};
use types::{Dispute, DisputeResult, DisputeStatus};
const VOTING_PERIOD: u64 = 604_800; // 7 days in seconds
const MAX_SPLIT_ID_BYTES: usize = 64;
fn generate_dispute_id(env: &Env, split_id: &String) -> String {
let split_len = split_id.len() as usize;
assert!(split_len <= MAX_SPLIT_ID_BYTES);
let mut split_buf = [0u8; MAX_SPLIT_ID_BYTES];
split_id.copy_into_slice(&mut split_buf[..split_len]);
let split_bytes = Bytes::from_slice(env, &split_buf[..split_len]);
let mut input = Bytes::new(env);
input.append(&split_bytes);
let seq = env.ledger().sequence().to_be_bytes();
input.append(&Bytes::from_slice(env, &seq));
let hash = env.crypto().sha256(&input);
let hash_bytes = &hash.to_array()[..8];
let mut id_bytes = Bytes::from_slice(env, b"dis_");
id_bytes.append(&Bytes::from_slice(env, hash_bytes));
let id_buf = id_bytes.to_buffer::<32>();
String::from_bytes(env, id_buf.as_slice())
}
#[contract]
pub struct DisputeContract;
#[contractimpl]
impl DisputeContract {
/// Set the contract admin. Must be called once after deployment.
pub fn initialize(env: Env, admin: Address) -> Result<(), Error> {
if storage::has_admin(&env) {
return Err(Error::AlreadyExists);
}
storage::set_admin(&env, &admin);
Ok(())
}
/// Raise a new dispute against a split.
pub fn raise_dispute(
env: Env,
split_id: String,
raiser: Address,
reason: String,
escrow_contract: Address,
escrow_split_id: u64,
) -> Result<String, Error> {
raiser.require_auth();
let now = env.ledger().timestamp();
let dispute_id = generate_dispute_id(&env, &split_id);
if storage::has_dispute(&env, &dispute_id) {
return Err(Error::AlreadyExists);
}
let dispute = Dispute {
dispute_id: dispute_id.clone(),
split_id,
raiser,
reason,
status: DisputeStatus::Voting,
votes_for: 0,
votes_against: 0,
voters: soroban_sdk::Vec::new(&env),
created_at: now,
voting_ends_at: now + VOTING_PERIOD,
result: None,
escrow_contract,
escrow_split_id,
};
storage::save_dispute(&env, &dispute);
storage::add_to_list(&env, dispute_id.clone());
events::emit_dispute_raised(&env, &dispute_id, &dispute.split_id, &dispute.raiser);
Ok(dispute_id)
}
/// Cast a vote on an open dispute.
pub fn vote_on_dispute(
env: Env,
dispute_id: String,
voter: Address,
support: bool, // true = support the dispute, false = dismiss it
) -> Result<(), Error> {
voter.require_auth();
// FIX 1: Admin must never vote — prevents conflict of interest
let admin = storage::get_admin(&env);
if voter == admin {
return Err(Error::NotAuthorized);
}
let mut dispute = storage::get_dispute(&env, &dispute_id)?;
// Must be in Voting status
if dispute.status != DisputeStatus::Voting {
return Err(Error::DisputeClosed);
}
let now = env.ledger().timestamp();
// Voting window must still be open
if now > dispute.voting_ends_at {
return Err(Error::VotingPeriodEnded);
}
// Each address can only vote once
if storage::has_voted(&env, &dispute_id, &voter) {
return Err(Error::AlreadyVoted);
}
// FIX 2: Record voter BEFORE counting the vote (makes duplicate guard atomic)
dispute.voters.push_back(voter.clone());
storage::record_vote(&env, &dispute_id, &voter);
// Now count the vote
if support {
dispute.votes_for += 1;
} else {
dispute.votes_against += 1;
}
storage::save_dispute(&env, &dispute);
events::emit_vote_cast(&env, &dispute_id, &voter, support);
Ok(())
}
/// Resolve a dispute after voting period ends.
pub fn resolve_dispute(
env: Env,
dispute_id: String,
resolver: Address,
) -> Result<DisputeResult, Error> {
let mut dispute = storage::get_dispute(&env, &dispute_id)?;
if dispute.status != DisputeStatus::Voting {
return Err(Error::DisputeClosed);
}
let now = env.ledger().timestamp();
// Voting period must have ended
if now <= dispute.voting_ends_at {
return Err(Error::VotingPeriodActive);
}
// Determine result based on votes
let result = if dispute.votes_for > dispute.votes_against {
DisputeResult::UpheldForRaiser
} else if dispute.votes_against > dispute.votes_for {
DisputeResult::DismissedForRaiser
} else {
DisputeResult::Tied
};
// Auth boundary: only the escrow creator (owner) is allowed to finalize the escrow action.
resolver.require_auth();
let get_creator_sym = Symbol::new(&env, "get_creator");
let get_creator_args: soroban_sdk::Vec<Val> =
vec![&env, dispute.escrow_split_id.into_val(&env)];
let escrow_creator: Address =
env.invoke_contract(&dispute.escrow_contract, &get_creator_sym, get_creator_args);
if resolver != escrow_creator {
return Err(Error::UnauthorizedResolver);
}
// Drive the next step in the payment lifecycle via the action adapter.
let action = adapt_outcome(result)?;
execute_action(
&env,
action,
&dispute.escrow_contract,
dispute.escrow_split_id,
);
let result_code = match result {
DisputeResult::UpheldForRaiser => 0u32,
DisputeResult::DismissedForRaiser => 1u32,
DisputeResult::Tied => 2u32,
};
dispute.status = DisputeStatus::Resolved;
dispute.result = Some(result_code);
storage::save_dispute(&env, &dispute);
events::emit_dispute_resolved(&env, &dispute_id, result_code);
Ok(result)
}
/// Get a dispute record.
pub fn get_dispute(env: Env, dispute_id: String) -> Result<Dispute, Error> {
storage::get_dispute(&env, &dispute_id)
}
/// Get all dispute IDs.
pub fn get_all_disputes(env: Env) -> soroban_sdk::Vec<String> {
storage::get_list(&env)
}
}