oil-api 0.7.8

API for interacting with the OIL protocol on Solana
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
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
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
use solana_program::pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address;
use steel::*;

use crate::{
    consts::{AUCTION, BOARD, MINT_ADDRESS, REFINERY, SOL_MINT, TREASURY_ADDRESS},
    instruction::{self, *},
    state::*,
};

pub fn log(signer: Pubkey, msg: &[u8]) -> Instruction {
    let mut data = Log {}.to_bytes();
    data.extend_from_slice(msg);
    Instruction {
        program_id: crate::ID,
        accounts: vec![AccountMeta::new(signer, true)],
        data: data,
    }
}

pub fn program_log(accounts: &[AccountInfo], msg: &[u8]) -> Result<(), ProgramError> {
    // Derive Board PDA to use as signer for log instruction
    let (board_address, _) = board_pda();
    invoke_signed(&log(board_address, msg), accounts, &crate::ID, &[BOARD])
}

/// Log event for auction-based instructions (uses Auction PDA instead of Board)
pub fn auction_program_log(accounts: &[AccountInfo], msg: &[u8]) -> Result<(), ProgramError> {
    // Derive Auction PDA to use as signer for log instruction
    let (auction_address, _) = auction_pda();
    invoke_signed(&log(auction_address, msg), accounts, &crate::ID, &[AUCTION])
}

/// Log event for refinery instructions (uses Refinery PDA as signer)
pub fn refinery_program_log(accounts: &[AccountInfo], msg: &[u8]) -> Result<(), ProgramError> {
    let (refinery_address, _) = refinery_pda();
    invoke_signed(&log(refinery_address, msg), accounts, &crate::ID, &[REFINERY])
}

// let [signer_info, board_info, config_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] = accounts else {
// pub fn initialize(
//     signer: Pubkey,
//     barrel_authority: Pubkey,
//     fee_collector: Pubkey,
//     swap_program: Pubkey,
//     var_address: Pubkey,
//     admin_fee: u64,
// ) -> Instruction {
//     let board_address = board_pda().0;
//     let config_address = config_pda().0;
//     let mint_address = MINT_ADDRESS;
//     let treasury_address = TREASURY_ADDRESS;
//     let treasury_tokens_address = treasury_tokens_address();
//     Instruction {
//         program_id: crate::ID,
//         accounts: vec![
//             AccountMeta::new(signer, true),
//             AccountMeta::new(board_address, false),
//             AccountMeta::new(config_address, false),
//             AccountMeta::new(mint_address, false),
//             AccountMeta::new(treasury_address, false),
//             AccountMeta::new(treasury_tokens_address, false),
//             AccountMeta::new_readonly(system_program::ID, false),
//             AccountMeta::new_readonly(spl_token::ID, false),
//             AccountMeta::new_readonly(spl_associated_token_account::ID, false),
//         ],
//         data: Initialize {
//             barrel_authority: barrel_authority.to_bytes(),
//             fee_collector: fee_collector.to_bytes(),
//             swap_program: swap_program.to_bytes(),
//             var_address: var_address.to_bytes(),
//             admin_fee: admin_fee.to_le_bytes(),
//         }
//         .to_bytes(),
//     }
// }

// let [signer_info, automation_info, executor_info, miner_info, system_program] = accounts else {

/// Set up automation for a miner. If the miner doesn't exist yet, pass a referrer to set it.
/// If a referrer is provided and the miner is new, the referral account must be included.
pub fn automate(
    signer: Pubkey,
    authority: Pubkey,
    amount: u64,
    deposit: u64,
    executor: Pubkey,
    fee: u64,
    mask: u64,
    strategy: u8,
    reload: bool,
    referrer: Option<Pubkey>,
    pooled: bool,
    is_new_miner: bool,
) -> Instruction {
    let automation_address = automation_pda(authority).0;
    let miner_address = miner_pda(authority).0;
    let config_address = config_pda().0;
    let referrer_pk = referrer.unwrap_or(Pubkey::default());
    
    let mut accounts = vec![
            AccountMeta::new(signer, true), // 0: signer (payer)
            AccountMeta::new(authority, false), // 1: authority (user's wallet)
            AccountMeta::new(automation_address, false), // 2: automation
            AccountMeta::new(executor, false), // 3: executor
            AccountMeta::new(miner_address, false), // 4: miner
            AccountMeta::new_readonly(system_program::ID, false), // 5: system_program
            AccountMeta::new_readonly(crate::ID, false), // 6: oil_program
            AccountMeta::new_readonly(config_address, false), // 7: config
    ];
    
    // Token accounts (user_wrapped_sol, automation_wrapped_sol, token_program, program_signer (optional), payer (optional), mint, ata_program)
    // These are added by the client, not here in the SDK
    
    // Add referral account if referrer is provided and miner is new (for incrementing total_referred)
    if is_new_miner && referrer.is_some() && referrer_pk != Pubkey::default() {
        let referral_address = referral_pda(referrer_pk).0;
        accounts.push(AccountMeta::new(referral_address, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: Automate {
            amount: amount.to_le_bytes(),
            deposit: deposit.to_le_bytes(),
            fee: fee.to_le_bytes(),
            mask: mask.to_le_bytes(),
            strategy: strategy as u8,
            reload: (reload as u64).to_le_bytes(),
            referrer: referrer_pk.to_bytes(),
            pooled: pooled as u8,
        }
        .to_bytes(),
    }
}

/// Claim SOL rewards with single-tier referral system.
/// 
/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
/// 
/// Account structure:
/// - Base: signer, miner, system_program
/// - If miner has referrer (required): [miner_referrer, referral_referrer]
pub fn claim_sol(
    signer: Pubkey,
    referrer_miner: Option<Pubkey>, // Referrer's miner PDA (if miner has referrer)
    referrer_referral: Option<Pubkey>, // Referrer's referral PDA (if miner has referrer)
) -> Instruction {
    let miner_address = miner_pda(signer).0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(miner_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
    ];
    
    // Add referrer accounts if provided (required if miner has referrer)
    if let (Some(miner_pubkey), Some(referral_pubkey)) = (referrer_miner, referrer_referral) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimSOL {}.to_bytes(),
    }
}

// let [signer_info, miner_info, mint_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =

/// Claim OIL rewards with single-tier referral system.
/// 
/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
/// 
/// Account structure:
/// - Base: signer, miner, mint, recipient, treasury, treasury_tokens, system_program, token_program, associated_token_program
/// - If miner has referrer (required): [miner_referrer, referral_referrer, referral_referrer_oil_ata]
pub fn claim_oil(
    signer: Pubkey,
    referrer_miner: Option<Pubkey>, // Referrer's miner PDA (if miner has referrer)
    referrer_referral: Option<Pubkey>, // Referrer's referral PDA (if miner has referrer)
    referrer_referral_oil_ata: Option<Pubkey>, // Referrer's referral OIL ATA (if miner has referrer)
) -> Instruction {
    let miner_address = miner_pda(signer).0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let recipient_address = get_associated_token_address(&signer, &MINT_ADDRESS);
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(miner_address, false),
        AccountMeta::new(MINT_ADDRESS, false),
        AccountMeta::new(recipient_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_tokens_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
    ];
    
    // Add referrer accounts if provided (required if miner has referrer)
    if let (Some(miner_pubkey), Some(referral_pubkey), Some(oil_ata_pubkey)) = 
        (referrer_miner, referrer_referral, referrer_referral_oil_ata) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
        accounts.push(AccountMeta::new(oil_ata_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimOIL {}.to_bytes(),
    }
}


pub fn close(signer: Pubkey, round_id: u64, rent_payer: Pubkey) -> Instruction {
    let board_address = board_pda().0;
    let round_address = round_pda(round_id).0;
    let treasury_address = TREASURY_ADDRESS;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(board_address, false),
            AccountMeta::new(rent_payer, false),
            AccountMeta::new(round_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: Close {}.to_bytes(),
    }
}

/// Deploy SOL to prospect on squares.
/// 
/// This function uses native SOL transfers and is used for:
/// - Regular wallets (signer == authority)
/// - Automations (bot-executed deploys, signer != authority, using native SOL from automation account balance)
/// 
/// Pass a referrer pubkey for new miners to set up referral.
/// Set `pooled` to true to join the mining pool (rewards shared proportionally).
pub fn deploy(
    signer: Pubkey,
    authority: Pubkey,
    amount: u64,
    round_id: u64,
    squares: [bool; 25],
    referrer: Option<Pubkey>,
    pooled: bool,
) -> Instruction {
    let automation_address = automation_pda(authority).0;
    let board_address = board_pda().0;
    let miner_address = miner_pda(authority).0;
    let round_address = round_pda(round_id).0;
    let entropy_var_address = entropy_rng_api::state::var_pda(board_address, 0).0;

    let mut mask: u32 = 0;
    for (i, &square) in squares.iter().enumerate() {
        if square {
            mask |= 1 << i;
        }
    }
    
    let referrer_pubkey = referrer.unwrap_or(Pubkey::default());
    let referrer_bytes = referrer_pubkey.to_bytes();
    // Match program logic: has_referrer = referrer != Pubkey::default() && referrer != authority
    let has_referrer = referrer_pubkey != Pubkey::default() && referrer_pubkey != authority;

    // Build accounts list - must match program structure:
    // Oil accounts: base (8) + optional referral (1) = 8-9
    // Entropy accounts: var + program = 2 (always exactly 2)
    let mut accounts = vec![
        AccountMeta::new(signer, true), // 0: signer
        AccountMeta::new(authority, false), // 1: authority
        AccountMeta::new(automation_address, false), // 2: automation
        AccountMeta::new(board_address, false), // 3: board
        AccountMeta::new(miner_address, false), // 4: miner
        AccountMeta::new(round_address, false), // 5: round
        AccountMeta::new_readonly(system_program::ID, false), // 6: system_program
        AccountMeta::new_readonly(crate::ID, false), // 7: oil_program
    ];
    
    // Add referral account if referrer is provided and not equal to authority (matches program logic)
    if has_referrer {
        let referral_address = referral_pda(referrer_pubkey).0;
        accounts.push(AccountMeta::new(referral_address, false)); // referral (optional, in oil_accounts)
    }
    
    // Entropy accounts (always exactly 2, come after all oil_accounts)
    accounts.push(AccountMeta::new(entropy_var_address, false)); // entropy_var
    accounts.push(AccountMeta::new_readonly(entropy_rng_api::ID, false)); // entropy_program

    Instruction {
        program_id: crate::ID,
        accounts,
        data: Deploy {
            amount: amount.to_le_bytes(),
            squares: mask.to_le_bytes(),
            referrer: referrer_bytes,
            pooled: if pooled { 1 } else { 0 },
        }
        .to_bytes(),
    }
}


// let [pool, user_source_token, user_destination_token, a_vault, b_vault, a_token_vault, b_token_vault, a_vault_lp_mint, b_vault_lp_mint, a_vault_lp, b_vault_lp, protocol_token_fee, user_key, vault_program, token_program] =

pub fn wrap(signer: Pubkey, use_liquidity: bool, amount: u64) -> Instruction {
    let config_address = config_pda().0;
    let treasury_address = TREASURY_ADDRESS;
    let treasury_sol_address = get_associated_token_address(&treasury_address, &SOL_MINT);
    let data = Wrap {
        use_liquidity: if use_liquidity { 1 } else { 0 },
        amount: amount.to_le_bytes(),
    }
    .to_bytes();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new_readonly(config_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(treasury_sol_address, false),
            AccountMeta::new_readonly(solana_program::system_program::ID, false),
        ],
        data,
    }
}

pub fn buyback(signer: Pubkey, swap_accounts: &[AccountMeta], swap_data: &[u8]) -> Instruction {
    let board_address = board_pda().0;
    let mint_address = MINT_ADDRESS;
    let treasury_address = TREASURY_ADDRESS;
    let treasury_oil_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let treasury_sol_address = get_associated_token_address(&treasury_address, &SOL_MINT);
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(board_address, false),
        AccountMeta::new(mint_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_oil_address, false),
        AccountMeta::new(treasury_sol_address, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
    ];
    for account in swap_accounts.iter() {
        let mut acc_clone = account.clone();
        acc_clone.is_signer = false;
        accounts.push(acc_clone);
    }
    let mut data = Buyback {}.to_bytes();
    data.extend_from_slice(swap_data);
    Instruction {
        program_id: crate::ID,
        accounts,
        data,
    }
}

pub fn barrel(signer: Pubkey, amount: u64) -> Instruction {
    let board_address = board_pda().0;
    let mint_address = MINT_ADDRESS;
    let treasury_address = TREASURY_ADDRESS;
    let sender_oil_address = get_associated_token_address(&signer, &MINT_ADDRESS);
    let treasury_oil_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let data = Barrel {
        amount: amount.to_le_bytes(),
    }
    .to_bytes();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(sender_oil_address, false),
            AccountMeta::new(board_address, false),
            AccountMeta::new(mint_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(treasury_oil_address, false),
            AccountMeta::new_readonly(spl_token::ID, false),
            AccountMeta::new_readonly(crate::ID, false),
        ],
        data,
    }
}


// let [signer_info, board_info, config_info, fee_collector_info, mint_info, round_info, round_next_info, top_miner_info, treasury_info, treasury_tokens_info, system_program, token_program, oil_program, slot_hashes_sysvar] =

pub fn reset(
    signer: Pubkey,
    fee_collector: Pubkey,
    round_id: u64,
    top_miner: Pubkey,
    var_address: Pubkey,
) -> Instruction {
    reset_with_miners(signer, fee_collector, round_id, top_miner, var_address, &[])
}

pub fn reset_with_miners(
    signer: Pubkey,
    fee_collector: Pubkey,
    round_id: u64,
    top_miner: Pubkey,
    var_address: Pubkey,
    miner_accounts: &[Pubkey],
) -> Instruction {
    let board_address = board_pda().0;
    let config_address = config_pda().0;
    let mint_address = MINT_ADDRESS;
    let round_address = round_pda(round_id).0;
    let round_next_address = round_pda(round_id + 1).0;
    let top_miner_address = miner_pda(top_miner).0;
    let treasury_address = TREASURY_ADDRESS;
    let treasury_tokens_address = treasury_tokens_address();
    let pool_address = pool_pda().0;
    let mint_authority_address = oil_mint_api::state::authority_pda().0;
    let mut reset_instruction = Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(board_address, false),
            AccountMeta::new(config_address, false),
            AccountMeta::new(fee_collector, false),
            AccountMeta::new(mint_address, false),
            AccountMeta::new(round_address, false),
            AccountMeta::new(round_next_address, false),
            AccountMeta::new(top_miner_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(pool_address, false),
            AccountMeta::new(treasury_tokens_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(spl_token::ID, false),
            AccountMeta::new_readonly(crate::ID, false),
            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
            AccountMeta::new_readonly(SOL_MINT, false),
            // Entropy accounts (these are in "other_accounts" after the split)
            AccountMeta::new(var_address, false),
            AccountMeta::new_readonly(entropy_rng_api::ID, false),
            // Mint accounts.
            AccountMeta::new(mint_authority_address, false),
            AccountMeta::new_readonly(oil_mint_api::ID, false),
        ],
        data: Reset {}.to_bytes(),
    };
    
    // Add miner accounts for seeker rewards (optional)
    for miner_pubkey in miner_accounts {
        reset_instruction.accounts.push(AccountMeta::new(
            miner_pda(*miner_pubkey).0,
            false,
        ));
    }
    
    reset_instruction
}
    
// let [signer_info, automation_info, board_info, miner_info, round_info, treasury_info, system_program] =

pub fn checkpoint(signer: Pubkey, authority: Pubkey, round_id: u64) -> Instruction {
    let miner_address = miner_pda(authority).0;
    let board_address = board_pda().0;
    let config_address = config_pda().0;
    let round_address = round_pda(round_id).0;
    let treasury_address = TREASURY_ADDRESS;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // signer (used as authority by program)
            AccountMeta::new(board_address, false),
            AccountMeta::new(config_address, false), // config (needed for premine check)
            AccountMeta::new(miner_address, false),
            AccountMeta::new(round_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: Checkpoint {}.to_bytes(),
    }
}

pub fn set_admin(signer: Pubkey, admin: Pubkey) -> Instruction {
    let config_address = config_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(config_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: SetAdmin {
            admin: admin.to_bytes(),
        }
        .to_bytes(),
    }
}

pub fn set_admin_fee(signer: Pubkey, admin_fee: u64) -> Instruction {
    let config_address = config_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(config_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: SetAdminFee {
            admin_fee: admin_fee.to_le_bytes(),
        }
        .to_bytes(),
    }
}

pub fn set_fee_collector(signer: Pubkey, fee_collector: Pubkey) -> Instruction {
    let config_address = config_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(config_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: SetFeeCollector {
            fee_collector: fee_collector.to_bytes(),
        }
        .to_bytes(),
    }
}

/// Sets the TGE (Token Generation Event) timestamp.
/// If current time < tge_timestamp, pre-mine is active.
/// Set to 0 to disable pre-mine.
/// Admin-only instruction.
pub fn set_tge_timestamp(signer: Pubkey, tge_timestamp: i64) -> Instruction {
    let config_address = config_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(config_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: SetTgeTimestamp {
            tge_timestamp: tge_timestamp.to_le_bytes(),
        }
        .to_bytes(),
    }
}

pub fn set_auction(
    signer: Pubkey,
    halving_period_seconds: u64,
    last_halving_time: u64,
    base_mining_rates: [u64; 4],
    auction_duration_seconds: u64,
    starting_prices: [u64; 4],
    _well_id: u64, // Kept for backwards compatibility, but not used (always updates auction only)
) -> Instruction {
    let config_address = config_pda().0;
    let auction_address = auction_pda().0;
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new_readonly(config_address, false),
            AccountMeta::new(auction_address, false),
        ],
        data: SetAuction {
            halving_period_seconds: halving_period_seconds.to_le_bytes(),
            last_halving_time: last_halving_time.to_le_bytes(),
            base_mining_rates: [
                base_mining_rates[0].to_le_bytes(),
                base_mining_rates[1].to_le_bytes(),
                base_mining_rates[2].to_le_bytes(),
                base_mining_rates[3].to_le_bytes(),
            ],
            auction_duration_seconds: auction_duration_seconds.to_le_bytes(),
            starting_prices: [
                starting_prices[0].to_le_bytes(),
                starting_prices[1].to_le_bytes(),
                starting_prices[2].to_le_bytes(),
                starting_prices[3].to_le_bytes(),
            ],
            well_id: 4u64.to_le_bytes(), // Always use 4 to indicate auction-only update
        }
        .to_bytes(),
    }
}

// let [signer_info, mint_info, sender_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program] =

pub fn deposit(signer: Pubkey, authority: Pubkey, amount: u64, lock_duration_days: u64, stake_id: u64) -> Instruction {
    let mint_address = MINT_ADDRESS;
    let stake_address = stake_pda_with_id(authority, stake_id).0; // Derive from authority, not signer
    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
    let sender_address = get_associated_token_address(&authority, &MINT_ADDRESS); // Authority's ATA
    let pool_address = pool_pda().0;
    let pool_tokens_address = pool_tokens_address();
    let miner_address = miner_pda(authority).0; // Derive from authority
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // payer (session payer or regular wallet, pays fees)
            AccountMeta::new(authority, true), // authority (user's wallet, signs token transfer and used for PDA derivation)
            AccountMeta::new(mint_address, false),
            AccountMeta::new(sender_address, false),
            AccountMeta::new(stake_address, false),
            AccountMeta::new(stake_tokens_address, false),
            AccountMeta::new(pool_address, false),
            AccountMeta::new(pool_tokens_address, false),
            AccountMeta::new(miner_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(spl_token::ID, false),
            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
        ],
        data: Deposit {
            amount: amount.to_le_bytes(),
            lock_duration_days: lock_duration_days.to_le_bytes(),
            stake_id: stake_id.to_le_bytes(),
        }
        .to_bytes(),
    }
}

// let [signer_info, mint_info, recipient_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program] =

pub fn withdraw(signer: Pubkey, authority: Pubkey, amount: u64, stake_id: u64) -> Instruction {
    let stake_address = stake_pda_with_id(authority, stake_id).0; // Derive from authority, not signer
    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
    let mint_address = MINT_ADDRESS;
    let recipient_address = get_associated_token_address(&authority, &MINT_ADDRESS); // Authority's ATA
    let pool_address = pool_pda().0;
    let pool_tokens_address = pool_tokens_address();
    let miner_address = miner_pda(authority).0; // Derive from authority
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = treasury_tokens_address();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // payer (session payer or regular wallet)
            AccountMeta::new(authority, false), // authority (user's wallet, for PDA derivation)
            AccountMeta::new(mint_address, false),
            AccountMeta::new(recipient_address, false),
            AccountMeta::new(stake_address, false),
            AccountMeta::new(stake_tokens_address, false),
            AccountMeta::new(pool_address, false),
            AccountMeta::new(pool_tokens_address, false),
            AccountMeta::new(miner_address, false),
            AccountMeta::new(treasury_address, false), // Treasury account (writable, signed by PDA)
            AccountMeta::new(treasury_tokens_address, false), // Treasury OIL token account (writable, signed by PDA)
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(spl_token::ID, false),
            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
        ],
        data: Withdraw {
            amount: amount.to_le_bytes(),
            stake_id: stake_id.to_le_bytes(),
        }
        .to_bytes(),
    }
}

// let [signer_info, automation_info, miner_info, system_program] = accounts else {

/// Reload SOL from miner account to automation balance with single-tier referral system.
/// 
/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
/// 
/// Account structure:
/// - Base: signer, automation, miner, system_program
/// - If miner has referrer (required): [miner_referrer, referral_referrer]
pub fn reload_sol(
    signer: Pubkey,
    authority: Pubkey,
    referrer_miner: Option<Pubkey>,
    referrer_referral: Option<Pubkey>,
) -> Instruction {
    let automation_address = automation_pda(authority).0;
    let miner_address = miner_pda(authority).0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(automation_address, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
    ];
    
    // Add referral accounts if provided (required when miner has referrer)
    if let (Some(miner_ref), Some(referral_ref)) = (referrer_miner, referrer_referral) {
        accounts.push(AccountMeta::new(miner_ref, false));
        accounts.push(AccountMeta::new(referral_ref, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ReloadSOL {}.to_bytes(),
    }
}

// let [signer_info, mint_info, recipient_info, stake_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =

/// Claim SOL yield from staking. Stakers earn SOL rewards (2% of round winnings), not OIL.
pub fn claim_yield(signer: Pubkey, amount: u64, stake_id: u64) -> Instruction {
    let stake_address = stake_pda_with_id(signer, stake_id).0;
    let pool_address = pool_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // signer and writable for receiving SOL
            AccountMeta::new(stake_address, false),
            AccountMeta::new(pool_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: ClaimYield {
            amount: amount.to_le_bytes(),
        }
        .to_bytes(),
    }
}

pub fn new_var(
    signer: Pubkey,
    provider: Pubkey,
    id: u64,
    commit: [u8; 32],
    samples: u64,
) -> Instruction {
    let board_address = board_pda().0;
    let config_address = config_pda().0;
    let var_address = entropy_rng_api::state::var_pda(board_address, id).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(board_address, false),
            AccountMeta::new(config_address, false),
            AccountMeta::new(provider, false),
            AccountMeta::new(var_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(entropy_rng_api::ID, false),
        ],
        data: NewVar {
            id: id.to_le_bytes(),
            commit: commit,
            samples: samples.to_le_bytes(),
        }
        .to_bytes(),
    }
}

pub fn set_swap_program(signer: Pubkey, new_program: Pubkey) -> Instruction {
    let config_address = config_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(config_address, false),
            AccountMeta::new_readonly(new_program, false),
        ],
        data: SetSwapProgram {}.to_bytes(),
    }
}

pub fn set_var_address(signer: Pubkey, new_var_address: Pubkey) -> Instruction {
    let board_address = board_pda().0;
    let config_address = config_pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(board_address, false),
            AccountMeta::new(config_address, false),
            AccountMeta::new(new_var_address, false),
        ],
        data: SetVarAddress {}.to_bytes(),
    }
}

/// Migrate Miner: Extend Miner struct with 2 [u64; 4] arrays (current_epoch_id, checkpointed_epoch_id).
/// This migration moves current_epoch_id and checkpointed_epoch_id from Rig to Miner.
/// Must be called by the admin.
/// Accounts: signer, config, miner, system_program
pub fn migrate(signer: Pubkey, miner_authority: Pubkey) -> Instruction {
    let config_address = config_pda().0;
    let miner_address = miner_pda(miner_authority).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(config_address, false),
            AccountMeta::new(miner_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: Migrate {}.to_bytes(),
    }
}

/// Create a referral account to become a referrer.
pub fn create_referral(signer: Pubkey) -> Instruction {
    let referral_address = referral_pda(signer).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(referral_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: CreateReferral {}.to_bytes(),
    }
}

/// Creates a Whitelist account for a shared access code.
/// Admin-only instruction.
/// Accounts: signer (admin), config, whitelist, system_program
pub fn create_whitelist(
    signer: Pubkey,
    code_hash: [u8; 32],
) -> Instruction {
    let config_address = config_pda().0;
    let (whitelist_address, _) = Whitelist::pda(code_hash);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // signer (admin)
            AccountMeta::new_readonly(config_address, false), // config
            AccountMeta::new(whitelist_address, false), // whitelist
            AccountMeta::new_readonly(system_program::ID, false), // system_program
        ],
        data: CreateWhitelist {
            code_hash,
        }
        .to_bytes(),
    }
}

/// Claim pending referral rewards (both SOL and OIL).
/// 
/// Account structure (for Fogo sessions):
/// - Base: signer (payer), authority (user's wallet), referral, referral_tokens, mint, recipient, system_program, token_program, associated_token_program
pub fn claim_referral(signer: Pubkey, authority: Pubkey) -> Instruction {
    let referral_address = referral_pda(authority).0;
    let referral_oil_address = get_associated_token_address(&referral_address, &MINT_ADDRESS);
    let recipient_oil_address = get_associated_token_address(&authority, &MINT_ADDRESS);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // 0: signer (payer)
            AccountMeta::new(authority, false), // 1: authority (user's wallet, receives SOL)
            AccountMeta::new(referral_address, false), // 2: referral
            AccountMeta::new(referral_oil_address, false), // 3: referral_tokens (Referral account's OIL ATA)
            AccountMeta::new(MINT_ADDRESS, false), // 4: mint
            AccountMeta::new(recipient_oil_address, false), // 5: recipient (Recipient's OIL ATA - authority's wallet)
            AccountMeta::new_readonly(system_program::ID, false), // 6: system_program
            AccountMeta::new_readonly(spl_token::ID, false), // 7: token_program
            AccountMeta::new_readonly(spl_associated_token_account::ID, false), // 8: associated_token_program
        ],
        data: ClaimReferral {}.to_bytes(),
    }
}

/// Direct solo bid on an auction well (seize ownership).
/// The bid amount is calculated on-chain as current_price + 1 lamport.
/// User must have enough SOL in their wallet to cover the bid.
/// 
/// Account structure:
/// - Base: signer, authority, program_signer (optional), payer (optional), well, auction, treasury, treasury_tokens, mint, mint_authority, mint_program, staking_pool, fee_collector, config, token_program, system_program, oil_program
/// - If previous owner exists (optional): [previous_owner_miner, previous_owner]
/// - If referrer is provided (optional): [referral]
pub fn place_bid(
    signer: Pubkey,
    authority: Pubkey,
    square_id: u64,
    fee_collector: Pubkey,
    previous_owner_miner: Option<Pubkey>, // Previous owner's miner PDA (if previous owner exists)
    previous_owner: Option<Pubkey>, // Previous owner pubkey (if previous owner exists)
    referrer: Option<Pubkey>, // Optional referrer pubkey for new miners
) -> Instruction {
    let well_address = well_pda(square_id).0;
    let auction_address = auction_pda().0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let staking_pool_address = pool_pda().0;
    let config_address = config_pda().0;
    let mint_authority_address = oil_mint_api::state::authority_pda().0;
    let bidder_miner_address = miner_pda(authority).0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true), // 0: signer
        AccountMeta::new(authority, false), // 1: authority
    ];
    
    // Add program_signer and payer for Fogo sessions (these are added by the client, not here)
    // For now, we'll add placeholders or the client will add them
    
    accounts.extend_from_slice(&[
        AccountMeta::new(well_address, false), // well
        AccountMeta::new(auction_address, false), // Must be writable for auction_program_log CPI
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_tokens_address, false),
        AccountMeta::new(MINT_ADDRESS, false),
        AccountMeta::new(mint_authority_address, false),
        AccountMeta::new_readonly(oil_mint_api::ID, false),
        AccountMeta::new(staking_pool_address, false),
        AccountMeta::new(fee_collector, false),
        AccountMeta::new_readonly(config_address, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false), // oil_program
        AccountMeta::new(bidder_miner_address, false), // bidder_miner
    ]);
    
    // Add previous owner accounts if provided
    if let (Some(miner_pubkey), Some(owner_pubkey)) = (previous_owner_miner, previous_owner) {
        accounts.push(AccountMeta::new(miner_pubkey, false)); // previous_owner_miner
        accounts.push(AccountMeta::new(owner_pubkey, false)); // previous_owner
    }
    
    // Add referral account if referrer is provided
    if let Some(referrer_pubkey) = referrer {
        let referral_address = referral_pda(referrer_pubkey).0;
        accounts.push(AccountMeta::new(referral_address, false)); // referral
    }
    
    // Note: Wrapped token accounts are added by the client:
    // - user_wrapped_sol (source)
    // - treasury_wrapped_sol (temp ATA for all wrapped SOL - will be closed to get native SOL)
    // - token_program, native_mint, ata_program
    // The program distributes native SOL from treasury to pool and fee_collector after closing the temp ATA.
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: instruction::PlaceBid {
            square_id: square_id.to_le_bytes(),
            referrer: referrer.unwrap_or(Pubkey::default()).to_bytes(),
        }
        .to_bytes(),
    }
}

/// Claim auction-based OIL rewards
/// - OIL rewards: from current ownership and previous ownership (pre-minted)
/// 
/// Account structure:
/// - Base: signer, miner, well accounts (one per well in mask), auction pool accounts (optional, one per well), auction, treasury, treasury_tokens, mint, mint_authority, mint_program, recipient, token_program, associated_token_program, system_program, oil_program
/// - Bid accounts (one per well in mask, required for pool contributors): [bid_0, bid_1, bid_2, bid_3] (must include epoch_id in PDA)
/// Claim auction-based OIL rewards
/// 
/// Account structure:
/// - Base: signer, miner, well_0, well_1, well_2, well_3, auction, treasury, treasury_tokens, mint, mint_authority, mint_program, recipient, token_program, associated_token_program, system_program, oil_program
/// - If miner has referrer (required): [miner_referrer, referral_referrer, referral_referrer_oil_ata]
pub fn claim_auction_oil(
    signer: Pubkey,
    well_mask: u8, // Bitmask: bit 0 = well 0, bit 1 = well 1, etc.
    referrer_miner: Option<Pubkey>, // Referrer's miner PDA (if miner has referrer)
    referrer_referral: Option<Pubkey>, // Referrer's referral PDA (if miner has referrer)
    referrer_referral_oil_ata: Option<Pubkey>, // Referrer's referral OIL ATA (if miner has referrer)
) -> Instruction {
    let miner_address = miner_pda(signer).0;
    let well_0_address = well_pda(0).0;
    let well_1_address = well_pda(1).0;
    let well_2_address = well_pda(2).0;
    let well_3_address = well_pda(3).0;
    let auction_address = auction_pda().0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let recipient_address = get_associated_token_address(&signer, &MINT_ADDRESS);
    let mint_authority_address = oil_mint_api::state::authority_pda().0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(miner_address, false),
        AccountMeta::new(well_0_address, false),
        AccountMeta::new(well_1_address, false),
        AccountMeta::new(well_2_address, false),
        AccountMeta::new(well_3_address, false),
        AccountMeta::new(auction_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_tokens_address, false),
        AccountMeta::new(MINT_ADDRESS, false),
        AccountMeta::new(mint_authority_address, false),
        AccountMeta::new_readonly(oil_mint_api::ID, false),
        AccountMeta::new(recipient_address, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
    ];
    
    // Add referrer accounts if provided (required if miner has referrer)
    if let (Some(miner_pubkey), Some(referral_pubkey), Some(oil_ata_pubkey)) = 
        (referrer_miner, referrer_referral, referrer_referral_oil_ata) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
        accounts.push(AccountMeta::new(oil_ata_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimAuctionOIL {
            well_mask,
        }
        .to_bytes(),
    }
}

/// Claim auction-based SOL rewards
/// 
/// Account structure:
/// - Base: signer (writable), miner, treasury, auction, system_program, oil_program
/// - If miner has referrer (required): [miner_referrer, referral_referrer]
pub fn claim_auction_sol(
    signer: Pubkey,
    referrer_miner: Option<Pubkey>, // Referrer's miner PDA (if miner has referrer)
    referrer_referral: Option<Pubkey>, // Referrer's referral PDA (if miner has referrer)
) -> Instruction {
    let miner_address = miner_pda(signer).0;
    let (auction_address, _) = auction_pda();
    let treasury_address = treasury_pda().0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true), // signer and writable for receiving SOL
        AccountMeta::new(miner_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(auction_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
    ];
    
    // Add referrer accounts if provided (required if miner has referrer)
    if let (Some(miner_pubkey), Some(referral_pubkey)) = (referrer_miner, referrer_referral) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimAuctionSOL {
            _reserved: 0,
        }
        .to_bytes(),
    }
}

// ============================================================================
// FOGO Session SDK Functions
// ============================================================================

pub fn checkpoint_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    round_id: u64,
) -> Instruction {
    let miner_address = miner_pda(authority).0;
    let board_address = board_pda().0;
    let config_address = config_pda().0;
    let round_address = round_pda(round_id).0;
    let treasury_address = TREASURY_ADDRESS;
    
    // Manually construct instruction data with CheckpointWithSession discriminator (52)
    // Checkpoint is an empty struct, so we just need the discriminator byte
    let data = vec![52u8]; // CheckpointWithSession = 52
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // signer (session account)
            AccountMeta::new(authority, false), // authority (user's wallet)
            AccountMeta::new_readonly(program_signer, false), // program_signer
            AccountMeta::new(board_address, false),
            AccountMeta::new(config_address, false), // config (needed for premine check)
            AccountMeta::new(miner_address, false),
            AccountMeta::new(round_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data,
    }
}

pub fn deploy_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    amount: u64,
    round_id: u64,
    squares: [bool; 25],
    referrer: Option<Pubkey>,
    pooled: bool,
) -> Instruction {
    let automation_address = automation_pda(authority).0;
    let board_address = board_pda().0;
    let miner_address = miner_pda(authority).0;
    let round_address = round_pda(round_id).0;
    let entropy_var_address = entropy_rng_api::state::var_pda(board_address, 0).0;

    let mut mask: u32 = 0;
    for (i, &square) in squares.iter().enumerate() {
        if square {
            mask |= 1 << i;
        }
    }
    
    let referrer_pubkey = referrer.unwrap_or(Pubkey::default());
    // Match program logic: has_referrer = referrer != Pubkey::default() && referrer != authority
    let has_referrer = referrer_pubkey != Pubkey::default() && referrer_pubkey != authority;
    let user_wrapped_sol_ata = get_associated_token_address(&authority, &SOL_MINT);
    let round_wrapped_sol_ata = get_associated_token_address(&round_address, &SOL_MINT);

    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(automation_address, false),
        AccountMeta::new(board_address, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new(round_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
        AccountMeta::new(user_wrapped_sol_ata, false),
        AccountMeta::new(round_wrapped_sol_ata, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(SOL_MINT, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
    ];
    
    if has_referrer {
        let referral_address = referral_pda(referrer_pubkey).0;
        accounts.push(AccountMeta::new(referral_address, false));
    }
    
    accounts.push(AccountMeta::new(entropy_var_address, false));
    accounts.push(AccountMeta::new_readonly(entropy_rng_api::ID, false));

    Instruction {
        program_id: crate::ID,
        accounts,
        data: Deploy {
            amount: amount.to_le_bytes(),
            squares: mask.to_le_bytes(),
            referrer: referrer_pubkey.to_bytes(),
            pooled: if pooled { 1 } else { 0 },
        }
        .to_bytes(),
    }
}

pub fn automate_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    amount: u64,
    deposit: u64,
    executor: Pubkey,
    fee: u64,
    mask: u64,
    strategy: u8,
    reload: bool,
    referrer: Option<Pubkey>,
    pooled: bool,
    is_new_miner: bool,
) -> Instruction {
    let automation_address = automation_pda(authority).0;
    let miner_address = miner_pda(authority).0;
    let referrer_pk = referrer.unwrap_or(Pubkey::default());
    let user_wrapped_sol_ata = get_associated_token_address(&authority, &SOL_MINT);
    let automation_wrapped_sol_ata = get_associated_token_address(&automation_address, &SOL_MINT);
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(automation_address, false),
        AccountMeta::new(executor, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
        AccountMeta::new(user_wrapped_sol_ata, false),
        AccountMeta::new(automation_wrapped_sol_ata, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(SOL_MINT, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
    ];
    
    if is_new_miner && referrer.is_some() && referrer_pk != Pubkey::default() {
        let referral_address = referral_pda(referrer_pk).0;
        accounts.push(AccountMeta::new(referral_address, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: Automate {
            amount: amount.to_le_bytes(),
            deposit: deposit.to_le_bytes(),
            fee: fee.to_le_bytes(),
            mask: mask.to_le_bytes(),
            strategy: strategy as u8,
            reload: (reload as u64).to_le_bytes(),
            referrer: referrer_pk.to_bytes(),
            pooled: pooled as u8,
        }
        .to_bytes(),
    }
}

pub fn place_bid_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    square_id: u64,
    fee_collector: Pubkey,
    previous_owner_miner: Option<Pubkey>,
    previous_owner: Option<Pubkey>,
    referrer: Option<Pubkey>,
) -> Instruction {
    let well_address = well_pda(square_id).0;
    let auction_address = auction_pda().0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let staking_pool_address = pool_pda().0;
    let config_address = config_pda().0;
    let mint_authority_address = oil_mint_api::state::authority_pda().0;
    let bidder_miner_address = miner_pda(authority).0;
    let user_wrapped_sol_ata = get_associated_token_address(&authority, &SOL_MINT);
    let treasury_wrapped_sol_ata = get_associated_token_address(&treasury_address, &SOL_MINT);
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(well_address, false),
        AccountMeta::new(auction_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_tokens_address, false),
        AccountMeta::new(MINT_ADDRESS, false),
        AccountMeta::new(mint_authority_address, false),
        AccountMeta::new_readonly(oil_mint_api::ID, false),
        AccountMeta::new(staking_pool_address, false),
        AccountMeta::new(fee_collector, false),
        AccountMeta::new_readonly(config_address, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
        AccountMeta::new(bidder_miner_address, false),
    ];
    
    if let (Some(miner_pubkey), Some(owner_pubkey)) = (previous_owner_miner, previous_owner) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(owner_pubkey, false));
    }
    
    if let Some(referrer_pubkey) = referrer {
        let referral_address = referral_pda(referrer_pubkey).0;
        accounts.push(AccountMeta::new(referral_address, false));
    }
    
    accounts.extend_from_slice(&[
        AccountMeta::new(user_wrapped_sol_ata, false),
        AccountMeta::new(treasury_wrapped_sol_ata, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(SOL_MINT, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
    ]);
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: instruction::PlaceBid {
            square_id: square_id.to_le_bytes(),
            referrer: referrer.unwrap_or(Pubkey::default()).to_bytes(),
        }
        .to_bytes(),
    }
}

pub fn claim_auction_oil_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    well_mask: u8,
    referrer_miner: Option<Pubkey>,
    referrer_referral: Option<Pubkey>,
    referrer_referral_oil_ata: Option<Pubkey>,
) -> Instruction {
    let miner_address = miner_pda(authority).0;
    let well_0_address = well_pda(0).0;
    let well_1_address = well_pda(1).0;
    let well_2_address = well_pda(2).0;
    let well_3_address = well_pda(3).0;
    let auction_address = auction_pda().0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let recipient_address = get_associated_token_address(&authority, &MINT_ADDRESS);
    let mint_authority_address = oil_mint_api::state::authority_pda().0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new(well_0_address, false),
        AccountMeta::new(well_1_address, false),
        AccountMeta::new(well_2_address, false),
        AccountMeta::new(well_3_address, false),
        AccountMeta::new(auction_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_tokens_address, false),
        AccountMeta::new(MINT_ADDRESS, false),
        AccountMeta::new(mint_authority_address, false),
        AccountMeta::new_readonly(oil_mint_api::ID, false),
        AccountMeta::new(recipient_address, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
    ];
    
    if let (Some(miner_pubkey), Some(referral_pubkey), Some(oil_ata_pubkey)) = 
        (referrer_miner, referrer_referral, referrer_referral_oil_ata) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
        accounts.push(AccountMeta::new(oil_ata_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimAuctionOIL {
            well_mask,
        }
        .to_bytes(),
    }
}

pub fn claim_auction_sol_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    referrer_miner: Option<Pubkey>,
    referrer_referral: Option<Pubkey>,
) -> Instruction {
    let miner_address = miner_pda(authority).0;
    let (auction_address, _) = auction_pda();
    let treasury_address = treasury_pda().0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(auction_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(crate::ID, false),
    ];
    
    if let (Some(miner_pubkey), Some(referral_pubkey)) = (referrer_miner, referrer_referral) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimAuctionSOL {
            _reserved: 0,
        }
        .to_bytes(),
    }
}

pub fn claim_sol_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    referrer_miner: Option<Pubkey>,
    referrer_referral: Option<Pubkey>,
) -> Instruction {
    let miner_address = miner_pda(authority).0;
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
    ];
    
    if let (Some(miner_pubkey), Some(referral_pubkey)) = (referrer_miner, referrer_referral) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimSOL {}.to_bytes(),
    }
}

pub fn claim_oil_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    referrer_miner: Option<Pubkey>,
    referrer_referral: Option<Pubkey>,
    referrer_referral_oil_ata: Option<Pubkey>,
) -> Instruction {
    let miner_address = miner_pda(authority).0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
    let recipient_address = get_associated_token_address(&authority, &MINT_ADDRESS);
    
    let mut accounts = vec![
        AccountMeta::new(signer, true),
        AccountMeta::new(authority, false),
        AccountMeta::new_readonly(program_signer, false),
        AccountMeta::new(payer, false),
        AccountMeta::new(miner_address, false),
        AccountMeta::new(MINT_ADDRESS, false),
        AccountMeta::new(recipient_address, false),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_tokens_address, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(spl_token::ID, false),
        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
    ];
    
    if let (Some(miner_pubkey), Some(referral_pubkey), Some(oil_ata_pubkey)) = 
        (referrer_miner, referrer_referral, referrer_referral_oil_ata) {
        accounts.push(AccountMeta::new(miner_pubkey, false));
        accounts.push(AccountMeta::new(referral_pubkey, false));
        accounts.push(AccountMeta::new(oil_ata_pubkey, false));
    }
    
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimOIL {}.to_bytes(),
    }
}

pub fn withdraw_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    amount: u64,
    stake_id: u64,
) -> Instruction {
    let stake_address = stake_pda_with_id(authority, stake_id).0;
    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
    let mint_address = MINT_ADDRESS;
    let recipient_address = get_associated_token_address(&authority, &MINT_ADDRESS);
    let pool_address = pool_pda().0;
    let pool_tokens_address = pool_tokens_address();
    let miner_address = miner_pda(authority).0;
    let treasury_address = treasury_pda().0;
    let treasury_tokens_address = treasury_tokens_address();
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true),
            AccountMeta::new(authority, false),
            AccountMeta::new_readonly(program_signer, false),
            AccountMeta::new(payer, false),
            AccountMeta::new(mint_address, false),
            AccountMeta::new(recipient_address, false),
            AccountMeta::new(stake_address, false),
            AccountMeta::new(stake_tokens_address, false),
            AccountMeta::new(pool_address, false),
            AccountMeta::new(pool_tokens_address, false),
            AccountMeta::new(miner_address, false),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(treasury_tokens_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(spl_token::ID, false),
            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
        ],
        data: Withdraw {
            amount: amount.to_le_bytes(),
            stake_id: stake_id.to_le_bytes(),
        }
        .to_bytes(),
    }
}

pub fn deposit_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
    amount: u64,
    lock_duration_days: u64,
    stake_id: u64,
) -> Instruction {
    let mint_address = MINT_ADDRESS;
    let stake_address = stake_pda_with_id(authority, stake_id).0;
    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
    let sender_address = get_associated_token_address(&authority, &MINT_ADDRESS);
    let pool_address = pool_pda().0;
    let pool_tokens_address = pool_tokens_address();
    let miner_address = miner_pda(authority).0;
    
    let mut data = Deposit {
        amount: amount.to_le_bytes(),
        lock_duration_days: lock_duration_days.to_le_bytes(),
        stake_id: stake_id.to_le_bytes(),
    }
    .to_bytes();
    data[0] = 48u8; // DepositWithSession = 48
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // 0: signer (session account)
            AccountMeta::new(authority, false), // 1: authority (user's wallet)
            AccountMeta::new_readonly(program_signer, false), // 2: program_signer
            AccountMeta::new(payer, false), // 3: payer (paymaster sponsor)
            AccountMeta::new(mint_address, false), // 4: mint
            AccountMeta::new(sender_address, false), // 5: sender (authority's OIL ATA)
            AccountMeta::new(stake_address, false), // 6: stake
            AccountMeta::new(stake_tokens_address, false), // 7: stake_tokens
            AccountMeta::new(pool_address, false), // 8: pool
            AccountMeta::new(pool_tokens_address, false), // 9: pool_tokens
            AccountMeta::new(miner_address, false), // 10: miner
            AccountMeta::new_readonly(system_program::ID, false), // 11: system_program
            AccountMeta::new_readonly(spl_token::ID, false), // 12: token_program
            AccountMeta::new_readonly(spl_associated_token_account::ID, false), // 13: associated_token_program
        ],
        data,
    }
}

pub fn claim_yield_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    amount: u64,
    stake_id: u64,
) -> Instruction {
    let stake_address = stake_pda_with_id(authority, stake_id).0;
    let pool_address = pool_pda().0;
    
    let mut data = ClaimYield {
        amount: amount.to_le_bytes(),
    }
    .to_bytes();
    data[0] = 51u8; // ClaimYieldWithSession = 51
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // 0: signer (session account)
            AccountMeta::new(authority, true), // 1: authority (user's wallet, writable for receiving SOL)
            AccountMeta::new_readonly(program_signer, false), // 2: program_signer
            AccountMeta::new(stake_address, false), // 3: stake
            AccountMeta::new(pool_address, false), // 4: pool
            AccountMeta::new_readonly(system_program::ID, false), // 5: system_program
        ],
        data,
    }
}

pub fn create_referral_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
) -> Instruction {
    let referral_address = referral_pda(authority).0;
    
    // CreateReferralWithSession = 49
    let data = vec![49u8];
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // 0: signer (session account)
            AccountMeta::new(authority, false), // 1: authority (user's wallet)
            AccountMeta::new_readonly(program_signer, false), // 2: program_signer
            AccountMeta::new(payer, false), // 3: payer (paymaster sponsor)
            AccountMeta::new(referral_address, false), // 4: referral
            AccountMeta::new_readonly(system_program::ID, false), // 5: system_program
        ],
        data,
    }
}

pub fn claim_referral_with_session(
    signer: Pubkey,
    authority: Pubkey,
    program_signer: Pubkey,
    payer: Pubkey,
) -> Instruction {
    let referral_address = referral_pda(authority).0;
    let referral_oil_address = get_associated_token_address(&referral_address, &MINT_ADDRESS);
    let recipient_oil_address = get_associated_token_address(&authority, &MINT_ADDRESS);
    
    // ClaimReferralWithSession = 50
    let data = vec![50u8];
    
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(signer, true), // 0: signer (session account)
            AccountMeta::new(authority, false), // 1: authority (user's wallet, receives SOL)
            AccountMeta::new_readonly(program_signer, false), // 2: program_signer
            AccountMeta::new(payer, false), // 3: payer (paymaster sponsor)
            AccountMeta::new(referral_address, false), // 4: referral
            AccountMeta::new(referral_oil_address, false), // 5: referral_tokens (Referral account's OIL ATA)
            AccountMeta::new(MINT_ADDRESS, false), // 6: mint
            AccountMeta::new(recipient_oil_address, false), // 7: recipient (Recipient's OIL ATA - authority's wallet)
            AccountMeta::new_readonly(system_program::ID, false), // 8: system_program
            AccountMeta::new_readonly(spl_token::ID, false), // 9: token_program
            AccountMeta::new_readonly(spl_associated_token_account::ID, false), // 10: associated_token_program
        ],
        data,
    }
}