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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
#![forbid(unsafe_code)]
#![deny(clippy::all)]
#![allow(clippy::result_large_err)]

pub mod error;
pub mod event;
pub mod state;

use crate::error::RoundError;
use crate::event::*;
use crate::state::*;

use anchor_lang::{
    prelude::*,
    solana_program::{clock::UnixTimestamp, program_option::COption, program_pack::Pack},
    system_program, AnchorDeserialize, AnchorSerialize,
};
use anchor_spl::{
    associated_token::get_associated_token_address,
    token::{Mint, Token, TokenAccount},
};

pub const AUTHORITY_SEED: &[u8] = b"AUTH";
pub const OFFER_SEED: &[u8] = b"OFFER";
pub const BID_SEED: &[u8] = b"BID";
pub const VOUCHER_SEED: &[u8] = b"VOUCHER";

declare_id!("Round8ieb1Jcbp4m68kwCVyUJmHAVoz4orTwU3LtAuH");

#[program]
pub mod round {
    use super::*;

    pub fn create_round(ctx: Context<CreateRound>, params: CreateRoundParams) -> Result<()> {
        // validation
        let offer_amount = ctx.accounts.offer_source_wallet.delegated_amount;
        if ctx.accounts.offer_source_wallet.delegate.is_none() || offer_amount == 0 {
            return err!(RoundError::NoDelegatedAmount);
        }

        if params.bidding_start >= params.bidding_end {
            return err!(RoundError::SuspiciousOfferInterval);
        }

        // transfer round to wallet controlled by contract
        let cpi = CpiContext::new(
            ctx.accounts.token_program.to_account_info(),
            anchor_spl::token::Transfer {
                from: ctx.accounts.offer_source_wallet.to_account_info(),
                to: ctx.accounts.offer_wallet.to_account_info(),
                authority: ctx.accounts.offer_source_authority.to_account_info(),
            },
        );

        anchor_spl::token::transfer(cpi, offer_amount)?;

        let round = Round {
            status: RoundStatus::Pending,
            created_at: Clock::get()?.unix_timestamp,
            bid_mint: ctx.accounts.bid_mint.key(),
            offer_mint: ctx.accounts.offer_mint.key(),
            bidding_start: params.bidding_start,
            bidding_end: params.bidding_end,
            heir: params.heir,
            recipient: params.recipient,
            payer: *ctx.accounts.payer.key,
            vouchers_count: 0,
            total_bid: None,
            total_offer: Some(offer_amount),
            target_bid: params.target_bid,
            return_wallet: ctx.accounts.offer_source_wallet.key(),
            ..Default::default()
        };

        ctx.accounts.round.set_inner(round.clone());

        emit!(RoundCreatedEvent {
            round_addr: ctx.accounts.round.key(),
            round,
        });

        Ok(())
    }

    // Contribute bid mint to round
    pub fn contribute(ctx: Context<Contribute>) -> Result<()> {
        let now = Clock::get()?.unix_timestamp;
        ctx.accounts.round.assert_can_contribute(now)?;

        let Contribute {
            round,
            voucher,
            user,
            payer,
            user_wallet,
            user_wallet_authority,
            bid_wallet,
            token_program,
            ..
        } = ctx.accounts;

        let amount_to_deposit = user_wallet.delegated_amount;
        if user_wallet.delegate.is_none() || amount_to_deposit == 0 {
            return err!(RoundError::NoDelegatedAmount);
        }

        // transfer bid to wallet controlled by contract
        let cpi = CpiContext::new(
            token_program.to_account_info(),
            anchor_spl::token::Transfer {
                from: user_wallet.to_account_info(),
                to: bid_wallet.to_account_info(),
                authority: user_wallet_authority.to_account_info(),
            },
        );

        anchor_spl::token::transfer(cpi, amount_to_deposit)?;

        // add amount to user voucher (in case user contributed twice)
        voucher.amount_contributed = voucher
            .amount_contributed
            .checked_add(amount_to_deposit)
            .ok_or(error!(RoundError::Overflow))?;

        emit!(ContributeEvent {
            round: round.key(),
            user: user.key(),
            bid_mint: round.bid_mint,
            offer_mint: round.offer_mint,
            amount: amount_to_deposit,
        });

        if voucher.payer != Pubkey::default() {
            return Ok(());
        }

        // first time initialization things below

        // record voucher payer so it can't be changed later
        voucher.payer = payer.key();
        voucher.round = round.key();
        voucher.user = user.key();

        // keep track of vouchers
        round.vouchers_count = round
            .vouchers_count
            .checked_add(1)
            .ok_or(error!(RoundError::Overflow))?;

        Ok(())
    }

    // Withdraw user bid before round is expired
    pub fn withdraw(mut ctx: Context<Withdraw>) -> Result<()> {
        let now = Clock::get()?.unix_timestamp;
        ctx.accounts
            .round
            .assert_can_withdraw(now, ctx.accounts.user.is_signer)?;

        refund_bid_to_recipient(&mut ctx)?;

        // will be erased anyways, but to be sure
        ctx.accounts.voucher.amount_contributed = 0;
        ctx.accounts.round.vouchers_count = ctx
            .accounts
            .round
            .vouchers_count
            .checked_sub(1)
            .ok_or(error!(RoundError::Overflow))?;

        let reason = match (&ctx.accounts.round.status, ctx.accounts.user.is_signer) {
            (RoundStatus::Pending, true) => WithdrawReason::UserInitiated,
            (RoundStatus::Pending, false) => WithdrawReason::HeirTimeout,
            (RoundStatus::Rejected, _) => WithdrawReason::RoundRejected,
            _ => unreachable!("withdraw should not be called in this state"),
        };

        emit!(WithdrawEvent {
            round: ctx.accounts.round.key(),
            user: ctx.accounts.user.key(),
            bid_mint: ctx.accounts.round.bid_mint,
            offer_mint: ctx.accounts.round.offer_mint,
            amount: ctx.accounts.bid_wallet.amount,
            reason,
        });

        Ok(())
    }

    pub fn accept(mut ctx: Context<Accept>) -> Result<()> {
        let now = Clock::get()?.unix_timestamp;
        ctx.accounts.round.assert_can_accept_or_reject(now)?;

        if ctx.accounts.round.vouchers_count == 0 {
            return err!(RoundError::CantAcceptZeroOffering);
        }

        transfer_bid_to_recipient(&mut ctx)?;

        ctx.accounts.round.status = RoundStatus::Accepted;
        ctx.accounts.round.total_bid = Some(ctx.accounts.bid_wallet.amount);
        ctx.accounts.round.total_offer = Some(ctx.accounts.offer_wallet.amount);

        emit!(RoundAcceptedEvent {
            round: ctx.accounts.round.key(),
            heir: ctx.accounts.heir.key(),
            bid_mint: ctx.accounts.round.bid_mint,
            offer_mint: ctx.accounts.round.offer_mint,
            bid_amount: ctx.accounts.round.total_bid.unwrap(),
            offer_amount: ctx.accounts.round.total_offer.unwrap(),
        });

        Ok(())
    }

    pub fn reject(mut ctx: Context<Reject>) -> Result<()> {
        let Reject { round, .. } = ctx.accounts;

        let now = Clock::get()?.unix_timestamp;
        round.assert_can_accept_or_reject(now)?;

        round.status = RoundStatus::Rejected;

        transfer_offer_back_to_heir(&mut ctx)?;

        emit!(RoundRejectedEvent {
            round: ctx.accounts.round.key(),
            heir: ctx.accounts.heir.key(),
            bid_mint: ctx.accounts.round.bid_mint,
            offer_mint: ctx.accounts.round.offer_mint,
            offer_amount: ctx.accounts.offer_wallet.amount,
        });

        Ok(())
    }

    // SIR DO NOT REDEEM
    pub fn redeem(mut ctx: Context<Redeem>) -> Result<()> {
        ctx.accounts.round.assert_can_redeem()?;

        // remove in next update when target_bid can't be zero anymore
        let target = if ctx.accounts.round.target_bid != 0 {
            ctx.accounts.round.target_bid
        } else {
            ctx.accounts.round.total_bid.unwrap()
        };

        let amount = calculate_redeem_amount(
            target,
            ctx.accounts.round.total_bid.unwrap(),
            ctx.accounts.voucher.amount_contributed,
            ctx.accounts.round.total_offer.unwrap(),
        )
        .ok_or(error!(RoundError::Overflow))?;

        ctx.accounts.voucher.amount_contributed = 0;
        ctx.accounts.round.vouchers_count = ctx
            .accounts
            .round
            .vouchers_count
            .checked_sub(1)
            .ok_or(error!(RoundError::Overflow))?;

        transfer_to_user(&mut ctx, amount)?;

        emit!(RedeemEvent {
            round: ctx.accounts.round.key(),
            user: ctx.accounts.user.key(),
            bid_mint: ctx.accounts.round.bid_mint,
            offer_mint: ctx.accounts.round.offer_mint,
            amount,
        });

        Ok(())
    }

    // cancel round before it begins
    pub fn cancel(ctx: Context<Cancel>) -> Result<()> {
        let Cancel {
            round,
            offer_wallet,
            return_wallet,
            bid_wallet,
            token_program,
            authority,
            payer,
            heir,
            ..
        } = ctx.accounts;

        let now = Clock::get()?.unix_timestamp;
        round.assert_can_cancel(now)?;

        assert_eq!(round.vouchers_count, 0, "impossible voucher count");

        let round_key = round.key();
        let authority_seeds: &[&[&[u8]]] = &[&[
            AUTHORITY_SEED,
            round_key.as_ref(),
            &[*ctx.bumps.get("authority").unwrap()],
        ]];

        // transfer offer back
        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::Transfer {
                from: offer_wallet.to_account_info(),
                to: return_wallet.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::transfer(cpi, offer_wallet.amount)?;

        // close offer
        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::CloseAccount {
                account: offer_wallet.to_account_info(),
                destination: payer.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::close_account(cpi)?;

        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::CloseAccount {
                account: bid_wallet.to_account_info(),
                destination: payer.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::close_account(cpi)?;

        emit!(RoundCancelledEvent {
            round: round.key(),
            heir: heir.key(),
            bid_mint: round.bid_mint,
            offer_mint: round.offer_mint,
            bid_amount: bid_wallet.amount,
            offer_amount: offer_wallet.amount,
        });

        Ok(())
    }

    pub fn close(ctx: Context<Close>) -> Result<()> {
        // check if round can be cleaned up
        let Close {
            round,
            return_wallet,
            offer_wallet,
            bid_wallet,
            token_program,
            authority,
            payer,
            ..
        } = ctx.accounts;

        let now = Clock::get()?.unix_timestamp;
        round.assert_can_close(now)?;

        let round_key = round.key();
        let authority_seeds: &[&[&[u8]]] = &[&[
            AUTHORITY_SEED,
            round_key.as_ref(),
            &[*ctx.bumps.get("authority").unwrap()],
        ]];

        // transfer offer back
        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::Transfer {
                from: offer_wallet.to_account_info(),
                to: return_wallet.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::transfer(cpi, offer_wallet.amount)?;

        // close offer
        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::CloseAccount {
                account: offer_wallet.to_account_info(),
                destination: payer.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::close_account(cpi)?;

        // close bid if it exists
        if *bid_wallet.owner == spl_token::ID {
            let cpi = CpiContext::new_with_signer(
                token_program.to_account_info(),
                anchor_spl::token::CloseAccount {
                    account: bid_wallet.to_account_info(),
                    destination: payer.to_account_info(),
                    authority: authority.to_account_info(),
                },
                authority_seeds,
            );
            anchor_spl::token::close_account(cpi)?;
        }

        emit!(RoundClosedEvent {
            round_addr: round.key(),
            round: Clone::clone(round),
            returned_offer_amount: offer_wallet.amount,
        });

        Ok(())
    }

    pub fn migrate(ctx: Context<Migrate>) -> Result<()> {
        let round = &mut ctx.accounts.round;
        if round.return_wallet == Pubkey::default() {
            let return_wallet = get_associated_token_address(&round.heir, &round.offer_mint);

            round.return_wallet = return_wallet;
        }

        Ok(())
    }
}

fn refund_bid_to_recipient(ctx: &mut Context<Withdraw>) -> Result<()> {
    let Withdraw {
        round,
        voucher,
        user,
        user_wallet,
        bid_wallet,
        authority,
        wsol_mint,
        payer: _payer,
        current_payer,
        token_program,
        system_program,
    } = ctx.accounts;

    let round_key = round.key();
    let authority_seeds: &[&[&[u8]]] = &[&[
        AUTHORITY_SEED,
        round_key.as_ref(),
        &[*ctx.bumps.get("authority").unwrap()],
    ]];

    if !bid_wallet.is_native() {
        // if this is token (e.g. usdc), just transfer from bid to user wallet
        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::Transfer {
                from: bid_wallet.to_account_info(),
                to: user_wallet.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::transfer(cpi, voucher.amount_contributed)?;
        return Ok(());
    }

    // if wsol, transfer from bid wallet to temp addr, unwrap to another temp addr, transfer directly to user account/payer
    let space = spl_token::state::Account::LEN;
    let rent = Rent::get()?.minimum_balance(space);

    // init temp wallet
    {
        let cpi = CpiContext::new_with_signer(
            system_program.to_account_info(),
            system_program::CreateAccount {
                from: current_payer.to_account_info(),
                to: authority.to_account_info(),
            },
            authority_seeds,
        );
        system_program::create_account(cpi, rent, space as u64, &spl_token::ID)?;

        let cpi = CpiContext::new(
            token_program.to_account_info(),
            anchor_spl::token::InitializeAccount3 {
                account: authority.to_account_info(),
                mint: wsol_mint.to_account_info(),
                authority: authority.to_account_info(),
            },
        );
        anchor_spl::token::initialize_account3(cpi)?;
    }

    // transfer from bid wallet to authority wallet
    let cpi = CpiContext::new_with_signer(
        token_program.to_account_info(),
        anchor_spl::token::Transfer {
            from: bid_wallet.to_account_info(),
            to: authority.to_account_info(),
            authority: authority.to_account_info(),
        },
        authority_seeds,
    );
    anchor_spl::token::transfer(cpi, voucher.amount_contributed)?;

    // close account to current_payer
    let cpi = CpiContext::new_with_signer(
        token_program.to_account_info(),
        anchor_spl::token::CloseAccount {
            account: authority.to_account_info(),
            destination: current_payer.to_account_info(),
            authority: authority.to_account_info(),
        },
        authority_seeds,
    );
    anchor_spl::token::close_account(cpi)?;

    // payer should pay back to user
    let cpi = CpiContext::new_with_signer(
        system_program.to_account_info(),
        system_program::Transfer {
            from: current_payer.to_account_info(),
            to: user.to_account_info(),
        },
        authority_seeds,
    );
    system_program::transfer(cpi, voucher.amount_contributed)?;

    Ok(())
}

fn transfer_bid_to_recipient(ctx: &mut Context<Accept>) -> Result<()> {
    let Accept {
        round,
        bid_wallet,
        recipient,
        token_program,
        authority,
        payer,
        ..
    } = ctx.accounts;

    let round_key = round.key();
    let authority_seeds: &[&[&[u8]]] = &[&[
        AUTHORITY_SEED,
        round_key.as_ref(),
        &[*ctx.bumps.get("authority").unwrap()],
    ]];

    if let COption::Some(rent) = bid_wallet.is_native {
        // unwrap SOL's and send them directly
        // this way recipient can be simple SOL address
        // or a wrapped SOL wallet

        let balance = bid_wallet.amount;

        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            anchor_spl::token::CloseAccount {
                account: bid_wallet.to_account_info(),
                destination: authority.to_account_info(),
                authority: authority.to_account_info(),
            },
            authority_seeds,
        );
        anchor_spl::token::close_account(cpi)?;

        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            system_program::Transfer {
                from: authority.to_account_info(),
                to: recipient.to_account_info(),
            },
            authority_seeds,
        );
        system_program::transfer(cpi, balance)?;

        // refund rent
        let cpi = CpiContext::new_with_signer(
            token_program.to_account_info(),
            system_program::Transfer {
                from: authority.to_account_info(),
                to: payer.to_account_info(),
            },
            authority_seeds,
        );
        system_program::transfer(cpi, rent)?;

        return Ok(());
    }

    // or just transfer bid to spl wallet
    let cpi = CpiContext::new_with_signer(
        token_program.to_account_info(),
        anchor_spl::token::Transfer {
            from: bid_wallet.to_account_info(),
            to: recipient.to_account_info(),
            authority: authority.to_account_info(),
        },
        authority_seeds,
    );

    let amount = bid_wallet.amount; // transfer all round back
    anchor_spl::token::transfer(cpi, amount)?;
    Ok(())
}

fn transfer_offer_back_to_heir(ctx: &mut Context<Reject>) -> Result<()> {
    let Reject {
        round,
        offer_wallet,
        return_wallet,
        token_program,
        authority,
        ..
    } = ctx.accounts;

    let round_key = round.key();
    let authority_seeds: &[&[&[u8]]] = &[&[
        AUTHORITY_SEED,
        round_key.as_ref(),
        &[*ctx.bumps.get("authority").unwrap()],
    ]];

    // transfer bid to wallet controlled by contract
    let cpi = CpiContext::new_with_signer(
        token_program.to_account_info(),
        anchor_spl::token::Transfer {
            from: offer_wallet.to_account_info(),
            to: return_wallet.to_account_info(),
            authority: authority.to_account_info(),
        },
        authority_seeds,
    );

    let amount = offer_wallet.amount; // transfer all round back
    anchor_spl::token::transfer(cpi, amount)?;

    Ok(())
}

fn transfer_to_user(ctx: &mut Context<Redeem>, amount: u64) -> Result<()> {
    let Redeem {
        round,
        offer_wallet,
        user_wallet,
        token_program,
        authority,
        ..
    } = ctx.accounts;

    let round_key = round.key();
    let authority_seeds: &[&[&[u8]]] = &[&[
        AUTHORITY_SEED,
        round_key.as_ref(),
        &[*ctx.bumps.get("authority").unwrap()],
    ]];

    // transfer bid to wallet controlled by contract
    let cpi = CpiContext::new_with_signer(
        token_program.to_account_info(),
        anchor_spl::token::Transfer {
            from: offer_wallet.to_account_info(),
            to: user_wallet.to_account_info(),
            authority: authority.to_account_info(),
        },
        authority_seeds,
    );

    anchor_spl::token::transfer(cpi, amount)?;

    Ok(())
}

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct CreateRoundParams {
    /// has ability to accept/reject bid
    pub heir: Pubkey,
    /// bid recipient
    pub recipient: Pubkey,
    /// used to dillute
    pub target_bid: u64,

    pub bidding_start: UnixTimestamp,
    pub bidding_end: UnixTimestamp,
}

#[derive(Accounts)]
pub struct CreateRound<'info> {
    #[account(
        init,
        space = 8 + Round::INIT_SPACE,
        payer = payer,
    )]
    pub round: Box<Account<'info, Round>>,

    #[account(
        init,
        payer = payer,
        token::authority = authority,
        token::mint = offer_mint,
        seeds = [OFFER_SEED, round.key().as_ref()],
        bump,
    )]
    pub offer_wallet: Account<'info, TokenAccount>,
    pub offer_mint: Account<'info, Mint>,

    #[account(mut)]
    pub offer_source_wallet: Account<'info, TokenAccount>,
    pub offer_source_authority: Signer<'info>,

    #[account(
        init,
        payer = payer,
        token::authority = authority,
        token::mint = bid_mint,
        seeds = [BID_SEED, round.key().as_ref()],
        bump,
    )]
    pub bid_wallet: Account<'info, TokenAccount>,
    pub bid_mint: Account<'info, Mint>,

    /// CHECK: seeds are checked
    #[account(
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
    )]
    pub authority: UncheckedAccount<'info>,

    #[account(mut)]
    pub payer: Signer<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Contribute<'info> {
    #[account(mut)]
    pub round: Box<Account<'info, Round>>,

    #[account(
        init_if_needed,
        space = Voucher::calculate_space(),
        payer = payer,
        seeds = [VOUCHER_SEED, round.key().as_ref(), user.key.as_ref()],
        bump,
    )]
    pub voucher: Account<'info, Voucher>,
    pub user: Signer<'info>,

    #[account(mut)]
    pub user_wallet: Account<'info, TokenAccount>,
    pub user_wallet_authority: Signer<'info>,

    #[account(
        mut,
        seeds = [BID_SEED, round.key().as_ref()],
        bump,
    )]
    pub bid_wallet: Account<'info, TokenAccount>,

    #[account(mut)]
    pub payer: Signer<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Withdraw<'info> {
    #[account(mut)]
    pub round: Box<Account<'info, Round>>,

    #[account(
        mut,
        seeds = [VOUCHER_SEED, round.key().as_ref(), user.key().as_ref()],
        bump,
        has_one = user,
        close = payer,
        has_one = payer,
    )]
    pub voucher: Account<'info, Voucher>,

    /// CHECK: Somethimes withdraw call is permissionless, so signature is checked in code
    #[account(mut)]
    pub user: UncheckedAccount<'info>,

    /// CHECK: seeds are checked, we don't care about account being initialized
    #[account(
        mut,
        constraint = user_wallet.key() == get_associated_token_address(
            user.key,
            &bid_wallet.mint
        ),
    )]
    pub user_wallet: UncheckedAccount<'info>,

    #[account(
        mut,
        seeds = [BID_SEED, round.key().as_ref()],
        bump,
    )]
    pub bid_wallet: Account<'info, TokenAccount>,

    /// CHECK: seeds are checked
    #[account(
        mut,
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
    )]
    pub authority: UncheckedAccount<'info>,

    /// WSOL mint
    #[account(constraint = wsol_mint.key() == spl_token::native_mint::ID)]
    pub wsol_mint: Account<'info, Mint>,

    /// CHECK: We just transfer SOL from closing voucher to this account
    /// payer should equal to payer field on voucher
    /// so user can't just steal rent someone else maybe paid
    #[account(mut)]
    pub payer: UncheckedAccount<'info>,

    /// current payer should have some SOL to temporarily open WSOL account
    /// these funds are returned at the end of the call
    #[account(mut)]
    pub current_payer: Signer<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Accept<'info> {
    #[account(mut, has_one = heir, has_one = recipient)]
    pub round: Box<Account<'info, Round>>,

    pub heir: Signer<'info>,

    #[account(
        mut,
        seeds = [OFFER_SEED, round.key().as_ref()],
        bump,
    )]
    pub offer_wallet: Account<'info, TokenAccount>,

    #[account(
        mut,
        seeds = [BID_SEED, round.key().as_ref()],
        bump,
    )]
    pub bid_wallet: Account<'info, TokenAccount>,

    /// CHECK: this could be SOL address or token account, but the address is checked
    #[account(mut)]
    pub recipient: UncheckedAccount<'info>,

    /// CHECK: seeds are checked
    #[account(
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
        mut
    )]
    pub authority: UncheckedAccount<'info>,

    /// CHECK: we just refund bid_wallet rent to this account
    #[account(mut)]
    pub payer: UncheckedAccount<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Reject<'info> {
    #[account(mut, has_one = heir, has_one = payer, has_one = return_wallet)]
    pub round: Box<Account<'info, Round>>,

    pub heir: Signer<'info>,

    #[account(
        mut,
        seeds = [OFFER_SEED, round.key().as_ref()],
        bump,
    )]
    pub offer_wallet: Account<'info, TokenAccount>,

    #[account(mut)]
    pub return_wallet: Account<'info, TokenAccount>,

    /// CHECK: seeds are checked
    #[account(
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
        mut,
    )]
    pub authority: UncheckedAccount<'info>,

    /// CHECK: we just transfer SOL to this account if round has zero vouchers
    #[account(mut)]
    pub payer: UncheckedAccount<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Redeem<'info> {
    #[account(mut)]
    pub round: Box<Account<'info, Round>>,

    #[account(
        mut,
        seeds = [VOUCHER_SEED, round.key().as_ref(), user.key.as_ref()],
        bump,
        close = payer,
        has_one = payer,
    )]
    pub voucher: Account<'info, Voucher>,
    pub user: Signer<'info>,

    #[account(mut)]
    pub user_wallet: Account<'info, TokenAccount>,

    #[account(
        mut,
        seeds = [OFFER_SEED, round.key().as_ref()],
        bump,
    )]
    pub offer_wallet: Account<'info, TokenAccount>,

    /// CHECK: seeds are checked
    #[account(
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
    )]
    pub authority: UncheckedAccount<'info>,

    /// CHECK: we just transfer SOL to this account bc we close voucher
    #[account(mut)]
    pub payer: UncheckedAccount<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Cancel<'info> {
    #[account(
        mut,
        has_one = heir,
        has_one = payer,
        has_one = return_wallet,
        close = payer
    )]
    pub round: Box<Account<'info, Round>>,

    pub heir: Signer<'info>,

    // where to refund offer to
    #[account(mut)]
    pub return_wallet: Account<'info, TokenAccount>,

    #[account(
        mut,
        seeds = [OFFER_SEED, round.key().as_ref()],
        bump,
    )]
    pub offer_wallet: Account<'info, TokenAccount>,

    #[account(
        mut,
        seeds = [BID_SEED, round.key().as_ref()],
        bump,
    )]
    pub bid_wallet: Account<'info, TokenAccount>,

    /// CHECK: seeds are checked
    #[account(
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
    )]
    pub authority: UncheckedAccount<'info>,

    /// CHECK: we just transfer SOL to this account in case balance becomes zero
    #[account(mut)]
    pub payer: UncheckedAccount<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Close<'info> {
    #[account(
        mut,
        has_one = payer,
        has_one = return_wallet,
        close = payer
    )]
    pub round: Box<Account<'info, Round>>,

    // where to refund offer to
    #[account(mut)]
    pub return_wallet: Account<'info, TokenAccount>,

    #[account(
        mut,
        seeds = [OFFER_SEED, round.key().as_ref()],
        bump,
    )]
    pub offer_wallet: Account<'info, TokenAccount>,

    /// CHECK: could be closed
    #[account(
        mut,
        seeds = [BID_SEED, round.key().as_ref()],
        bump,
    )]
    pub bid_wallet: UncheckedAccount<'info>,

    /// CHECK: seeds are checked
    #[account(
        seeds = [AUTHORITY_SEED, round.key().as_ref()],
        bump,
    )]
    pub authority: UncheckedAccount<'info>,

    /// CHECK: we just transfer SOL to this account in case balance becomes zero
    #[account(mut)]
    pub payer: UncheckedAccount<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Migrate<'info> {
    pub round: Box<Account<'info, Round>>,
}