may_cpi 0.6.0

CPI for the Mayflower program
Documentation
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
use anchor_lang::prelude::*;

#[cfg(feature = "local")]
declare_id!("43bYyFn8WBwxXvXh28yQ22SQz1TMY1w5DYnRAzRqpJ1J");

#[cfg(feature = "devnet")]
declare_id!("MDKJQPtnnJob8bJhs8eNXDBuS3Q7RWsbpPNg1FjEXxv");

#[cfg(feature = "mainnet")]
declare_id!("MMkP6WPG4ySTudigPQpKNpranEYBzYRDe8Ua7Dx89Rk");

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

    /// Initialize a platform tenant
    pub fn tenant_init(
        _ctx: Context<TenantInitAccounts>,
        _fee_bps: u32,
        _permissionless_group_creation: bool,
    ) -> Result<()> {
        panic!("not implemented")
    }

    /// Initialize a market group
    /// A market group owns all markets under it and can perform admin actions on them
    pub fn market_group_init(
        _ctx: Context<MarketGroupInitAccounts>,
        _args: MarketGroupInitArgs,
    ) -> Result<()> {
        panic!("not implemented")
    }

    /// Propose a new admin for the market group
    pub fn market_group_propose_admin(
        _ctx: Context<MarketGroupProposeAdminAccounts>,
        _new_admin: Pubkey,
    ) -> Result<MarketGroupProposeAdminEvent> {
        panic!("not implemented")
    }

    /// Accept the new admin for the market group
    pub fn market_group_accept_new_admin(
        _ctx: Context<MarketGroupAcceptNewAdminAccounts>,
    ) -> Result<MarketGroupAcceptNewAdminEvent> {
        panic!("not implemented")
    }

    /// Initialize a linear market
    pub fn market_linear_init(
        _ctx: Context<MarketLinearInitAccounts>,
        _market_linear_args: MarketLinearArgs,
    ) -> Result<()> {
        panic!("not implemented")
    }

    /// Mint options
    pub fn mint_options(
        _ctx: Context<MintOptionsAccounts>,
        _amount: u64,
    ) -> Result<MintOptionsEvent> {
        panic!("not implemented")
    }

    /// Collect revenue for a market group
    pub fn market_group_collect_rev(
        _ctx: Context<MarketGroupCollectRevAccounts>,
        _amount: FullOrPartialU64,
    ) -> Result<MarketGroupCollectRevEvent> {
        panic!("not implemented")
    }

    /// Raise the floor while preserving the area under the curve
    pub fn raise_floor_preserve_area(
        _ctx: Context<RaiseFloorPreserveAreaAccounts>,
        _new_floor: DecimalSerialized,
        _new_shoulder_end: u64,
    ) -> Result<RaiseFloorFromTriggerEvent> {
        panic!("not implemented")
    }

    /// Raise the floor from excess liquidity
    pub fn raise_floor_from_excess_liquidity(
        _ctx: Context<RaiseFloorFromExcessLiquidityAccounts>,
        _new_floor: DecimalSerialized,
    ) -> Result<RaiseFloorFromExcessLiquidityEvent> {
        panic!("not implemented")
    }

    /// Change the flags for a market
    pub fn market_flags_change(
        _ctx: Context<MarketFlagsChangeAccounts>,
        _new_flags: MarketPermissions,
    ) -> Result<MarketFlagsChangeEvent> {
        panic!("not implemented")
    }

    /// Change the fees for a market group
    pub fn market_group_change_fees(
        _ctx: Context<MarketGroupChangeFeesAccounts>,
        _new_fees: Fees,
    ) -> Result<MarketGroupChangeFeesEvent> {
        panic!("not implemented")
    }

    /// Initialize a personal position for a market
    /// This position tracks collateral and debt
    pub fn personal_position_init(
        _ctx: Context<PersonalPositionInitAccounts>,
    ) -> Result<PersonalPositionInitEvent> {
        panic!("not implemented")
    }

    /// Donate liquidity to the market, increasing its cash balance
    /// Adding excess liquidity to the market will enable the floor to be raised
    pub fn donate_liquidity(
        _ctx: Context<DonateLiquidityAccounts>,
        _amount: u64,
    ) -> Result<DonateLiquidityEvent> {
        panic!("not implemented")
    }

    /// Buy a market token with exact cash in, expecting a minimum amount of tokens out
    pub fn buy_with_exact_cash_in(
        _ctx: Context<BuyWithExactCashInAccounts>,
        _cash_in: u64,
        _min_token_out: u64,
    ) -> Result<BuyEvent> {
        panic!("not implemented")
    }

    /// Buy a market token with exact cash in, expecting a minimum amount of tokens out, and deposit the tokens into a personal position
    pub fn buy_with_exact_cash_in_and_deposit(
        _ctx: Context<BuyWithExactCashInAndDepositAccounts>,
        _cash_in: u64,
        _min_token_out: u64,
    ) -> Result<BuyWithExactCashInAndDepositEvent> {
        panic!("not implemented")
    }

    /// Sell exact market tokens in, expecting a minimum amount of cash out
    pub fn sell_with_exact_token_in(
        _ctx: Context<SellWithExactTokenInAccounts>,
        _amount_in: u64,
        _cash_out_min: u64,
    ) -> Result<SellWithExactTokenInEvent> {
        panic!("not implemented")
    }

    /// Sell exact market tokens in, expecting a minimum amount of cash out, and deposit the tokens into a personal position
    pub fn sell_with_exact_token_in_after_withdraw(
        _ctx: Context<SellWithExactTokenInAfterWithdrawAccounts>,
        _amount_in: u64,
        _cash_out_min: u64,
    ) -> Result<SellWithExactTokenInAfterWithdrawEvent> {
        panic!("not implemented")
    }

    /// Deposit collateral into a personal position
    pub fn deposit(_ctx: Context<DepositAccounts>, _amount: u64) -> Result<DepositEvent> {
        panic!("not implemented")
    }

    /// Withdraw collateral from a personal position
    pub fn withdraw(_ctx: Context<WithdrawAccounts>, _amount: u64) -> Result<WithdrawEvent> {
        panic!("not implemented")
    }

    /// Borrow cash from the market, using the collateral in the personal position
    pub fn borrow(_ctx: Context<BorrowAccounts>, _amount: u64) -> Result<BorrowEvent> {
        panic!("not implemented")
    }

    /// Repay cash to the market, lowering the debt in the personal position
    pub fn repay(_ctx: Context<RepayAccounts>, _amount: u64) -> Result<RepayEvent> {
        panic!("not implemented")
    }

    /// Exercise options, receiving the market tokens in return
    pub fn exercise_options(
        _ctx: Context<ExerciseOptionsAccounts>,
        _amount: u64,
    ) -> Result<ExerciseOptionsEvent> {
        panic!("not implemented")
    }

    /// Realloc the market meta account
    pub fn realloc_market_meta(_ctx: Context<ReallocMarketMetaAccounts>) -> Result<()> {
        panic!("not implemented")
    }
}

// Instruction account structures
#[derive(Accounts)]
pub struct TenantInitAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    #[account(signer)]
    pub root: AccountInfo<'info>,
    #[account(signer)]
    pub tenant_seed: AccountInfo<'info>,
    #[account(mut)]
    pub tenant: AccountInfo<'info>,
    pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketGroupInitAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    /// Optional account, which only needs to match if the tenant does not allow permissionless group creation
    #[account(signer)]
    pub tenant_admin: AccountInfo<'info>,
    #[account(signer)]
    pub seed: AccountInfo<'info>,
    #[account(mut)]
    pub market_group: AccountInfo<'info>,
    pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketGroupProposeAdminAccounts<'info> {
    #[account(mut, signer)]
    pub admin: AccountInfo<'info>,
    #[account(mut)]
    pub market_group: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketGroupAcceptNewAdminAccounts<'info> {
    #[account(mut, signer)]
    pub new_admin: AccountInfo<'info>,
    #[account(mut)]
    pub market_group: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketLinearInitAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    #[account(mut, signer)]
    pub group_admin: AccountInfo<'info>,
    #[account(signer)]
    pub seed: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    /// The token account for the market
    /// This account must be created in a previous instruction
    /// With supply set to zero
    /// And mint authority assigned to the market_meta
    pub mint_token: AccountInfo<'info>,
    #[account(mut)]
    pub mint_options: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub market_meta: AccountInfo<'info>,
    pub system_program: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MintOptionsAccounts<'info> {
    #[account(mut, signer)]
    pub admin: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    #[account(mut)]
    pub mint_options: AccountInfo<'info>,
    #[account(mut)]
    pub options_dst: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketGroupCollectRevAccounts<'info> {
    #[account(mut, signer)]
    pub group_admin: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_dst: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct RaiseFloorPreserveAreaAccounts<'info> {
    #[account(mut, signer)]
    pub admin: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct RaiseFloorFromExcessLiquidityAccounts<'info> {
    #[account(mut, signer)]
    pub admin: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketFlagsChangeAccounts<'info> {
    #[account(mut, signer)]
    pub admin: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    #[account(mut)]
    pub market_meta: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MarketGroupChangeFeesAccounts<'info> {
    #[account(mut, signer)]
    pub admin: AccountInfo<'info>,
    #[account(mut)]
    pub market_group: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct PersonalPositionInitAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    pub owner: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    pub mint_token: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    #[account(mut)]
    pub escrow: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
    pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct DonateLiquidityAccounts<'info> {
    /// Donor of the liquidity
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub main_src: AccountInfo<'info>,
    /// The liquidity vault for the main token in the market
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct BuyWithExactCashInAccounts<'info> {
    #[account(mut, signer)]
    pub signer: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub mint_token: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub token_dst: AccountInfo<'info>,
    #[account(mut)]
    pub main_src: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct BuyWithExactCashInAndDepositAccounts<'info> {
    #[account(mut, signer)]
    pub owner: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    #[account(mut)]
    pub escrow: AccountInfo<'info>,
    #[account(mut)]
    pub mint_token: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub token_dst: AccountInfo<'info>,
    #[account(mut)]
    pub main_src: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct SellWithExactTokenInAccounts<'info> {
    #[account(mut, signer)]
    pub user: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub mint_token: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub main_dst: AccountInfo<'info>,
    #[account(mut)]
    pub token_src: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct SellWithExactTokenInAfterWithdrawAccounts<'info> {
    #[account(mut, signer)]
    pub owner: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    #[account(mut)]
    pub mint_token: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub main_dst: AccountInfo<'info>,
    #[account(mut)]
    pub token_src: AccountInfo<'info>,
    #[account(mut)]
    pub escrow: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct DepositAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    pub mint_token: AccountInfo<'info>,
    #[account(mut)]
    pub market_linear: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    #[account(mut)]
    pub escrow: AccountInfo<'info>,
    #[account(mut)]
    pub token_src: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct WithdrawAccounts<'info> {
    #[account(mut, signer)]
    pub owner: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    pub mint_token: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    #[account(mut)]
    pub escrow: AccountInfo<'info>,
    #[account(mut)]
    pub token_dst: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct BorrowAccounts<'info> {
    #[account(mut, signer)]
    pub owner: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub main_dst: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct RepayAccounts<'info> {
    #[account(mut, signer)]
    pub repayer: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub personal_position: AccountInfo<'info>,
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub main_src: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct ExerciseOptionsAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    pub tenant: AccountInfo<'info>,
    pub market_group: AccountInfo<'info>,
    pub market_meta: AccountInfo<'info>,
    #[account(mut)]
    pub market: AccountInfo<'info>,
    #[account(mut)]
    pub main_src: AccountInfo<'info>,
    #[account(mut)]
    pub options_src: AccountInfo<'info>,
    #[account(mut)]
    pub token_dst: AccountInfo<'info>,
    #[account(mut)]
    pub liq_vault_main: AccountInfo<'info>,
    #[account(mut)]
    pub mint_options: AccountInfo<'info>,
    #[account(mut)]
    pub mint_token: AccountInfo<'info>,
    #[account(mut)]
    pub mint_main: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_tenant: AccountInfo<'info>,
    #[account(mut)]
    pub rev_escrow_group: AccountInfo<'info>,
    pub token_program: AccountInfo<'info>,
    pub token_program_main: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct ReallocMarketMetaAccounts<'info> {
    #[account(mut, signer)]
    pub payer: AccountInfo<'info>,
    #[account(mut)]
    pub market_meta: AccountInfo<'info>,
    pub system_program: AccountInfo<'info>,
}

// Account structures
/// Configuration for a group of markets
#[account]
pub struct MarketGroup {
    pub tenant: Pubkey,
    pub admin: Pubkey,
    pub proposed_admin: Option<Pubkey>,
    pub pda_meta: PdaMeta,
    pub fees: Fees,
}

#[account]
pub struct MarketLinear {
    /// The market_meta for the linear
    pub market_meta: Pubkey,
    /// The state for the market
    pub state: MarketState,
    /// The price curve for the market
    pub price_curve: LinearPriceCurveSerialized,
}

#[account]
pub struct MarketMultiCurve {
    /// The market_meta for the multi-curve
    pub market_meta: Pubkey,
    /// The state for the market
    pub state: MarketState,
    /// The price curve for the market
    pub price_curve: MultiPriceCurveSerialized,
}

/// Generic metadata for all kinds of markets
#[account]
pub struct MarketMeta {
    /// Mint for the main backing token (the cash token)
    pub mint_main: Pubkey,
    /// Mint for the token being traded
    pub mint_token: Pubkey,
    /// Mint for the options token
    pub mint_options: Pubkey,
    /// The market group for the market
    pub market_group: Pubkey,
    /// The market for the meta
    /// Must link here in order to ensure that there is a 1:1 relationship between market and market meta
    pub market: Pubkey,
    /// The program ID for the main token
    pub token_program_main: Pubkey,
    /// Vault that holds the liquidity for the main token
    pub liq_vault_main: Pubkey,
    /// The token account that holds the revenue for the market
    pub rev_escrow_group: Pubkey,
    /// The token account that holds the revenue for the tenant
    pub rev_escrow_tenant: Pubkey,
    /// The PDA meta for the market
    pub pda_meta: PdaMeta,
    /// Decimals for the main token
    pub decimals: u8,
    pub permissions: MarketPermissions,
    /// Timestamp of when the market starts
    pub start_time: u32,
    /// The Dutch auction config for the market
    pub dutch_config: DutchConfigSerialized,
}

#[account]
pub struct PersonalPosition {
    pub market_meta: Pubkey,
    pub owner: Pubkey,
    pub escrow: Pubkey,
    pub deposited_token_balance: u64,
    pub debt: u64,
    pub bump: [u8; 1],
}

#[account]
pub struct Tenant {
    pub seed: Pubkey,
    pub bump: [u8; 1],
    pub admin: Pubkey,
    pub proposed_admin: Option<Pubkey>,
    pub platform_fee_micro_bps: u32,
    /// Whether a group can be created without permission from the root admin
    pub permissionless_group_creation: bool,
}

// Type structures
#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct BuyWithExactCashInAndDepositEvent {
    pub trader: Pubkey,
    pub market: Pubkey,
    pub personal_position: Pubkey,
    pub token_out: u64,
    pub new_personal_position_balance: u64,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct MarketGroupInitArgs {
    pub fees: Fees,
    pub group_admin: Pubkey,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct MarketLinearArgs {
    pub x2: u64,
    pub m2: DecimalSerialized,
    pub m1: DecimalSerialized,
    pub f: DecimalSerialized,
    pub b2: DecimalSerialized,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct SellWithExactTokenInAfterWithdrawEvent {
    pub trader: Pubkey,
    pub market_meta: Pubkey,
    pub token_in: u64,
    pub actual_cash_out: u64,
    pub fee_platform: u64,
    pub fee_market_group: u64,
    /// The new balance of the personal position
    pub new_token_balance: u64,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct DecimalSerialized {
    pub val: [u8; 16],
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct PdaMeta {
    pub bump: [u8; 1],
    pub seed: Pubkey,
}

/// All fees denominated in micro-basis points (1/10000th of a percent)
#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct Fees {
    pub buy: u32,
    pub sell: u32,
    pub borrow: u32,
    pub exercise_option: u32,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct MultiPriceCurveSerialized {
    /// Height of the floor
    pub floor: [u8; 16],
    /// End of the floor (beginning of the shoulder)
    pub floor_end: u64,
    /// Slope of the shoulder as a multiple of the next neighbor's slope
    pub shoulder_slope_scalar: [u8; 16],
    /// Middle vertices could be empty
    pub middle_vertices: Vec<Vertex>,
    /// Final vertex, which extends into infinity
    pub final_vertex: Vertex,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct Vertex {
    pub x: u64,
    pub m: [u8; 16],
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct MarketState {
    /// The total supply of the token
    pub token_supply: u64,
    /// The total cash liquidity in the market
    pub total_cash_liquidity: u64,
    /// The total debt in the market
    pub total_debt: u64,
    /// The total collateral in the market
    pub total_collateral: u64,
    /// The cumulative revenue for the market
    pub cumulative_revenue_market: u128,
    /// The cumulative revenue for the tenant
    pub cumulative_revenue_tenant: u128,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct MarketPermissions {
    pub val: u16,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct DutchConfigSerialized {
    /// Initial boost for the dutch auction
    pub init_boost: f64,
    /// Duration of the dutch auction in seconds
    pub duration: u32,
    /// Curvature of the dutch auction
    pub curvature: f64,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub struct LinearPriceCurveSerialized {
    pub floor: [u8; 16],
    /// Shoulder slope
    pub m1: [u8; 16],
    /// main slope
    pub m2: [u8; 16],
    /// Shoulder end
    pub x2: u64,
    /// Main y-intercept
    pub b2: [u8; 16],
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub enum MarketPermission {
    CanBuy,
    CanSell,
    CanBorrow,
    CanRepay,
    CanMintOptions,
    CanExerciseOptions,
}

#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)]
pub enum FullOrPartialU64 {
    Full,
    Partial(u64),
}

pub type MicroBasisPoints = u32;

// Event structures
#[event]
pub struct BorrowEvent {
    pub market: Pubkey,
    pub personal_position: Pubkey,
    pub fee_market_group: u64,
    pub fee_tenant: u64,
    pub net_cash_out: u64,
}

#[event]
pub struct BuyEvent {
    pub trader: Pubkey,
    pub market: Pubkey,
    pub token_out: u64,
    pub net_cash_in: u64,
    pub fee_market: u64,
    pub fee_tenant: u64,
}

#[event]
pub struct DepositEvent {
    pub payer: Pubkey,
    pub market: Pubkey,
    pub personal_position: Pubkey,
    pub amount: u64,
    pub new_balance: u64,
}

#[event]
pub struct DonateLiquidityEvent {
    pub payer: Pubkey,
    pub market: Pubkey,
    pub new_cash_balance: u64,
    pub amount: u64,
}

#[event]
pub struct ExerciseOptionsEvent {
    pub owner: Pubkey,
    pub market_meta: Pubkey,
    pub amount: u64,
    pub fee_tenant: u64,
    pub fee_market: u64,
}

#[event]
pub struct MarketFlagsChangeEvent {
    pub market_meta: Pubkey,
    pub new_flags: MarketPermissions,
}

#[event]
pub struct MarketGroupAcceptNewAdminEvent {
    pub market_group: Pubkey,
    pub new_admin: Pubkey,
}

#[event]
pub struct MarketGroupChangeFeesEvent {
    pub market_group: Pubkey,
    pub new_fees: Fees,
}

#[event]
pub struct MarketGroupCollectRevEvent {
    pub market_group: Pubkey,
    pub market_meta: Pubkey,
    pub amount: u64,
    pub to: Pubkey,
}

#[event]
pub struct MarketGroupInitialized {
    pub tenant: Pubkey,
    pub market_group_seed: Pubkey,
    pub group_admin: Pubkey,
    pub fees: Fees,
}

#[event]
pub struct MarketGroupProposeAdminEvent {
    pub market_group: Pubkey,
    pub new_admin: Pubkey,
}

#[event]
pub struct MintOptionsEvent {
    pub market_meta: Pubkey,
    pub amount: u64,
}

#[event]
pub struct PersonalPositionInitEvent {
    pub market_meta: Pubkey,
    pub payer: Pubkey,
    pub owner: Pubkey,
    pub personal_position: Pubkey,
}

#[event]
pub struct RaiseFloorFromExcessLiquidityEvent {
    pub market: Pubkey,
    pub new_floor: DecimalSerialized,
}

#[event]
pub struct RaiseFloorFromTriggerEvent {
    pub market: Pubkey,
    pub new_floor: DecimalSerialized,
    pub new_shoulder_end: u64,
}

#[event]
pub struct RepayEvent {
    pub repayer: Pubkey,
    pub market: Pubkey,
    pub amount: u64,
    pub new_debt: u64,
}

#[event]
pub struct SellWithExactTokenInEvent {
    pub trader: Pubkey,
    pub market_meta: Pubkey,
    pub token_in: u64,
    pub actual_cash_out: u64,
    pub fee_platform: u64,
    pub fee_market_group: u64,
}

#[event]
pub struct TenantInitialized {
    pub tenant_seed: Pubkey,
    pub admin: Pubkey,
    pub fee_bps: u32,
    pub permissionless_group_creation: bool,
}

#[event]
pub struct WithdrawEvent {
    pub owner: Pubkey,
    pub market_meta: Pubkey,
    pub personal_position: Pubkey,
    pub amount: u64,
    pub new_balance: u64,
}