radion-sdk 0.3.0

Official, async-first, fully-typed Rust SDK for the Radion platform
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
//! Typed event payloads for every Radion realtime channel.
//!
//! Each channel emits a `data` object discriminated by a snake_case `type`
//! field. The structs below type the fields documented for each channel's
//! payload.
//!
//! Provenance mirrors the published channel docs (`/websockets/channels/*`).
//! An event whose `type` this SDK version does not enumerate — or whose shape
//! does not match the channel's typed payload — is preserved as
//! [`Payload::Other`] rather than dropped, mirroring the TS/Python SDKs' loose
//! validation for forward compatibility.
//!
//! On-chain amounts stay **strings** to remain bigint-safe; do not assume they
//! fit a numeric type.

use serde::{Deserialize, Serialize};

use super::channels::Channel;

/// Hex-encoded string (`0x…`) or other opaque on-chain string value.
pub type Hex = String;

macro_rules! event_type_enum {
    ($(#[$meta:meta])* $name:ident { $($(#[$vmeta:meta])* $variant:ident),* $(,)? }) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
        #[serde(rename_all = "snake_case")]
        #[non_exhaustive]
        // Variants map 1:1 to documented wire event types; names are the doc,
        // and shared prefixes (e.g. `Uma`) come straight from the protocol.
        #[allow(missing_docs, clippy::enum_variant_names)]
        pub enum $name {
            $($(#[$vmeta])* $variant,)*
        }
    };
}

event_type_enum!(
    /// Discriminator for [`TradingPayload`].
    TradingEventType {
        OrderFilledV1,
        OrderFilledV2,
        OrdersMatchedV1,
        OrdersMatchedV2,
        OrderCancelled,
        OrderPreapproved,
        OrderPreapprovalInvalidated,
        TradingPaused,
        TradingUnpaused,
    }
);

/// Order flow on the exchange (fills, matches, cancels, preapprovals, pauses).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TradingPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: TradingEventType,
    /// `0` = buy, `1` = sell. v2 fills and matches only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub side: Option<i64>,
    /// Builder address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub builder: Option<Hex>,
    /// Fee paid.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fee: Option<Hex>,
    /// Maker address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maker: Option<Hex>,
    /// Maker amount filled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maker_amount_filled: Option<Hex>,
    /// Opaque order metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Hex>,
    /// Order hash.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_hash: Option<Hex>,
    /// Taker address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub taker: Option<Hex>,
    /// Taker amount filled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub taker_amount_filled: Option<Hex>,
    /// ERC-1155 token id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_id: Option<Hex>,
}

event_type_enum!(
    /// Discriminator for [`FeesPayload`].
    FeesEventType {
        FeeChargedV1,
        FeeChargedV2,
    }
);

/// Exchange fee charged.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FeesPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: FeesEventType,
    /// Fee amount charged.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fee: Option<Hex>,
    /// Address charged the fee.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payer: Option<Hex>,
    /// Address receiving the fee.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub receiver: Option<Hex>,
    /// Order hash the fee is attached to.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_hash: Option<Hex>,
    /// ERC-1155 token id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_id: Option<Hex>,
}

event_type_enum!(
    /// Discriminator for [`OraclePayload`].
    OracleEventType {
        UmaAdapterQuestionInitialized,
        UmaAdapterQuestionResolved,
        UmaAdapterQuestionEmergencyResolved,
        UmaAdapterQuestionFlagged,
        UmaAdapterQuestionPaused,
        UmaAdapterQuestionUnpaused,
        UmaAdapterQuestionReset,
        UmaAdapterAncillaryDataUpdated,
        UmaOptimisticQuestionInitialized,
        UmaOptimisticQuestionResolved,
        UmaOptimisticQuestionPaused,
        UmaOptimisticQuestionUnpaused,
        UmaOptimisticQuestionSettled,
        UmaOptimisticResolutionDataRequested,
        UmaOptimisticQuestionUpdated,
        UmaOptimisticQuestionFlaggedForAdminResolution,
    }
);

/// UMA question mechanism payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OraclePayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: OracleEventType,
    /// UMA question id.
    #[serde(rename = "questionID", skip_serializing_if = "Option::is_none")]
    pub question_id: Option<Hex>,
    /// Resolution payouts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payouts: Option<Vec<Hex>>,
    /// `int256` price as a signed decimal string (e.g. `"-1"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub settled_price: Option<String>,
}

event_type_enum!(
    /// Discriminator for [`ResolutionPayload`].
    ResolutionEventType {
        ConditionResolution,
        ConditionResolved,
        OutcomeReported,
        ResultReported,
        ResolutionPaused,
        ResolutionUnpaused,
        ResolverPaused,
        ResolverUnpaused,
    }
);

/// Settlement outcome payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ResolutionPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: ResolutionEventType,
    /// Condition id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition_id: Option<Hex>,
    /// Question id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub question_id: Option<Hex>,
    /// Resolution payouts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payouts: Option<Vec<Hex>>,
}

event_type_enum!(
    /// Discriminator for [`LifecyclePayload`].
    LifecycleEventType {
        MarketPrepared,
        EventPrepared,
        ConditionPreparation,
        TokenRegistered,
        NegRiskQuestionPrepared,
        CombinatorialConditionPrepared,
        MigrationConditionRegistered,
    }
);

/// Market creation and prep payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LifecyclePayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: LifecycleEventType,
    /// Condition id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition_id: Option<Hex>,
    /// Oracle address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oracle: Option<Hex>,
    /// Outcome slot count.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub outcome_slot_count: Option<Hex>,
    /// Question id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub question_id: Option<Hex>,
}

event_type_enum!(
    /// Discriminator for [`PositionsPayload`].
    PositionsEventType {
        CtfPositionSplit,
        CtfPositionsMerge,
        CtfPayoutRedemption,
        CollateralPositionSplit,
        CollateralPositionsMerged,
        PositionsRedeemed,
    }
);

/// Plain CTF base-layer split / merge / redemption payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PositionsPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: PositionsEventType,
    /// Amounts involved.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amounts: Option<Vec<Hex>>,
    /// Condition id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition_id: Option<Hex>,
    /// Initiating address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initiator: Option<Hex>,
    /// Payout amount.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payout: Option<Hex>,
    /// ERC-1155 token id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_id: Option<Hex>,
}

event_type_enum!(
    /// Discriminator for [`CombosPayload`].
    CombosEventType {
        Redemption,
        BinaryRedemption,
        NegRiskRedemption,
        CollateralPositionsConverted,
        NegRiskPositionsConverted,
        PositionConverted,
        PositionRedeemed,
        ModulePositionsMerged,
        ModulePositionsSplit,
        HorizontalMerge,
        HorizontalSplit,
        SplitOnCondition,
        MergedOnCondition,
        ConvertedToYesBasket,
        MergedFromYesBasket,
        Extracted,
        Injected,
        Compressed,
        CombinatorialWrapped,
        CombinatorialUnwrapped,
        PositionMigrated,
        MigrationResolved,
        BridgePositionMinted,
        BridgePositionsBurned,
        LegacyCollateralSettled,
    }
);

/// Module / redeemer / neg-risk / combinatorial system payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CombosPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: CombosEventType,
    /// Amount.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<Hex>,
    /// Condition id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition_id: Option<Hex>,
    /// Sender address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<Hex>,
    /// Token / position id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<Hex>,
    /// Operator address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operator: Option<Hex>,
    /// Recipient address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<Hex>,
}

event_type_enum!(
    /// Discriminator for [`TransfersPayload`].
    TransfersEventType {
        TransferSingle,
        TransferBatch,
    }
);

/// ERC-1155 outcome-token move payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransfersPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: TransfersEventType,
    /// Operator address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operator: Option<Hex>,
    /// Sender address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<Hex>,
    /// Recipient address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<Hex>,
    /// Token id (`TransferSingle`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<Hex>,
    /// Amount moved (`TransferSingle`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<Hex>,
    /// Token ids (`TransferBatch`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ids: Option<Vec<Hex>>,
    /// Amounts moved (`TransferBatch`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub values: Option<Vec<Hex>>,
}

event_type_enum!(
    /// Discriminator for [`AccountsPayload`].
    AccountsEventType {
        WalletDeployed,
        ProxyCreation,
    }
);

/// Proxy wallet creation payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AccountsPayload {
    /// Event discriminator.
    #[serde(rename = "type")]
    pub kind: AccountsEventType,
    /// The deployed / created proxy wallet address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wallet: Option<Hex>,
    /// The owner controlling the proxy wallet.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner: Option<Hex>,
    /// Proxy implementation address.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proxy: Option<Hex>,
}

/// The typed payload carried by a channel event.
///
/// The active variant is determined by the event frame's `channel` field. The
/// `wallets` and `markets` filter channels re-emit confirmed payloads, so they
/// deserialize to whichever confirmed variant matches its `type`. Unknown
/// channels, unknown `type` values, or data that does not match any typed
/// payload fall back to [`Payload::Other`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum Payload {
    /// `trading` payload.
    Trading(TradingPayload),
    /// `fees` payload.
    Fees(FeesPayload),
    /// `oracle` payload.
    Oracle(OraclePayload),
    /// `resolution` payload.
    Resolution(ResolutionPayload),
    /// `lifecycle` payload.
    Lifecycle(LifecyclePayload),
    /// `positions` payload.
    Positions(PositionsPayload),
    /// `combos` payload.
    Combos(CombosPayload),
    /// `transfers` payload.
    Transfers(TransfersPayload),
    /// `accounts` payload.
    Accounts(AccountsPayload),
    /// Any structurally valid payload the SDK does not type.
    Other(serde_json::Value),
}

impl Payload {
    /// Decode raw event `data` into the typed payload for `channel`.
    ///
    /// Never fails: data that does not match the channel's typed shape is
    /// preserved as [`Payload::Other`].
    pub(crate) fn from_channel(channel: Channel, data: serde_json::Value) -> Self {
        fn typed<T, F>(data: serde_json::Value, wrap: F) -> Payload
        where
            T: for<'de> Deserialize<'de>,
            F: FnOnce(T) -> Payload,
        {
            match serde_json::from_value::<T>(data.clone()) {
                Ok(value) => wrap(value),
                Err(_) => Payload::Other(data),
            }
        }

        match channel {
            Channel::Trading => typed(data, Payload::Trading),
            Channel::Fees => typed(data, Payload::Fees),
            Channel::Oracle => typed(data, Payload::Oracle),
            Channel::Resolution => typed(data, Payload::Resolution),
            Channel::Lifecycle => typed(data, Payload::Lifecycle),
            Channel::Positions => typed(data, Payload::Positions),
            Channel::Combos => typed(data, Payload::Combos),
            Channel::Transfers => typed(data, Payload::Transfers),
            Channel::Accounts => typed(data, Payload::Accounts),
            // Filtered views re-emit confirmed payloads; the untagged enum picks
            // the variant whose `type` matches, preserving unknowns.
            Channel::Wallets | Channel::Markets => {
                serde_json::from_value(data.clone()).unwrap_or(Payload::Other(data))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn oracle_renames_question_id() {
        let data = json!({"type":"uma_optimistic_question_resolved","questionID":"0xq","settledPrice":"-1"});
        match Payload::from_channel(Channel::Oracle, data) {
            Payload::Oracle(o) => {
                assert_eq!(o.kind, OracleEventType::UmaOptimisticQuestionResolved);
                assert_eq!(o.question_id.as_deref(), Some("0xq"));
                assert_eq!(o.settled_price.as_deref(), Some("-1"));
            }
            other => panic!("expected oracle, got {other:?}"),
        }
    }

    #[test]
    fn trading_types_a_fill() {
        let data = json!({"type":"order_filled_v2","side":1,"tokenId":"0xabc"});
        match Payload::from_channel(Channel::Trading, data) {
            Payload::Trading(t) => {
                assert_eq!(t.kind, TradingEventType::OrderFilledV2);
                assert_eq!(t.side, Some(1));
                assert_eq!(t.token_id.as_deref(), Some("0xabc"));
            }
            other => panic!("expected trading, got {other:?}"),
        }
    }

    #[test]
    fn wallets_view_discriminates_by_type() {
        // A lifecycle event arriving on the `wallets` filter channel types correctly.
        let data = json!({"type":"market_prepared","conditionId":"0xc"});
        assert!(matches!(
            Payload::from_channel(Channel::Wallets, data),
            Payload::Lifecycle(_)
        ));
    }

    #[test]
    fn unknown_event_type_falls_back_to_other() {
        let data = json!({"type":"brand_new_event","foo":1});
        assert!(matches!(
            Payload::from_channel(Channel::Trading, data),
            Payload::Other(_)
        ));
    }
}