lightcone 0.5.1

Rust SDK for the Lightcone Protocol — unified native + WASM client
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
//! Type definitions for the Lightcone Pinocchio on-chain program.
//!
//! This module contains enums, parameter structs, and other type definitions
//! used for on-chain program interaction.

use solana_pubkey::Pubkey;

use crate::domain::market::Market;
use crate::program::error::SdkError;
use crate::program::orders::OrderPayload;
use crate::shared::DepositSource;

// ============================================================================
// Enums
// ============================================================================

/// Market status enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum MarketStatus {
    /// Market is pending activation
    Pending = 0,
    /// Market is active and trading is enabled
    Active = 1,
    /// Market has been resolved with a winning outcome
    Resolved = 2,
    /// Market has been cancelled
    Cancelled = 3,
}

impl TryFrom<u8> for MarketStatus {
    type Error = SdkError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(MarketStatus::Pending),
            1 => Ok(MarketStatus::Active),
            2 => Ok(MarketStatus::Resolved),
            3 => Ok(MarketStatus::Cancelled),
            _ => Err(SdkError::InvalidMarketStatus(value)),
        }
    }
}

/// Order side enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum OrderSide {
    /// Bid/Buy - maker wants to buy base tokens, gives quote tokens
    Bid = 0,
    /// Ask/Sell - maker wants to sell base tokens, receives quote tokens
    Ask = 1,
}

impl TryFrom<u8> for OrderSide {
    type Error = SdkError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(OrderSide::Bid),
            1 => Ok(OrderSide::Ask),
            _ => Err(SdkError::InvalidSide(value)),
        }
    }
}

// ============================================================================
// Parameter Structs
// ============================================================================

/// Parameters for creating a market
#[derive(Debug, Clone)]
pub struct CreateMarketParams {
    /// Authority pubkey (must be exchange authority)
    pub authority: Pubkey,
    /// Number of outcomes (2-6)
    pub num_outcomes: u8,
    /// Oracle pubkey that can settle the market
    pub oracle: Pubkey,
    /// Question ID (32 bytes)
    pub question_id: [u8; 32],
}

/// Metadata for a single outcome token
#[derive(Debug, Clone)]
pub struct OutcomeMetadata {
    /// Token name (max 32 chars)
    pub name: String,
    /// Token symbol (max 10 chars)
    pub symbol: String,
    /// Token URI (max 200 chars)
    pub uri: String,
}

/// Parameters for adding a deposit mint to a market
#[derive(Debug, Clone)]
pub struct AddDepositMintParams {
    /// Authority pubkey (must be exchange authority)
    pub authority: Pubkey,
    /// Deposit mint pubkey
    pub deposit_mint: Pubkey,
    /// Metadata for each outcome token
    pub outcome_metadata: Vec<OutcomeMetadata>,
}

/// Parameters for minting a complete set
#[derive(Debug, Clone)]
pub struct BuildDepositParams {
    /// User pubkey (payer and recipient)
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Deposit mint pubkey
    pub deposit_mint: Pubkey,
    /// Amount of collateral to deposit
    pub amount: u64,
}

/// Parameters for merging a complete set
#[derive(Debug, Clone)]
pub struct BuildMergeParams {
    /// User pubkey
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Deposit mint pubkey
    pub deposit_mint: Pubkey,
    /// Amount of each outcome token to burn
    pub amount: u64,
}

/// Parameters for settling a market
#[derive(Debug, Clone)]
pub struct SettleMarketParams {
    /// Oracle pubkey (must match market oracle)
    pub oracle: Pubkey,
    /// Market ID
    pub market_id: u64,
    /// Winning outcome index
    pub winning_outcome: u8,
}

/// Parameters for redeeming winnings
#[derive(Debug, Clone)]
pub struct RedeemWinningsParams {
    /// User pubkey
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Deposit mint pubkey
    pub deposit_mint: Pubkey,
    /// Amount of winning tokens to redeem
    pub amount: u64,
}

/// Parameters for withdrawing from a position
#[derive(Debug, Clone)]
pub struct WithdrawFromPositionParams {
    /// User pubkey (must be position owner)
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Mint pubkey (deposit or conditional)
    pub mint: Pubkey,
    /// Amount to withdraw
    pub amount: u64,
    /// Outcome index (255 for collateral)
    pub outcome_index: u8,
}

/// Parameters for activating a market
#[derive(Debug, Clone)]
pub struct ActivateMarketParams {
    /// Authority pubkey (must be exchange authority)
    pub authority: Pubkey,
    /// Market ID
    pub market_id: u64,
}

/// Parameters for creating a bid order
#[derive(Debug, Clone)]
pub struct BidOrderParams {
    /// Order nonce (unique per user)
    pub nonce: u64,
    /// Random salt for order uniqueness
    pub salt: u64,
    /// Maker pubkey
    pub maker: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Base mint (token being bought)
    pub base_mint: Pubkey,
    /// Quote mint (token used for payment)
    pub quote_mint: Pubkey,
    /// Quote tokens to give (on-chain amount_in, maps to API `amount_in`)
    pub amount_in: u64,
    /// Base tokens to receive (on-chain amount_out, maps to API `amount_out`)
    pub amount_out: u64,
    /// Expiration timestamp (0 for no expiration)
    pub expiration: i64,
}

/// Parameters for creating an ask order
#[derive(Debug, Clone)]
pub struct AskOrderParams {
    /// Order nonce (unique per user)
    pub nonce: u64,
    /// Random salt for order uniqueness
    pub salt: u64,
    /// Maker pubkey
    pub maker: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Base mint (token being sold)
    pub base_mint: Pubkey,
    /// Quote mint (token to receive)
    pub quote_mint: Pubkey,
    /// Base tokens to give (on-chain amount_in, maps to API `amount_in`)
    pub amount_in: u64,
    /// Quote tokens to receive (on-chain amount_out, maps to API `amount_out`)
    pub amount_out: u64,
    /// Expiration timestamp (0 for no expiration)
    pub expiration: i64,
}

/// Parameters for matching orders
#[derive(Debug, Clone)]
pub struct MatchOrdersMultiParams {
    /// Operator pubkey (must be exchange operator)
    pub operator: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Base mint pubkey
    pub base_mint: Pubkey,
    /// Quote mint pubkey
    pub quote_mint: Pubkey,
    /// Taker order (signed)
    pub taker_order: OrderPayload,
    /// Maker orders (signed)
    pub maker_orders: Vec<OrderPayload>,
    /// Fill amounts for each maker (maker side)
    pub maker_fill_amounts: Vec<u64>,
    /// Fill amounts for each maker (taker side)
    pub taker_fill_amounts: Vec<u64>,
    /// Bitmask indicating which orders require full fill (bit i = maker i, bit 7 = taker)
    pub full_fill_bitmask: u8,
}

/// Parameters for creating an on-chain orderbook
#[derive(Debug, Clone)]
pub struct CreateOrderbookParams {
    /// Authority pubkey (must be exchange authority, pays for account creation)
    pub authority: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Mint A pubkey
    pub mint_a: Pubkey,
    /// Mint B pubkey
    pub mint_b: Pubkey,
    /// Recent slot for ALT creation
    pub recent_slot: u64,
    /// Which mint is the base asset (0 = mint_a, 1 = mint_b)
    pub base_index: u8,
}

/// Parameters for setting a new authority
#[derive(Debug, Clone)]
pub struct SetAuthorityParams {
    /// Current authority pubkey
    pub current_authority: Pubkey,
    /// New authority pubkey
    pub new_authority: Pubkey,
}

/// Parameters for whitelisting a deposit token for global deposits
#[derive(Debug, Clone)]
pub struct WhitelistDepositTokenParams {
    /// Authority pubkey (must be exchange authority)
    pub authority: Pubkey,
    /// Mint pubkey to whitelist
    pub mint: Pubkey,
}

/// Parameters for depositing tokens to a global deposit account
#[derive(Debug, Clone)]
pub struct DepositToGlobalParams {
    /// User pubkey (depositor)
    pub user: Pubkey,
    /// Deposit token mint pubkey
    pub mint: Pubkey,
    /// Amount to deposit
    pub amount: u64,
}

/// Parameters for transferring from global deposit to a market vault
#[derive(Debug, Clone)]
pub struct GlobalToMarketDepositParams {
    /// User pubkey
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Deposit mint pubkey (e.g., USDC)
    pub deposit_mint: Pubkey,
    /// Amount of collateral to transfer and mint
    pub amount: u64,
}

/// Parameters for initializing position token accounts and ALT.
///
/// Permissionless — anyone can pay to create positions/ATAs/ALTs for any user.
#[derive(Debug, Clone)]
pub struct InitPositionTokensParams {
    /// Payer for account creation (signer, does not need to be the user)
    pub payer: Pubkey,
    /// Position owner (does not need to sign)
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Deposit mints to initialize (must be in ascending GDT index order)
    pub deposit_mints: Vec<Pubkey>,
    /// Recent slot for ALT address derivation
    pub recent_slot: u64,
}

/// Per-maker fill info for deposit_and_swap
#[derive(Debug, Clone)]
pub struct MakerFill {
    /// Maker order (signed)
    pub order: OrderPayload,
    /// Fill amount (maker side)
    pub maker_fill_amount: u64,
    /// Fill amount (taker side)
    pub taker_fill_amount: u64,
    /// Whether this maker requires full fill (skips order_status account)
    pub is_full_fill: bool,
    /// Whether this maker deposits from global (vs swapping existing tokens)
    pub is_deposit: bool,
    /// Deposit mint for this maker (only used when is_deposit is true)
    pub deposit_mint: Pubkey,
}

/// Parameters for deposit-and-swap (atomic deposit + mint + swap)
#[derive(Debug, Clone)]
pub struct DepositAndSwapParams {
    /// Operator pubkey (must be exchange operator)
    pub operator: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Base mint pubkey (conditional token A)
    pub base_mint: Pubkey,
    /// Quote mint pubkey (conditional token B)
    pub quote_mint: Pubkey,
    /// Taker order (signed)
    pub taker_order: OrderPayload,
    /// Whether the taker requires full fill
    pub taker_is_full_fill: bool,
    /// Whether the taker deposits from global
    pub taker_is_deposit: bool,
    /// Taker's deposit mint (only used when taker_is_deposit is true)
    pub taker_deposit_mint: Pubkey,
    /// Number of outcomes for the market
    pub num_outcomes: u8,
    /// Per-maker fill info
    pub makers: Vec<MakerFill>,
}

/// Parameters for extending a position ALT with new deposit mints
#[derive(Debug, Clone)]
pub struct ExtendPositionTokensParams {
    /// Payer for account creation (signer)
    pub payer: Pubkey,
    /// Position owner (does not need to sign)
    pub user: Pubkey,
    /// Market pubkey
    pub market: Pubkey,
    /// Existing ALT pubkey from init_position_tokens
    pub lookup_table: Pubkey,
    /// New deposit mints to add (must be in ascending GDT index order)
    pub deposit_mints: Vec<Pubkey>,
}

/// Parameters for withdrawing tokens from a global deposit account
#[derive(Debug, Clone)]
pub struct WithdrawFromGlobalParams {
    /// User pubkey (must be the depositor / signer)
    pub user: Pubkey,
    /// Deposit token mint pubkey
    pub mint: Pubkey,
    /// Amount to withdraw
    pub amount: u64,
}

// ============================================================================
// Unified Deposit/Withdraw Parameters
// ============================================================================

/// Unified deposit parameters — dispatches to global or market deposit
/// based on the client's deposit source setting.
///
/// Prefer using the builder via `client.positions().deposit().await` which
/// pre-seeds the client's deposit source. Direct construction is also available.
#[derive(Debug)]
pub struct DepositParams<'a> {
    /// User pubkey (depositor).
    pub user: Pubkey,
    /// Deposit token mint pubkey.
    pub mint: Pubkey,
    /// Amount to deposit.
    pub amount: u64,
    /// Required when deposit source is `Market`. Provides the market pubkey
    /// and outcome count.
    pub market: Option<&'a Market>,
    /// Per-call override. If `None`, uses the client-level deposit source.
    pub deposit_source: Option<DepositSource>,
}

/// Market-specific context required for withdrawals when deposit source is `Market`.
#[derive(Debug)]
pub struct MarketWithdrawContext<'a> {
    /// The market to withdraw from.
    pub market: &'a Market,
    /// Outcome index to withdraw. Use `255` for collateral.
    pub outcome_index: u8,
    /// `true` for conditional tokens (Token-2022), `false` for deposit tokens (SPL Token).
    pub is_token_2022: bool,
}

/// Unified withdraw parameters — dispatches to global or market withdrawal
/// based on the client's deposit source setting.
///
/// Prefer using the builder via `client.positions().withdraw().await` which
/// pre-seeds the client's deposit source. Direct construction is also available.
#[derive(Debug)]
pub struct WithdrawParams<'a> {
    /// User pubkey (must be the depositor / position owner).
    pub user: Pubkey,
    /// Token mint pubkey.
    pub mint: Pubkey,
    /// Amount to withdraw.
    pub amount: u64,
    /// Required when deposit source is `Market`.
    pub market_context: Option<MarketWithdrawContext<'a>>,
    /// Per-call override. If `None`, uses the client-level deposit source.
    pub deposit_source: Option<DepositSource>,
}