Skip to main content

streak_api/
sdk.rs

1//! Off-chain instruction builders. Fixed account order mirrors on-chain `process_*` comments.
2
3use solana_program::pubkey::Pubkey;
4use spl_associated_token_account::{
5    get_associated_token_address, get_associated_token_address_with_program_id,
6};
7use steel::*;
8
9use crate::{
10    consts::{EXECUTOR_ADDRESS, FEE_COLLECTOR, USDC_MAINNET_MINT},
11    instruction::{AdminPayout, AdminRouteFees, BuyTicket, Initialize, PlaceBet},
12    state::{Treasury, User},
13};
14
15#[inline(always)]
16pub fn program_id() -> Pubkey {
17    crate::ID
18}
19
20// ── ATA helpers ──────────────────────────────────────────────────────────────
21
22pub fn treasury_usdc_ata() -> Pubkey {
23    get_associated_token_address(&Treasury::pda().0, &USDC_MAINNET_MINT)
24}
25
26pub fn treasury_usdc_ata_with_token_program(token_program: &Pubkey) -> Pubkey {
27    get_associated_token_address_with_program_id(
28        &Treasury::pda().0,
29        &USDC_MAINNET_MINT,
30        token_program,
31    )
32}
33
34pub fn associated_usdc_ata(owner: Pubkey, token_program: &Pubkey) -> Pubkey {
35    get_associated_token_address_with_program_id(&owner, &USDC_MAINNET_MINT, token_program)
36}
37
38pub fn usdc_ata(owner: Pubkey) -> Pubkey {
39    get_associated_token_address(&owner, &USDC_MAINNET_MINT)
40}
41
42pub fn fee_collector_usdc_ata_with_token_program(token_program: &Pubkey) -> Pubkey {
43    get_associated_token_address_with_program_id(&FEE_COLLECTOR, &USDC_MAINNET_MINT, token_program)
44}
45
46// ── Instruction builders ─────────────────────────────────────────────────────
47
48/// One-time treasury initialization.
49pub fn initialize(payer: Pubkey, token_program: Pubkey) -> Instruction {
50    let treasury_address = Treasury::pda().0;
51    let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
52    Instruction {
53        program_id: crate::ID,
54        accounts: vec![
55            AccountMeta::new(payer, true),
56            AccountMeta::new(treasury_address, false),
57            AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
58            AccountMeta::new(treasury_ata, false),
59            AccountMeta::new_readonly(system_program::ID, false),
60            AccountMeta::new_readonly(token_program, false),
61            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
62        ],
63        data: Initialize {}.to_bytes(),
64    }
65}
66
67/// Purchase gaming credits (tickets) with USDC.
68///
69/// Splits `amount` on-chain: 95% → treasury_ata (daily/weekly/buyback), 5% → fee_collector_ata
70/// directly (user signs both transfers; team share never touches the treasury).
71/// Credits the user's `User` PDA `balance` by the full `amount`.
72///
73/// **Pre-requisite:** the `FEE_COLLECTOR` USDC ATA must exist before calling this.
74pub fn buy_ticket(user: Pubkey, amount: u64, token_program: Pubkey) -> Instruction {
75    let user_ata = associated_usdc_ata(user, &token_program);
76    let treasury_address = Treasury::pda().0;
77    let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
78    let user_pda = User::pda(&user).0;
79    let fee_collector_ata = fee_collector_usdc_ata_with_token_program(&token_program);
80    Instruction {
81        program_id: crate::ID,
82        accounts: vec![
83            AccountMeta::new(user, true),
84            AccountMeta::new(user_ata, false),
85            AccountMeta::new(treasury_address, false),
86            AccountMeta::new(treasury_ata, false),
87            AccountMeta::new(user_pda, false),
88            AccountMeta::new(fee_collector_ata, false),
89            AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
90            AccountMeta::new_readonly(token_program, false),
91            AccountMeta::new_readonly(system_program::ID, false),
92        ],
93        data: BuyTicket {
94            amount: amount.to_le_bytes(),
95        }
96        .to_bytes(),
97    }
98}
99
100/// Spend ticket credits on a prediction. User signs; `User.balance` is debited on-chain.
101/// The indexer reads the `BetPlaced` event to record the bet in the DB.
102pub fn place_bet(
103    user: Pubkey,
104    amount: u64,
105    side: u8,
106    series_id: u16,
107    period: u64,
108) -> Instruction {
109    let user_pda = User::pda(&user).0;
110    Instruction {
111        program_id: crate::ID,
112        accounts: vec![
113            AccountMeta::new(user, true),
114            AccountMeta::new(user_pda, false),
115        ],
116        data: PlaceBet {
117            amount: amount.to_le_bytes(),
118            side,
119            _pad: [0; 1],
120            series_id: series_id.to_le_bytes(),
121            _pad2: [0; 4],
122            period: period.to_le_bytes(),
123        }
124        .to_bytes(),
125    }
126}
127
128/// Route `amount` µUSDC of claimed DBC / DAMM v2 fees into the treasury.
129pub fn route_fees(executor: Pubkey, amount: u64, token_program: Pubkey) -> Instruction {
130    let treasury_address = Treasury::pda().0;
131    let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
132    let executor_ata = associated_usdc_ata(executor, &token_program);
133    let fee_collector_ata = fee_collector_usdc_ata_with_token_program(&token_program);
134    Instruction {
135        program_id: crate::ID,
136        accounts: vec![
137            AccountMeta::new(executor, true),
138            AccountMeta::new(executor_ata, false),
139            AccountMeta::new(treasury_address, false),
140            AccountMeta::new(treasury_ata, false),
141            AccountMeta::new(fee_collector_ata, false),
142            AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
143            AccountMeta::new_readonly(token_program, false),
144        ],
145        data: AdminRouteFees {
146            amount: amount.to_le_bytes(),
147        }
148        .to_bytes(),
149    }
150}
151
152/// Convenience: `route_fees` signed by the canonical `EXECUTOR_ADDRESS`.
153pub fn route_fees_default_executor(amount: u64, token_program: Pubkey) -> Instruction {
154    route_fees(EXECUTOR_ADDRESS, amount, token_program)
155}
156
157/// Pay `amount` µUSDC from the treasury to `recipient_ata`.
158pub fn payout(
159    executor: Pubkey,
160    recipient_ata: Pubkey,
161    amount: u64,
162    series_id: u16,
163    period: u64,
164    token_program: Pubkey,
165) -> Instruction {
166    let treasury_address = Treasury::pda().0;
167    let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
168    Instruction {
169        program_id: crate::ID,
170        accounts: vec![
171            AccountMeta::new(executor, true),
172            AccountMeta::new(treasury_address, false),
173            AccountMeta::new(treasury_ata, false),
174            AccountMeta::new(recipient_ata, false),
175            AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
176            AccountMeta::new_readonly(token_program, false),
177        ],
178        data: AdminPayout {
179            amount: amount.to_le_bytes(),
180            series_id: series_id.to_le_bytes(),
181            _pad_ix: [0; 6],
182            period: period.to_le_bytes(),
183        }
184        .to_bytes(),
185    }
186}