streak-api 0.1.3

API for interacting with the STREAK directional markets protocol on Solana
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Off-chain instruction builders (Ore **`api::sdk`** style): fixed account order mirrors on-chain **`process_*`** comment headers.
//!
//! For **`ClaimPositionFee`**, pass the **`cp\_amm`** account tail (13 metas, Anchor `ClaimPositionFeeCtx` order).

use solana_program::pubkey::Pubkey;
use spl_associated_token_account::{
    get_associated_token_address,
    get_associated_token_address_with_program_id,
};
use steel::*;

use crate::{
    consts::{
        EXECUTOR_ADDRESS, EXECUTOR_PAY_DAILY_JACKPOT, EXECUTOR_PAY_WEEKLY_JACKPOT, FEE_COLLECTOR,
        USDC_MAINNET_MINT,
    },
    instruction::*,
    state::{Ledger, Market, Position, Treasury},
};

#[inline(always)]
pub fn program_id() -> Pubkey {
    crate::ID
}

#[inline(always)]
fn executor_treasury_ix_bytes(kind: u8, pool: u8, series_id: u16, amount: u64) -> Vec<u8> {
    ExecutorTreasury {
        kind,
        pool,
        series_id: series_id.to_le_bytes(),
        _pad_ix: [0; 4],
        amount: amount.to_le_bytes(),
    }
    .to_bytes()
}

/// SPL USDC ATA (**classic** SPL Token program). For Token-2022 mints use [`treasury_usdc_ata_with_token_program`] / [`associated_usdc_ata`] with the mint owner's program id (for example **`spl_token_2022`**'s **`id()`**).
#[inline(always)]
pub fn treasury_usdc_ata() -> Pubkey {
    get_associated_token_address(&Treasury::pda().0, &USDC_MAINNET_MINT)
}

#[inline(always)]
pub fn treasury_usdc_ata_with_token_program(token_program: &Pubkey) -> Pubkey {
    get_associated_token_address_with_program_id(&Treasury::pda().0, &USDC_MAINNET_MINT, token_program)
}

#[inline(always)]
pub fn associated_usdc_ata(owner: Pubkey, token_program: &Pubkey) -> Pubkey {
    get_associated_token_address_with_program_id(&owner, &USDC_MAINNET_MINT, token_program)
}

/// SPL USDC ATA for **`owner`** (classic Token program — mainnet Circle USDC).
#[inline(always)]
pub fn usdc_ata(owner: Pubkey) -> Pubkey {
    get_associated_token_address(&owner, &USDC_MAINNET_MINT)
}

/// Team-fee SPL destination used by **`BuyTickets`** / **`ClaimPositionFee`** (classic Token mint).
#[inline(always)]
pub fn fee_collector_usdc_ata() -> Pubkey {
    get_associated_token_address(&FEE_COLLECTOR, &USDC_MAINNET_MINT)
}

#[inline(always)]
pub fn fee_collector_usdc_ata_with_token_program(token_program: &Pubkey) -> Pubkey {
    get_associated_token_address_with_program_id(&FEE_COLLECTOR, &USDC_MAINNET_MINT, token_program)
}

/// **`previous_market`** / **`previous_position`** (**`series_id`**, **`period - 1`**). For **`period == 0`** use placeholders (**no signer**).
#[inline(always)]
pub fn buy_tickets_or_place_bet_previous_accounts(
    user: Pubkey,
    series_id: u16,
    period: u64,
) -> (Pubkey, Pubkey) {
    match period.checked_sub(1) {
        Some(p) => (
            Market::pda(series_id, p).0,
            Position::pda(series_id, p, user).0,
        ),
        None => (system_program::ID, system_program::ID),
    }
}

// let [payer_info, treasury_info, mint_info, treasury_ata, system_program, token_program, ata_program]

/// One-time **`Initialize`**. **`payer`** must be [`ADMIN_ADDRESS`](crate::consts::ADMIN_ADDRESS).
///
/// **`token_program`** must be the mint owner's program id (`spl_token` or `spl_token_2022` for the configured USDC mint).
pub fn initialize(payer: Pubkey, token_program: Pubkey) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let mint_address = USDC_MAINNET_MINT;
    let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(payer, true),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new_readonly(mint_address, false),
            AccountMeta::new(treasury_ata, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(token_program, false),
            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
        ],
        data: Initialize {}.to_bytes(),
    }
}

// let [user, treasury_info, ledger_info, mint_info, user_ata, treasury_ata, team_ata, system_program, token_program, ata_program, previous_market_info, previous_position_info]

/// Builds **`BuyTickets`**.
pub fn buy_tickets(
    user: Pubkey,
    series_id: u16,
    period: u64,
    amount: u64,
    token_program: Pubkey,
) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let ledger_address = Ledger::pda(user).0;
    let mint_address = USDC_MAINNET_MINT;
    let user_ata = associated_usdc_ata(user, &token_program);
    let treasury_ata_addr = treasury_usdc_ata_with_token_program(&token_program);
    let team_ata_addr = fee_collector_usdc_ata_with_token_program(&token_program);
    let (prev_mkt, prev_pos) =
        buy_tickets_or_place_bet_previous_accounts(user, series_id, period);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(user, true),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(ledger_address, false),
            AccountMeta::new_readonly(mint_address, false),
            AccountMeta::new(user_ata, false),
            AccountMeta::new(treasury_ata_addr, false),
            AccountMeta::new(team_ata_addr, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(token_program, false),
            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
            AccountMeta::new_readonly(prev_mkt, false),
            AccountMeta::new_readonly(prev_pos, false),
        ],
        data: BuyTickets {
            series_id: series_id.to_le_bytes(),
            _pad_ix: [0; 6],
            period: period.to_le_bytes(),
            amount: amount.to_le_bytes(),
        }
        .to_bytes(),
    }
}

// let [user, ledger_info, market_info, position_info, previous_market_info, previous_position_info, system_program]

/// Builds **`PlaceBet`**.
///
/// **`ticket_units`** may be **0** in the live window to activate a **`Position::STATE_COMMITTED_PREOPEN`** stake (**merge into `Market::total_*`** without debiting **`Ledger`**). See **`PlaceBet`** ix docs on-chain.
pub fn place_bet(
    user: Pubkey,
    series_id: u16,
    period: u64,
    ticket_units: u64,
    side: u8,
) -> Instruction {
    let ledger_address = Ledger::pda(user).0;
    let treasury_address = Treasury::pda().0;
    let market_address = Market::pda(series_id, period).0;
    let position_address = Position::pda(series_id, period, user).0;
    let (prev_mkt, prev_pos) =
        buy_tickets_or_place_bet_previous_accounts(user, series_id, period);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(user, true),
            AccountMeta::new(ledger_address, false),
            AccountMeta::new_readonly(treasury_address, false),
            AccountMeta::new(market_address, false),
            AccountMeta::new(position_address, false),
            AccountMeta::new_readonly(prev_mkt, false),
            AccountMeta::new_readonly(prev_pos, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: PlaceBet {
            series_id: series_id.to_le_bytes(),
            _pad_ix: [0; 6],
            period: period.to_le_bytes(),
            ticket_units: ticket_units.to_le_bytes(),
            side,
            _pad: [0; 7],
        }
        .to_bytes(),
    }
}

// let [executor, treasury_readonly, market, position]

/// **`ExecutorTreasury`** **`EXECUTOR_KIND_MERGE_COMMITTED_POSITION`**: merges **[`crate::state::Position::STATE_COMMITTED_PREOPEN`]** → **`STATE_PENDING`** + **`Market::total_*`**. **`pool`** **`0`**, **`amount`** **`0`**, **`series_id`** in payload.
pub fn executor_treasury_merge_committed(
    executor: Pubkey,
    series_id: u16,
    period: u64,
    position_owner: Pubkey,
) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let market_address = Market::pda(series_id, period).0;
    let position_address = Position::pda(series_id, period, position_owner).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(executor, true),
            AccountMeta::new_readonly(treasury_address, false),
            AccountMeta::new(market_address, false),
            AccountMeta::new(position_address, false),
        ],
        data: executor_treasury_ix_bytes(
            EXECUTOR_KIND_MERGE_COMMITTED_POSITION,
            0,
            series_id,
            0,
        ),
    }
}

/// Builds **`AdminInstantSettlement`**. **`authority`** must be [`crate::consts::EXECUTOR_ADDRESS`].
/// Subsidy is computed on-chain (see [`crate::instruction::AdminInstantSettlement`]).
pub fn settle_market(
    authority: Pubkey,
    series_id: u16,
    period: u64,
    pyth_price_feed: Pubkey,
) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let market_address = Market::pda(series_id, period).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(market_address, false),
            AccountMeta::new_readonly(pyth_price_feed, false),
        ],
        data: AdminInstantSettlement {
            series_id: series_id.to_le_bytes(),
            _pad_ix: [0; 6],
            period: period.to_le_bytes(),
        }
        .to_bytes(),
    }
}

/// Builds **`InitMarket`**. **`authority`** must be [`crate::consts::EXECUTOR_ADDRESS`] for **create** and **open-anchor crank**.
pub fn init_market(
    authority: Pubkey,
    series_id: u16,
    period: u64,
    open_ts: i64,
    close_ts: i64,
    pyth_price_feed: Pubkey,
    rules_hash: [u8; 32],
) -> Instruction {
    let market_address = Market::pda(series_id, period).0;
    let treasury_address = Treasury::pda().0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(market_address, false),
            AccountMeta::new_readonly(treasury_address, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(pyth_price_feed, false),
        ],
        data: InitMarket {
            series_id: series_id.to_le_bytes(),
            _pad_ix: [0; 6],
            period: period.to_le_bytes(),
            open_ts: open_ts.to_le_bytes(),
            close_ts: close_ts.to_le_bytes(),
            pyth_price_feed: pyth_price_feed.to_bytes(),
            rules_hash,
        }
        .to_bytes(),
    }
}

// let [executor, treasury_info, treasury_ata, recipient_ata, mint_info, token_program, market_info, position_info, ledger_info]

/// **`recipient_ata`** must be **[`associated_usdc_ata`](associated_usdc_ata)(winning_authority, &token_program)**.
pub fn distribute_market_reward(
    executor: Pubkey,
    winning_authority: Pubkey,
    series_id: u16,
    market_period: u64,
    token_program: Pubkey,
) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let treasury_ata_addr = treasury_usdc_ata_with_token_program(&token_program);
    let recipient_ata = associated_usdc_ata(winning_authority, &token_program);
    let mint_address = USDC_MAINNET_MINT;
    let market_address = Market::pda(series_id, market_period).0;
    let position_address = Position::pda(series_id, market_period, winning_authority).0;
    let ledger_address = Ledger::pda(winning_authority).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(executor, true),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(treasury_ata_addr, false),
            AccountMeta::new(recipient_ata, false),
            AccountMeta::new_readonly(mint_address, false),
            AccountMeta::new_readonly(token_program, false),
            AccountMeta::new_readonly(market_address, false),
            AccountMeta::new(position_address, false),
            AccountMeta::new(ledger_address, false),
        ],
        data: executor_treasury_ix_bytes(EXECUTOR_KIND_DISTRIBUTE_MARKET_REWARD, 0, series_id, 0),
    }
}

/// Convenience: fixed executor pubkey ([`EXECUTOR_ADDRESS`](crate::consts::EXECUTOR_ADDRESS)).
pub fn distribute_market_reward_default_executor(
    winning_authority: Pubkey,
    series_id: u16,
    market_period: u64,
    token_program: Pubkey,
) -> Instruction {
    distribute_market_reward(
        EXECUTOR_ADDRESS,
        winning_authority,
        series_id,
        market_period,
        token_program,
    )
}

/// **`ExecutorTreasury`** **`EXECUTOR_KIND_MARKET_LINE_RELEASE`**: after **`DISTRIBUTE`** batch(es), **`Treasury`** advances **`next_period[slot]`** toward **`≥ settled_period + 1`** (**`max`** with prior cursor).
///
/// **Accounts:** **`executor`**, **`treasury`**, **`market`** (readonly).
pub fn release_market_line(executor: Pubkey, series_id: u16, period: u64) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let market_address = Market::pda(series_id, period).0;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(executor, true),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new_readonly(market_address, false),
        ],
        data: executor_treasury_ix_bytes(EXECUTOR_KIND_MARKET_LINE_RELEASE, 0, series_id, period),
    }
}

#[inline(always)]
pub fn release_market_line_default_executor(series_id: u16, period: u64) -> Instruction {
    release_market_line(EXECUTOR_ADDRESS, series_id, period)
}

// let [executor, treasury_info, treasury_ata, mint_info, token_program, recipient_ata]

/// **`pool`**: [`EXECUTOR_PAY_DAILY_JACKPOT`](crate::consts::EXECUTOR_PAY_DAILY_JACKPOT) or [`EXECUTOR_PAY_WEEKLY_JACKPOT`](crate::consts::EXECUTOR_PAY_WEEKLY_JACKPOT).
///
/// **`recipient_ata`** is any SPL token account holding USDC (**mint + program-id** enforced on-chain); use **[`associated_usdc_ata`]** when building the ATA address.
pub fn executor_treasury_pay_recipient_account(
    executor: Pubkey,
    recipient_ata: Pubkey,
    amount: u64,
    pool: u8,
    token_program: Pubkey,
) -> Instruction {
    let treasury_address = Treasury::pda().0;
    let treasury_ata_addr = treasury_usdc_ata_with_token_program(&token_program);
    let mint_address = USDC_MAINNET_MINT;
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(executor, true),
            AccountMeta::new(treasury_address, false),
            AccountMeta::new(treasury_ata_addr, false),
            AccountMeta::new_readonly(mint_address, false),
            AccountMeta::new_readonly(token_program, false),
            AccountMeta::new(recipient_ata, false),
        ],
        data: executor_treasury_ix_bytes(EXECUTOR_KIND_JACKPOT_PAY, pool, 0, amount),
    }
}

/// Same as **`executor_treasury_pay_recipient_account`** with **`recipient_ata`** = **[`associated_usdc_ata`](associated_usdc_ata)(recipient_owner, token_program)**.
#[inline(always)]
pub fn executor_treasury_pay(
    executor: Pubkey,
    recipient_owner: Pubkey,
    amount: u64,
    pool: u8,
    token_program: Pubkey,
) -> Instruction {
    executor_treasury_pay_recipient_account(
        executor,
        associated_usdc_ata(recipient_owner, &token_program),
        amount,
        pool,
        token_program,
    )
}

/// Daily jackpot pool preset.
#[inline(always)]
pub fn executor_treasury_pay_daily(
    executor: Pubkey,
    recipient_owner: Pubkey,
    amount: u64,
    token_program: Pubkey,
) -> Instruction {
    executor_treasury_pay_recipient_account(
        executor,
        associated_usdc_ata(recipient_owner, &token_program),
        amount,
        EXECUTOR_PAY_DAILY_JACKPOT,
        token_program,
    )
}

/// Weekly jackpot pool preset.
#[inline(always)]
pub fn executor_treasury_pay_weekly(
    executor: Pubkey,
    recipient_owner: Pubkey,
    amount: u64,
    token_program: Pubkey,
) -> Instruction {
    executor_treasury_pay_recipient_account(
        executor,
        associated_usdc_ata(recipient_owner, &token_program),
        amount,
        EXECUTOR_PAY_WEEKLY_JACKPOT,
        token_program,
    )
}

// Streak indices 0..8: executor, owner, treasury, treasury_ata, fee_collector_ata, mint, system_program, token_program, owner_usdc_ata
// Indices 9..22: cp_amm ClaimPositionFeeCtx (see `programs/program/src/admin/claim_position_fee.rs`)

/// CPI prefix metas only; caller appends **`cp\_amm`** accounts.
pub fn claim_position_fee_accounts_prefix(
    owner: Pubkey,
    owner_usdc_ata: Pubkey,
    token_program: Pubkey,
) -> [AccountMeta; 9] {
    let executor = EXECUTOR_ADDRESS;
    let treasury_address = Treasury::pda().0;
    let treasury_ata_addr = treasury_usdc_ata_with_token_program(&token_program);
    let team_ata_addr = fee_collector_usdc_ata_with_token_program(&token_program);
    [
        AccountMeta::new(executor, true),
        AccountMeta::new(owner, true),
        AccountMeta::new(treasury_address, false),
        AccountMeta::new(treasury_ata_addr, false),
        AccountMeta::new(team_ata_addr, false),
        AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
        AccountMeta::new_readonly(system_program::ID, false),
        AccountMeta::new_readonly(token_program, false),
        AccountMeta::new(owner_usdc_ata, false),
    ]
}

pub fn claim_position_fee(
    owner: Pubkey,
    owner_usdc_ata: Pubkey,
    cp_amm_accounts: &[AccountMeta],
    token_program: Pubkey,
) -> Instruction {
    let prefix = claim_position_fee_accounts_prefix(owner, owner_usdc_ata, token_program);
    let mut accounts = Vec::with_capacity(9 + cp_amm_accounts.len());
    accounts.extend_from_slice(&prefix);
    accounts.extend_from_slice(cp_amm_accounts);
    Instruction {
        program_id: crate::ID,
        accounts,
        data: ClaimPositionFee {}.to_bytes(),
    }
}