|
1 | | -/// Pure computation functions for streaming payments |
| 1 | +/// Pure computation functions and types for streaming payments |
2 | 2 | /// |
3 | 3 | /// These functions can be tested without the aztec context. |
4 | 4 |
|
| 5 | +use aztec::protocol_types::{address::AztecAddress, traits::{Deserialize, Packable, Serialize}}; |
| 6 | + |
| 7 | +/// Public stream data stored in contract state |
| 8 | +/// Contains all stream parameters and current status |
| 9 | +#[derive(Eq, Serialize, Deserialize, Packable)] |
| 10 | +pub struct StreamData { |
| 11 | + /// Address that created the stream (can cancel) |
| 12 | + pub sender: AztecAddress, |
| 13 | + /// Address that receives the streamed tokens |
| 14 | + pub recipient: AztecAddress, |
| 15 | + /// Total tokens to be streamed |
| 16 | + pub total_amount: u128, |
| 17 | + /// Unix timestamp when streaming starts |
| 18 | + pub start_time: u64, |
| 19 | + /// Unix timestamp when streaming ends (fully vested) |
| 20 | + pub end_time: u64, |
| 21 | + /// Unix timestamp before which no tokens can be withdrawn |
| 22 | + pub cliff_time: u64, |
| 23 | + /// Amount already claimed by recipient |
| 24 | + pub claimed_amount: u128, |
| 25 | + /// Whether the stream has been cancelled by sender |
| 26 | + pub cancelled: bool, |
| 27 | +} |
| 28 | + |
| 29 | +impl StreamData { |
| 30 | + pub fn new( |
| 31 | + sender: AztecAddress, |
| 32 | + recipient: AztecAddress, |
| 33 | + total_amount: u128, |
| 34 | + start_time: u64, |
| 35 | + end_time: u64, |
| 36 | + cliff_time: u64, |
| 37 | + ) -> Self { |
| 38 | + StreamData { |
| 39 | + sender, |
| 40 | + recipient, |
| 41 | + total_amount, |
| 42 | + start_time, |
| 43 | + end_time, |
| 44 | + cliff_time, |
| 45 | + claimed_amount: 0, |
| 46 | + cancelled: false, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Calculate how many tokens are unlocked at the given timestamp |
| 51 | + pub fn compute_unlocked(self, current_time: u64) -> u128 { |
| 52 | + compute_unlocked_amount( |
| 53 | + self.total_amount, |
| 54 | + self.start_time, |
| 55 | + self.end_time, |
| 56 | + self.cliff_time, |
| 57 | + current_time, |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + /// Calculate how many tokens can be withdrawn (unlocked minus already claimed) |
| 62 | + pub fn compute_withdrawable(self, current_time: u64) -> u128 { |
| 63 | + let unlocked = self.compute_unlocked(current_time); |
| 64 | + compute_withdrawable_amount(unlocked, self.claimed_amount) |
| 65 | + } |
| 66 | + |
| 67 | + /// Calculate unvested amount (for cancellation) |
| 68 | + pub fn compute_unvested(self, current_time: u64) -> u128 { |
| 69 | + let vested = self.compute_unlocked(current_time); |
| 70 | + self.total_amount - vested |
| 71 | + } |
| 72 | +} |
| 73 | + |
5 | 74 | /// Calculate how many tokens are unlocked at a given timestamp |
6 | 75 | /// |
7 | 76 | /// # Arguments |
@@ -264,11 +333,7 @@ fn test_linear_vesting_precision() { |
264 | 333 | use crate::StreamingPayments; |
265 | 334 | use dep::aztec::{ |
266 | 335 | oracle::random::random, |
267 | | - protocol_types::address::AztecAddress, |
268 | | - test::helpers::{ |
269 | | - authwit::add_private_authwit_from_call, |
270 | | - test_environment::TestEnvironment, |
271 | | - }, |
| 336 | + test::helpers::{authwit::add_private_authwit_from_call, test_environment::TestEnvironment}, |
272 | 337 | }; |
273 | 338 | use dep::token::Token; |
274 | 339 |
|
@@ -439,11 +504,8 @@ unconstrained fn test_cancel_stream() { |
439 | 504 | // Stream is already fully vested - cancel with unvested_amount = 0 |
440 | 505 | // This tests that cancellation works for a fully vested stream |
441 | 506 | let unvested_amount: u128 = 0; |
442 | | - let cancel_call = StreamingPayments::at(streaming_address).cancel_stream( |
443 | | - stream_id, |
444 | | - recipient, |
445 | | - unvested_amount, |
446 | | - ); |
| 507 | + let cancel_call = |
| 508 | + StreamingPayments::at(streaming_address).cancel_stream(stream_id, unvested_amount); |
447 | 509 | env.call_private(sender, cancel_call); |
448 | 510 |
|
449 | 511 | // Sender should receive nothing back (all vested) |
|
0 commit comments