txline 0.4.0

Devnet-only Rust SDK for TxLINE.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Low-level Devnet TxODDS public trading instruction builders.

use solana_sdk::instruction::{AccountMeta, Instruction};
use solana_sdk::pubkey::Pubkey;

use super::codec::{
    encode_binary_expression, encode_option, encode_proof_vec, encode_scores_batch_summary,
    encode_stat_term, encode_trader_predicate, put_bool, put_i64, put_u16, put_u32, put_u64,
};
use crate::Result;
use crate::validation::legacy::{FixtureSummaryInput, StatTermInput};
use crate::validation::proof::ProofNode;
use crate::validation::strategy::{BinaryExpression, TraderPredicate};

pub const CREATE_INTENT_DISCRIMINATOR: [u8; 8] = [216, 214, 79, 121, 23, 194, 96, 104];
pub const CREATE_TRADE_DISCRIMINATOR: [u8; 8] = [183, 82, 24, 245, 248, 30, 204, 246];
pub const EXECUTE_MATCH_DISCRIMINATOR: [u8; 8] = [76, 47, 91, 223, 20, 10, 147, 232];
pub const CLOSE_INTENT_DISCRIMINATOR: [u8; 8] = [112, 245, 154, 249, 57, 126, 54, 122];
pub const SETTLE_TRADE_DISCRIMINATOR: [u8; 8] = [252, 176, 98, 248, 73, 123, 8, 157];
pub const SETTLE_MATCHED_TRADE_DISCRIMINATOR: [u8; 8] = [191, 233, 149, 116, 32, 239, 18, 65];
pub const CLAIM_VIA_RESOLUTION_DISCRIMINATOR: [u8; 8] = [98, 206, 250, 87, 151, 135, 162, 181];
pub const CLAIM_BATCH_LEGACY_DISCRIMINATOR: [u8; 8] = [254, 101, 89, 255, 169, 75, 207, 66];
pub const REFUND_BATCH_DISCRIMINATOR: [u8; 8] = [227, 54, 194, 2, 78, 8, 104, 29];
pub const AUDIT_TRADE_RESULT_DISCRIMINATOR: [u8; 8] = [50, 242, 243, 5, 209, 75, 76, 91];

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateIntentAccounts {
    pub maker: Pubkey,
    pub order_intent: Pubkey,
    pub intent_vault: Pubkey,
    pub maker_token_account: Pubkey,
    pub token_mint: Pubkey,
    pub token_treasury_pda: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateIntentParams {
    pub intent_id: u64,
    pub terms_hash: [u8; 32],
    pub deposit_amount: u64,
    pub expiration_ts: i64,
    pub claim_period: u16,
    pub fixture_id: i64,
}

/// Account order:
/// 0. `[writable, signer]` maker
/// 1. `[writable]` order_intent
/// 2. `[writable]` intent_vault
/// 3. `[writable]` maker_token_account
/// 4. `[]` token_mint
/// 5. `[]` token_treasury_pda
/// 6. `[]` token_program
/// 7. `[]` system_program
pub fn create_intent_instruction(
    program_id: Pubkey,
    accounts: CreateIntentAccounts,
    params: CreateIntentParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&CREATE_INTENT_DISCRIMINATOR);
    put_u64(&mut data, params.intent_id);
    data.extend_from_slice(&params.terms_hash);
    put_u64(&mut data, params.deposit_amount);
    put_i64(&mut data, params.expiration_ts);
    put_u16(&mut data, params.claim_period);
    put_i64(&mut data, params.fixture_id);

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.maker, true),
            AccountMeta::new(accounts.order_intent, false),
            AccountMeta::new(accounts.intent_vault, false),
            AccountMeta::new(accounts.maker_token_account, false),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_treasury_pda, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateTradeAccounts {
    pub authority: Pubkey,
    pub trader_a: Pubkey,
    pub trader_b: Pubkey,
    pub trader_a_token_account: Pubkey,
    pub trader_b_token_account: Pubkey,
    pub trade_escrow: Pubkey,
    pub escrow_vault: Pubkey,
    pub stake_token_mint: Pubkey,
    pub token_treasury_pda: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateTradeParams {
    pub trade_id: u64,
    pub stake_a: u64,
    pub stake_b: u64,
    pub trade_terms_hash: [u8; 32],
}

/// Account order:
/// 0. `[writable, signer]` authority
/// 1. `[writable, signer]` trader_a
/// 2. `[writable, signer]` trader_b
/// 3. `[writable]` trader_a_token_account
/// 4. `[writable]` trader_b_token_account
/// 5. `[writable]` trade_escrow
/// 6. `[writable]` escrow_vault
/// 7. `[]` stake_token_mint
/// 8. `[]` token_treasury_pda
/// 9. `[]` token_program
/// 10. `[]` system_program
pub fn create_trade_instruction(
    program_id: Pubkey,
    accounts: CreateTradeAccounts,
    params: CreateTradeParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&CREATE_TRADE_DISCRIMINATOR);
    put_u64(&mut data, params.trade_id);
    put_u64(&mut data, params.stake_a);
    put_u64(&mut data, params.stake_b);
    data.extend_from_slice(&params.trade_terms_hash);

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.authority, true),
            AccountMeta::new(accounts.trader_a, true),
            AccountMeta::new(accounts.trader_b, true),
            AccountMeta::new(accounts.trader_a_token_account, false),
            AccountMeta::new(accounts.trader_b_token_account, false),
            AccountMeta::new(accounts.trade_escrow, false),
            AccountMeta::new(accounts.escrow_vault, false),
            AccountMeta::new_readonly(accounts.stake_token_mint, false),
            AccountMeta::new_readonly(accounts.token_treasury_pda, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecuteMatchAccounts {
    pub solver: Pubkey,
    pub maker_intent: Pubkey,
    pub taker_intent: Pubkey,
    pub maker_vault: Pubkey,
    pub taker_vault: Pubkey,
    pub matched_trade: Pubkey,
    pub trade_vault: Pubkey,
    pub token_mint: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecuteMatchParams {
    pub trade_id: u64,
    pub maker_stake: u64,
    pub taker_stake: u64,
}

/// Account order:
/// 0. `[writable, signer]` solver
/// 1. `[writable]` maker_intent
/// 2. `[writable]` taker_intent
/// 3. `[writable]` maker_vault
/// 4. `[writable]` taker_vault
/// 5. `[writable]` matched_trade
/// 6. `[writable]` trade_vault
/// 7. `[]` token_mint
/// 8. `[]` token_program
/// 9. `[]` system_program
pub fn execute_match_instruction(
    program_id: Pubkey,
    accounts: ExecuteMatchAccounts,
    params: ExecuteMatchParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&EXECUTE_MATCH_DISCRIMINATOR);
    put_u64(&mut data, params.trade_id);
    put_u64(&mut data, params.maker_stake);
    put_u64(&mut data, params.taker_stake);

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.solver, true),
            AccountMeta::new(accounts.maker_intent, false),
            AccountMeta::new(accounts.taker_intent, false),
            AccountMeta::new(accounts.maker_vault, false),
            AccountMeta::new(accounts.taker_vault, false),
            AccountMeta::new(accounts.matched_trade, false),
            AccountMeta::new(accounts.trade_vault, false),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CloseIntentAccounts {
    pub maker: Pubkey,
    pub authority: Pubkey,
    pub order_intent: Pubkey,
    pub intent_vault: Pubkey,
    pub maker_token_account: Pubkey,
    pub token_mint: Pubkey,
    pub token_program: Pubkey,
    pub token_treasury_pda: Pubkey,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CloseIntentParams {}

/// Account order:
/// 0. `[writable]` maker
/// 1. `[writable, signer]` authority
/// 2. `[writable]` order_intent
/// 3. `[writable]` intent_vault
/// 4. `[writable]` maker_token_account
/// 5. `[]` token_mint
/// 6. `[]` token_program
/// 7. `[]` token_treasury_pda
pub fn close_intent_instruction(
    program_id: Pubkey,
    accounts: CloseIntentAccounts,
    _params: CloseIntentParams,
) -> Result<Instruction> {
    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.maker, false),
            AccountMeta::new(accounts.authority, true),
            AccountMeta::new(accounts.order_intent, false),
            AccountMeta::new(accounts.intent_vault, false),
            AccountMeta::new(accounts.maker_token_account, false),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.token_treasury_pda, false),
        ],
        data: CLOSE_INTENT_DISCRIMINATOR.to_vec(),
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettleTradeAccounts {
    pub winner: Pubkey,
    pub daily_scores_merkle_roots: Pubkey,
    pub trade_escrow: Pubkey,
    pub escrow_vault: Pubkey,
    pub winner_token_account: Pubkey,
    pub token_mint: Pubkey,
    pub token_treasury_pda: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettleTradeParams {
    pub trade_id: u64,
    pub ts: i64,
    pub fixture_summary: FixtureSummaryInput,
    pub fixture_proof: Vec<ProofNode>,
    pub main_tree_proof: Vec<ProofNode>,
    pub predicate: TraderPredicate,
    pub stat_a: StatTermInput,
    pub stat_b: Option<StatTermInput>,
    pub op: Option<BinaryExpression>,
}

/// Account order:
/// 0. `[writable, signer]` winner
/// 1. `[]` daily_scores_merkle_roots
/// 2. `[writable]` trade_escrow
/// 3. `[writable]` escrow_vault
/// 4. `[writable]` winner_token_account
/// 5. `[]` token_mint
/// 6. `[]` token_treasury_pda
/// 7. `[]` token_program
/// 8. `[]` system_program
pub fn settle_trade_instruction(
    program_id: Pubkey,
    accounts: SettleTradeAccounts,
    params: SettleTradeParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&SETTLE_TRADE_DISCRIMINATOR);
    put_u64(&mut data, params.trade_id);
    put_i64(&mut data, params.ts);
    encode_scores_batch_summary(&mut data, &params.fixture_summary);
    encode_proof_vec(&mut data, &params.fixture_proof)?;
    encode_proof_vec(&mut data, &params.main_tree_proof)?;
    encode_trader_predicate(&mut data, &params.predicate);
    encode_stat_term(&mut data, &params.stat_a)?;
    encode_option(&mut data, params.stat_b.as_ref(), encode_stat_term)?;
    encode_option(&mut data, params.op.as_ref(), |out, op| {
        encode_binary_expression(out, op);
        Ok(())
    })?;

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.winner, true),
            AccountMeta::new_readonly(accounts.daily_scores_merkle_roots, false),
            AccountMeta::new(accounts.trade_escrow, false),
            AccountMeta::new(accounts.escrow_vault, false),
            AccountMeta::new(accounts.winner_token_account, false),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_treasury_pda, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettleMatchedTradeAccounts {
    pub winner: Pubkey,
    pub daily_scores_merkle_roots: Pubkey,
    pub matched_trade: Pubkey,
    pub trade_vault: Pubkey,
    pub winner_token_account: Pubkey,
    pub token_mint: Pubkey,
    pub token_treasury_pda: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettleMatchedTradeParams {
    pub trade_id: u64,
    pub ts: i64,
    pub fixture_summary: FixtureSummaryInput,
    pub fixture_proof: Vec<ProofNode>,
    pub main_tree_proof: Vec<ProofNode>,
    pub stat_a: StatTermInput,
    pub stat_b: Option<StatTermInput>,
    pub terms: MarketIntentParams,
}

/// Account order:
/// 0. `[writable, signer]` winner
/// 1. `[]` daily_scores_merkle_roots
/// 2. `[writable]` matched_trade
/// 3. `[writable]` trade_vault
/// 4. `[writable]` winner_token_account
/// 5. `[]` token_mint
/// 6. `[]` token_treasury_pda
/// 7. `[]` token_program
/// 8. `[]` system_program
pub fn settle_matched_trade_instruction(
    program_id: Pubkey,
    accounts: SettleMatchedTradeAccounts,
    params: SettleMatchedTradeParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&SETTLE_MATCHED_TRADE_DISCRIMINATOR);
    put_u64(&mut data, params.trade_id);
    put_i64(&mut data, params.ts);
    encode_scores_batch_summary(&mut data, &params.fixture_summary);
    encode_proof_vec(&mut data, &params.fixture_proof)?;
    encode_proof_vec(&mut data, &params.main_tree_proof)?;
    encode_stat_term(&mut data, &params.stat_a)?;
    encode_option(&mut data, params.stat_b.as_ref(), encode_stat_term)?;
    encode_market_intent_params(&mut data, &params.terms)?;

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.winner, true),
            AccountMeta::new_readonly(accounts.daily_scores_merkle_roots, false),
            AccountMeta::new(accounts.matched_trade, false),
            AccountMeta::new(accounts.trade_vault, false),
            AccountMeta::new(accounts.winner_token_account, false),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_treasury_pda, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClaimViaResolutionAccounts {
    pub winner: Pubkey,
    pub daily_resolution_roots: Pubkey,
    pub matched_trade: Pubkey,
    pub trade_vault: Pubkey,
    pub winner_token_account: Pubkey,
    pub token_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClaimViaResolutionParams {
    pub epoch_day: u16,
    pub interval_index: u16,
    pub merkle_proof: Vec<ProofNode>,
}

/// Account order:
/// 0. `[writable, signer]` winner
/// 1. `[]` daily_resolution_roots
/// 2. `[writable]` matched_trade
/// 3. `[writable]` trade_vault
/// 4. `[writable]` winner_token_account
/// 5. `[]` token_program
pub fn claim_via_resolution_instruction(
    program_id: Pubkey,
    accounts: ClaimViaResolutionAccounts,
    params: ClaimViaResolutionParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&CLAIM_VIA_RESOLUTION_DISCRIMINATOR);
    put_u16(&mut data, params.epoch_day);
    put_u16(&mut data, params.interval_index);
    encode_proof_vec(&mut data, &params.merkle_proof)?;

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.winner, true),
            AccountMeta::new_readonly(accounts.daily_resolution_roots, false),
            AccountMeta::new(accounts.matched_trade, false),
            AccountMeta::new(accounts.trade_vault, false),
            AccountMeta::new(accounts.winner_token_account, false),
            AccountMeta::new_readonly(accounts.token_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClaimBatchLegacyAccounts {
    pub payer: Pubkey,
    pub daily_resolution_roots: Pubkey,
    pub token_mint: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClaimBatchLegacyParams {
    pub epoch_day: u16,
    pub interval_index: u16,
    pub terms_hash: [u8; 32],
    pub winner_is_maker: bool,
    pub seq: u32,
    pub merkle_proof: Vec<ProofNode>,
}

/// Account order:
/// 0. `[writable, signer]` payer
/// 1. `[]` daily_resolution_roots
/// 2. `[]` token_mint
/// 3. `[]` token_program
/// 4. `[]` system_program
pub fn claim_batch_legacy_instruction(
    program_id: Pubkey,
    accounts: ClaimBatchLegacyAccounts,
    params: ClaimBatchLegacyParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&CLAIM_BATCH_LEGACY_DISCRIMINATOR);
    put_u16(&mut data, params.epoch_day);
    put_u16(&mut data, params.interval_index);
    data.extend_from_slice(&params.terms_hash);
    put_bool(&mut data, params.winner_is_maker);
    put_u32(&mut data, params.seq);
    encode_proof_vec(&mut data, &params.merkle_proof)?;

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.payer, true),
            AccountMeta::new_readonly(accounts.daily_resolution_roots, false),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefundBatchAccounts {
    pub payer: Pubkey,
    pub token_mint: Pubkey,
    pub token_program: Pubkey,
    pub system_program: Pubkey,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RefundBatchParams {}

/// Account order:
/// 0. `[writable, signer]` payer
/// 1. `[]` token_mint
/// 2. `[]` token_program
/// 3. `[]` system_program
pub fn refund_batch_instruction(
    program_id: Pubkey,
    accounts: RefundBatchAccounts,
    _params: RefundBatchParams,
) -> Result<Instruction> {
    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.payer, true),
            AccountMeta::new_readonly(accounts.token_mint, false),
            AccountMeta::new_readonly(accounts.token_program, false),
            AccountMeta::new_readonly(accounts.system_program, false),
        ],
        data: REFUND_BATCH_DISCRIMINATOR.to_vec(),
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuditTradeResultAccounts {
    pub payer: Pubkey,
    pub daily_scores_merkle_roots: Pubkey,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuditTradeResultParams {
    pub terms: MarketIntentParams,
    pub fixture_summary: FixtureSummaryInput,
    pub main_tree_proof: Vec<ProofNode>,
    pub fixture_proof: Vec<ProofNode>,
    pub stat_a: StatTermInput,
    pub stat_b: Option<StatTermInput>,
    pub ts: i64,
}

/// Account order:
/// 0. `[writable, signer]` payer
/// 1. `[]` daily_scores_merkle_roots
pub fn audit_trade_result_instruction(
    program_id: Pubkey,
    accounts: AuditTradeResultAccounts,
    params: AuditTradeResultParams,
) -> Result<Instruction> {
    let mut data = Vec::new();
    data.extend_from_slice(&AUDIT_TRADE_RESULT_DISCRIMINATOR);
    encode_market_intent_params(&mut data, &params.terms)?;
    encode_scores_batch_summary(&mut data, &params.fixture_summary);
    encode_proof_vec(&mut data, &params.main_tree_proof)?;
    encode_proof_vec(&mut data, &params.fixture_proof)?;
    encode_stat_term(&mut data, &params.stat_a)?;
    encode_option(&mut data, params.stat_b.as_ref(), encode_stat_term)?;
    put_i64(&mut data, params.ts);

    Ok(Instruction {
        program_id,
        accounts: vec![
            AccountMeta::new(accounts.payer, true),
            AccountMeta::new_readonly(accounts.daily_scores_merkle_roots, false),
        ],
        data,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarketIntentParams {
    pub fixture_id: i64,
    pub period: u16,
    pub stat_a_key: u32,
    pub stat_b_key: Option<u32>,
    pub predicate: TraderPredicate,
    pub op: Option<BinaryExpression>,
    pub negation: bool,
}

fn encode_market_intent_params(out: &mut Vec<u8>, terms: &MarketIntentParams) -> Result<()> {
    put_i64(out, terms.fixture_id);
    put_u16(out, terms.period);
    put_u32(out, terms.stat_a_key);
    encode_option(out, terms.stat_b_key.as_ref(), |out, value| {
        put_u32(out, *value);
        Ok(())
    })?;
    encode_trader_predicate(out, &terms.predicate);
    encode_option(out, terms.op.as_ref(), |out, op| {
        encode_binary_expression(out, op);
        Ok(())
    })?;
    put_bool(out, terms.negation);
    Ok(())
}