Skip to main content

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    // Derive wrapped SOL ATAs
256    let user_wrapped_sol_ata = get_associated_token_address(&authority, &SOL_MINT);
257    let round_wrapped_sol_ata = get_associated_token_address(&round_address, &SOL_MINT);
258
259    // Build accounts list - must match program structure:
260    // Oil accounts: base (9) + token (5) + optional referral (1) = 14-15
261    // Entropy accounts: var + program = 2 (always exactly 2)
262    let mut accounts = vec![
263        // Base accounts (9)
264        AccountMeta::new(signer, true), // 0: signer
265        AccountMeta::new(authority, false), // 1: authority
266        AccountMeta::new(automation_address, false), // 2: automation
267        AccountMeta::new(board_address, false), // 3: board
268        AccountMeta::new_readonly(config_address, false), // 4: config
269        AccountMeta::new(miner_address, false), // 5: miner
270        AccountMeta::new(round_address, false), // 6: round
271        AccountMeta::new_readonly(system_program::ID, false), // 7: system_program
272        AccountMeta::new_readonly(crate::ID, false), // 8: oil_program
273        // Token accounts (5)
274        AccountMeta::new(user_wrapped_sol_ata, false), // 9: user_wrapped_sol
275        AccountMeta::new(round_wrapped_sol_ata, false), // 10: round_wrapped_sol
276        AccountMeta::new_readonly(spl_token::ID, false), // 11: token_program
277        AccountMeta::new_readonly(SOL_MINT, false), // 12: mint (SOL_MINT)
278        AccountMeta::new_readonly(spl_associated_token_account::ID, false), // 13: associated_token_program
279    ];
280    
281    // Add referral account if referrer is provided (in oil_accounts, before entropy accounts)
282    if referrer_pubkey != Pubkey::default() {
283        let referral_address = referral_pda(referrer_pubkey).0;
284        accounts.push(AccountMeta::new(referral_address, false)); // 14: referral (optional, in oil_accounts)
285    }
286    
287    // Entropy accounts (always exactly 2, come after all oil_accounts)
288    accounts.push(AccountMeta::new(entropy_var_address, false)); // entropy_var
289    accounts.push(AccountMeta::new_readonly(entropy_rng_api::ID, false)); // entropy_program
290
291    Instruction {
292        program_id: crate::ID,
293        accounts,
294        data: Deploy {
295            amount: amount.to_le_bytes(),
296            squares: mask.to_le_bytes(),
297            referrer: referrer_bytes,
298            pooled: if pooled { 1 } else { 0 },
299        }
300        .to_bytes(),
301    }
302}
303
304// 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] =
305
306pub fn buyback(signer: Pubkey, swap_accounts: &[AccountMeta], swap_data: &[u8]) -> Instruction {
307    let board_address = board_pda().0;
308    let config_address = config_pda().0;
309    let mint_address = MINT_ADDRESS;
310    let treasury_address = TREASURY_ADDRESS;
311    let treasury_oil_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
312    let treasury_sol_address = get_associated_token_address(&treasury_address, &SOL_MINT);
313    let mut accounts = vec![
314        AccountMeta::new(signer, true),
315        AccountMeta::new(board_address, false),
316        AccountMeta::new_readonly(config_address, false),
317        AccountMeta::new(mint_address, false),
318        AccountMeta::new(treasury_address, false),
319        AccountMeta::new(treasury_oil_address, false),
320        AccountMeta::new(treasury_sol_address, false),
321        AccountMeta::new_readonly(spl_token::ID, false),
322        AccountMeta::new_readonly(crate::ID, false),
323    ];
324    for account in swap_accounts.iter() {
325        let mut acc_clone = account.clone();
326        acc_clone.is_signer = false;
327        accounts.push(acc_clone);
328    }
329    let mut data = Buyback {}.to_bytes();
330    data.extend_from_slice(swap_data);
331    Instruction {
332        program_id: crate::ID,
333        accounts,
334        data,
335    }
336}
337
338// 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] =
339
340pub fn reset(
341    signer: Pubkey,
342    fee_collector: Pubkey,
343    round_id: u64,
344    top_miner: Pubkey,
345    var_address: Pubkey,
346) -> Instruction {
347    reset_with_miners(signer, fee_collector, round_id, top_miner, var_address, &[])
348}
349
350pub fn reset_with_miners(
351    signer: Pubkey,
352    fee_collector: Pubkey,
353    round_id: u64,
354    top_miner: Pubkey,
355    var_address: Pubkey,
356    miner_accounts: &[Pubkey],
357) -> Instruction {
358    let board_address = board_pda().0;
359    let config_address = config_pda().0;
360    let mint_address = MINT_ADDRESS;
361    let round_address = round_pda(round_id).0;
362    let round_next_address = round_pda(round_id + 1).0;
363    let top_miner_address = miner_pda(top_miner).0;
364    let treasury_address = TREASURY_ADDRESS;
365    let treasury_tokens_address = treasury_tokens_address();
366    let pool_address = pool_pda().0;
367    let mint_authority_address = oil_mint_api::state::authority_pda().0;
368    let mut reset_instruction = Instruction {
369        program_id: crate::ID,
370        accounts: vec![
371            AccountMeta::new(signer, true),
372            AccountMeta::new(board_address, false),
373            AccountMeta::new(config_address, false),
374            AccountMeta::new(fee_collector, false),
375            AccountMeta::new(mint_address, false),
376            AccountMeta::new(round_address, false),
377            AccountMeta::new(round_next_address, false),
378            AccountMeta::new(top_miner_address, false),
379            AccountMeta::new(treasury_address, false),
380            AccountMeta::new(pool_address, false),
381            AccountMeta::new(treasury_tokens_address, false),
382            AccountMeta::new_readonly(system_program::ID, false),
383            AccountMeta::new_readonly(spl_token::ID, false),
384            AccountMeta::new_readonly(crate::ID, false),
385            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
386            // Entropy accounts.
387            AccountMeta::new(var_address, false),
388            AccountMeta::new_readonly(entropy_rng_api::ID, false),
389            // Mint accounts.
390            AccountMeta::new(mint_authority_address, false),
391            AccountMeta::new_readonly(oil_mint_api::ID, false),
392        ],
393        data: Reset {}.to_bytes(),
394    };
395    
396    // Add miner accounts for seeker rewards (optional)
397    for miner_pubkey in miner_accounts {
398        reset_instruction.accounts.push(AccountMeta::new(
399            miner_pda(*miner_pubkey).0,
400            false,
401        ));
402    }
403    
404    reset_instruction
405}
406    
407// let [signer_info, automation_info, board_info, miner_info, round_info, treasury_info, system_program] =
408
409pub fn checkpoint(signer: Pubkey, authority: Pubkey, round_id: u64) -> Instruction {
410    let miner_address = miner_pda(authority).0;
411    let board_address = board_pda().0;
412    let round_address = round_pda(round_id).0;
413    let treasury_address = TREASURY_ADDRESS;
414    Instruction {
415        program_id: crate::ID,
416        accounts: vec![
417            AccountMeta::new(signer, true), // payer (session payer or regular wallet, receives bot fee)
418            AccountMeta::new(authority, false), // authority (user's wallet, for PDA derivation - must be writable when combined with deploy)
419            AccountMeta::new(board_address, false),
420            AccountMeta::new(miner_address, false),
421            AccountMeta::new(round_address, false),
422            AccountMeta::new(treasury_address, false),
423            AccountMeta::new_readonly(system_program::ID, false),
424        ],
425        data: Checkpoint {}.to_bytes(),
426    }
427}
428
429pub fn set_admin(signer: Pubkey, admin: Pubkey) -> Instruction {
430    let config_address = config_pda().0;
431    Instruction {
432        program_id: crate::ID,
433        accounts: vec![
434            AccountMeta::new(signer, true),
435            AccountMeta::new(config_address, false),
436            AccountMeta::new_readonly(system_program::ID, false),
437        ],
438        data: SetAdmin {
439            admin: admin.to_bytes(),
440        }
441        .to_bytes(),
442    }
443}
444
445pub fn set_admin_fee(signer: Pubkey, admin_fee: u64) -> Instruction {
446    let config_address = config_pda().0;
447    Instruction {
448        program_id: crate::ID,
449        accounts: vec![
450            AccountMeta::new(signer, true),
451            AccountMeta::new(config_address, false),
452            AccountMeta::new_readonly(system_program::ID, false),
453        ],
454        data: SetAdminFee {
455            admin_fee: admin_fee.to_le_bytes(),
456        }
457        .to_bytes(),
458    }
459}
460
461pub fn set_fee_collector(signer: Pubkey, fee_collector: Pubkey) -> Instruction {
462    let config_address = config_pda().0;
463    Instruction {
464        program_id: crate::ID,
465        accounts: vec![
466            AccountMeta::new(signer, true),
467            AccountMeta::new(config_address, false),
468            AccountMeta::new_readonly(system_program::ID, false),
469        ],
470        data: SetFeeCollector {
471            fee_collector: fee_collector.to_bytes(),
472        }
473        .to_bytes(),
474    }
475}
476
477pub fn set_auction(
478    signer: Pubkey,
479    halving_period_seconds: u64,
480    last_halving_time: u64,
481    base_mining_rates: [u64; 4],
482    auction_duration_seconds: u64,
483    starting_prices: [u64; 4],
484    _well_id: u64, // Kept for backwards compatibility, but not used (always updates auction only)
485) -> Instruction {
486    let config_address = config_pda().0;
487    let auction_address = auction_pda().0;
488    
489    Instruction {
490        program_id: crate::ID,
491        accounts: vec![
492            AccountMeta::new(signer, true),
493            AccountMeta::new_readonly(config_address, false),
494            AccountMeta::new(auction_address, false),
495        ],
496        data: SetAuction {
497            halving_period_seconds: halving_period_seconds.to_le_bytes(),
498            last_halving_time: last_halving_time.to_le_bytes(),
499            base_mining_rates: [
500                base_mining_rates[0].to_le_bytes(),
501                base_mining_rates[1].to_le_bytes(),
502                base_mining_rates[2].to_le_bytes(),
503                base_mining_rates[3].to_le_bytes(),
504            ],
505            auction_duration_seconds: auction_duration_seconds.to_le_bytes(),
506            starting_prices: [
507                starting_prices[0].to_le_bytes(),
508                starting_prices[1].to_le_bytes(),
509                starting_prices[2].to_le_bytes(),
510                starting_prices[3].to_le_bytes(),
511            ],
512            well_id: 4u64.to_le_bytes(), // Always use 4 to indicate auction-only update
513        }
514        .to_bytes(),
515    }
516}
517
518// let [signer_info, mint_info, sender_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program] =
519
520pub fn deposit(signer: Pubkey, authority: Pubkey, amount: u64, lock_duration_days: u64, stake_id: u64) -> Instruction {
521    let mint_address = MINT_ADDRESS;
522    let stake_address = stake_pda_with_id(authority, stake_id).0; // Derive from authority, not signer
523    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
524    let sender_address = get_associated_token_address(&authority, &MINT_ADDRESS); // Authority's ATA
525    let pool_address = pool_pda().0;
526    let pool_tokens_address = pool_tokens_address();
527    let miner_address = miner_pda(authority).0; // Derive from authority
528    Instruction {
529        program_id: crate::ID,
530        accounts: vec![
531            AccountMeta::new(signer, true), // payer (session payer or regular wallet, pays fees)
532            AccountMeta::new(authority, true), // authority (user's wallet, signs token transfer and used for PDA derivation)
533            AccountMeta::new(mint_address, false),
534            AccountMeta::new(sender_address, false),
535            AccountMeta::new(stake_address, false),
536            AccountMeta::new(stake_tokens_address, false),
537            AccountMeta::new(pool_address, false),
538            AccountMeta::new(pool_tokens_address, false),
539            AccountMeta::new(miner_address, false),
540            AccountMeta::new_readonly(system_program::ID, false),
541            AccountMeta::new_readonly(spl_token::ID, false),
542            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
543        ],
544        data: Deposit {
545            amount: amount.to_le_bytes(),
546            lock_duration_days: lock_duration_days.to_le_bytes(),
547            stake_id: stake_id.to_le_bytes(),
548        }
549        .to_bytes(),
550    }
551}
552
553// let [signer_info, mint_info, recipient_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program] =
554
555pub fn withdraw(signer: Pubkey, authority: Pubkey, amount: u64, stake_id: u64) -> Instruction {
556    let stake_address = stake_pda_with_id(authority, stake_id).0; // Derive from authority, not signer
557    let stake_tokens_address = get_associated_token_address(&stake_address, &MINT_ADDRESS);
558    let mint_address = MINT_ADDRESS;
559    let recipient_address = get_associated_token_address(&authority, &MINT_ADDRESS); // Authority's ATA
560    let pool_address = pool_pda().0;
561    let pool_tokens_address = pool_tokens_address();
562    let miner_address = miner_pda(authority).0; // Derive from authority
563    Instruction {
564        program_id: crate::ID,
565        accounts: vec![
566            AccountMeta::new(signer, true), // payer (session payer or regular wallet)
567            AccountMeta::new(authority, false), // authority (user's wallet, for PDA derivation)
568            AccountMeta::new(mint_address, false),
569            AccountMeta::new(recipient_address, false),
570            AccountMeta::new(stake_address, false),
571            AccountMeta::new(stake_tokens_address, false),
572            AccountMeta::new(pool_address, false),
573            AccountMeta::new(pool_tokens_address, false),
574            AccountMeta::new(miner_address, false),
575            AccountMeta::new_readonly(system_program::ID, false),
576            AccountMeta::new_readonly(spl_token::ID, false),
577            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
578        ],
579        data: Withdraw {
580            amount: amount.to_le_bytes(),
581            stake_id: stake_id.to_le_bytes(),
582        }
583        .to_bytes(),
584    }
585}
586
587// let [signer_info, automation_info, miner_info, system_program] = accounts else {
588
589/// Reload SOL from miner account to automation balance with single-tier referral system.
590/// 
591/// If the miner has a referrer, 1.0% of the claim goes to the referrer.
592/// 
593/// Account structure:
594/// - Base: signer, automation, miner, system_program
595/// - If miner has referrer (required): [miner_referrer, referral_referrer]
596pub fn reload_sol(
597    signer: Pubkey,
598    authority: Pubkey,
599    referrer_miner: Option<Pubkey>,
600    referrer_referral: Option<Pubkey>,
601) -> Instruction {
602    let automation_address = automation_pda(authority).0;
603    let miner_address = miner_pda(authority).0;
604    
605    let mut accounts = vec![
606        AccountMeta::new(signer, true),
607        AccountMeta::new(automation_address, false),
608        AccountMeta::new(miner_address, false),
609        AccountMeta::new_readonly(system_program::ID, false),
610    ];
611    
612    // Add referral accounts if provided (required when miner has referrer)
613    if let (Some(miner_ref), Some(referral_ref)) = (referrer_miner, referrer_referral) {
614        accounts.push(AccountMeta::new(miner_ref, false));
615        accounts.push(AccountMeta::new(referral_ref, false));
616    }
617    
618    Instruction {
619        program_id: crate::ID,
620        accounts,
621        data: ReloadSOL {}.to_bytes(),
622    }
623}
624
625// let [signer_info, mint_info, recipient_info, stake_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
626
627/// Claim SOL yield from staking. Stakers earn SOL rewards (2% of round winnings), not OIL.
628pub fn claim_yield(signer: Pubkey, authority: Pubkey, amount: u64, stake_id: u64) -> Instruction {
629    let stake_address = stake_pda_with_id(authority, stake_id).0; // Derive from authority, not signer
630    let pool_address = pool_pda().0;
631    Instruction {
632        program_id: crate::ID,
633        accounts: vec![
634            AccountMeta::new(signer, true), // payer (session payer or regular wallet)
635            AccountMeta::new(authority, true), // authority (user's wallet, receives SOL and used for PDA derivation)
636            AccountMeta::new(stake_address, false),
637            AccountMeta::new(pool_address, false),
638            AccountMeta::new_readonly(system_program::ID, false),
639        ],
640        data: ClaimYield {
641            amount: amount.to_le_bytes(),
642        }
643        .to_bytes(),
644    }
645}
646
647pub fn new_var(
648    signer: Pubkey,
649    provider: Pubkey,
650    id: u64,
651    commit: [u8; 32],
652    samples: u64,
653) -> Instruction {
654    let board_address = board_pda().0;
655    let config_address = config_pda().0;
656    let var_address = entropy_rng_api::state::var_pda(board_address, id).0;
657    Instruction {
658        program_id: crate::ID,
659        accounts: vec![
660            AccountMeta::new(signer, true),
661            AccountMeta::new(board_address, false),
662            AccountMeta::new(config_address, false),
663            AccountMeta::new(provider, false),
664            AccountMeta::new(var_address, false),
665            AccountMeta::new_readonly(system_program::ID, false),
666            AccountMeta::new_readonly(entropy_rng_api::ID, false),
667        ],
668        data: NewVar {
669            id: id.to_le_bytes(),
670            commit: commit,
671            samples: samples.to_le_bytes(),
672        }
673        .to_bytes(),
674    }
675}
676
677pub fn set_swap_program(signer: Pubkey, new_program: Pubkey) -> Instruction {
678    let config_address = config_pda().0;
679    Instruction {
680        program_id: crate::ID,
681        accounts: vec![
682            AccountMeta::new(signer, true),
683            AccountMeta::new(config_address, false),
684            AccountMeta::new_readonly(new_program, false),
685        ],
686        data: SetSwapProgram {}.to_bytes(),
687    }
688}
689
690pub fn set_var_address(signer: Pubkey, new_var_address: Pubkey) -> Instruction {
691    let board_address = board_pda().0;
692    let config_address = config_pda().0;
693    Instruction {
694        program_id: crate::ID,
695        accounts: vec![
696            AccountMeta::new(signer, true),
697            AccountMeta::new(board_address, false),
698            AccountMeta::new(config_address, false),
699            AccountMeta::new(new_var_address, false),
700        ],
701        data: SetVarAddress {}.to_bytes(),
702    }
703}
704
705/// Migrate a Miner account to add total_stake_score field.
706/// Anyone can call this (signer pays for rent increase).
707pub fn migrate(signer: Pubkey, miner_authority: Pubkey) -> Instruction {
708    let miner_address = miner_pda(miner_authority).0;
709    Instruction {
710        program_id: crate::ID,
711        accounts: vec![
712            AccountMeta::new(signer, true),
713            AccountMeta::new(miner_address, false),
714            AccountMeta::new_readonly(system_program::ID, false),
715        ],
716        data: Migrate {}.to_bytes(),
717    }
718}
719
720/// Create a referral account to become a referrer.
721pub fn create_referral(signer: Pubkey) -> Instruction {
722    let referral_address = referral_pda(signer).0;
723    Instruction {
724        program_id: crate::ID,
725        accounts: vec![
726            AccountMeta::new(signer, true),
727            AccountMeta::new(referral_address, false),
728            AccountMeta::new_readonly(system_program::ID, false),
729        ],
730        data: CreateReferral {}.to_bytes(),
731    }
732}
733
734/// Claim pending referral rewards (both SOL and OIL).
735
736pub fn claim_referral(signer: Pubkey) -> Instruction {
737    let referral_address = referral_pda(signer).0;
738    let referral_oil_address = get_associated_token_address(&referral_address, &MINT_ADDRESS);
739    let recipient_oil_address = get_associated_token_address(&signer, &MINT_ADDRESS);
740    Instruction {
741        program_id: crate::ID,
742        accounts: vec![
743            AccountMeta::new(signer, true),
744            AccountMeta::new(referral_address, false),
745            AccountMeta::new(referral_oil_address, false), // Referral account's OIL ATA
746            AccountMeta::new(MINT_ADDRESS, false),
747            AccountMeta::new(recipient_oil_address, false), // Recipient's OIL ATA
748            AccountMeta::new_readonly(system_program::ID, false),
749            AccountMeta::new_readonly(spl_token::ID, false),
750            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
751        ],
752        data: ClaimReferral {}.to_bytes(),
753    }
754}
755
756/// Direct solo bid on an auction well (seize ownership).
757/// The bid amount is calculated on-chain as current_price + 1 lamport.
758/// User must have enough SOL in their wallet to cover the bid.
759/// 
760/// Account structure:
761/// - Base: signer, square, fund (optional), auction, treasury, treasury_tokens, mint, mint_authority, mint_program, staking_pool, fee_collector, config, token_program, system_program
762/// - If previous owner exists (optional): [previous_owner_miner, previous_owner]
763pub fn place_bid(
764    signer: Pubkey,
765    square_id: u64,
766    fee_collector: Pubkey,
767    pool_account: Option<Pubkey>, // Pool account for current epoch (optional, if pool exists)
768    previous_owner_miner: Option<Pubkey>, // Previous owner's miner PDA (if previous owner exists)
769    previous_owner: Option<Pubkey>, // Previous owner pubkey (if previous owner exists)
770    referrer: Option<Pubkey>, // Optional referrer pubkey for new miners
771) -> Instruction {
772    let well_address = well_pda(square_id).0;
773    let auction_address = auction_pda().0;
774    let treasury_address = treasury_pda().0;
775    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
776    let staking_pool_address = pool_pda().0;
777    let config_address = config_pda().0;
778    let mint_authority_address = oil_mint_api::state::authority_pda().0;
779    
780    let mut accounts = vec![
781        AccountMeta::new(signer, true),
782        AccountMeta::new(well_address, false),
783    ];
784    
785    // Add pool account if provided (for current epoch)
786    if let Some(pool_pubkey) = pool_account {
787        accounts.push(AccountMeta::new(pool_pubkey, false));
788    } else {
789        // Add placeholder (account order must be consistent)
790        accounts.push(AccountMeta::new_readonly(system_program::ID, false)); // Placeholder, will be ignored
791    }
792    
793    accounts.extend_from_slice(&[
794        AccountMeta::new(auction_address, false),
795        AccountMeta::new(treasury_address, false),
796        AccountMeta::new(treasury_tokens_address, false),
797        AccountMeta::new(MINT_ADDRESS, false),
798        AccountMeta::new(mint_authority_address, false),
799        AccountMeta::new_readonly(oil_mint_api::ID, false),
800        AccountMeta::new(staking_pool_address, false),
801        AccountMeta::new(fee_collector, false),
802        AccountMeta::new(config_address, false),
803        AccountMeta::new_readonly(spl_token::ID, false),
804        AccountMeta::new_readonly(system_program::ID, false),
805        AccountMeta::new_readonly(crate::ID, false), // oil_program
806    ]);
807    
808    // Add previous owner accounts if provided
809    if let (Some(miner_pubkey), Some(owner_pubkey)) = (previous_owner_miner, previous_owner) {
810        accounts.push(AccountMeta::new(miner_pubkey, false));
811        accounts.push(AccountMeta::new(owner_pubkey, false));
812    }
813    
814    // Add referral account if referrer is provided
815    if let Some(referrer_pubkey) = referrer {
816        let referral_address = referral_pda(referrer_pubkey).0;
817        accounts.push(AccountMeta::new(referral_address, false));
818    }
819    
820    Instruction {
821        program_id: crate::ID,
822        accounts,
823        data: instruction::PlaceBid {
824            square_id: square_id.to_le_bytes(),
825            referrer: referrer.unwrap_or(Pubkey::default()).to_bytes(),
826        }
827        .to_bytes(),
828    }
829}
830
831/// Claim auction-based OIL rewards
832/// - OIL rewards: from current ownership and previous ownership (pre-minted)
833/// 
834/// Account structure:
835/// - 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
836/// - 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)
837pub fn claim_auction_oil(
838    signer: Pubkey,
839    well_mask: u8, // Bitmask: bit 0 = well 0, bit 1 = well 1, etc.
840    well_accounts: [Option<Pubkey>; 4], // Well PDAs for wells 0-3 (required for wells in mask)
841    auction_pool_accounts: Option<[Option<Pubkey>; 4]>, // Auction Pool PDAs for wells 0-3 (optional, for pool contributors)
842    bid_accounts: Option<[Option<Pubkey>; 4]>, // Bid PDAs for wells 0-3 (required for pool contributors, must include epoch_id in PDA)
843) -> Instruction {
844    let miner_address = miner_pda(signer).0;
845    let auction_address = auction_pda().0;
846    let treasury_address = treasury_pda().0;
847    let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
848    let recipient_address = get_associated_token_address(&signer, &MINT_ADDRESS);
849    let mint_authority_address = oil_mint_api::state::authority_pda().0;
850    
851    let mut accounts = vec![
852        AccountMeta::new(signer, true),
853        AccountMeta::new(miner_address, false),
854    ];
855    
856    // Add well accounts for wells in mask (all 4 required, but only wells in mask are used)
857    for well_opt in well_accounts.iter() {
858        if let Some(well_pubkey) = well_opt {
859            accounts.push(AccountMeta::new(*well_pubkey, false));
860        } else {
861            // Use a placeholder account if well is not provided
862            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
863        }
864    }
865    
866    // Add auction pool accounts if provided (optional, for pool contributors)
867    if let Some(auction_pool_pdas) = auction_pool_accounts {
868        for auction_pool_pda_opt in auction_pool_pdas.iter() {
869            if let Some(auction_pool_pubkey) = auction_pool_pda_opt {
870                accounts.push(AccountMeta::new(*auction_pool_pubkey, false));
871            } else {
872                // Use a placeholder account if auction pool is not provided
873                accounts.push(AccountMeta::new_readonly(system_program::ID, false));
874            }
875        }
876    } else {
877        // Add 4 placeholder accounts if auction_pool_accounts is None
878        for _ in 0..4 {
879            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
880        }
881    }
882    
883    accounts.extend_from_slice(&[
884        AccountMeta::new(auction_address, false), // Must be writable for auction_program_log CPI
885        AccountMeta::new(treasury_address, false),
886        AccountMeta::new(treasury_tokens_address, false),
887        AccountMeta::new(MINT_ADDRESS, false), // Must be writable for mint_oil CPI
888        AccountMeta::new(mint_authority_address, false), // Must be writable for mint_oil CPI
889        AccountMeta::new_readonly(oil_mint_api::ID, false),
890        AccountMeta::new(recipient_address, false),
891        AccountMeta::new_readonly(spl_token::ID, false),
892        AccountMeta::new_readonly(spl_associated_token_account::ID, false),
893        AccountMeta::new_readonly(system_program::ID, false),
894        AccountMeta::new_readonly(crate::ID, false), // oil_program
895    ]);
896    
897    // Add bid accounts if provided (for pool contributors)
898    if let Some(bid_pdas) = bid_accounts {
899        for bid_pda_opt in bid_pdas.iter() {
900            if let Some(bid_pubkey) = bid_pda_opt {
901                accounts.push(AccountMeta::new(*bid_pubkey, false));
902            }
903        }
904    }
905    
906    Instruction {
907        program_id: crate::ID,
908        accounts,
909        data: ClaimAuctionOIL {
910            well_mask,
911        }
912        .to_bytes(),
913    }
914}
915
916/// Claim auction-based SOL rewards
917/// - SOL rewards: from being outbid and refunds from abandoned pools
918/// 
919/// Account structure:
920/// - 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
921/// - Bid accounts (one per well, required for refunds): [bid_0, bid_1, bid_2, bid_3] (must include epoch_id in PDA)
922pub fn claim_auction_sol(
923    signer: Pubkey,
924    well_accounts: [Option<Pubkey>; 4], // Well PDAs for wells 0-3 (for checking abandoned pools)
925    auction_pool_accounts: Option<[Option<Pubkey>; 4]>, // Auction Pool PDAs for wells 0-3 (optional, for refunds)
926    bid_accounts: Option<[Option<Pubkey>; 4]>, // Bid PDAs for wells 0-3 (required for refunds, must include epoch_id in PDA)
927) -> Instruction {
928    let miner_address = miner_pda(signer).0;
929    let (auction_address, _) = auction_pda();
930    let treasury_address = treasury_pda().0;
931    
932    let mut accounts = vec![
933        AccountMeta::new(signer, true),
934        AccountMeta::new(miner_address, false),
935    ];
936    
937    // Add well accounts (all 4 wells for checking abandoned pools)
938    for well_opt in well_accounts.iter() {
939        if let Some(well_pubkey) = well_opt {
940            accounts.push(AccountMeta::new(*well_pubkey, false));
941        } else {
942            // Use a placeholder account if well is not provided
943            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
944        }
945    }
946    
947    // Add auction pool accounts if provided (optional, for refunds)
948    if let Some(auction_pool_pdas) = auction_pool_accounts {
949        for auction_pool_pda_opt in auction_pool_pdas.iter() {
950            if let Some(auction_pool_pubkey) = auction_pool_pda_opt {
951                accounts.push(AccountMeta::new(*auction_pool_pubkey, false));
952            } else {
953                // Use a placeholder account if auction pool is not provided
954                accounts.push(AccountMeta::new_readonly(system_program::ID, false));
955            }
956        }
957    } else {
958        // Add 4 placeholder accounts if auction_pool_accounts is None
959        for _ in 0..4 {
960            accounts.push(AccountMeta::new_readonly(system_program::ID, false));
961        }
962    }
963    
964    accounts.extend_from_slice(&[
965        AccountMeta::new(auction_address, false), // Must be writable for auction_program_log CPI
966        AccountMeta::new(treasury_address, false),
967        AccountMeta::new_readonly(system_program::ID, false),
968        AccountMeta::new_readonly(crate::ID, false), // oil_program
969    ]);
970    
971    // Add bid accounts if provided (for refunds)
972    if let Some(bid_pdas) = bid_accounts {
973        for bid_pda_opt in bid_pdas.iter() {
974            if let Some(bid_pubkey) = bid_pda_opt {
975                accounts.push(AccountMeta::new(*bid_pubkey, false));
976            }
977        }
978    }
979    
980    Instruction {
981        program_id: crate::ID,
982        accounts,
983        data: ClaimAuctionSOL {
984            _reserved: 0,
985        }
986        .to_bytes(),
987    }
988}
989
990pub fn claim_seeker(signer: Pubkey, mint: Pubkey) -> Instruction {
991    let seeker_address = seeker_pda(mint).0;
992    let token_account_address = get_associated_token_address(&signer, &mint);
993    Instruction {
994        program_id: crate::ID,
995        accounts: vec![
996            AccountMeta::new(signer, true),
997            AccountMeta::new_readonly(mint, false),
998            AccountMeta::new(seeker_address, false),
999            AccountMeta::new(token_account_address, false),
1000            AccountMeta::new_readonly(system_program::ID, false),
1001        ],
1002        data: ClaimSeeker {}.to_bytes(),
1003    }
1004}