oil_api/
sdk.rs

1use solana_program::pubkey::Pubkey;
2use spl_associated_token_account::get_associated_token_address;
3use steel::*;
4
5use crate::{
6    consts::{AUCTION, BOARD, MINT_ADDRESS, SOL_MINT, TREASURY_ADDRESS},
7    instruction::{self, *},
8    state::*,
9};
10
11pub fn log(signer: Pubkey, msg: &[u8]) -> Instruction {
12    let mut data = Log {}.to_bytes();
13    data.extend_from_slice(msg);
14    Instruction {
15        program_id: crate::ID,
16        accounts: vec![AccountMeta::new(signer, true)],
17        data: data,
18    }
19}
20
21pub fn program_log(accounts: &[AccountInfo], msg: &[u8]) -> Result<(), ProgramError> {
22    // Derive Board PDA to use as signer for log instruction
23    let (board_address, _) = board_pda();
24    invoke_signed(&log(board_address, msg), accounts, &crate::ID, &[BOARD])
25}
26
27/// Log event for auction-based instructions (uses Auction PDA instead of Board)
28pub fn auction_program_log(accounts: &[AccountInfo], msg: &[u8]) -> Result<(), ProgramError> {
29    // Derive Auction PDA to use as signer for log instruction
30    let (auction_address, _) = auction_pda();
31    invoke_signed(&log(auction_address, msg), accounts, &crate::ID, &[AUCTION])
32}
33
34// let [signer_info, board_info, config_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] = accounts else {
35
36// pub fn initialize(
37//     signer: Pubkey,
38//     barrel_authority: Pubkey,
39//     fee_collector: Pubkey,
40//     swap_program: Pubkey,
41//     var_address: Pubkey,
42//     admin_fee: u64,
43// ) -> Instruction {
44//     let board_address = board_pda().0;
45//     let config_address = config_pda().0;
46//     let mint_address = MINT_ADDRESS;
47//     let treasury_address = TREASURY_ADDRESS;
48//     let treasury_tokens_address = treasury_tokens_address();
49//     Instruction {
50//         program_id: crate::ID,
51//         accounts: vec![
52//             AccountMeta::new(signer, true),
53//             AccountMeta::new(board_address, false),
54//             AccountMeta::new(config_address, false),
55//             AccountMeta::new(mint_address, false),
56//             AccountMeta::new(treasury_address, false),
57//             AccountMeta::new(treasury_tokens_address, false),
58//             AccountMeta::new_readonly(system_program::ID, false),
59//             AccountMeta::new_readonly(spl_token::ID, false),
60//             AccountMeta::new_readonly(spl_associated_token_account::ID, false),
61//         ],
62//         data: Initialize {
63//             barrel_authority: barrel_authority.to_bytes(),
64//             fee_collector: fee_collector.to_bytes(),
65//             swap_program: swap_program.to_bytes(),
66//             var_address: var_address.to_bytes(),
67//             admin_fee: admin_fee.to_le_bytes(),
68//         }
69//         .to_bytes(),
70//     }
71// }
72
73// let [signer_info, automation_info, executor_info, miner_info, system_program] = accounts else {
74
75/// Set up automation for a miner. If the miner doesn't exist yet, pass a referrer to set it.
76/// If a referrer is provided and the miner is new, the referral account must be included.
77pub fn automate(
78    signer: Pubkey,
79    amount: u64,
80    deposit: u64,
81    executor: Pubkey,
82    fee: u64,
83    mask: u64,
84    strategy: u8,
85    reload: bool,
86    referrer: Option<Pubkey>,
87    pooled: bool,
88    is_new_miner: bool,
89) -> Instruction {
90    let automation_address = automation_pda(signer).0;
91    let miner_address = miner_pda(signer).0;
92    let referrer_pk = referrer.unwrap_or(Pubkey::default());
93    
94    let mut accounts = vec![
95            AccountMeta::new(signer, true),
96            AccountMeta::new(automation_address, false),
97            AccountMeta::new(executor, false),
98            AccountMeta::new(miner_address, false),
99            AccountMeta::new_readonly(system_program::ID, false),
100    ];
101    
102    // Add referral account if referrer is provided and miner is new (for incrementing total_referred)
103    if is_new_miner && referrer.is_some() && referrer_pk != Pubkey::default() {
104        let referral_address = referral_pda(referrer_pk).0;
105        accounts.push(AccountMeta::new(referral_address, false));
106    }
107    
108    Instruction {
109        program_id: crate::ID,
110        accounts,
111        data: Automate {
112            amount: amount.to_le_bytes(),
113            deposit: deposit.to_le_bytes(),
114            fee: fee.to_le_bytes(),
115            mask: mask.to_le_bytes(),
116            strategy: strategy as u8,
117            reload: (reload as u64).to_le_bytes(),
118            referrer: referrer_pk.to_bytes(),
119            pooled: pooled as u8,
120        }
121        .to_bytes(),
122    }
123}
124
125/// Claim SOL rewards with single-tier referral system.
126/// 
127/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
128/// 
129/// Account structure:
130/// - Base: signer, miner, system_program
131/// - If miner has referrer (required): [miner_referrer, referral_referrer]
132pub fn claim_sol(
133    signer: Pubkey,
134    referrer_miner: Option<Pubkey>, // Referrer's miner PDA (if miner has referrer)
135    referrer_referral: Option<Pubkey>, // Referrer's referral PDA (if miner has referrer)
136) -> Instruction {
137    let miner_address = miner_pda(signer).0;
138    
139    let mut accounts = vec![
140        AccountMeta::new(signer, true),
141        AccountMeta::new(miner_address, false),
142        AccountMeta::new_readonly(system_program::ID, false),
143    ];
144    
145    // Add referrer accounts if provided (required if miner has referrer)
146    if let (Some(miner_pubkey), Some(referral_pubkey)) = (referrer_miner, referrer_referral) {
147        accounts.push(AccountMeta::new(miner_pubkey, false));
148        accounts.push(AccountMeta::new(referral_pubkey, false));
149    }
150    
151    Instruction {
152        program_id: crate::ID,
153        accounts,
154        data: ClaimSOL {}.to_bytes(),
155    }
156}
157
158// let [signer_info, miner_info, mint_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
159
160/// Claim OIL rewards with single-tier referral system.
161/// 
162/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
163/// 
164/// Account structure:
165/// - Base: signer, miner, mint, recipient, treasury, treasury_tokens, system_program, token_program, associated_token_program
166/// - If miner has referrer (required): [miner_referrer, referral_referrer, referral_referrer_oil_ata]
167pub fn claim_oil(
168    signer: Pubkey,
169    referrer_miner: Option<Pubkey>, // Referrer's miner PDA (if miner has referrer)
170    referrer_referral: Option<Pubkey>, // Referrer's referral PDA (if miner has referrer)
171    referrer_referral_oil_ata: Option<Pubkey>, // Referrer's referral OIL ATA (if miner has referrer)
172) -> Instruction {
173    let miner_address = miner_pda(signer).0;
174    let treasury_address = treasury_pda().0;
175    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
176    let recipient_address = get_associated_token_address(&signer, &MINT_ADDRESS);
177    
178    let mut accounts = vec![
179        AccountMeta::new(signer, true),
180        AccountMeta::new(miner_address, false),
181        AccountMeta::new(MINT_ADDRESS, false),
182        AccountMeta::new(recipient_address, false),
183        AccountMeta::new(treasury_address, false),
184        AccountMeta::new(treasury_tokens_address, false),
185        AccountMeta::new_readonly(system_program::ID, false),
186        AccountMeta::new_readonly(spl_token::ID, false),
187        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
188    ];
189    
190    // Add referrer accounts if provided (required if miner has referrer)
191    if let (Some(miner_pubkey), Some(referral_pubkey), Some(oil_ata_pubkey)) = 
192        (referrer_miner, referrer_referral, referrer_referral_oil_ata) {
193        accounts.push(AccountMeta::new(miner_pubkey, false));
194        accounts.push(AccountMeta::new(referral_pubkey, false));
195        accounts.push(AccountMeta::new(oil_ata_pubkey, false));
196    }
197    
198    Instruction {
199        program_id: crate::ID,
200        accounts,
201        data: ClaimOIL {}.to_bytes(),
202    }
203}
204
205
206pub fn close(signer: Pubkey, round_id: u64, rent_payer: Pubkey) -> Instruction {
207    let board_address = board_pda().0;
208    let round_address = round_pda(round_id).0;
209    let treasury_address = TREASURY_ADDRESS;
210    Instruction {
211        program_id: crate::ID,
212        accounts: vec![
213            AccountMeta::new(signer, true),
214            AccountMeta::new(board_address, false),
215            AccountMeta::new(rent_payer, false),
216            AccountMeta::new(round_address, false),
217            AccountMeta::new(treasury_address, false),
218            AccountMeta::new_readonly(system_program::ID, false),
219        ],
220        data: Close {}.to_bytes(),
221    }
222}
223
224/// Deploy SOL to prospect on squares. Pass a referrer pubkey for new miners to set up referral.
225/// Set `pooled` to true to join the mining pool (rewards shared proportionally).
226pub fn deploy(
227    signer: Pubkey,
228    authority: Pubkey,
229    amount: u64,
230    round_id: u64,
231    squares: [bool; 25],
232    referrer: Option<Pubkey>,
233    pooled: bool,
234) -> Instruction {
235    let automation_address = automation_pda(authority).0;
236    let board_address = board_pda().0;
237    let config_address = config_pda().0;
238    let miner_address = miner_pda(authority).0;
239    let round_address = round_pda(round_id).0;
240    let entropy_var_address = entropy_rng_api::state::var_pda(board_address, 0).0;
241
242    // Convert array of 25 booleans into a 32-bit mask where each bit represents whether
243    // that square index is selected (1) or not (0)
244    let mut mask: u32 = 0;
245    for (i, &square) in squares.iter().enumerate() {
246        if square {
247            mask |= 1 << i;
248        }
249    }
250    
251    // Get referrer bytes (default to zero pubkey if no referrer).
252    let referrer_pubkey = referrer.unwrap_or(Pubkey::default());
253    let referrer_bytes = referrer_pubkey.to_bytes();
254
255    let mut accounts = vec![
256        AccountMeta::new(signer, true),
257        AccountMeta::new(authority, false),
258        AccountMeta::new(automation_address, false),
259        AccountMeta::new(board_address, false),
260        AccountMeta::new(config_address, false),
261        AccountMeta::new(miner_address, false),
262        AccountMeta::new(round_address, false),
263        AccountMeta::new_readonly(system_program::ID, false),
264        AccountMeta::new_readonly(crate::ID, false),
265        // Entropy accounts.
266        AccountMeta::new(entropy_var_address, false),
267        AccountMeta::new_readonly(entropy_rng_api::ID, false),
268    ];
269    
270    // Add referral account if referrer is provided (for incrementing total_referred on new miners).
271    if referrer_pubkey != Pubkey::default() {
272        let referral_address = referral_pda(referrer_pubkey).0;
273        accounts.push(AccountMeta::new(referral_address, false));
274    }
275
276    Instruction {
277        program_id: crate::ID,
278        accounts,
279        data: Deploy {
280            amount: amount.to_le_bytes(),
281            squares: mask.to_le_bytes(),
282            referrer: referrer_bytes,
283            pooled: if pooled { 1 } else { 0 },
284        }
285        .to_bytes(),
286    }
287}
288
289// 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] =
290
291pub fn buyback(signer: Pubkey, swap_accounts: &[AccountMeta], swap_data: &[u8]) -> Instruction {
292    let board_address = board_pda().0;
293    let config_address = config_pda().0;
294    let mint_address = MINT_ADDRESS;
295    let treasury_address = TREASURY_ADDRESS;
296    let treasury_oil_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
297    let treasury_sol_address = get_associated_token_address(&treasury_address, &SOL_MINT);
298    let mut accounts = vec![
299        AccountMeta::new(signer, true),
300        AccountMeta::new(board_address, false),
301        AccountMeta::new_readonly(config_address, false),
302        AccountMeta::new(mint_address, false),
303        AccountMeta::new(treasury_address, false),
304        AccountMeta::new(treasury_oil_address, false),
305        AccountMeta::new(treasury_sol_address, false),
306        AccountMeta::new_readonly(spl_token::ID, false),
307        AccountMeta::new_readonly(crate::ID, false),
308    ];
309    for account in swap_accounts.iter() {
310        let mut acc_clone = account.clone();
311        acc_clone.is_signer = false;
312        accounts.push(acc_clone);
313    }
314    let mut data = Buyback {}.to_bytes();
315    data.extend_from_slice(swap_data);
316    Instruction {
317        program_id: crate::ID,
318        accounts,
319        data,
320    }
321}
322
323// 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] =
324
325pub fn reset(
326    signer: Pubkey,
327    fee_collector: Pubkey,
328    round_id: u64,
329    top_miner: Pubkey,
330    var_address: Pubkey,
331) -> Instruction {
332    let board_address = board_pda().0;
333    let config_address = config_pda().0;
334    let mint_address = MINT_ADDRESS;
335    let round_address = round_pda(round_id).0;
336    let round_next_address = round_pda(round_id + 1).0;
337    let top_miner_address = miner_pda(top_miner).0;
338    let treasury_address = TREASURY_ADDRESS;
339    let treasury_tokens_address = treasury_tokens_address();
340    let pool_address = pool_pda().0;
341    let mint_authority_address = oil_mint_api::state::authority_pda().0;
342    Instruction {
343        program_id: crate::ID,
344        accounts: vec![
345            AccountMeta::new(signer, true),
346            AccountMeta::new(board_address, false),
347            AccountMeta::new(config_address, false),
348            AccountMeta::new(fee_collector, false),
349            AccountMeta::new(mint_address, false),
350            AccountMeta::new(round_address, false),
351            AccountMeta::new(round_next_address, false),
352            AccountMeta::new(top_miner_address, false),
353            AccountMeta::new(treasury_address, false),
354            AccountMeta::new(pool_address, false),
355            AccountMeta::new(treasury_tokens_address, false),
356            AccountMeta::new_readonly(system_program::ID, false),
357            AccountMeta::new_readonly(spl_token::ID, false),
358            AccountMeta::new_readonly(crate::ID, false),
359            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
360            // Entropy accounts.
361            AccountMeta::new(var_address, false),
362            AccountMeta::new_readonly(entropy_rng_api::ID, false),
363            // Mint accounts.
364            AccountMeta::new(mint_authority_address, false),
365            AccountMeta::new_readonly(oil_mint_api::ID, false),
366        ],
367        data: Reset {}.to_bytes(),
368    }
369}
370    
371// let [signer_info, automation_info, board_info, miner_info, round_info, treasury_info, system_program] =
372
373pub fn checkpoint(signer: Pubkey, authority: Pubkey, round_id: u64) -> Instruction {
374    let miner_address = miner_pda(authority).0;
375    let board_address = board_pda().0;
376    let round_address = round_pda(round_id).0;
377    let treasury_address = TREASURY_ADDRESS;
378    Instruction {
379        program_id: crate::ID,
380        accounts: vec![
381            AccountMeta::new(signer, true),
382            AccountMeta::new(board_address, false),
383            AccountMeta::new(miner_address, false),
384            AccountMeta::new(round_address, false),
385            AccountMeta::new(treasury_address, false),
386            AccountMeta::new_readonly(system_program::ID, false),
387        ],
388        data: Checkpoint {}.to_bytes(),
389    }
390}
391
392pub fn set_admin(signer: Pubkey, admin: Pubkey) -> Instruction {
393    let config_address = config_pda().0;
394    Instruction {
395        program_id: crate::ID,
396        accounts: vec![
397            AccountMeta::new(signer, true),
398            AccountMeta::new(config_address, false),
399            AccountMeta::new_readonly(system_program::ID, false),
400        ],
401        data: SetAdmin {
402            admin: admin.to_bytes(),
403        }
404        .to_bytes(),
405    }
406}
407
408pub fn set_admin_fee(signer: Pubkey, admin_fee: u64) -> Instruction {
409    let config_address = config_pda().0;
410    Instruction {
411        program_id: crate::ID,
412        accounts: vec![
413            AccountMeta::new(signer, true),
414            AccountMeta::new(config_address, false),
415            AccountMeta::new_readonly(system_program::ID, false),
416        ],
417        data: SetAdminFee {
418            admin_fee: admin_fee.to_le_bytes(),
419        }
420        .to_bytes(),
421    }
422}
423
424pub fn set_fee_collector(signer: Pubkey, fee_collector: Pubkey) -> Instruction {
425    let config_address = config_pda().0;
426    Instruction {
427        program_id: crate::ID,
428        accounts: vec![
429            AccountMeta::new(signer, true),
430            AccountMeta::new(config_address, false),
431            AccountMeta::new_readonly(system_program::ID, false),
432        ],
433        data: SetFeeCollector {
434            fee_collector: fee_collector.to_bytes(),
435        }
436        .to_bytes(),
437    }
438}
439
440pub fn set_auction(
441    signer: Pubkey,
442    halving_period_seconds: u64,
443    last_halving_time: u64,
444    base_mining_rates: [u64; 4],
445    auction_duration_seconds: u64,
446    starting_prices: [u64; 4],
447    _well_id: u64, // Kept for backwards compatibility, but not used (always updates auction only)
448) -> Instruction {
449    let config_address = config_pda().0;
450    let auction_address = auction_pda().0;
451    
452    Instruction {
453        program_id: crate::ID,
454        accounts: vec![
455            AccountMeta::new(signer, true),
456            AccountMeta::new_readonly(config_address, false),
457            AccountMeta::new(auction_address, false),
458        ],
459        data: SetAuction {
460            halving_period_seconds: halving_period_seconds.to_le_bytes(),
461            last_halving_time: last_halving_time.to_le_bytes(),
462            base_mining_rates: [
463                base_mining_rates[0].to_le_bytes(),
464                base_mining_rates[1].to_le_bytes(),
465                base_mining_rates[2].to_le_bytes(),
466                base_mining_rates[3].to_le_bytes(),
467            ],
468            auction_duration_seconds: auction_duration_seconds.to_le_bytes(),
469            starting_prices: [
470                starting_prices[0].to_le_bytes(),
471                starting_prices[1].to_le_bytes(),
472                starting_prices[2].to_le_bytes(),
473                starting_prices[3].to_le_bytes(),
474            ],
475            well_id: 4u64.to_le_bytes(), // Always use 4 to indicate auction-only update
476        }
477        .to_bytes(),
478    }
479}
480
481// let [signer_info, mint_info, sender_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program] =
482
483pub fn deposit(signer: Pubkey, payer: Pubkey, amount: u64, lock_duration_days: u64, stake_id: u64) -> Instruction {
484    let mint_address = MINT_ADDRESS;
485    let stake_address = stake_pda_with_id(signer, stake_id).0;
486    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
487    let sender_address = get_associated_token_address(&signer, &MINT_ADDRESS);
488    let pool_address = pool_pda().0;
489    let pool_tokens_address = pool_tokens_address();
490    Instruction {
491        program_id: crate::ID,
492        accounts: vec![
493            AccountMeta::new(signer, true),
494            AccountMeta::new(payer, true),
495            AccountMeta::new(mint_address, false),
496            AccountMeta::new(sender_address, false),
497            AccountMeta::new(stake_address, false),
498            AccountMeta::new(stake_tokens_address, false),
499            AccountMeta::new(pool_address, false),
500            AccountMeta::new(pool_tokens_address, false),
501            AccountMeta::new_readonly(system_program::ID, false),
502            AccountMeta::new_readonly(spl_token::ID, false),
503            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
504        ],
505        data: Deposit {
506            amount: amount.to_le_bytes(),
507            lock_duration_days: lock_duration_days.to_le_bytes(),
508            stake_id: stake_id.to_le_bytes(),
509        }
510        .to_bytes(),
511    }
512}
513
514// let [signer_info, mint_info, recipient_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program] =
515
516pub fn withdraw(signer: Pubkey, amount: u64, stake_id: u64) -> Instruction {
517    let stake_address = stake_pda_with_id(signer, stake_id).0;
518    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
519    let mint_address = MINT_ADDRESS;
520    let recipient_address = get_associated_token_address(&signer, &MINT_ADDRESS);
521    let pool_address = pool_pda().0;
522    let pool_tokens_address = pool_tokens_address();
523    Instruction {
524        program_id: crate::ID,
525        accounts: vec![
526            AccountMeta::new(signer, true),
527            AccountMeta::new(mint_address, false),
528            AccountMeta::new(recipient_address, false),
529            AccountMeta::new(stake_address, false),
530            AccountMeta::new(stake_tokens_address, false),
531            AccountMeta::new(pool_address, false),
532            AccountMeta::new(pool_tokens_address, false),
533            AccountMeta::new_readonly(system_program::ID, false),
534            AccountMeta::new_readonly(spl_token::ID, false),
535            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
536        ],
537        data: Withdraw {
538            amount: amount.to_le_bytes(),
539            stake_id: stake_id.to_le_bytes(),
540        }
541        .to_bytes(),
542    }
543}
544
545// let [signer_info, automation_info, miner_info, system_program] = accounts else {
546
547/// Reload SOL from miner account to automation balance with single-tier referral system.
548/// 
549/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
550/// 
551/// Account structure:
552/// - Base: signer, automation, miner, system_program
553/// - If miner has referrer (required): [miner_referrer, referral_referrer]
554pub fn reload_sol(
555    signer: Pubkey,
556    authority: Pubkey,
557    referrer_miner: Option<Pubkey>,
558    referrer_referral: Option<Pubkey>,
559) -> Instruction {
560    let automation_address = automation_pda(authority).0;
561    let miner_address = miner_pda(authority).0;
562    
563    let mut accounts = vec![
564        AccountMeta::new(signer, true),
565        AccountMeta::new(automation_address, false),
566        AccountMeta::new(miner_address, false),
567        AccountMeta::new_readonly(system_program::ID, false),
568    ];
569    
570    // Add referral accounts if provided (required when miner has referrer)
571    if let (Some(miner_ref), Some(referral_ref)) = (referrer_miner, referrer_referral) {
572        accounts.push(AccountMeta::new(miner_ref, false));
573        accounts.push(AccountMeta::new(referral_ref, false));
574    }
575    
576    Instruction {
577        program_id: crate::ID,
578        accounts,
579        data: ReloadSOL {}.to_bytes(),
580    }
581}
582
583// let [signer_info, mint_info, recipient_info, stake_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
584
585/// Claim SOL yield from staking. Stakers earn SOL rewards (2% of round winnings), not OIL.
586pub fn claim_yield(signer: Pubkey, amount: u64, stake_id: u64) -> Instruction {
587    let stake_address = stake_pda_with_id(signer, stake_id).0;
588    let recipient_address = signer; // SOL recipient (wallet, not token account)
589    let pool_address = pool_pda().0;
590    Instruction {
591        program_id: crate::ID,
592        accounts: vec![
593            AccountMeta::new(signer, true),
594            AccountMeta::new(recipient_address, false), // SOL recipient wallet
595            AccountMeta::new(stake_address, false),
596            AccountMeta::new(pool_address, false),
597            AccountMeta::new_readonly(system_program::ID, false),
598        ],
599        data: ClaimYield {
600            amount: amount.to_le_bytes(),
601        }
602        .to_bytes(),
603    }
604}
605
606pub fn new_var(
607    signer: Pubkey,
608    provider: Pubkey,
609    id: u64,
610    commit: [u8; 32],
611    samples: u64,
612) -> Instruction {
613    let board_address = board_pda().0;
614    let config_address = config_pda().0;
615    let var_address = entropy_rng_api::state::var_pda(board_address, id).0;
616    Instruction {
617        program_id: crate::ID,
618        accounts: vec![
619            AccountMeta::new(signer, true),
620            AccountMeta::new(board_address, false),
621            AccountMeta::new(config_address, false),
622            AccountMeta::new(provider, false),
623            AccountMeta::new(var_address, false),
624            AccountMeta::new_readonly(system_program::ID, false),
625            AccountMeta::new_readonly(entropy_rng_api::ID, false),
626        ],
627        data: NewVar {
628            id: id.to_le_bytes(),
629            commit: commit,
630            samples: samples.to_le_bytes(),
631        }
632        .to_bytes(),
633    }
634}
635
636pub fn set_swap_program(signer: Pubkey, new_program: Pubkey) -> Instruction {
637    let config_address = config_pda().0;
638    Instruction {
639        program_id: crate::ID,
640        accounts: vec![
641            AccountMeta::new(signer, true),
642            AccountMeta::new(config_address, false),
643            AccountMeta::new_readonly(new_program, false),
644        ],
645        data: SetSwapProgram {}.to_bytes(),
646    }
647}
648
649pub fn set_var_address(signer: Pubkey, new_var_address: Pubkey) -> Instruction {
650    let board_address = board_pda().0;
651    let config_address = config_pda().0;
652    Instruction {
653        program_id: crate::ID,
654        accounts: vec![
655            AccountMeta::new(signer, true),
656            AccountMeta::new(board_address, false),
657            AccountMeta::new(config_address, false),
658            AccountMeta::new(new_var_address, false),
659        ],
660        data: SetVarAddress {}.to_bytes(),
661    }
662}
663
664/// Migrate a Round account to add pool fields.
665/// Anyone can call this (signer pays for rent increase).
666pub fn migrate(signer: Pubkey, well_id: u64) -> Instruction {
667    let well_address = well_pda(well_id).0;
668    Instruction {
669        program_id: crate::ID,
670        accounts: vec![
671            AccountMeta::new(signer, true),
672            AccountMeta::new(well_address, false),
673            AccountMeta::new_readonly(system_program::ID, false),
674        ],
675        data: Migrate {}.to_bytes(),
676    }
677}
678
679/// Migrate a Treasury account (no-op, kept for backwards compatibility).
680/// Stakers earn SOL rewards (2% of round winnings), not OIL.
681pub fn migrate_treasury(signer: Pubkey) -> Instruction {
682    let treasury_address = treasury_pda().0;
683    Instruction {
684        program_id: crate::ID,
685        accounts: vec![
686            AccountMeta::new(signer, true),
687            AccountMeta::new(treasury_address, false),
688            AccountMeta::new_readonly(system_program::ID, false),
689        ],
690        data: Migrate {}.to_bytes(),
691    }
692}
693
694/// Create a referral account to become a referrer.
695pub fn create_referral(signer: Pubkey) -> Instruction {
696    let referral_address = referral_pda(signer).0;
697    Instruction {
698        program_id: crate::ID,
699        accounts: vec![
700            AccountMeta::new(signer, true),
701            AccountMeta::new(referral_address, false),
702            AccountMeta::new_readonly(system_program::ID, false),
703        ],
704        data: CreateReferral {}.to_bytes(),
705    }
706}
707
708/// Claim pending referral rewards (both SOL and OIL).
709
710pub fn claim_referral(signer: Pubkey) -> Instruction {
711    let referral_address = referral_pda(signer).0;
712    let referral_oil_address = get_associated_token_address(&referral_address, &MINT_ADDRESS);
713    let recipient_oil_address = get_associated_token_address(&signer, &MINT_ADDRESS);
714    Instruction {
715        program_id: crate::ID,
716        accounts: vec![
717            AccountMeta::new(signer, true),
718            AccountMeta::new(referral_address, false),
719            AccountMeta::new(referral_oil_address, false), // Referral account's OIL ATA
720            AccountMeta::new(MINT_ADDRESS, false),
721            AccountMeta::new(recipient_oil_address, false), // Recipient's OIL ATA
722            AccountMeta::new_readonly(system_program::ID, false),
723            AccountMeta::new_readonly(spl_token::ID, false),
724            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
725        ],
726        data: ClaimReferral {}.to_bytes(),
727    }
728}
729
730/// Direct solo bid on an auction well (seize ownership).
731/// The bid amount is calculated on-chain as current_price + 1 lamport.
732/// User must have enough SOL in their wallet to cover the bid.
733/// 
734/// Account structure:
735/// - Base: signer, square, fund (optional), auction, treasury, treasury_tokens, mint, mint_authority, mint_program, staking_pool, fee_collector, config, token_program, system_program
736/// - If previous owner exists (optional): [previous_owner_miner, previous_owner]
737pub fn place_bid(
738    signer: Pubkey,
739    square_id: u64,
740    fee_collector: Pubkey,
741    pool_account: Option<Pubkey>, // Pool account for current epoch (optional, if pool exists)
742    previous_owner_miner: Option<Pubkey>, // Previous owner's miner PDA (if previous owner exists)
743    previous_owner: Option<Pubkey>, // Previous owner pubkey (if previous owner exists)
744    referrer: Option<Pubkey>, // Optional referrer pubkey for new miners
745) -> Instruction {
746    let well_address = well_pda(square_id).0;
747    let auction_address = auction_pda().0;
748    let treasury_address = treasury_pda().0;
749    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
750    let staking_pool_address = pool_pda().0;
751    let config_address = config_pda().0;
752    let mint_authority_address = oil_mint_api::state::authority_pda().0;
753    
754    let mut accounts = vec![
755        AccountMeta::new(signer, true),
756        AccountMeta::new(well_address, false),
757    ];
758    
759    // Add pool account if provided (for current epoch)
760    if let Some(pool_pubkey) = pool_account {
761        accounts.push(AccountMeta::new(pool_pubkey, false));
762    } else {
763        // Add placeholder (account order must be consistent)
764        accounts.push(AccountMeta::new_readonly(system_program::ID, false)); // Placeholder, will be ignored
765    }
766    
767    accounts.extend_from_slice(&[
768        AccountMeta::new(auction_address, false),
769        AccountMeta::new(treasury_address, false),
770        AccountMeta::new(treasury_tokens_address, false),
771        AccountMeta::new(MINT_ADDRESS, false),
772        AccountMeta::new(mint_authority_address, false),
773        AccountMeta::new_readonly(oil_mint_api::ID, false),
774        AccountMeta::new(staking_pool_address, false),
775        AccountMeta::new(fee_collector, false),
776        AccountMeta::new(config_address, false),
777        AccountMeta::new_readonly(spl_token::ID, false),
778        AccountMeta::new_readonly(system_program::ID, false),
779        AccountMeta::new_readonly(crate::ID, false), // oil_program
780    ]);
781    
782    // Add previous owner accounts if provided
783    if let (Some(miner_pubkey), Some(owner_pubkey)) = (previous_owner_miner, previous_owner) {
784        accounts.push(AccountMeta::new(miner_pubkey, false));
785        accounts.push(AccountMeta::new(owner_pubkey, false));
786    }
787    
788    // Add referral account if referrer is provided
789    if let Some(referrer_pubkey) = referrer {
790        let referral_address = referral_pda(referrer_pubkey).0;
791        accounts.push(AccountMeta::new(referral_address, false));
792    }
793    
794    Instruction {
795        program_id: crate::ID,
796        accounts,
797        data: instruction::PlaceBid {
798            square_id: square_id.to_le_bytes(),
799            referrer: referrer.unwrap_or(Pubkey::default()).to_bytes(),
800        }
801        .to_bytes(),
802    }
803}
804
805/// Contribute SOL to an auction pool for a specific well.
806/// 
807/// Account structure:
808/// - Base: signer, bid, square, fund (may need to create), auction, system_program
809/// - If pool wins (optional): [treasury, treasury_tokens, mint, mint_authority, mint_program, staking_pool, fee_collector, config, token_program, previous_owner_miner, previous_owner]
810pub fn join_auction_pool(
811    signer: Pubkey,
812    square_id: u64,
813    epoch_id: u64, // Current epoch ID (from Well account)
814    amount: u64, // SOL amount in lamports
815    auction_pool_account: Pubkey, // Auction pool account for current epoch (may need to create)
816    fee_collector: Option<Pubkey>, // Required if pool wins
817    previous_owner_miner: Option<Pubkey>, // Required if pool wins and previous owner exists
818    previous_owner: Option<Pubkey>, // Required if pool wins and previous owner exists
819) -> Instruction {
820    let miner_address = miner_pda(signer).0;
821    let bid_address = bid_pda(signer, square_id, epoch_id).0;
822    let well_address = well_pda(square_id).0;
823    let auction_address = auction_pda().0;
824    
825    let mut accounts = vec![
826        AccountMeta::new(signer, true),
827        AccountMeta::new(miner_address, false), // For tracking pool contributions
828        AccountMeta::new(bid_address, false),
829        AccountMeta::new(well_address, false),
830        AccountMeta::new(auction_pool_account, false), // May need to create
831        AccountMeta::new(auction_address, false),
832        AccountMeta::new_readonly(system_program::ID, false),
833        AccountMeta::new_readonly(crate::ID, false), // oil_program
834    ];
835    
836    // Add accounts needed if pool wins
837    if let Some(fee_collector_pubkey) = fee_collector {
838        let treasury_address = treasury_pda().0;
839        let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
840        let staking_pool_address = pool_pda().0;
841        let config_address = config_pda().0;
842        let mint_authority_address = oil_mint_api::state::authority_pda().0;
843        
844        accounts.push(AccountMeta::new(treasury_address, false));
845        accounts.push(AccountMeta::new(treasury_tokens_address, false));
846        accounts.push(AccountMeta::new(MINT_ADDRESS, false));
847        accounts.push(AccountMeta::new(mint_authority_address, false));
848        accounts.push(AccountMeta::new_readonly(oil_mint_api::ID, false));
849        accounts.push(AccountMeta::new(staking_pool_address, false));
850        accounts.push(AccountMeta::new(fee_collector_pubkey, false));
851        accounts.push(AccountMeta::new(config_address, false));
852        accounts.push(AccountMeta::new_readonly(spl_token::ID, false));
853        
854        // Add previous owner accounts if provided
855        if let (Some(miner_pubkey), Some(owner_pubkey)) = (previous_owner_miner, previous_owner) {
856            accounts.push(AccountMeta::new(miner_pubkey, false));
857            accounts.push(AccountMeta::new(owner_pubkey, false));
858        }
859    }
860    
861    Instruction {
862        program_id: crate::ID,
863        accounts,
864        data: JoinAuctionPool {
865            square_id: square_id.to_le_bytes(),
866            amount: amount.to_le_bytes(),
867        }
868        .to_bytes(),
869    }
870}
871
872/// Claim auction-based OIL rewards
873/// - OIL rewards: from current ownership and previous ownership (pre-minted)
874/// 
875/// Account structure:
876/// - 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
877/// - 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)
878pub fn claim_auction_oil(
879    signer: Pubkey,
880    well_mask: u8, // Bitmask: bit 0 = well 0, bit 1 = well 1, etc.
881    well_accounts: [Option<Pubkey>; 4], // Well PDAs for wells 0-3 (required for wells in mask)
882    auction_pool_accounts: Option<[Option<Pubkey>; 4]>, // Auction Pool PDAs for wells 0-3 (optional, for pool contributors)
883    bid_accounts: Option<[Option<Pubkey>; 4]>, // Bid PDAs for wells 0-3 (required for pool contributors, must include epoch_id in PDA)
884) -> Instruction {
885    let miner_address = miner_pda(signer).0;
886    let auction_address = auction_pda().0;
887    let treasury_address = treasury_pda().0;
888    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
889    let recipient_address = get_associated_token_address(&signer, &MINT_ADDRESS);
890    let mint_authority_address = oil_mint_api::state::authority_pda().0;
891    
892    let mut accounts = vec![
893        AccountMeta::new(signer, true),
894        AccountMeta::new(miner_address, false),
895    ];
896    
897    // Add well accounts for wells in mask (all 4 required, but only wells in mask are used)
898    for well_opt in well_accounts.iter() {
899        if let Some(well_pubkey) = well_opt {
900            accounts.push(AccountMeta::new(*well_pubkey, false));
901        } else {
902            // Use a placeholder account if well is not provided
903            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
904        }
905    }
906    
907    // Add auction pool accounts if provided (optional, for pool contributors)
908    if let Some(auction_pool_pdas) = auction_pool_accounts {
909        for auction_pool_pda_opt in auction_pool_pdas.iter() {
910            if let Some(auction_pool_pubkey) = auction_pool_pda_opt {
911                accounts.push(AccountMeta::new(*auction_pool_pubkey, false));
912            } else {
913                // Use a placeholder account if auction pool is not provided
914                accounts.push(AccountMeta::new_readonly(system_program::ID, false));
915            }
916        }
917    } else {
918        // Add 4 placeholder accounts if auction_pool_accounts is None
919        for _ in 0..4 {
920            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
921        }
922    }
923    
924    accounts.extend_from_slice(&[
925        AccountMeta::new(auction_address, false), // Must be writable for auction_program_log CPI
926        AccountMeta::new(treasury_address, false),
927        AccountMeta::new(treasury_tokens_address, false),
928        AccountMeta::new(MINT_ADDRESS, false), // Must be writable for mint_oil CPI
929        AccountMeta::new(mint_authority_address, false), // Must be writable for mint_oil CPI
930        AccountMeta::new_readonly(oil_mint_api::ID, false),
931        AccountMeta::new(recipient_address, false),
932        AccountMeta::new_readonly(spl_token::ID, false),
933        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
934        AccountMeta::new_readonly(system_program::ID, false),
935        AccountMeta::new_readonly(crate::ID, false), // oil_program
936    ]);
937    
938    // Add bid accounts if provided (for pool contributors)
939    if let Some(bid_pdas) = bid_accounts {
940        for bid_pda_opt in bid_pdas.iter() {
941            if let Some(bid_pubkey) = bid_pda_opt {
942                accounts.push(AccountMeta::new(*bid_pubkey, false));
943            }
944        }
945    }
946    
947    Instruction {
948        program_id: crate::ID,
949        accounts,
950        data: ClaimAuctionOIL {
951            well_mask,
952        }
953        .to_bytes(),
954    }
955}
956
957/// Claim auction-based SOL rewards
958/// - SOL rewards: from being outbid and refunds from abandoned pools
959/// 
960/// Account structure:
961/// - Base: signer, miner, well accounts (one per well 0-3, for checking abandoned pools), auction pool accounts (optional, one per well), auction, treasury, system_program, oil_program
962/// - Bid accounts (one per well, required for refunds): [bid_0, bid_1, bid_2, bid_3] (must include epoch_id in PDA)
963pub fn claim_auction_sol(
964    signer: Pubkey,
965    well_accounts: [Option<Pubkey>; 4], // Well PDAs for wells 0-3 (for checking abandoned pools)
966    auction_pool_accounts: Option<[Option<Pubkey>; 4]>, // Auction Pool PDAs for wells 0-3 (optional, for refunds)
967    bid_accounts: Option<[Option<Pubkey>; 4]>, // Bid PDAs for wells 0-3 (required for refunds, must include epoch_id in PDA)
968) -> Instruction {
969    let miner_address = miner_pda(signer).0;
970    let (auction_address, _) = auction_pda();
971    let treasury_address = treasury_pda().0;
972    
973    let mut accounts = vec![
974        AccountMeta::new(signer, true),
975        AccountMeta::new(miner_address, false),
976    ];
977    
978    // Add well accounts (all 4 wells for checking abandoned pools)
979    for well_opt in well_accounts.iter() {
980        if let Some(well_pubkey) = well_opt {
981            accounts.push(AccountMeta::new(*well_pubkey, false));
982        } else {
983            // Use a placeholder account if well is not provided
984            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
985        }
986    }
987    
988    // Add auction pool accounts if provided (optional, for refunds)
989    if let Some(auction_pool_pdas) = auction_pool_accounts {
990        for auction_pool_pda_opt in auction_pool_pdas.iter() {
991            if let Some(auction_pool_pubkey) = auction_pool_pda_opt {
992                accounts.push(AccountMeta::new(*auction_pool_pubkey, false));
993            } else {
994                // Use a placeholder account if auction pool is not provided
995                accounts.push(AccountMeta::new_readonly(system_program::ID, false));
996            }
997        }
998    } else {
999        // Add 4 placeholder accounts if auction_pool_accounts is None
1000        for _ in 0..4 {
1001            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
1002        }
1003    }
1004    
1005    accounts.extend_from_slice(&[
1006        AccountMeta::new(auction_address, false), // Must be writable for auction_program_log CPI
1007        AccountMeta::new(treasury_address, false),
1008        AccountMeta::new_readonly(system_program::ID, false),
1009        AccountMeta::new_readonly(crate::ID, false), // oil_program
1010    ]);
1011    
1012    // Add bid accounts if provided (for refunds)
1013    if let Some(bid_pdas) = bid_accounts {
1014        for bid_pda_opt in bid_pdas.iter() {
1015            if let Some(bid_pubkey) = bid_pda_opt {
1016                accounts.push(AccountMeta::new(*bid_pubkey, false));
1017            }
1018        }
1019    }
1020    
1021    Instruction {
1022        program_id: crate::ID,
1023        accounts,
1024        data: ClaimAuctionSOL {
1025            _reserved: 0,
1026        }
1027        .to_bytes(),
1028    }
1029}