raydium-launchlab-sdk 0.1.1

Rust SDK for Raydium LaunchLab 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
//! Instruction builders for Raydium LaunchLab

use borsh::BorshSerialize;
use solana_program::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
};


use crate::constants::*;
use crate::pda;
use super::types::*;

// =============================================================================
// Buy Instructions
// =============================================================================

/// Arguments for buy_exact_in instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct BuyExactInArgs {
    pub amount_in: u64,
    pub minimum_amount_out: u64,
    pub share_fee_rate: u64,
}

/// Build a buy_exact_in instruction
/// 
/// Use the given amount of quote tokens to purchase base tokens.
pub fn buy_exact_in(
    payer: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    pool_state: &Pubkey,
    user_base_token: &Pubkey,
    user_quote_token: &Pubkey,
    base_vault: &Pubkey,
    quote_vault: &Pubkey,
    base_token_mint: &Pubkey,
    quote_token_mint: &Pubkey,
    base_token_program: &Pubkey,
    amount_in: u64,
    minimum_amount_out: u64,
    share_fee_rate: u64,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (event_authority, _) = pda::get_event_authority_pda();
    
    let accounts = vec![
        AccountMeta::new(*payer, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new(*user_base_token, false),
        AccountMeta::new(*user_quote_token, false),
        AccountMeta::new(*base_vault, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new_readonly(*base_token_mint, false),
        AccountMeta::new_readonly(*quote_token_mint, false),
        AccountMeta::new_readonly(*base_token_program, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(event_authority, false),
        AccountMeta::new_readonly(PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::BUY_EXACT_IN.to_vec();
    let args = BuyExactInArgs {
        amount_in,
        minimum_amount_out,
        share_fee_rate,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for buy_exact_out instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct BuyExactOutArgs {
    pub amount_out: u64,
    pub maximum_amount_in: u64,
    pub share_fee_rate: u64,
}

/// Build a buy_exact_out instruction
/// 
/// Use quote tokens to purchase the given amount of base tokens.
pub fn buy_exact_out(
    payer: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    pool_state: &Pubkey,
    user_base_token: &Pubkey,
    user_quote_token: &Pubkey,
    base_vault: &Pubkey,
    quote_vault: &Pubkey,
    base_token_mint: &Pubkey,
    quote_token_mint: &Pubkey,
    base_token_program: &Pubkey,
    amount_out: u64,
    maximum_amount_in: u64,
    share_fee_rate: u64,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (event_authority, _) = pda::get_event_authority_pda();
    
    let accounts = vec![
        AccountMeta::new(*payer, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new(*user_base_token, false),
        AccountMeta::new(*user_quote_token, false),
        AccountMeta::new(*base_vault, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new_readonly(*base_token_mint, false),
        AccountMeta::new_readonly(*quote_token_mint, false),
        AccountMeta::new_readonly(*base_token_program, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(event_authority, false),
        AccountMeta::new_readonly(PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::BUY_EXACT_OUT.to_vec();
    let args = BuyExactOutArgs {
        amount_out,
        maximum_amount_in,
        share_fee_rate,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

// =============================================================================
// Sell Instructions
// =============================================================================

/// Arguments for sell_exact_in instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct SellExactInArgs {
    pub amount_in: u64,
    pub minimum_amount_out: u64,
    pub share_fee_rate: u64,
}

/// Build a sell_exact_in instruction
/// 
/// Use the given amount of base tokens to sell for quote tokens.
pub fn sell_exact_in(
    payer: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    pool_state: &Pubkey,
    user_base_token: &Pubkey,
    user_quote_token: &Pubkey,
    base_vault: &Pubkey,
    quote_vault: &Pubkey,
    base_token_mint: &Pubkey,
    quote_token_mint: &Pubkey,
    base_token_program: &Pubkey,
    amount_in: u64,
    minimum_amount_out: u64,
    share_fee_rate: u64,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (event_authority, _) = pda::get_event_authority_pda();
    
    let accounts = vec![
        AccountMeta::new(*payer, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new(*user_base_token, false),
        AccountMeta::new(*user_quote_token, false),
        AccountMeta::new(*base_vault, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new_readonly(*base_token_mint, false),
        AccountMeta::new_readonly(*quote_token_mint, false),
        AccountMeta::new_readonly(*base_token_program, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(event_authority, false),
        AccountMeta::new_readonly(PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::SELL_EXACT_IN.to_vec();
    let args = SellExactInArgs {
        amount_in,
        minimum_amount_out,
        share_fee_rate,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for sell_exact_out instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct SellExactOutArgs {
    pub amount_out: u64,
    pub maximum_amount_in: u64,
    pub share_fee_rate: u64,
}

/// Build a sell_exact_out instruction
/// 
/// Sell base tokens for the given amount of quote tokens.
pub fn sell_exact_out(
    payer: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    pool_state: &Pubkey,
    user_base_token: &Pubkey,
    user_quote_token: &Pubkey,
    base_vault: &Pubkey,
    quote_vault: &Pubkey,
    base_token_mint: &Pubkey,
    quote_token_mint: &Pubkey,
    base_token_program: &Pubkey,
    amount_out: u64,
    maximum_amount_in: u64,
    share_fee_rate: u64,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (event_authority, _) = pda::get_event_authority_pda();
    
    let accounts = vec![
        AccountMeta::new(*payer, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new(*user_base_token, false),
        AccountMeta::new(*user_quote_token, false),
        AccountMeta::new(*base_vault, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new_readonly(*base_token_mint, false),
        AccountMeta::new_readonly(*quote_token_mint, false),
        AccountMeta::new_readonly(*base_token_program, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(event_authority, false),
        AccountMeta::new_readonly(PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::SELL_EXACT_OUT.to_vec();
    let args = SellExactOutArgs {
        amount_out,
        maximum_amount_in,
        share_fee_rate,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

// =============================================================================
// Initialize Instructions
// =============================================================================

/// Arguments for initialize instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct InitializeArgs {
    pub base_mint_param: MintParams,
    pub curve_param: CurveParams,
    pub vesting_param: VestingParams,
}

/// Build an initialize instruction
/// 
/// Initializes a new trading pool.
pub fn initialize(
    payer: &Pubkey,
    creator: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    base_mint: &Pubkey,
    quote_mint: &Pubkey,
    base_mint_param: MintParams,
    curve_param: CurveParams,
    vesting_param: VestingParams,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (pool_state, _) = pda::get_pool_state_pda(base_mint, quote_mint);
    let (base_vault, _) = pda::get_pool_vault_pda(&pool_state, base_mint);
    let (quote_vault, _) = pda::get_pool_vault_pda(&pool_state, quote_mint);
    let (metadata_account, _) = pda::get_metadata_address(base_mint);
    let (event_authority, _) = pda::get_event_authority_pda();
    
    let accounts = vec![
        AccountMeta::new(*payer, true),
        AccountMeta::new_readonly(*creator, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new(pool_state, false),
        AccountMeta::new(*base_mint, true),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new(base_vault, false),
        AccountMeta::new(quote_vault, false),
        AccountMeta::new(metadata_account, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(METADATA_PROGRAM_ID, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(RENT_PROGRAM_ID, false),
        AccountMeta::new_readonly(event_authority, false),
        AccountMeta::new_readonly(PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::INITIALIZE.to_vec();
    let args = InitializeArgs {
        base_mint_param,
        curve_param,
        vesting_param,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for initialize_v2 instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct InitializeV2Args {
    pub base_mint_param: MintParams,
    pub curve_param: CurveParams,
    pub vesting_param: VestingParams,
    pub amm_fee_on: AmmCreatorFeeOn,
}

/// Build an initialize_v2 instruction (with AMM creator fee option)
pub fn initialize_v2(
    payer: &Pubkey,
    creator: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    base_mint: &Pubkey,
    quote_mint: &Pubkey,
    base_mint_param: MintParams,
    curve_param: CurveParams,
    vesting_param: VestingParams,
    amm_fee_on: AmmCreatorFeeOn,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (pool_state, _) = pda::get_pool_state_pda(base_mint, quote_mint);
    let (base_vault, _) = pda::get_pool_vault_pda(&pool_state, base_mint);
    let (quote_vault, _) = pda::get_pool_vault_pda(&pool_state, quote_mint);
    let (metadata_account, _) = pda::get_metadata_address(base_mint);
    let (event_authority, _) = pda::get_event_authority_pda();
    
    let accounts = vec![
        AccountMeta::new(*payer, true),
        AccountMeta::new_readonly(*creator, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new(pool_state, false),
        AccountMeta::new(*base_mint, true),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new(base_vault, false),
        AccountMeta::new(quote_vault, false),
        AccountMeta::new(metadata_account, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(METADATA_PROGRAM_ID, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(RENT_PROGRAM_ID, false),
        AccountMeta::new_readonly(event_authority, false),
        AccountMeta::new_readonly(PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::INITIALIZE_V2.to_vec();
    let args = InitializeV2Args {
        base_mint_param,
        curve_param,
        vesting_param,
        amm_fee_on,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

// =============================================================================
// Fee Claiming Instructions
// =============================================================================

/// Build a claim_creator_fee instruction
pub fn claim_creator_fee(
    creator: &Pubkey,
    quote_mint: &Pubkey,
) -> Instruction {
    let (fee_vault_authority, _) = pda::get_creator_fee_vault_authority_pda();
    let (creator_fee_vault, _) = pda::get_creator_fee_vault_pda(creator, quote_mint);
    let recipient_token_account = pda::get_associated_token_address(creator, quote_mint);
    
    let accounts = vec![
        AccountMeta::new(*creator, true),
        AccountMeta::new_readonly(fee_vault_authority, false),
        AccountMeta::new(creator_fee_vault, false),
        AccountMeta::new(recipient_token_account, false),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
    ];
    
    let data = discriminators::CLAIM_CREATOR_FEE.to_vec();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Build a claim_platform_fee instruction
pub fn claim_platform_fee(
    platform_fee_wallet: &Pubkey,
    pool_state: &Pubkey,
    platform_config: &Pubkey,
    quote_vault: &Pubkey,
    quote_mint: &Pubkey,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let recipient_token_account = pda::get_associated_token_address(platform_fee_wallet, quote_mint);
    
    let accounts = vec![
        AccountMeta::new(*platform_fee_wallet, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new(recipient_token_account, false),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
    ];
    
    let data = discriminators::CLAIM_PLATFORM_FEE.to_vec();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Build a claim_platform_fee_from_vault instruction
pub fn claim_platform_fee_from_vault(
    platform_fee_wallet: &Pubkey,
    platform_config: &Pubkey,
    quote_mint: &Pubkey,
) -> Instruction {
    let (fee_vault_authority, _) = pda::get_platform_fee_vault_authority_pda();
    let (platform_fee_vault, _) = pda::get_platform_fee_vault_pda(platform_config, quote_mint);
    let recipient_token_account = pda::get_associated_token_address(platform_fee_wallet, quote_mint);
    
    let accounts = vec![
        AccountMeta::new(*platform_fee_wallet, true),
        AccountMeta::new_readonly(fee_vault_authority, false),
        AccountMeta::new_readonly(*platform_config, false),
        AccountMeta::new(platform_fee_vault, false),
        AccountMeta::new(recipient_token_account, false),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
    ];
    
    let data = discriminators::CLAIM_PLATFORM_FEE_FROM_VAULT.to_vec();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Build a collect_fee instruction (protocol fee)
pub fn collect_fee(
    owner: &Pubkey,
    pool_state: &Pubkey,
    global_config: &Pubkey,
    quote_vault: &Pubkey,
    quote_mint: &Pubkey,
    recipient_token_account: &Pubkey,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    
    let accounts = vec![
        AccountMeta::new_readonly(*owner, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new(*recipient_token_account, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
    ];
    
    let data = discriminators::COLLECT_FEE.to_vec();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Build a collect_migrate_fee instruction
pub fn collect_migrate_fee(
    owner: &Pubkey,
    pool_state: &Pubkey,
    global_config: &Pubkey,
    quote_vault: &Pubkey,
    quote_mint: &Pubkey,
    recipient_token_account: &Pubkey,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    
    let accounts = vec![
        AccountMeta::new_readonly(*owner, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new(*quote_vault, false),
        AccountMeta::new_readonly(*quote_mint, false),
        AccountMeta::new(*recipient_token_account, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
    ];
    
    let data = discriminators::COLLECT_MIGRATE_FEE.to_vec();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

// =============================================================================
// Vesting Instructions
// =============================================================================

/// Arguments for create_vesting_account instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct CreateVestingAccountArgs {
    pub share_amount: u64,
}

/// Build a create_vesting_account instruction
pub fn create_vesting_account(
    creator: &Pubkey,
    beneficiary: &Pubkey,
    pool_state: &Pubkey,
    share_amount: u64,
) -> Instruction {
    let (vesting_record, _) = pda::get_vesting_record_pda(pool_state, beneficiary);
    
    let accounts = vec![
        AccountMeta::new(*creator, true),
        AccountMeta::new(*beneficiary, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new(vesting_record, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::CREATE_VESTING_ACCOUNT.to_vec();
    let args = CreateVestingAccountArgs { share_amount };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Build a claim_vested_token instruction
pub fn claim_vested_token(
    beneficiary: &Pubkey,
    pool_state: &Pubkey,
    base_vault: &Pubkey,
    base_token_mint: &Pubkey,
) -> Instruction {
    let (authority, _) = pda::get_authority_pda();
    let (vesting_record, _) = pda::get_vesting_record_pda(pool_state, beneficiary);
    let user_base_token = pda::get_associated_token_address(beneficiary, base_token_mint);
    
    let accounts = vec![
        AccountMeta::new(*beneficiary, true),
        AccountMeta::new_readonly(authority, false),
        AccountMeta::new(*pool_state, false),
        AccountMeta::new(vesting_record, false),
        AccountMeta::new(*base_vault, false),
        AccountMeta::new(user_base_token, false),
        AccountMeta::new_readonly(*base_token_mint, false),
        AccountMeta::new_readonly(TOKEN_PROGRAM_ID, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
    ];
    
    let data = discriminators::CLAIM_VESTED_TOKEN.to_vec();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

// =============================================================================
// Config Instructions
// =============================================================================

/// Arguments for create_config instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct CreateConfigArgs {
    pub curve_type: u8,
    pub index: u16,
    pub migrate_fee: u64,
    pub trade_fee_rate: u64,
}

/// Build a create_config instruction (admin only)
pub fn create_config(
    owner: &Pubkey,
    quote_token_mint: &Pubkey,
    protocol_fee_owner: &Pubkey,
    migrate_fee_owner: &Pubkey,
    migrate_to_amm_wallet: &Pubkey,
    migrate_to_cpswap_wallet: &Pubkey,
    curve_type: u8,
    index: u16,
    migrate_fee: u64,
    trade_fee_rate: u64,
) -> Instruction {
    let (global_config, _) = pda::get_global_config_pda(quote_token_mint, curve_type, index);
    
    let accounts = vec![
        AccountMeta::new(*owner, true),
        AccountMeta::new(global_config, false),
        AccountMeta::new_readonly(*quote_token_mint, false),
        AccountMeta::new_readonly(*protocol_fee_owner, false),
        AccountMeta::new_readonly(*migrate_fee_owner, false),
        AccountMeta::new_readonly(*migrate_to_amm_wallet, false),
        AccountMeta::new_readonly(*migrate_to_cpswap_wallet, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::CREATE_CONFIG.to_vec();
    let args = CreateConfigArgs {
        curve_type,
        index,
        migrate_fee,
        trade_fee_rate,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for update_config instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct UpdateConfigArgs {
    pub param: u8,
    pub value: u64,
}

/// Build an update_config instruction (admin only)
pub fn update_config(
    owner: &Pubkey,
    global_config: &Pubkey,
    param: u8,
    value: u64,
) -> Instruction {
    let accounts = vec![
        AccountMeta::new_readonly(*owner, true),
        AccountMeta::new(*global_config, false),
    ];
    
    let mut data = discriminators::UPDATE_CONFIG.to_vec();
    let args = UpdateConfigArgs { param, value };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for create_platform_config instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct CreatePlatformConfigArgs {
    pub platform_params: PlatformParams,
}

/// Build a create_platform_config instruction
pub fn create_platform_config(
    platform_admin: &Pubkey,
    platform_fee_wallet: &Pubkey,
    platform_nft_wallet: &Pubkey,
    cpswap_config: &Pubkey,
    transfer_fee_extension_authority: &Pubkey,
    platform_params: PlatformParams,
) -> Instruction {
    let (platform_config, _) = pda::get_platform_config_pda(platform_admin);
    
    let accounts = vec![
        AccountMeta::new(*platform_admin, true),
        AccountMeta::new_readonly(*platform_fee_wallet, false),
        AccountMeta::new_readonly(*platform_nft_wallet, false),
        AccountMeta::new(platform_config, false),
        AccountMeta::new_readonly(*cpswap_config, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
        AccountMeta::new_readonly(*transfer_fee_extension_authority, false),
    ];
    
    let mut data = discriminators::CREATE_PLATFORM_CONFIG.to_vec();
    let args = CreatePlatformConfigArgs { platform_params };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for update_platform_config instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct UpdatePlatformConfigArgs {
    pub param: PlatformConfigParam,
}

/// Build an update_platform_config instruction
pub fn update_platform_config(
    platform_admin: &Pubkey,
    param: PlatformConfigParam,
) -> Instruction {
    let (platform_config, _) = pda::get_platform_config_pda(platform_admin);
    
    let accounts = vec![
        AccountMeta::new_readonly(*platform_admin, true),
        AccountMeta::new(platform_config, false),
    ];
    
    let mut data = discriminators::UPDATE_PLATFORM_CONFIG.to_vec();
    let args = UpdatePlatformConfigArgs { param };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for update_platform_curve_param instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct UpdatePlatformCurveParamArgs {
    pub index: u8,
    pub bonding_curve_param: BondingCurveParam,
}

/// Build an update_platform_curve_param instruction
pub fn update_platform_curve_param(
    platform_admin: &Pubkey,
    global_config: &Pubkey,
    index: u8,
    bonding_curve_param: BondingCurveParam,
) -> Instruction {
    let (platform_config, _) = pda::get_platform_config_pda(platform_admin);
    
    let accounts = vec![
        AccountMeta::new(*platform_admin, true),
        AccountMeta::new(platform_config, false),
        AccountMeta::new_readonly(*global_config, false),
        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
    ];
    
    let mut data = discriminators::UPDATE_PLATFORM_CURVE_PARAM.to_vec();
    let args = UpdatePlatformCurveParamArgs {
        index,
        bonding_curve_param,
    };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

/// Arguments for remove_platform_curve_param instruction
#[derive(BorshSerialize, Clone, Debug)]
pub struct RemovePlatformCurveParamArgs {
    pub index: u8,
}

/// Build a remove_platform_curve_param instruction
pub fn remove_platform_curve_param(
    platform_admin: &Pubkey,
    index: u8,
) -> Instruction {
    let (platform_config, _) = pda::get_platform_config_pda(platform_admin);
    
    let accounts = vec![
        AccountMeta::new_readonly(*platform_admin, true),
        AccountMeta::new(platform_config, false),
    ];
    
    let mut data = discriminators::REMOVE_PLATFORM_CURVE_PARAM.to_vec();
    let args = RemovePlatformCurveParamArgs { index };
    args.serialize(&mut data).unwrap();
    
    Instruction {
        program_id: PROGRAM_ID,
        accounts,
        data,
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Create a simple buy instruction with auto-derived accounts
/// 
/// This is a convenience function that derives vault and token accounts.
pub fn create_buy_instruction(
    payer: &Pubkey,
    base_mint: &Pubkey,
    quote_mint: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    amount_in: u64,
    minimum_amount_out: u64,
    share_fee_rate: u64,
    is_token_2022: bool,
) -> Instruction {
    let (pool_state, _) = pda::get_pool_state_pda(base_mint, quote_mint);
    let (base_vault, _) = pda::get_pool_vault_pda(&pool_state, base_mint);
    let (quote_vault, _) = pda::get_pool_vault_pda(&pool_state, quote_mint);
    
    let base_token_program = if is_token_2022 {
        TOKEN_2022_PROGRAM_ID
    } else {
        TOKEN_PROGRAM_ID
    };
    
    let user_base_token = if is_token_2022 {
        pda::get_associated_token_address_2022(payer, base_mint)
    } else {
        pda::get_associated_token_address(payer, base_mint)
    };
    let user_quote_token = pda::get_associated_token_address(payer, quote_mint);
    
    buy_exact_in(
        payer,
        global_config,
        platform_config,
        &pool_state,
        &user_base_token,
        &user_quote_token,
        &base_vault,
        &quote_vault,
        base_mint,
        quote_mint,
        &base_token_program,
        amount_in,
        minimum_amount_out,
        share_fee_rate,
    )
}

/// Create a simple sell instruction with auto-derived accounts
pub fn create_sell_instruction(
    payer: &Pubkey,
    base_mint: &Pubkey,
    quote_mint: &Pubkey,
    global_config: &Pubkey,
    platform_config: &Pubkey,
    amount_in: u64,
    minimum_amount_out: u64,
    share_fee_rate: u64,
    is_token_2022: bool,
) -> Instruction {
    let (pool_state, _) = pda::get_pool_state_pda(base_mint, quote_mint);
    let (base_vault, _) = pda::get_pool_vault_pda(&pool_state, base_mint);
    let (quote_vault, _) = pda::get_pool_vault_pda(&pool_state, quote_mint);
    
    let base_token_program = if is_token_2022 {
        TOKEN_2022_PROGRAM_ID
    } else {
        TOKEN_PROGRAM_ID
    };
    
    let user_base_token = if is_token_2022 {
        pda::get_associated_token_address_2022(payer, base_mint)
    } else {
        pda::get_associated_token_address(payer, base_mint)
    };
    let user_quote_token = pda::get_associated_token_address(payer, quote_mint);
    
    sell_exact_in(
        payer,
        global_config,
        platform_config,
        &pool_state,
        &user_base_token,
        &user_quote_token,
        &base_vault,
        &quote_vault,
        base_mint,
        quote_mint,
        &base_token_program,
        amount_in,
        minimum_amount_out,
        share_fee_rate,
    )
}