forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaim.rs
More file actions
251 lines (207 loc) · 8.4 KB
/
Copy pathclaim.rs
File metadata and controls
251 lines (207 loc) · 8.4 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Claim lifecycle and DAO voting will be implemented here.
//
// Planned public functions:
// file_claim(env, policy_id, amount, details, image_urls)
// vote_on_claim(env, voter, claim_id, vote)
//
// Open claim accounting: `storage::OpenClaimCount(holder, policy_id)` must be
// incremented when a claim enters `Processing` and decremented when it reaches
// a terminal status (`Approved` / `Rejected`), so policy termination can block
// or audit in-flight claims. Until `file_claim` ships, admins may use
// `admin_set_open_claim_count` in tests or break-glass ops only.
use crate::{
ledger,
storage,
types::{Claim, ClaimProcessed, ClaimStatus, VoteOption},
validate::Error,
};
use soroban_sdk::{symbol_short, token, Address, Env, String, Vec};
// ── file_claim ────────────────────────────────────────────────────────────────
/// File a new claim against an active policy.
///
/// Window checks (all via `ledger` helpers):
/// - Policy must be active: `now` in `[start_ledger, end_ledger)`.
/// - Rate-limit: `now >= last_filed_at + RATE_LIMIT_WINDOW_LEDGERS` (or first claim).
///
/// Returns the new `claim_id`.
pub fn file_claim(
env: &Env,
holder: &Address,
policy_id: u32,
amount: i128,
details: &String,
image_urls: &Vec<String>,
) -> Result<u64, Error> {
// Check pause: claims are blocked if claims_paused is true
storage::assert_claims_not_paused(env);
let policy = storage::get_policy(env, holder, policy_id).ok_or(Error::ClaimNotFound)?;
// Policy active window check using ledger helper.
let now = env.ledger().sequence();
if !ledger::is_within_window(now, policy.start_ledger, policy.end_ledger) {
return if ledger::is_expired(now, policy.end_ledger) {
Err(Error::PolicyExpired)
} else {
Err(Error::PolicyInactive)
};
}
if !policy.is_active {
return Err(Error::PolicyInactive);
}
// Rate-limit check.
if let Some(last) = storage::get_last_claim_ledger(env, holder) {
if !ledger::is_rate_limit_elapsed(now, last, ledger::RATE_LIMIT_WINDOW_LEDGERS) {
return Err(Error::RateLimitExceeded);
}
}
crate::validate::check_claim_fields(env, amount, policy.coverage, details, image_urls)?;
let claim_id = storage::next_claim_id(env);
let claim = Claim {
claim_id,
policy_id,
claimant: holder.clone(),
amount,
details: details.clone(),
image_urls: image_urls.clone(),
status: ClaimStatus::Processing,
approve_votes: 0,
reject_votes: 0,
filed_at: now,
};
storage::set_claim(env, &claim);
storage::snapshot_claim_voters(env, claim_id);
storage::set_last_claim_ledger(env, holder, now);
env.events().publish(
(symbol_short!("clm_filed"), claim_id),
holder.clone(),
);
Ok(claim_id)
}
// ── vote_on_claim ─────────────────────────────────────────────────────────────
/// Cast a vote on a pending claim.
///
/// Window check: `now < filed_at + VOTE_WINDOW_LEDGERS` (via `ledger::is_vote_open`).
/// Returns the updated `ClaimStatus` after tallying.
pub fn vote_on_claim(
env: &Env,
voter: &Address,
claim_id: u64,
vote: &VoteOption,
) -> Result<ClaimStatus, Error> {
// Check pause: voting is blocked if claims_paused is true
storage::assert_claims_not_paused(env);
let mut claim = storage::get_claim(env, claim_id).ok_or(Error::ClaimNotFound)?;
if claim.status.is_terminal() {
return Err(Error::ClaimAlreadyTerminal);
}
// Voting window check.
let now = env.ledger().sequence();
if !ledger::is_vote_open(now, claim.filed_at, ledger::VOTE_WINDOW_LEDGERS) {
return Err(Error::VotingWindowClosed);
}
// Voter must be in the claim's snapshot electorate.
let snapshot = storage::get_claim_voters(env, claim_id);
let eligible = snapshot.iter().any(|v| v == *voter);
if !eligible {
return Err(Error::NotEligibleVoter);
}
// Duplicate vote check.
if storage::get_vote(env, claim_id, voter).is_some() {
return Err(Error::DuplicateVote);
}
storage::set_vote(env, claim_id, voter, vote);
match vote {
VoteOption::Approve => claim.approve_votes += 1,
VoteOption::Reject => claim.reject_votes += 1,
}
env.events().publish(
(symbol_short!("c_paid"), claim.claim_id),
ClaimProcessed {
claim_id: claim.claim_id,
recipient: claim.claimant.clone(),
amount: claim.amount,
asset: claim.asset.clone(),
},
);
// Auto-finalize on majority.
let total = snapshot.len();
let majority = total / 2 + 1;
if claim.approve_votes >= majority {
claim.status = ClaimStatus::Approved;
// Payout is triggered by admin via process_claim, not here.
// Setting Approved makes the claim eligible for process_claim.
} else if claim.reject_votes >= majority {
claim.status = ClaimStatus::Rejected;
}
storage::set_claim(env, &claim);
Ok(claim.status)
}
// ── finalize_claim ────────────────────────────────────────────────────────────
/// Finalize a claim after the voting deadline has passed.
///
/// Window check: `now >= filed_at + VOTE_WINDOW_LEDGERS` (via `ledger::is_vote_deadline_passed`).
/// Plurality wins; tie resolves to Rejected.
pub fn finalize_claim(env: &Env, claim_id: u64) -> Result<ClaimStatus, Error> {
// Check pause: finalization is blocked if claims_paused is true
storage::assert_claims_not_paused(env);
let mut claim = storage::get_claim(env, claim_id).ok_or(Error::ClaimNotFound)?;
if claim.status.is_terminal() {
return Err(Error::ClaimAlreadyTerminal);
}
let now = env.ledger().sequence();
if !ledger::is_vote_deadline_passed(now, claim.filed_at, ledger::VOTE_WINDOW_LEDGERS) {
return Err(Error::VotingWindowStillOpen);
}
claim.status = if claim.approve_votes > claim.reject_votes {
ClaimStatus::Approved
} else {
// Tie or reject plurality → Rejected (insurer wins tie).
ClaimStatus::Rejected
};
if claim.status == ClaimStatus::Approved {
// Payout triggered by admin via process_claim.
}
storage::set_claim(env, &claim);
Ok(claim.status)
}
// ── process_claim (admin payout trigger) ─────────────────────────────────────
pub fn process_claim(env: &Env, claim_id: u64) -> Result<(), Error> {
let mut claim = storage::get_claim(env, claim_id).ok_or(Error::ClaimNotFound)?;
if claim.status == ClaimStatus::Paid {
return Err(Error::AlreadyPaid);
}
if claim.status != ClaimStatus::Approved {
return Err(Error::ClaimNotApproved);
}
payout(env, &claim)?;
claim.status = ClaimStatus::Paid;
storage::set_claim(env, &claim);
Ok(())
}
// ── Internal helpers ──────────────────────────────────────────────────────────
fn payout(env: &Env, claim: &Claim) -> Result<(), Error> {
let token_addr = storage::get_token(env);
let token_client = token::Client::new(env, &token_addr);
let treasury = env.current_contract_address();
if token_client.balance(&treasury) < claim.amount {
return Err(Error::InsufficientTreasury);
}
token_client.transfer(&treasury, &claim.claimant, &claim.amount);
env.events().publish(
(symbol_short!("clm_paid"), claim.claim_id),
ClaimProcessed {
claim_id: claim.claim_id,
recipient: claim.claimant.clone(),
amount: claim.amount,
},
);
Ok(())
}
pub fn get_claim(env: &Env, claim_id: u64) -> Result<Claim, Error> {
storage::get_claim(env, claim_id).ok_or(Error::ClaimNotFound)
}
pub fn is_allowed_asset(env: &Env, asset: &Address) -> bool {
storage::is_allowed_asset(env, asset)
}
pub fn set_allowed_asset(env: &Env, asset: &Address, allowed: bool) {
storage::set_allowed_asset(env, asset, allowed);
}