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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Superstream is a protocol and a collection of libraries for real-time money streaming on Solana. It allows anyone to
//! continuously stream money to anyone else transparently and efficiently.
//!
//! Superstream protocol is completely open-source. View it on
//! [GitHub](https://github.com/gpahal/superstream).
//!
//! Learn more about Superstream on [superstream.finance](https://superstream.finance/).
//!
//! ## Usage in CPI (Cross-Program Invocation)
//!
//! > **NOTE:** For a complete example, see
//! > [superstream-cpi-example](https://github.com/gpahal/superstream/tree/main/program/programs/superstream-cpi-example)
//!
//! - Add the dependency in your program's Cargo.toml
//!
//! ```toml ignore
//! superstream = { version = "0.3.1", features = ["cpi"] }
//! ```
//!
//! - Invoke Superstream's instruction. In the example below, we are calling Superstream's cancel instruction.
//!
//! ```rust ignore
//! #[program]
//! pub mod superstream_cpi_example {
//!     /// Cancel a stream.
//!     pub fn cancel(ctx: Context<Cancel>, seed: u64, name: String, recipient: Pubkey) -> Result<()> {
//!         let cpi_program = ctx.accounts.superstream_program.to_account_info();
//!         let cpi_accounts = superstream::cpi::accounts::Cancel {
//!             stream: ctx.accounts.stream.to_account_info(),
//!             signer: ctx.accounts.signer.to_account_info(),
//!             sender: ctx.accounts.sender.to_account_info(),
//!             mint: ctx.accounts.sender.to_account_info(),
//!             signer_token: ctx.accounts.signer_token.to_account_info(),
//!             sender_token: ctx.accounts.sender_token.to_account_info(),
//!             recipient_token: ctx.accounts.recipient_token.to_account_info(),
//!             escrow_token: ctx.accounts.escrow_token.to_account_info(),
//!             token_program: ctx.accounts.token_program.to_account_info(),
//!         };
//!         let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
//!
//!         superstream::cpi::cancel(cpi_ctx, seed, name, recipient)
//!     }
//!
//!     // ... other stuff
//! }
//!
//! /// Accounts struct for cancelling a stream.
//! #[derive(Accounts)]
//! pub struct Cancel<'info> {
//!     /// Stream PDA account.
//!     #[account(mut)]
//!     pub stream: AccountInfo<'info>,
//!
//!     /// Signer wallet.
//!     pub signer: Signer<'info>,
//!
//!     /// Stream sender account.
//!     pub sender: AccountInfo<'info>,
//!     /// SPL token mint account.
//!     pub mint: Box<Account<'info, Mint>>,
//!
//!     /// Associated token account of the signer.
//!     #[account(mut)]
//!     pub signer_token: Box<Account<'info, TokenAccount>>,
//!     /// Associated token account of the sender.
//!     #[account(mut)]
//!     pub sender_token: Box<Account<'info, TokenAccount>>,
//!     /// Associated token account of the recipient.
//!     #[account(mut)]
//!     pub recipient_token: Box<Account<'info, TokenAccount>>,
//!     /// Associated token escrow account holding the funds for this stream.
//!     #[account(mut)]
//!     pub escrow_token: Box<Account<'info, TokenAccount>>,
//!
//!     /// SPL token program.
//!     pub token_program: Program<'info, Token>,
//!
//!     /// Superstream program.
//!     pub superstream_program: Program<'info, superstream::program::Superstream>,
//! }
//!
//! // ... other stuff
//! ```

mod transfer;
mod utils;

pub mod error;
pub mod state;

use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, Token, TokenAccount};

use crate::{
    error::StreamError,
    state::Stream,
    transfer::{transfer_from_escrow, transfer_to_escrow},
    utils::is_token_account_rent_exempt,
};

declare_id!("4WLNkJ6RKt54sv85iTgJPLgoaxfrxAasZWBxAPLUfuVG");

/// PDA account seed to create new stream PDA accounts.
pub const STREAM_ACCOUNT_SEED: &[u8] = b"stream";

#[program]
pub mod superstream {
    //! Module for superstream cpi methods and other utilities.

    use super::*;

    /// Create a new prepaid stream.
    ///
    /// # Arguments
    ///
    /// For more information on the arguments, see fields of the [`Stream`] struct.
    pub fn create_prepaid(
        mut ctx: Context<Create>,
        seed: u64,
        name: String,
        recipient: Pubkey,
        starts_at: u64,
        ends_at: u64,
        initial_amount: u64,
        flow_interval: u64,
        flow_rate: u64,
        sender_can_cancel: bool,
        sender_can_cancel_at: u64,
        sender_can_change_sender: bool,
        sender_can_change_sender_at: u64,
        sender_can_pause: bool,
        sender_can_pause_at: u64,
        recipient_can_resume_pause_by_sender: bool,
        recipient_can_resume_pause_by_sender_at: u64,
        anyone_can_withdraw_for_recipient: bool,
        anyone_can_withdraw_for_recipient_at: u64,
    ) -> Result<()> {
        create(
            &mut ctx,
            true,
            recipient,
            name,
            starts_at,
            ends_at,
            initial_amount,
            flow_interval,
            flow_rate,
            sender_can_cancel,
            sender_can_cancel_at,
            sender_can_change_sender,
            sender_can_change_sender_at,
            sender_can_pause,
            sender_can_pause_at,
            recipient_can_resume_pause_by_sender,
            recipient_can_resume_pause_by_sender_at,
            anyone_can_withdraw_for_recipient,
            anyone_can_withdraw_for_recipient_at,
            seed,
        )?;

        let stream = &mut ctx.accounts.stream;
        let prepaid_amount_needed = stream.initialize_prepaid()?;
        ctx.accounts.transfer_to_escrow(prepaid_amount_needed)
    }

    /// Create a new non-prepaid stream.
    ///
    /// # Arguments
    ///
    /// * `topup_amount` - Initial topup amount for the stream. The topup amount should be >= minimum deposit required.
    ///   See [`DEPOSIT_AMOUNT_PERIOD_IN_SECS`](crate::state::DEPOSIT_AMOUNT_PERIOD_IN_SECS) for more information.
    ///
    /// For more information on the other arguments, see fields of the [`Stream`] struct.
    pub fn create_non_prepaid(
        mut ctx: Context<Create>,
        seed: u64,
        name: String,
        recipient: Pubkey,
        starts_at: u64,
        ends_at: u64,
        initial_amount: u64,
        flow_interval: u64,
        flow_rate: u64,
        sender_can_cancel: bool,
        sender_can_cancel_at: u64,
        sender_can_change_sender: bool,
        sender_can_change_sender_at: u64,
        sender_can_pause: bool,
        sender_can_pause_at: u64,
        recipient_can_resume_pause_by_sender: bool,
        recipient_can_resume_pause_by_sender_at: u64,
        anyone_can_withdraw_for_recipient: bool,
        anyone_can_withdraw_for_recipient_at: u64,
        topup_amount: u64,
    ) -> Result<()> {
        create(
            &mut ctx,
            false,
            recipient,
            name,
            starts_at,
            ends_at,
            initial_amount,
            flow_interval,
            flow_rate,
            sender_can_cancel,
            sender_can_cancel_at,
            sender_can_change_sender,
            sender_can_change_sender_at,
            sender_can_pause,
            sender_can_pause_at,
            recipient_can_resume_pause_by_sender,
            recipient_can_resume_pause_by_sender_at,
            anyone_can_withdraw_for_recipient,
            anyone_can_withdraw_for_recipient_at,
            seed,
        )?;

        let stream = &mut ctx.accounts.stream;
        stream.initialize_non_prepaid(topup_amount)?;
        ctx.accounts.transfer_to_escrow(topup_amount)
    }

    /// Cancel a stream.
    ///
    /// # Arguments
    ///
    /// For more information on the arguments, see fields of the [`Stream`] struct.
    pub fn cancel(ctx: Context<Cancel>, seed: u64, name: String, recipient: Pubkey) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        let stream_key = stream.to_account_info().key;
        let bump = stream.bump;
        let params = stream.cancel(*stream_key, &ctx.accounts.signer, recipient)?;
        ctx.accounts
            .transfer_from_escrow_to_sender(seed, &name, bump, params.transfer_amount_sender)?;
        ctx.accounts
            .transfer_from_escrow_to_signer(seed, &name, bump, params.transfer_amount_signer)?;
        ctx.accounts
            .transfer_from_escrow_to_recipient(seed, &name, bump, params.transfer_amount_recipient)?;

        Ok(())
    }

    /// Withdraw excess sender topup from a non-prepaid stream.
    ///
    /// # Arguments
    ///
    /// For more information on the arguments, see fields of the [`Stream`] struct.
    pub fn withdraw_excess_topup_non_prepaid_ended(
        ctx: Context<WithdrawExcessTopupNonPrepaidEnded>,
        seed: u64,
        name: String,
    ) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        let amount = stream.withdraw_excess_topup_non_prepaid_ended()?;
        if amount > 0 {
            let bump = stream.bump;
            ctx.accounts.transfer_from_escrow(seed, &name, bump, amount)?;
        }
        Ok(())
    }

    /// Topup a non-prepaid stream.
    ///
    /// # Arguments
    ///
    /// * `topup_amount` - Topup amount for the stream. The topup amount should be <= maximum acceptable topup amount.
    ///
    /// For more information on the other arguments, see fields of the [`Stream`] struct.
    pub fn topup_non_prepaid(
        ctx: Context<TopupNonPrepaid>,
        _seed: u64,
        _name: String,
        topup_amount: u64,
    ) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        stream.topup_non_prepaid(topup_amount)?;
        ctx.accounts.transfer_to_escrow(topup_amount)
    }

    /// Change sender of a non-prepaid stream.
    ///
    /// # Arguments
    ///
    /// * `new_sender` - The new sender
    ///
    /// For more information on the other arguments, see fields of the [`Stream`] struct.
    pub fn change_sender_non_prepaid(
        ctx: Context<ChangeSenderNonPrepaid>,
        _seed: u64,
        _name: String,
        new_sender: Pubkey,
    ) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        stream.change_sender_non_prepaid(&ctx.accounts.sender, new_sender)
    }

    /// Withdraw recipient funds from a stream.
    ///
    /// # Arguments
    ///
    /// For more information on the arguments, see fields of the [`Stream`] struct.
    pub fn withdraw(
        ctx: Context<WithdrawAndChangeRecipient>,
        seed: u64,
        name: String,
        recipient: Pubkey,
    ) -> Result<()> {
        withdraw_and_change_recipient(ctx, seed, name, recipient, Pubkey::default())
    }

    /// Withdraw recipient funds from a stream and change recipient of a stream.
    ///
    /// # Arguments
    ///
    /// * `new_recipient` - The new recipient
    ///
    /// For more information on the other arguments, see fields of the [`Stream`] struct.
    pub fn withdraw_and_change_recipient(
        ctx: Context<WithdrawAndChangeRecipient>,
        seed: u64,
        name: String,
        recipient: Pubkey,
        new_recipient: Pubkey,
    ) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        let amount_available_to_withdraw =
            stream.withdraw_and_change_recipient(&ctx.accounts.signer, recipient, new_recipient)?;
        let bump = stream.bump;
        ctx.accounts
            .transfer_from_escrow(seed, &name, bump, amount_available_to_withdraw)
    }

    /// Pause a non-prepaid stream.
    ///
    /// # Arguments
    ///
    /// For more information on the arguments, see fields of the [`Stream`] struct.
    pub fn pause_non_prepaid(ctx: Context<PauseNonPrepaid>, _seed: u64, _name: String) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        stream.pause_non_prepaid(&ctx.accounts.signer)
    }

    /// Resume a non-prepaid stream.
    ///
    /// # Arguments
    ///
    /// For more information on the arguments, see fields of the [`Stream`] struct.
    pub fn resume_non_prepaid(ctx: Context<ResumeNonPrepaid>, _seed: u64, _name: String) -> Result<()> {
        let stream = &mut ctx.accounts.stream;
        stream.resume_non_prepaid(&ctx.accounts.signer)
    }
}

pub(crate) fn create(
    ctx: &mut Context<Create>,
    is_prepaid: bool,
    recipient: Pubkey,
    name: String,
    starts_at: u64,
    ends_at: u64,
    initial_amount: u64,
    flow_interval: u64,
    flow_rate: u64,
    sender_can_cancel: bool,
    sender_can_cancel_at: u64,
    sender_can_change_sender: bool,
    sender_can_change_sender_at: u64,
    sender_can_pause: bool,
    sender_can_pause_at: u64,
    recipient_can_resume_pause_by_sender: bool,
    recipient_can_resume_pause_by_sender_at: u64,
    anyone_can_withdraw_for_recipient: bool,
    anyone_can_withdraw_for_recipient_at: u64,
    seed: u64,
) -> Result<()> {
    let escrow_token_account = &ctx.accounts.escrow_token;
    require!(
        is_token_account_rent_exempt(escrow_token_account)?,
        StreamError::EscrowNotRentExempt,
    );

    let stream = &mut ctx.accounts.stream;
    stream.initialize(
        is_prepaid,
        ctx.accounts.mint.key(),
        ctx.accounts.sender.key(),
        recipient,
        name,
        starts_at,
        ends_at,
        initial_amount,
        flow_interval,
        flow_rate,
        sender_can_cancel,
        sender_can_cancel_at,
        sender_can_change_sender,
        sender_can_change_sender_at,
        sender_can_pause,
        sender_can_pause_at,
        recipient_can_resume_pause_by_sender,
        recipient_can_resume_pause_by_sender_at,
        anyone_can_withdraw_for_recipient,
        anyone_can_withdraw_for_recipient_at,
        seed,
        *ctx.bumps.get("stream").unwrap(),
    )
}

/// Accounts struct for creating a new stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String)]
pub struct Create<'info> {
    /// Stream PDA account. This is initialized by the program.
    #[account(
        init,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        payer = sender,
        space = Stream::space(&name),
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Stream sender wallet.
    #[account(mut)]
    pub sender: Signer<'info>,
    /// SPL token mint account.
    pub mint: Box<Account<'info, Mint>>,

    /// Associated token account of the sender.
    #[account(
        mut,
        constraint =
            sender_token.mint == mint.key()
            && sender_token.owner == sender.key(),
    )]
    pub sender_token: Box<Account<'info, TokenAccount>>,
    /// Associated token escrow account holding the funds for this stream.
    #[account(
        mut,
        constraint =
            escrow_token.mint == mint.key()
            && escrow_token.owner == stream.key(),
    )]
    pub escrow_token: Box<Account<'info, TokenAccount>>,

    /// SPL token program.
    pub token_program: Program<'info, Token>,
    /// Solana system program.
    pub system_program: Program<'info, System>,
}

/// Accounts struct for cancelling a stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String, recipient: Pubkey)]
pub struct Cancel<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Signer wallet. Either the sender or the receiver can cancel the stream till it's solvent. After insolvency,
    /// anyone can cancel.
    pub signer: Signer<'info>,

    /// Stream sender account.
    ///
    /// CHECK: Only 1 check is needed which is in the constraint. That is enough to verify that we are sending the funds
    /// to the stream sender.
    #[account(constraint = sender.key() == stream.sender)]
    pub sender: UncheckedAccount<'info>,
    /// SPL token mint account.
    pub mint: Box<Account<'info, Mint>>,

    /// Associated token account of the signer.
    #[account(
        mut,
        constraint =
            signer_token.mint == mint.key()
            && signer_token.owner == signer.key(),
    )]
    pub signer_token: Box<Account<'info, TokenAccount>>,
    /// Associated token account of the sender.
    #[account(
        mut,
        constraint =
            sender_token.mint == mint.key()
            && sender_token.owner == sender.key(),
    )]
    pub sender_token: Box<Account<'info, TokenAccount>>,
    /// Associated token account of the recipient.
    #[account(
        mut,
        constraint =
            recipient_token.mint == mint.key()
            && recipient_token.owner == recipient,
    )]
    pub recipient_token: Box<Account<'info, TokenAccount>>,
    /// Associated token escrow account holding the funds for this stream.
    #[account(
        mut,
        constraint =
            escrow_token.mint == mint.key()
            && escrow_token.owner == stream.key(),
    )]
    pub escrow_token: Box<Account<'info, TokenAccount>>,

    /// SPL token program.
    pub token_program: Program<'info, Token>,
}

/// Accounts struct for withdrawing excess sender topup from a non-prepaid stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String)]
pub struct WithdrawExcessTopupNonPrepaidEnded<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Signer wallet.
    pub signer: Signer<'info>,

    /// Stream sender account.
    ///
    /// CHECK: Only 1 check is needed which is in the constraint. That is enough to verify that we are sending the funds
    /// to the stream sender.
    #[account(constraint = sender.key() == stream.sender)]
    pub sender: UncheckedAccount<'info>,
    /// SPL token mint account.
    pub mint: Box<Account<'info, Mint>>,

    /// Associated token account of the sender.
    #[account(
        mut,
        constraint =
            sender_token.mint == mint.key()
            && sender_token.owner == sender.key(),
    )]
    pub sender_token: Box<Account<'info, TokenAccount>>,
    /// Associated token escrow account holding the funds for this stream.
    #[account(
        mut,
        constraint =
            escrow_token.mint == mint.key()
            && escrow_token.owner == stream.key(),
    )]
    pub escrow_token: Box<Account<'info, TokenAccount>>,

    /// SPL token program.
    pub token_program: Program<'info, Token>,
}

/// Accounts struct for topping up a non-prepaid stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String)]
pub struct TopupNonPrepaid<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Signer wallet. Anyone can topup a stream. But the refund when the stream gets cancelled will only go to the
    /// stream sender.
    pub signer: Signer<'info>,
    /// SPL token mint account.
    pub mint: Account<'info, Mint>,

    /// Associated token account of the signer.
    #[account(
        mut,
        constraint =
            signer_token.mint == mint.key()
            && signer_token.owner == signer.key(),
    )]
    pub signer_token: Account<'info, TokenAccount>,
    /// Associated token escrow account holding the funds for this stream.
    #[account(
        mut,
        constraint =
            escrow_token.mint == mint.key()
            && escrow_token.owner == stream.key(),
    )]
    pub escrow_token: Account<'info, TokenAccount>,

    /// SPL token program.
    pub token_program: Program<'info, Token>,
}

/// Accounts struct for changing the sender of a non-prepaid stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String)]
pub struct ChangeSenderNonPrepaid<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    // Stream sender wallet.
    pub sender: Signer<'info>,
    /// SPL token mint account.
    pub mint: Account<'info, Mint>,
}

/// Accounts struct for withdrawing recipient funds from a stream and changing recipient of a stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String, recipient: Pubkey)]
pub struct WithdrawAndChangeRecipient<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Signer wallet. Anybody can call the withdraw method. The recipient of the withdrawn amount is not related to the
    /// signer. Recipient is passed as an argument, based on which the stream PDA is accessed, so if a malicious user
    /// tries to send themselves as a recipient, but a different stream account, the constraint for the stream account
    /// will fail.
    pub signer: Signer<'info>,
    /// SPL token mint account.
    pub mint: Box<Account<'info, Mint>>,

    /// Associated token account of the recipient.
    #[account(
        mut,
        constraint =
            recipient_token.mint == mint.key()
            && recipient_token.owner == recipient,
    )]
    pub recipient_token: Box<Account<'info, TokenAccount>>,
    /// Associated token escrow account holding the funds for this stream.
    #[account(
        mut,
        constraint =
            escrow_token.mint == mint.key()
            && escrow_token.owner == stream.key(),
    )]
    pub escrow_token: Box<Account<'info, TokenAccount>>,

    /// SPL token program.
    pub token_program: Program<'info, Token>,
}

/// Accounts struct for pausing a non-prepaid stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String)]
pub struct PauseNonPrepaid<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Signer wallet. Signer needs to be either the sender (if they are allowed to) or the recipient.
    pub signer: Signer<'info>,
    /// SPL token mint account.
    pub mint: Account<'info, Mint>,
}

/// Accounts struct for resuming a non-prepaid stream.
#[derive(Accounts)]
#[instruction(seed: u64, name: String)]
pub struct ResumeNonPrepaid<'info> {
    /// Stream PDA account.
    #[account(
        mut,
        seeds = [
            STREAM_ACCOUNT_SEED,
            seed.to_le_bytes().as_ref(),
            mint.key().as_ref(),
            name.as_bytes(),
        ],
        bump,
    )]
    pub stream: Account<'info, Stream>,

    /// Signer wallet. Signer needs to be either the sender (if they are allowed to) or the recipient (exception is if
    /// the stream was paused by the sender and recipient is not allowed to resume a stream paused by sender).
    pub signer: Signer<'info>,
    /// SPL token mint account.
    pub mint: Account<'info, Mint>,
}

impl<'info> Create<'info> {
    /// Transfer funds from the associated token account of the sender to associated token escrow account holding the
    /// funds for this stream.
    pub fn transfer_to_escrow(&self, amount: u64) -> Result<()> {
        transfer_to_escrow(
            &self.sender,
            &self.sender_token,
            &self.escrow_token,
            &self.token_program,
            amount,
        )
    }
}

impl<'info> Cancel<'info> {
    /// Transfer funds from the associated token escrow account holding the funds for this stream to the associated
    /// token account of the sender.
    pub fn transfer_from_escrow_to_sender(&self, seed: u64, name: &str, bump: u8, amount: u64) -> Result<()> {
        self.transfer_from_escrow(&self.sender_token, seed, name, bump, amount)
    }

    /// Transfer funds from the associated token escrow account holding the funds for this stream to the associated
    /// token account of the signer.
    pub fn transfer_from_escrow_to_signer(&self, seed: u64, name: &str, bump: u8, amount: u64) -> Result<()> {
        self.transfer_from_escrow(&self.signer_token, seed, name, bump, amount)
    }

    /// Transfer funds from the associated token escrow account holding the funds for this stream to the associated
    /// token account of the recipient.
    pub fn transfer_from_escrow_to_recipient(&self, seed: u64, name: &str, bump: u8, amount: u64) -> Result<()> {
        self.transfer_from_escrow(&self.recipient_token, seed, name, bump, amount)
    }

    fn transfer_from_escrow(
        &self,
        destination_token: &Account<'info, TokenAccount>,
        seed: u64,
        name: &str,
        bump: u8,
        amount: u64,
    ) -> Result<()> {
        transfer_from_escrow(
            &self.stream,
            destination_token,
            &self.escrow_token,
            &self.token_program,
            seed,
            &self.mint.key(),
            name,
            bump,
            amount,
        )
    }
}

impl<'info> WithdrawExcessTopupNonPrepaidEnded<'info> {
    /// Transfer funds from the associated token escrow account holding the funds for this stream to the associated
    /// token account of the sender.
    fn transfer_from_escrow(&self, seed: u64, name: &str, bump: u8, amount: u64) -> Result<()> {
        transfer_from_escrow(
            &self.stream,
            &self.sender_token,
            &self.escrow_token,
            &self.token_program,
            seed,
            &self.mint.key(),
            name,
            bump,
            amount,
        )
    }
}

impl<'info> TopupNonPrepaid<'info> {
    /// Transfer funds from the associated token account of the sender to associated token escrow account holding the
    /// funds for this stream.
    pub fn transfer_to_escrow(&self, amount: u64) -> Result<()> {
        transfer_to_escrow(
            &self.signer,
            &self.signer_token,
            &self.escrow_token,
            &self.token_program,
            amount,
        )
    }
}

impl<'info> WithdrawAndChangeRecipient<'info> {
    /// Transfer funds from the associated token escrow account holding the funds for this stream to the associated
    /// token account of the recipient.
    pub fn transfer_from_escrow(&self, seed: u64, name: &str, bump: u8, amount: u64) -> Result<()> {
        transfer_from_escrow(
            &self.stream,
            &self.recipient_token,
            &self.escrow_token,
            &self.token_program,
            seed,
            &self.mint.key(),
            name,
            bump,
            amount,
        )
    }
}