forked from LabsCrypt/flowfi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
607 lines (519 loc) · 21.4 KB
/
Copy pathlib.rs
File metadata and controls
607 lines (519 loc) · 21.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#![no_std]
mod errors;
mod events;
mod storage;
mod types;
#[cfg(test)]
mod test;
use soroban_sdk::{contract, contractimpl, token, vec, Address, Env, InvokeError, Symbol};
use errors::StreamError;
use events::{
FeeCollectedEvent, StreamCancelledEvent, StreamCompletedEvent, StreamCreatedEvent,
StreamPausedEvent, StreamResumedEvent, StreamToppedUpEvent, TokensWithdrawnEvent,
};
use storage::{
config_exists, load_config, load_stream, next_stream_id, save_config, save_stream,
try_load_config, try_load_stream,
};
use types::{ProtocolConfig, Stream, StreamStatus};
/// Maximum allowed protocol fee: 1 000 bps = 10%.
const MAX_FEE_RATE_BPS: u32 = 1_000;
#[contract]
pub struct StreamContract;
#[contractimpl]
impl StreamContract {
// ─── Protocol Administration ──────────────────────────────────────────────
/// One-time initialization of the protocol fee configuration.
///
/// # Errors
/// - `AlreadyInitialized` — called more than once.
/// - `InvalidFeeRate` — `fee_rate_bps` exceeds `MAX_FEE_RATE_BPS`.
pub fn initialize(
env: Env,
admin: Address,
treasury: Address,
fee_rate_bps: u32,
) -> Result<(), StreamError> {
admin.require_auth();
if config_exists(&env) {
return Err(StreamError::AlreadyInitialized);
}
if fee_rate_bps > MAX_FEE_RATE_BPS {
return Err(StreamError::InvalidFeeRate);
}
save_config(
&env,
&ProtocolConfig {
admin,
treasury,
fee_rate_bps,
},
);
Ok(())
}
/// Update the treasury address and/or fee rate. Admin-only.
///
/// # Errors
/// - `NotInitialized` — `initialize` has not been called.
/// - `NotAdmin` — caller is not the current admin.
/// - `InvalidFeeRate` — `fee_rate_bps` exceeds `MAX_FEE_RATE_BPS`.
pub fn update_fee_config(
env: Env,
admin: Address,
treasury: Address,
fee_rate_bps: u32,
) -> Result<(), StreamError> {
admin.require_auth();
let config = load_config(&env)?;
if config.admin != admin {
return Err(StreamError::NotAdmin);
}
if fee_rate_bps > MAX_FEE_RATE_BPS {
return Err(StreamError::InvalidFeeRate);
}
save_config(
&env,
&ProtocolConfig {
admin: config.admin,
treasury,
fee_rate_bps,
},
);
Ok(())
}
/// Returns the current protocol fee configuration, or `None` if not yet initialized.
pub fn get_fee_config(env: Env) -> Option<ProtocolConfig> {
try_load_config(&env)
}
// ─── Stream Operations ────────────────────────────────────────────────────
/// Create a new payment stream.
///
/// Transfers `amount` tokens from `sender` to the contract, deducts the
/// protocol fee (if configured), and records the stream with a calculated
/// `rate_per_second = net_amount / duration`.
///
/// Returns the new stream ID (starts at 1, increments monotonically).
///
/// # Errors
/// - `InvalidAmount` — `amount` ≤ 0.
/// - `InvalidDuration` — `duration` is 0.
/// - `InvalidTokenAddress` — `token_address` is not a token contract.
pub fn create_stream(
env: Env,
sender: Address,
recipient: Address,
token_address: Address,
amount: i128,
duration: u64,
) -> Result<u64, StreamError> {
sender.require_auth();
if amount <= 0 {
return Err(StreamError::InvalidAmount);
}
if duration == 0 {
return Err(StreamError::InvalidDuration);
}
Self::validate_token_contract(&env, &token_address)?;
let stream_id = next_stream_id(&env);
let start_time = env.ledger().timestamp();
// Transfer gross amount from sender to this contract.
let token_client = token::Client::new(&env, &token_address);
let contract_address = env.current_contract_address();
token_client.transfer(&sender, &contract_address, &amount);
// Deduct protocol fee; returns net amount (== amount when no fee config).
let net_amount = Self::collect_fee(&env, &token_address, amount, stream_id);
let rate_per_second = net_amount / (duration as i128);
save_stream(
&env,
stream_id,
&Stream {
sender: sender.clone(),
recipient: recipient.clone(),
token_address: token_address.clone(),
rate_per_second,
deposited_amount: net_amount,
withdrawn_amount: 0,
start_time,
last_update_time: start_time,
is_active: true,
paused: false,
paused_at: None,
status: StreamStatus::Active,
},
);
env.events().publish(
(Symbol::new(&env, "stream_created"), stream_id),
StreamCreatedEvent {
stream_id,
sender,
recipient,
rate_per_second,
token_address,
deposited_amount: net_amount,
start_time,
},
);
Ok(stream_id)
}
/// Top up an active stream with additional tokens.
///
/// Only the original sender may top up their own stream. The top-up amount
/// is subject to protocol fees (if configured) before being added to the stream.
///
/// # Errors
/// - `InvalidAmount` — `amount` ≤ 0.
/// - `StreamNotFound` — no stream exists with `stream_id`.
/// - `Unauthorized` — caller is not the stream's sender.
/// - `StreamInactive` — stream has been cancelled or fully withdrawn.
pub fn top_up_stream(
env: Env,
sender: Address,
stream_id: u64,
amount: i128,
) -> Result<(), StreamError> {
sender.require_auth();
if amount <= 0 {
return Err(StreamError::InvalidAmount);
}
let mut stream = load_stream(&env, stream_id)?;
// Validate ownership and active status using helper functions
Self::validate_stream_ownership(&stream, &sender)?;
Self::validate_stream_active(&stream)?;
// Transfer tokens from sender to contract
let token_client = token::Client::new(&env, &stream.token_address);
let contract_address = env.current_contract_address();
token_client.transfer(&sender, &contract_address, &amount);
// Collect protocol fee and get net amount
let net_amount = Self::collect_fee(&env, &stream.token_address, amount, stream_id);
// Update stream state
stream.deposited_amount += net_amount;
stream.last_update_time = env.ledger().timestamp();
save_stream(&env, stream_id, &stream);
// Emit top-up event
env.events().publish(
(Symbol::new(&env, "stream_topped_up"), stream_id),
StreamToppedUpEvent {
stream_id,
sender,
amount: net_amount,
new_deposited_amount: stream.deposited_amount,
},
);
Ok(())
}
// ─── Internal Helpers ─────────────────────────────────────────────────────
/// Ensures the supplied token address implements the Soroban token interface.
fn validate_token_contract(env: &Env, token_address: &Address) -> Result<(), StreamError> {
match env.try_invoke_contract::<u32, InvokeError>(
token_address,
&Symbol::new(env, "decimals"),
vec![env],
) {
Ok(Ok(_)) => Ok(()),
_ => Err(StreamError::InvalidTokenAddress),
}
}
/// Calculate the claimable amount for a stream at a given timestamp.
///
/// Excludes any time the stream was paused. If the stream is currently
/// paused, accrual stops at `paused_at`.
///
/// # Overflow Protection
/// - Uses `checked_mul` for rate_per_second * elapsed_seconds multiplication
/// - Caps at stream.deposited_amount if overflow would occur
/// - Uses `checked_sub` for deposited - already_withdrawn calculation
/// - Overflow boundary: i128::MAX (~1.7e19) for both rate and duration
fn calculate_claimable(stream: &Stream, now: u64) -> i128 {
let effective_now = if stream.paused {
stream.paused_at.unwrap_or(stream.last_update_time)
} else {
now
};
let elapsed = effective_now.saturating_sub(stream.last_update_time);
// Use checked_mul to prevent overflow when multiplying rate * elapsed
// If overflow would occur, cap at deposited_amount (full deposit)
let streamed = match (elapsed as i128).checked_mul(stream.rate_per_second) {
Some(result) => result,
None => return stream.deposited_amount, // Overflow: cap at full deposit
};
// Use checked_sub for deposited - withdrawn calculation
let remaining = match stream
.deposited_amount
.checked_sub(stream.withdrawn_amount)
{
Some(result) => result,
None => 0, // Underflow: already withdrawn more than deposited
};
streamed.min(remaining)
}
/// Validate that a stream exists and is owned by the caller.
///
/// # Errors
/// - `StreamNotFound` — no stream exists with `stream_id`.
/// - `Unauthorized` — caller is not the stream's sender.
fn validate_stream_ownership(
stream: &Stream,
caller: &Address,
) -> Result<(), StreamError> {
if stream.sender != *caller {
return Err(StreamError::Unauthorized);
}
Ok(())
}
/// Validate that a stream is active.
///
/// # Errors
/// - `StreamInactive` — stream has been cancelled or fully withdrawn.
fn validate_stream_active(stream: &Stream) -> Result<(), StreamError> {
if !stream.is_active {
return Err(StreamError::StreamInactive);
}
Ok(())
}
/// Transfer tokens from contract to recipient and update stream state.
///
/// This helper consolidates the token transfer logic and stream state updates
/// to reduce code duplication across withdrawal operations.
fn transfer_and_update_stream(
env: &Env,
stream: &mut Stream,
recipient: &Address,
amount: i128,
now: u64,
) {
let token_client = token::Client::new(env, &stream.token_address);
let contract_address = env.current_contract_address();
token_client.transfer(&contract_address, recipient, &amount);
stream.withdrawn_amount += amount;
stream.last_update_time = now;
// Mark stream as inactive and completed if fully drained
if stream.withdrawn_amount >= stream.deposited_amount {
stream.is_active = false;
stream.status = StreamStatus::Completed;
}
}
/// Withdraw all currently claimable tokens from a stream.
///
/// Only the stream's recipient may call this. The amount withdrawn is calculated
/// based on elapsed time and the stream's rate. The stream is automatically marked
/// inactive once fully drained.
///
/// # Errors
/// - `StreamNotFound` — no stream exists with `stream_id`.
/// - `Unauthorized` — caller is not the stream's recipient.
/// - `StreamInactive` — stream is already inactive.
/// - `InvalidAmount` — no claimable balance (fully withdrawn already).
pub fn withdraw(env: Env, recipient: Address, stream_id: u64) -> Result<i128, StreamError> {
recipient.require_auth();
let mut stream = load_stream(&env, stream_id)?;
// Validate recipient authorization
if stream.recipient != recipient {
return Err(StreamError::Unauthorized);
}
// Validate stream is active and not paused
Self::validate_stream_active(&stream)?;
if stream.paused {
return Err(StreamError::StreamInactive);
}
let now = env.ledger().timestamp();
let claimable = Self::calculate_claimable(&stream, now);
if claimable <= 0 {
return Err(StreamError::InvalidAmount);
}
// Use helper function to transfer tokens and update state
Self::transfer_and_update_stream(&env, &mut stream, &recipient, claimable, now);
let completed = stream.status == StreamStatus::Completed;
save_stream(&env, stream_id, &stream);
env.events().publish(
(Symbol::new(&env, "tokens_withdrawn"), stream_id),
TokensWithdrawnEvent {
stream_id,
recipient: recipient.clone(),
amount: claimable,
timestamp: stream.last_update_time,
},
);
// Emit COMPLETED event on final withdrawal
if completed {
env.events().publish(
(Symbol::new(&env, "stream_completed"), stream_id),
StreamCompletedEvent {
stream_id,
recipient,
total_withdrawn: stream.withdrawn_amount,
},
);
}
Ok(claimable)
}
/// Cancel an active stream.
///
/// Only the stream's original sender may cancel. The recipient receives all
/// accrued tokens up to the cancellation moment, and any remaining unspent
/// balance is refunded to the sender.
///
/// # Errors
/// - `StreamNotFound` — no stream exists with `stream_id`.
/// - `Unauthorized` — caller is not the stream's sender.
/// - `StreamInactive` — stream is already inactive.
pub fn cancel_stream(env: Env, sender: Address, stream_id: u64) -> Result<(), StreamError> {
sender.require_auth();
let mut stream = load_stream(&env, stream_id)?;
// Validate ownership and active status
Self::validate_stream_ownership(&stream, &sender)?;
Self::validate_stream_active(&stream)?;
let now = env.ledger().timestamp();
let accrued_amount = Self::calculate_claimable(&stream, now);
let token_client = token::Client::new(&env, &stream.token_address);
let contract_address = env.current_contract_address();
// Settle recipient with all accrued tokens at cancellation
if accrued_amount > 0 {
token_client.transfer(&contract_address, &stream.recipient, &accrued_amount);
stream.withdrawn_amount = stream.withdrawn_amount.saturating_add(accrued_amount);
}
// Calculate and refund remaining balance to sender
let refunded_amount = stream
.deposited_amount
.saturating_sub(stream.withdrawn_amount);
if refunded_amount > 0 {
token_client.transfer(&contract_address, &sender, &refunded_amount);
}
// Mark stream as inactive
stream.is_active = false;
stream.status = StreamStatus::Cancelled;
stream.last_update_time = now;
let recipient = stream.recipient.clone();
let amount_withdrawn = stream.withdrawn_amount;
save_stream(&env, stream_id, &stream);
// Emit cancellation event
env.events().publish(
(Symbol::new(&env, "stream_cancelled"), stream_id),
StreamCancelledEvent {
stream_id,
sender,
recipient,
amount_withdrawn,
refunded_amount,
},
);
Ok(())
}
/// Pause an active stream. Only the sender may pause.
///
/// # Errors
/// - `StreamNotFound` — no stream exists with `stream_id`.
/// - `Unauthorized` — caller is not the stream's sender.
/// - `StreamInactive` — stream is already inactive.
pub fn pause_stream(env: Env, sender: Address, stream_id: u64) -> Result<(), StreamError> {
sender.require_auth();
let mut stream = load_stream(&env, stream_id)?;
Self::validate_stream_ownership(&stream, &sender)?;
Self::validate_stream_active(&stream)?;
if stream.paused {
return Err(StreamError::StreamInactive);
}
let now = env.ledger().timestamp();
stream.paused = true;
stream.paused_at = Some(now);
stream.status = StreamStatus::Paused;
save_stream(&env, stream_id, &stream);
env.events().publish(
(Symbol::new(&env, "stream_paused"), stream_id),
StreamPausedEvent { stream_id, sender, paused_at: now },
);
Ok(())
}
/// Resume a paused stream. Adjusts `end_time` by the pause duration.
///
/// The `last_update_time` is advanced to `now` so that accrual resumes
/// from the current moment, effectively extending the stream by the
/// duration it was paused.
///
/// # Errors
/// - `StreamNotFound` — no stream exists with `stream_id`.
/// - `Unauthorized` — caller is not the stream's sender.
/// - `StreamInactive` — stream is not paused (already active or cancelled).
pub fn resume_stream(env: Env, sender: Address, stream_id: u64) -> Result<u64, StreamError> {
sender.require_auth();
let mut stream = load_stream(&env, stream_id)?;
Self::validate_stream_ownership(&stream, &sender)?;
if !stream.paused {
return Err(StreamError::StreamInactive);
}
let now = env.ledger().timestamp();
let paused_at = stream.paused_at.unwrap_or(now);
let pause_duration = now.saturating_sub(paused_at);
// Advance last_update_time by pause duration so accrual resumes from now.
stream.last_update_time = stream.last_update_time.saturating_add(pause_duration);
// new_end_time represents when the stream will fully drain from now.
let remaining = stream.deposited_amount.saturating_sub(stream.withdrawn_amount);
let new_end_time = if stream.rate_per_second > 0 {
now + (remaining / stream.rate_per_second) as u64
} else {
now
};
stream.paused = false;
stream.paused_at = None;
stream.status = StreamStatus::Active;
save_stream(&env, stream_id, &stream);
env.events().publish(
(Symbol::new(&env, "stream_resumed"), stream_id),
StreamResumedEvent { stream_id, sender, new_end_time },
);
Ok(new_end_time)
}
// ─── Read-only Queries ────────────────────────────────────────────────────
/// Returns the stream record for `stream_id`, or `None` if it does not exist.
pub fn get_stream(env: Env, stream_id: u64) -> Option<Stream> {
try_load_stream(&env, stream_id)
}
/// Returns `true` if the stream exists and has status `Completed`.
pub fn is_stream_completed(env: Env, stream_id: u64) -> bool {
try_load_stream(&env, stream_id)
.map(|s| s.status == StreamStatus::Completed)
.unwrap_or(false)
}
/// Get the current claimable amount for a stream without modifying state.
///
/// This is a read-only query that calculates how many tokens the recipient
/// can currently withdraw based on elapsed time and stream rate.
///
/// Returns `None` if the stream doesn't exist, otherwise returns the claimable amount.
pub fn get_claimable_amount(env: Env, stream_id: u64) -> Option<i128> {
try_load_stream(&env, stream_id).map(|stream| {
if !stream.is_active {
return 0;
}
let now = env.ledger().timestamp();
Self::calculate_claimable(&stream, now)
})
}
// ─── Internal Helpers ─────────────────────────────────────────────────────
/// Deducts the protocol fee from `amount`, transfers it to the treasury,
/// emits a `fee_collected` event, and returns the net amount.
///
/// If no protocol config exists or the fee rate is 0, returns `amount` unchanged.
/// Time complexity: O(1).
fn collect_fee(env: &Env, token_address: &Address, amount: i128, stream_id: u64) -> i128 {
match try_load_config(env) {
Some(cfg) if cfg.fee_rate_bps > 0 => {
let fee = amount * (cfg.fee_rate_bps as i128) / 10_000;
if fee > 0 {
let token_client = token::Client::new(env, token_address);
token_client.transfer(&env.current_contract_address(), &cfg.treasury, &fee);
env.events().publish(
(Symbol::new(env, "fee_collected"), stream_id),
FeeCollectedEvent {
stream_id,
treasury: cfg.treasury,
fee_amount: fee,
token: token_address.clone(),
},
);
}
amount - fee
}
_ => amount,
}
}
}