-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathdiff.txt
More file actions
362 lines (347 loc) · 11.3 KB
/
Copy pathdiff.txt
File metadata and controls
362 lines (347 loc) · 11.3 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
diff --git a/contracts/settlement/src/lib.rs b/contracts/settlement/src/lib.rs
index 18ab453..e4541b0 100644
--- a/contracts/settlement/src/lib.rs
+++ b/contracts/settlement/src/lib.rs
@@ -1,5 +1,6 @@
#![no_std]
-use soroban_sdk::{contract, contractimpl, contracttype, token, Address, Env, String, Symbol, Vec};
+pub mod archive;
+use soroban_sdk::{contract, contractimpl, contracttype, Address, Env};
#[contracttype]
#[derive(Clone)]
@@ -8,20 +9,164 @@ pub enum DataKey {
TotalSettled,
}
-pub mod admin;
-pub mod archive;
-pub mod batch;
-pub mod errors;
-pub mod events;
-pub mod migrate;
-pub mod pagination;
-pub mod timelock;
-pub mod types;
-
pub use errors::SettlementError;
pub use timelock::PendingDeveloperMigration;
pub use types::*;
+/// Tracks a developer's cumulative withdrawal amount for a given epoch day.
+///
+/// `day` is `timestamp / 86400` (UTC epoch day). When the current call's day
+/// differs from the stored day the accumulator is silently reset.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DailyWithdrawState {
+ pub day: u64,
+ pub amount: i128,
+}
+
+/// Timestamp range during which a developer may claim accrued balance.
+///
+/// `start_ts` and `end_ts` are ledger timestamps in seconds. The window is
+/// inclusive on both ends: a withdrawal is allowed when
+/// `start_ts <= env.ledger().timestamp() <= end_ts`.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DeveloperClaimWindow {
+ pub start_ts: u64,
+ pub end_ts: u64,
+}
+
+/// Payment received event
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct PaymentReceivedEvent {
+ pub from_vault: Address,
+ pub amount: i128,
+ pub to_pool: bool, // true if credited to global pool, false if to specific developer
+ pub developer: Option<Address>, // developer address if credited to specific developer
+ pub token: Address,
+}
+
+/// Balance credited event
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct BalanceCreditedEvent {
+ pub developer: Address,
+ pub amount: i128,
+ pub new_balance: i128,
+ pub token: Address,
+}
+
+/// Emitted when a deposit is made for a developer.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DepositEvent {
+ pub developer: Address,
+ pub token: Address,
+ pub amount: i128,
+}
+
+/// Emitted when a new vault address is proposed via `propose_vault()`.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct VaultProposedEvent {
+ pub current_vault: Address,
+ pub proposed_vault: Address,
+}
+
+/// Emitted when the proposed vault is accepted via `accept_vault()`.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct VaultAcceptedEvent {
+ pub old_vault: Address,
+ pub new_vault: Address,
+ pub accepted_by: Address,
+}
+
+/// Emitted when a developer withdraws their balance.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DeveloperWithdrawEvent {
+ pub developer: Address,
+ pub amount: i128,
+ pub remaining_balance: i128,
+ pub to: Address,
+ pub token: Address,
+}
+
+/// Emitted when the admin sets or changes a developer's daily withdrawal cap.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DailyWithdrawCapChanged {
+ pub developer: Address,
+ pub new_cap: i128,
+}
+
+/// Emitted when the admin sets or clears a developer claim window.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DeveloperClaimWindowChanged {
+ pub developer: Address,
+ pub start_ts: u64,
+ pub end_ts: u64,
+ pub enabled: bool,
+}
+
+/// Emitted when an admin force-credits a developer balance (escape hatch).
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct DeveloperForceCreditedEvent {
+ pub developer: Address,
+ pub amount: i128,
+ pub reason: Symbol,
+ pub new_balance: i128,
+ pub token: Address,
+}
+
+/// Emitted when the admin proposes or executes a timelock'd developer balance migration.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct AdminMigrationEvent {
+ pub from: Address,
+ pub to: Address,
+ pub amount: i128,
+ pub executed_at: u64,
+}
+
+/// Storage TTL entry for a given storage key category.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct StorageEntryTtl {
+ pub category: String,
+ pub key_desc: String,
+ pub storage_type: String,
+ pub ttl: u32,
+ pub threshold: u32,
+ pub bump_amount: u32,
+}
+
+/// Severity levels for admin broadcast messages.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub enum Severity {
+ Info,
+ Warn,
+ Crit,
+}
+
+/// Payload for the `admin_broadcast` event.
+#[contracttype]
+#[derive(Clone, Debug, PartialEq)]
+pub struct AdminBroadcast {
+ pub severity: Severity,
+ pub message: String,
+}
+
+/// Maximum byte length for the `reason` Symbol in `force_credit_developer`.
+/// The Soroban SDK enforces a 32-byte limit on Symbol values at construction;
+/// this constant is used for explicit defense-in-depth validation.
+pub const MAX_REASON_LENGTH: u32 = 32;
+
#[contract]
pub struct CalloraSettlement;
@@ -35,20 +180,6 @@ impl CalloraSettlement {
env.storage().instance().set(&DataKey::TotalSettled, &0i128);
}
- pub fn record_deduction(env: Env, amount: i128, _request_id: u64) {
- let vault = Self::get_vault(env.clone());
- vault.require_auth();
- let total = env
- .storage()
- .instance()
- .get::<_, i128>(&DataKey::TotalSettled)
- .unwrap_or(0);
- let new_total = total.checked_add(amount).unwrap();
- env.storage()
- .instance()
- .set(&DataKey::TotalSettled, &new_total);
- }
-
/// Receive payment from vault and credit to pool or developer balance.
///
/// # Arguments
@@ -289,6 +420,7 @@ impl CalloraSettlement {
soroban_sdk::String::from_str(&_env, env!("CARGO_PKG_VERSION"))
}
+
/// Get registered vault address
pub fn get_vault(env: Env) -> Address {
env.storage()
@@ -301,7 +433,7 @@ impl CalloraSettlement {
pub fn get_global_pool(env: Env) -> GlobalPool {
env.storage()
.instance()
- .get::<_, GlobalPool>(&StorageKey::GlobalPool)
+ .get(&StorageKey::GlobalPool)
.unwrap_or_else(|| env.panic_with_error(SettlementError::NotInitialized))
}
@@ -431,32 +563,49 @@ impl CalloraSettlement {
Self::require_claim_window_open(&env, &developer)?;
- let balance_key = StorageKey::DeveloperBalance(developer.clone(), usdc_address.clone());
- let current_balance: i128 = env.storage().persistent().get(&balance_key).unwrap_or(0);
+ let usdc_address = Self::get_usdc_token(env.clone())?;
+ let current_balance: i128 = env
+ .storage()
+ .instance()
+ .get::<_, Address>(&DataKey::Vault)
+ .unwrap();
+ vault.require_auth();
+ let total = env
+ .storage()
+ .instance()
+ .get::<_, i128>(&DataKey::TotalSettled)
+ .unwrap_or(0);
+ let new_total = total.checked_add(amount).unwrap();
+ env.storage()
+ .instance()
+ .set(&DataKey::TotalSettled, &new_total);
+ }
- if current_balance < amount {
- return Err(SettlementError::InsufficientDeveloperBalance);
- }
+ /// Migrate a single developer's V1 balance to V2 (admin only).
+ pub fn migrate_developer_balance(
+ env: Env,
+ caller: Address,
+ developer: Address,
+ ) -> Result<(), SettlementError> {
+ migrate::migrate_single_developer(&env, &caller, &developer)
+ }
- let new_balance = current_balance
- .checked_sub(amount)
- .ok_or(SettlementError::DeveloperBalanceUnderflow)?;
- env.storage().persistent().set(&balance_key, &new_balance);
-
- usdc.transfer(&contract_address, &recipient, &amount);
-
- env.events().publish(
- (events::event_developer_withdraw(&env), developer.clone()),
- DeveloperWithdrawEvent {
- developer: developer.clone(),
- amount,
- remaining_balance: new_balance,
- to: recipient,
- token: usdc_address.clone(),
- },
- );
+ /// Migrate a single developer's V1 balance to V2 (admin only).
+ pub fn migrate_single_dev_v2(
+ env: Env,
+ caller: Address,
+ developer: Address,
+ ) -> Result<(), SettlementError> {
+ migrate::migrate_single_developer(&env, &caller, &developer)
+ }
- Ok(())
+ /// Migrate a single developer's V1 balance to V2 (admin only).
+ pub fn migrate_developer_balance(
+ env: Env,
+ caller: Address,
+ developer: Address,
+ ) -> Result<(), SettlementError> {
+ migrate::migrate_single_developer(&env, &caller, &developer)
}
/// Migrate a single developer's V1 balance to V2 (admin only).
@@ -478,7 +627,7 @@ impl CalloraSettlement {
///
/// Returns `(next_cursor, is_complete)`. When `is_complete` is `true` the
/// full list has been processed.
- pub fn batch_withdraw_cursor(
+ pub fn batch_withdraw_developer_balance_cursor(
env: Env,
developers: Vec<Address>,
amounts: Vec<i128>,
@@ -494,12 +643,8 @@ impl CalloraSettlement {
let end = (start + safe_limit as usize).min(count as usize);
for i in start..end {
- let developer = developers
- .get(i as u32)
- .ok_or(SettlementError::InsufficientDeveloperBalance)?;
- let amount = amounts
- .get(i as u32)
- .ok_or(SettlementError::AmountNotPositive)?;
+ let developer = developers.get(i as u32).ok_or(SettlementError::InsufficientDeveloperBalance)?;
+ let amount = amounts.get(i as u32).ok_or(SettlementError::AmountNotPositive)?;
Self::withdraw_developer_balance(env.clone(), developer, amount, None)?;
}
@@ -507,39 +652,4 @@ impl CalloraSettlement {
let is_complete = next_cursor >= count;
Ok((next_cursor, is_complete))
}
-
- fn require_authorized_caller(env: Env, caller: Address) {
- let vault = Self::get_vault(env.clone());
- let admin = Self::get_admin(env.clone());
- if caller != vault && caller != admin {
- env.panic_with_error(SettlementError::Unauthorized);
- }
- }
-
- fn sorted_insert(env: &Env, index: &mut soroban_sdk::Vec<Address>, address: Address) {
- if !index.contains(&address) {
- index.push_back(address);
- }
- }
-
- fn require_claim_window_open(env: &Env, developer: &Address) -> Result<(), SettlementError> {
- let window: Option<crate::types::DeveloperClaimWindow> = env
- .storage()
- .persistent()
- .get(&StorageKey::DeveloperClaimWindow(developer.clone()));
- if let Some(w) = window {
- let now = env.ledger().timestamp();
- if now < w.start_ts || now > w.end_ts {
- return Err(SettlementError::ClaimWindowClosed);
- }
- }
- Ok(())
- }
-
- pub fn batch_settle(
- env: Env,
- settlements: soroban_sdk::Vec<batch::SettleInput>,
- ) -> soroban_sdk::Vec<batch::SettleOutcome> {
- batch::batch_settle(&env, settlements)
- }
}