defi-tracker-lifecycle 0.1.5

Pure-logic crate for DeFi order lifecycle tracking on Solana — classification, correlation, and state machine logic
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
pub mod dca;
pub mod kamino;
pub mod limit_v1;
pub mod limit_v2;

use serde::{Deserialize, Serialize};

use crate::error::Error;

#[cfg(feature = "wasm")]
pub const DCA_PROGRAM_ID: &str = "DCA265Vj8a9CEuX1eb1LWRnDT7uK6q1xMipnNyatn23M";
#[cfg(feature = "wasm")]
pub const LIMIT_V1_PROGRAM_ID: &str = "jupoNjAxXgZ4rjzxzPMP4oxduvQsQtZzyknqvzYNrNu";
#[cfg(feature = "wasm")]
pub const LIMIT_V2_PROGRAM_ID: &str = "j1o2qRpjcyUwEvwtcfhEQefh773ZgjxcVRry7LDqg5X";
#[cfg(feature = "wasm")]
pub const KAMINO_PROGRAM_ID: &str = "LiMoM9rMhrdYrfzUCxQppvxCSG1FcrUK9G8uLq4A1GF";

/// Supported DeFi protocols.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, strum_macros::Display, strum_macros::AsRefStr,
)]
#[strum(serialize_all = "snake_case")]
pub enum Protocol {
    /// Jupiter Dollar-Cost Averaging.
    Dca,
    /// Jupiter Limit Order v1.
    LimitV1,
    /// Jupiter Limit Order v2.
    LimitV2,
    /// Kamino Limit Order.
    Kamino,
}

impl Protocol {
    /// Resolves a base58 program id string to its [`Protocol`], or `None` if unrecognised.
    #[cfg(feature = "native")]
    pub fn from_program_id(program_id: &str) -> Option<Self> {
        let key: solana_pubkey::Pubkey = program_id.parse().ok()?;
        match key {
            carbon_jupiter_dca_decoder::PROGRAM_ID => Some(Self::Dca),
            carbon_jupiter_limit_order_decoder::PROGRAM_ID => Some(Self::LimitV1),
            carbon_jupiter_limit_order_2_decoder::PROGRAM_ID => Some(Self::LimitV2),
            carbon_kamino_limit_order_decoder::PROGRAM_ID => Some(Self::Kamino),
            _ => None,
        }
    }

    #[cfg(all(feature = "wasm", not(feature = "native")))]
    pub fn from_program_id(program_id: &str) -> Option<Self> {
        match program_id {
            DCA_PROGRAM_ID => Some(Self::Dca),
            LIMIT_V1_PROGRAM_ID => Some(Self::LimitV1),
            LIMIT_V2_PROGRAM_ID => Some(Self::LimitV2),
            KAMINO_PROGRAM_ID => Some(Self::Kamino),
            _ => None,
        }
    }

    /// Returns the on-chain program id for every supported protocol.
    #[cfg(feature = "native")]
    pub fn all_program_ids() -> [solana_pubkey::Pubkey; 4] {
        [
            carbon_jupiter_dca_decoder::PROGRAM_ID,
            carbon_jupiter_limit_order_decoder::PROGRAM_ID,
            carbon_jupiter_limit_order_2_decoder::PROGRAM_ID,
            carbon_kamino_limit_order_decoder::PROGRAM_ID,
        ]
    }

    #[cfg(feature = "wasm")]
    pub fn program_id_str(&self) -> &'static str {
        match self {
            Self::Dca => DCA_PROGRAM_ID,
            Self::LimitV1 => LIMIT_V1_PROGRAM_ID,
            Self::LimitV2 => LIMIT_V2_PROGRAM_ID,
            Self::Kamino => KAMINO_PROGRAM_ID,
        }
    }
}

/// Canonical event classification shared across all protocols.
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum_macros::Display, strum_macros::AsRefStr)]
#[strum(serialize_all = "snake_case")]
pub enum EventType {
    /// Order was created on-chain.
    Created,
    /// A fill was initiated (e.g. flash-fill start).
    FillInitiated,
    /// A fill was completed (partial or full).
    FillCompleted,
    /// Order was explicitly cancelled.
    Cancelled,
    /// Order expired without completing.
    Expired,
    /// Order reached a terminal close (protocol-level).
    Closed,
    /// Protocol fee was collected.
    FeeCollected,
    /// Funds were withdrawn from the order.
    Withdrawn,
    /// Funds were deposited into the order.
    Deposited,
}

/// A single account entry from a decoded instruction's account list.
#[derive(Debug, Deserialize)]
pub struct AccountInfo {
    /// Base58-encoded account public key.
    pub pubkey: String,
    /// Whether this account signed the transaction.
    #[serde(default)]
    pub is_signer: bool,
    /// Whether this account was marked writable.
    #[serde(default)]
    pub is_writable: bool,
    /// Optional IDL-derived account name (e.g. `"dca"`, `"order"`).
    pub name: Option<String>,
}

/// Shared stateless helpers used across all protocol adapters.
pub struct ProtocolHelpers;

impl ProtocolHelpers {
    /// Looks up an [`EventType`] by name from a static mapping table.
    pub fn lookup_event_type(
        name: &str,
        mapping: &[(&'static str, EventType)],
    ) -> Option<EventType> {
        mapping
            .iter()
            .find_map(|(candidate, event_type)| (*candidate == name).then_some(*event_type))
    }

    /// Deserializes a JSON array of accounts into [`AccountInfo`] structs.
    pub fn parse_accounts(accounts_json: &serde_json::Value) -> Result<Vec<AccountInfo>, Error> {
        serde_json::from_value(accounts_json.clone()).map_err(|e| Error::Protocol {
            reason: format!("failed to parse accounts: {e}"),
        })
    }

    /// Returns the pubkey of the first signer in the account list.
    pub fn find_signer(accounts: &[AccountInfo]) -> Option<&str> {
        accounts
            .iter()
            .find(|a| a.is_signer)
            .map(|a| a.pubkey.as_str())
    }

    /// Finds an account by its IDL-derived name.
    pub fn find_account_by_name<'a>(
        accounts: &'a [AccountInfo],
        name: &str,
    ) -> Option<&'a AccountInfo> {
        accounts.iter().find(|a| a.name.as_deref() == Some(name))
    }

    /// Returns `true` if the JSON object's keys contain any of the `known_names`.
    pub fn contains_known_variant(fields: &serde_json::Value, known_names: &[&str]) -> bool {
        fields
            .as_object()
            .is_some_and(|obj| obj.keys().any(|name| known_names.contains(&name.as_str())))
    }

    /// Converts `u64` to `i64`, returning an error if the value exceeds `i64::MAX`.
    pub fn checked_u64_to_i64(value: u64, field: &str) -> Result<i64, Error> {
        i64::try_from(value).map_err(|_| Error::Protocol {
            reason: format!("{field} exceeds i64::MAX: {value}"),
        })
    }

    /// Converts `u64` to `i64` for optional fields.
    /// Returns `None` if the value exceeds `i64::MAX` (e.g. `u64::MAX` sentinel for "no limit").
    pub fn optional_u64_to_i64(value: u64) -> Option<i64> {
        i64::try_from(value).ok()
    }

    /// Converts `u16` to `i16`, returning an error if the value exceeds `i16::MAX`.
    pub fn checked_u16_to_i16(value: u16, field: &str) -> Result<i16, Error> {
        i16::try_from(value).map_err(|_| Error::Protocol {
            reason: format!("{field} exceeds i16::MAX: {value}"),
        })
    }
}

#[cfg(test)]
#[expect(clippy::unwrap_used, clippy::panic, reason = "test assertions")]
mod tests {
    use super::*;
    use crate::lifecycle::adapters::{ProtocolAdapter, adapter_for};
    use crate::types::{RawEvent, RawInstruction, ResolveContext};
    use std::collections::HashSet;

    #[cfg(feature = "native")]
    #[test]
    fn protocol_program_id_mapping_and_string_names_are_stable() {
        let cases = [
            (
                &carbon_jupiter_dca_decoder::PROGRAM_ID,
                Protocol::Dca,
                "dca",
            ),
            (
                &carbon_jupiter_limit_order_decoder::PROGRAM_ID,
                Protocol::LimitV1,
                "limit_v1",
            ),
            (
                &carbon_jupiter_limit_order_2_decoder::PROGRAM_ID,
                Protocol::LimitV2,
                "limit_v2",
            ),
            (
                &carbon_kamino_limit_order_decoder::PROGRAM_ID,
                Protocol::Kamino,
                "kamino",
            ),
        ];
        for (program_id, expected_protocol, expected_name) in cases {
            assert_eq!(
                Protocol::from_program_id(&program_id.to_string()),
                Some(expected_protocol)
            );
            assert_eq!(expected_protocol.as_ref(), expected_name);
        }

        assert_eq!(Protocol::from_program_id("unknown_program"), None);
        assert_eq!(Protocol::from_program_id("not_even_base58!@#"), None);
        assert_eq!(
            Protocol::all_program_ids(),
            [
                carbon_jupiter_dca_decoder::PROGRAM_ID,
                carbon_jupiter_limit_order_decoder::PROGRAM_ID,
                carbon_jupiter_limit_order_2_decoder::PROGRAM_ID,
                carbon_kamino_limit_order_decoder::PROGRAM_ID,
            ]
        );
    }

    #[cfg(all(feature = "native", feature = "wasm"))]
    #[test]
    fn hardcoded_program_ids_match_carbon_constants() {
        assert_eq!(
            carbon_jupiter_dca_decoder::PROGRAM_ID.to_string(),
            DCA_PROGRAM_ID
        );
        assert_eq!(
            carbon_jupiter_limit_order_decoder::PROGRAM_ID.to_string(),
            LIMIT_V1_PROGRAM_ID
        );
        assert_eq!(
            carbon_jupiter_limit_order_2_decoder::PROGRAM_ID.to_string(),
            LIMIT_V2_PROGRAM_ID
        );
        assert_eq!(
            carbon_kamino_limit_order_decoder::PROGRAM_ID.to_string(),
            KAMINO_PROGRAM_ID
        );
    }

    #[cfg(feature = "wasm")]
    #[test]
    fn protocol_program_id_str_roundtrips() {
        for protocol in [
            Protocol::Dca,
            Protocol::LimitV1,
            Protocol::LimitV2,
            Protocol::Kamino,
        ] {
            assert_eq!(
                Protocol::from_program_id(protocol.program_id_str()),
                Some(protocol)
            );
        }
    }

    #[test]
    fn event_type_strings_match_expected_labels() {
        let cases = [
            (EventType::Created, "created"),
            (EventType::FillInitiated, "fill_initiated"),
            (EventType::FillCompleted, "fill_completed"),
            (EventType::Cancelled, "cancelled"),
            (EventType::Expired, "expired"),
            (EventType::Closed, "closed"),
            (EventType::FeeCollected, "fee_collected"),
            (EventType::Withdrawn, "withdrawn"),
            (EventType::Deposited, "deposited"),
        ];
        for (event_type, expected_label) in cases {
            assert_eq!(event_type.as_ref(), expected_label);
        }
    }

    #[test]
    fn parse_accounts_supports_defaults_and_find_helpers() {
        let accounts_json = serde_json::json!([
            {
                "pubkey": "signer_pubkey",
                "is_signer": true,
                "is_writable": true,
                "name": "order"
            },
            {
                "pubkey": "readonly_pubkey"
            }
        ]);

        let parsed = ProtocolHelpers::parse_accounts(&accounts_json).unwrap();
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0].pubkey, "signer_pubkey");
        assert!(parsed[0].is_signer);
        assert!(parsed[0].is_writable);
        assert_eq!(parsed[0].name.as_deref(), Some("order"));

        assert_eq!(parsed[1].pubkey, "readonly_pubkey");
        assert!(!parsed[1].is_signer);
        assert!(!parsed[1].is_writable);
        assert!(parsed[1].name.is_none());

        assert_eq!(ProtocolHelpers::find_signer(&parsed), Some("signer_pubkey"));
        assert_eq!(
            ProtocolHelpers::find_account_by_name(&parsed, "order").map(|a| a.pubkey.as_str()),
            Some("signer_pubkey")
        );
        assert!(ProtocolHelpers::find_account_by_name(&parsed, "missing").is_none());
    }

    #[test]
    fn parse_accounts_rejects_non_array() {
        let err = ProtocolHelpers::parse_accounts(&serde_json::json!({"pubkey": "not-an-array"}))
            .unwrap_err();
        let Error::Protocol { reason } = err else {
            panic!("expected protocol error");
        };
        assert!(reason.contains("failed to parse accounts"), "{reason}");
    }

    #[test]
    fn parse_accounts_rejects_missing_pubkey() {
        let err =
            ProtocolHelpers::parse_accounts(&serde_json::json!([{"is_signer": true}])).unwrap_err();
        let Error::Protocol { reason } = err else {
            panic!("expected protocol error");
        };
        assert!(reason.contains("failed to parse accounts"), "{reason}");
    }

    #[test]
    fn find_signer_returns_none_when_no_signer_present() {
        let accounts = vec![AccountInfo {
            pubkey: "p1".to_string(),
            is_signer: false,
            is_writable: false,
            name: Some("order".to_string()),
        }];

        assert_eq!(ProtocolHelpers::find_signer(&accounts), None);
    }

    fn make_ix(name: &str) -> RawInstruction {
        RawInstruction {
            id: 1,
            signature: "sig".to_string(),
            instruction_index: 0,
            instruction_path: None,
            program_id: "p".to_string(),
            inner_program_id: "p".to_string(),
            instruction_name: name.to_string(),
            accounts: None,
            args: None,
            slot: 1,
        }
    }

    fn collect_instruction_event_types(
        instruction_names: &[&str],
        adapter: &dyn ProtocolAdapter,
    ) -> HashSet<String> {
        instruction_names
            .iter()
            .filter_map(|name| adapter.classify_instruction(&make_ix(name)))
            .map(|et| et.as_ref().to_string())
            .collect()
    }

    fn make_event(fields: serde_json::Value) -> RawEvent {
        RawEvent {
            id: 1,
            signature: "sig".to_string(),
            event_index: 0,
            event_path: None,
            program_id: "p".to_string(),
            inner_program_id: "p".to_string(),
            event_name: "test".to_string(),
            fields: Some(fields),
            slot: 1,
        }
    }

    fn resolve_event_type(
        json: serde_json::Value,
        adapter: &dyn ProtocolAdapter,
        ctx: &ResolveContext,
    ) -> Option<String> {
        adapter
            .classify_and_resolve_event(&make_event(json), ctx)
            .and_then(|r| r.ok())
            .map(|(et, _, _)| et.as_ref().to_string())
    }

    #[test]
    fn event_type_reachability_all_variants_covered() {
        let mut all_event_types: HashSet<String> = HashSet::new();
        let default_ctx = ResolveContext {
            pre_fetched_order_pdas: None,
        };

        let dca = adapter_for(Protocol::Dca);
        let dca_ix_names = [
            "OpenDca",
            "OpenDcaV2",
            "InitiateFlashFill",
            "InitiateDlmmFill",
            "FulfillFlashFill",
            "FulfillDlmmFill",
            "CloseDca",
            "EndAndClose",
            "Transfer",
            "Deposit",
            "Withdraw",
            "WithdrawFees",
        ];
        all_event_types.extend(collect_instruction_event_types(&dca_ix_names, dca));

        let dca_event_payloads = [
            serde_json::json!({"OpenedEvent": {"dca_key": "t"}}),
            serde_json::json!({"FilledEvent": {"dca_key": "t", "in_amount": 1_u64, "out_amount": 1_u64}}),
            serde_json::json!({"ClosedEvent": {"dca_key": "t", "user_closed": false, "unfilled_amount": 0_u64}}),
            serde_json::json!({"CollectedFeeEvent": {"dca_key": "t"}}),
            serde_json::json!({"WithdrawEvent": {"dca_key": "t"}}),
            serde_json::json!({"DepositEvent": {"dca_key": "t"}}),
        ];
        for json in &dca_event_payloads {
            if let Some(et) = resolve_event_type(json.clone(), dca, &default_ctx) {
                all_event_types.insert(et);
            }
        }

        let v1 = adapter_for(Protocol::LimitV1);
        let v1_ix_names = [
            "InitializeOrder",
            "PreFlashFillOrder",
            "FillOrder",
            "FlashFillOrder",
            "CancelOrder",
            "CancelExpiredOrder",
            "WithdrawFee",
            "InitFee",
            "UpdateFee",
        ];
        all_event_types.extend(collect_instruction_event_types(&v1_ix_names, v1));

        let v1_event_payloads = [
            serde_json::json!({"CreateOrderEvent": {"order_key": "t"}}),
            serde_json::json!({"CancelOrderEvent": {"order_key": "t"}}),
            serde_json::json!({"TradeEvent": {"order_key": "t", "in_amount": 1_u64, "out_amount": 1_u64, "remaining_in_amount": 0_u64, "remaining_out_amount": 0_u64}}),
        ];
        for json in &v1_event_payloads {
            if let Some(et) = resolve_event_type(json.clone(), v1, &default_ctx) {
                all_event_types.insert(et);
            }
        }

        let v2 = adapter_for(Protocol::LimitV2);
        let v2_ix_names = [
            "InitializeOrder",
            "PreFlashFillOrder",
            "FlashFillOrder",
            "CancelOrder",
            "UpdateFee",
            "WithdrawFee",
        ];
        all_event_types.extend(collect_instruction_event_types(&v2_ix_names, v2));

        let v2_event_payloads = [
            serde_json::json!({"CreateOrderEvent": {"order_key": "t"}}),
            serde_json::json!({"CancelOrderEvent": {"order_key": "t"}}),
            serde_json::json!({"TradeEvent": {"order_key": "t", "making_amount": 1_u64, "taking_amount": 1_u64, "remaining_making_amount": 0_u64, "remaining_taking_amount": 0_u64}}),
        ];
        for json in &v2_event_payloads {
            if let Some(et) = resolve_event_type(json.clone(), v2, &default_ctx) {
                all_event_types.insert(et);
            }
        }

        let kamino = adapter_for(Protocol::Kamino);
        let kamino_ix_names = [
            "CreateOrder",
            "TakeOrder",
            "FlashTakeOrderStart",
            "FlashTakeOrderEnd",
            "CloseOrderAndClaimTip",
            "InitializeGlobalConfig",
            "InitializeVault",
            "UpdateGlobalConfig",
            "UpdateGlobalConfigAdmin",
            "WithdrawHostTip",
            "LogUserSwapBalances",
        ];
        all_event_types.extend(collect_instruction_event_types(&kamino_ix_names, kamino));

        let kamino_ctx = ResolveContext {
            pre_fetched_order_pdas: Some(vec!["test_pda".to_string()]),
        };
        let kamino_event_payloads = [
            serde_json::json!({"OrderDisplayEvent": {"status": 1_u8}}),
            serde_json::json!({"UserSwapBalancesEvent": {}}),
        ];
        for json in &kamino_event_payloads {
            if let Some(et) = resolve_event_type(json.clone(), kamino, &kamino_ctx) {
                all_event_types.insert(et);
            }
        }

        let expected: HashSet<String> = [
            "created",
            "fill_initiated",
            "fill_completed",
            "cancelled",
            "expired",
            "closed",
            "fee_collected",
            "withdrawn",
            "deposited",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        assert_eq!(
            all_event_types,
            expected,
            "missing EventTypes: {:?}, extra: {:?}",
            expected.difference(&all_event_types).collect::<Vec<_>>(),
            all_event_types.difference(&expected).collect::<Vec<_>>()
        );
    }
}