nautilus-hyperliquid 0.62.0

Hyperliquid integration adapter for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Hyperliquid-specific custom data types.
//!
//! These types carry Hyperliquid domain data through the Nautilus data engine as
//! [`CustomData`](nautilus_model::data::CustomData).

use std::collections::HashMap;

use nautilus_core::UnixNanos;
use nautilus_model::{
    enums::{AggressorSide, OrderSide},
    identifiers::InstrumentId,
    types::{Price, Quantity},
};
use nautilus_persistence_macros::custom_data;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use crate::common::enums::HyperliquidTwapStatus;

/// Hyperliquid all mid prices snapshot from the `allMids` WebSocket channel.
#[cfg_attr(
    feature = "arrow",
    custom_data(pyo3, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
#[cfg_attr(
    not(feature = "arrow"),
    custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
pub struct HyperliquidAllMids {
    /// Mapping of instrument ID to mid price for all tradable coins.
    #[custom_data_field(serde)]
    pub mids: HashMap<InstrumentId, Price>,
    /// UNIX timestamp (nanoseconds) when the data event occurred.
    pub ts_event: UnixNanos,
    /// UNIX timestamp (nanoseconds) when the instance was initialized.
    pub ts_init: UnixNanos,
}

/// Hyperliquid open interest update from the `activeAssetCtx` WebSocket channel.
///
/// Hyperliquid does not provide a native event timestamp on this payload, so
/// `ts_event` mirrors `ts_init` like the peer asset-context update types.
#[cfg_attr(
    feature = "arrow",
    custom_data(pyo3, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
#[cfg_attr(
    not(feature = "arrow"),
    custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
pub struct HyperliquidOpenInterest {
    /// The instrument ID for this open interest update.
    pub instrument_id: InstrumentId,
    /// The current open interest for the perpetual instrument.
    #[custom_data_field(serde)]
    pub open_interest: Decimal,
    /// UNIX timestamp (nanoseconds) when the data event occurred.
    pub ts_event: UnixNanos,
    /// UNIX timestamp (nanoseconds) when the instance was initialized.
    pub ts_init: UnixNanos,
}

/// A complete public Hyperliquid trade, including the venue-provided counterparties.
///
/// This is opt-in adapter-specific data. It deliberately does not extend the
/// generic [`TradeTick`](nautilus_model::data::TradeTick), and is self-contained
/// so one catalog stream can be recorded and replayed without joining sidecar data.
#[cfg_attr(
    feature = "arrow",
    custom_data(pyo3, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
#[cfg_attr(
    not(feature = "arrow"),
    custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
pub struct HyperliquidPublicTrade {
    /// The instrument ID for this trade.
    pub instrument_id: InstrumentId,
    /// The trade price normalized to the instrument's precision.
    #[custom_data_field(serde)]
    pub price: Price,
    /// The trade size normalized to the instrument's precision.
    #[custom_data_field(serde)]
    pub size: Quantity,
    /// The aggressor side reported by Hyperliquid.
    #[custom_data_field(serde)]
    pub aggressor_side: AggressorSide,
    /// Hyperliquid venue trade identifier.
    pub trade_id: String,
    /// Buyer wallet address reported by Hyperliquid.
    pub buyer: String,
    /// Seller wallet address reported by Hyperliquid.
    pub seller: String,
    /// Hyperliquid trade hash.
    pub hash: String,
    /// UNIX timestamp (nanoseconds) when the trade occurred.
    pub ts_event: UnixNanos,
    /// UNIX timestamp (nanoseconds) when the instance was initialized.
    pub ts_init: UnixNanos,
}

/// Impact prices reported by Hyperliquid for venue-side execution estimates.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HyperliquidImpactPrices {
    /// Impact bid price.
    pub bid: Price,
    /// Impact ask price.
    pub ask: Price,
}

/// Normalized per-instrument entry within `allDexsAssetCtxs`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HyperliquidDexAssetCtx {
    /// Hyperliquid dex identifier. The default perp dex uses the empty string.
    pub dex: String,
    /// Canonical Nautilus instrument ID.
    pub instrument_id: InstrumentId,
    /// Mark price.
    pub mark_price: Price,
    /// Oracle/index price.
    pub oracle_price: Price,
    /// Previous day price.
    pub prev_day_price: Price,
    /// Optional mid price.
    pub mid_price: Option<Price>,
    /// Optional impact prices.
    pub impact_prices: Option<HyperliquidImpactPrices>,
    /// Current funding rate.
    pub funding_rate: Decimal,
    /// Current open interest.
    pub open_interest: Decimal,
    /// Optional premium.
    pub premium: Option<Decimal>,
    /// 24h notional volume.
    pub day_ntl_volume: Decimal,
    /// 24h base volume.
    pub day_base_volume: Decimal,
}

/// Hyperliquid normalized aggregate snapshot from the `allDexsAssetCtxs` WebSocket channel.
///
/// This feed is live-only and intentionally JSON-backed; it is not coupled to Arrow persistence.
#[custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.adapters.hyperliquid")]
pub struct HyperliquidAllDexsAssetCtxs {
    /// Normalized per-instrument entries across all perp dexes.
    #[custom_data_field(serde)]
    pub entries: Vec<HyperliquidDexAssetCtx>,
    /// UNIX timestamp (nanoseconds) when the data event occurred.
    pub ts_event: UnixNanos,
    /// UNIX timestamp (nanoseconds) when the instance was initialized.
    pub ts_init: UnixNanos,
}

/// One history row from the Hyperliquid `userTwapHistory` WebSocket channel.
///
/// Opt-in custom data (not part of default user execution subscriptions).
#[cfg_attr(
    feature = "arrow",
    custom_data(pyo3, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
#[cfg_attr(
    not(feature = "arrow"),
    custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
pub struct HyperliquidTwapHistory {
    /// User address from the subscription envelope.
    pub user: String,
    /// Venue TWAP id (`twapId`) when present on the row.
    #[custom_data_field(serde)]
    pub twap_id: Option<u64>,
    /// Raw Hyperliquid coin symbol from `state.coin`.
    pub coin: String,
    /// Resolved Nautilus instrument ID when the coin is known in cache.
    #[custom_data_field(serde)]
    pub instrument_id: Option<InstrumentId>,
    /// TWAP order side.
    #[custom_data_field(serde)]
    pub side: OrderSide,
    /// Total TWAP size.
    #[custom_data_field(serde)]
    pub size: Decimal,
    /// Executed size so far.
    #[custom_data_field(serde)]
    pub executed_size: Decimal,
    /// Executed notional so far.
    #[custom_data_field(serde)]
    pub executed_notional: Decimal,
    /// TWAP duration in minutes.
    pub minutes: u32,
    /// Whether the TWAP is reduce-only.
    pub reduce_only: bool,
    /// Whether slice timing is randomized.
    pub randomize: bool,
    /// Venue TWAP status.
    #[custom_data_field(serde)]
    pub status: HyperliquidTwapStatus,
    /// Venue status description.
    pub status_description: String,
    /// `state.timestamp` converted to UNIX nanoseconds.
    pub state_timestamp: UnixNanos,
    /// Whether this event belongs to a venue snapshot batch.
    pub is_snapshot: bool,
    /// UNIX timestamp (nanoseconds) when the history row was produced (`history.time`).
    pub ts_event: UnixNanos,
    /// UNIX timestamp (nanoseconds) when the instance was initialized.
    pub ts_init: UnixNanos,
}

/// One slice fill from the Hyperliquid `userTwapSliceFills` WebSocket channel.
///
/// Opt-in custom data (not part of default user execution subscriptions).
#[cfg_attr(
    feature = "arrow",
    custom_data(pyo3, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
#[cfg_attr(
    not(feature = "arrow"),
    custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.adapters.hyperliquid")
)]
pub struct HyperliquidTwapSliceFill {
    /// User address from the subscription envelope.
    pub user: String,
    /// Venue TWAP order identifier.
    pub twap_id: u64,
    /// Raw Hyperliquid coin symbol from the fill.
    pub coin: String,
    /// Resolved Nautilus instrument ID when the coin is known in cache.
    #[custom_data_field(serde)]
    pub instrument_id: Option<InstrumentId>,
    /// Fill price.
    #[custom_data_field(serde)]
    pub price: Decimal,
    /// Fill size.
    #[custom_data_field(serde)]
    pub size: Decimal,
    /// Fill side.
    #[custom_data_field(serde)]
    pub side: OrderSide,
    /// L1 transaction hash.
    pub hash: String,
    /// Venue order id for the slice.
    pub oid: u64,
    /// Venue trade id.
    pub tid: u64,
    /// Whether the fill crossed the spread (taker).
    pub crossed: bool,
    /// Fee amount (negative means rebate).
    #[custom_data_field(serde)]
    pub fee: Decimal,
    /// Token the fee was paid in.
    pub fee_token: String,
    /// Frontend display direction string from the venue.
    pub dir: String,
    /// Closed PnL for the fill.
    #[custom_data_field(serde)]
    pub closed_pnl: Decimal,
    /// Whether this event belongs to a venue snapshot batch.
    pub is_snapshot: bool,
    /// UNIX timestamp (nanoseconds) when the fill occurred.
    pub ts_event: UnixNanos,
    /// UNIX timestamp (nanoseconds) when the instance was initialized.
    pub ts_init: UnixNanos,
}

/// Registers Hyperliquid custom data types.
///
/// Safe to call multiple times (idempotent via internal `Once` guards).
pub fn register_hyperliquid_custom_data() {
    #[cfg(feature = "arrow")]
    {
        nautilus_serialization::ensure_custom_data_registered::<HyperliquidAllMids>();
        nautilus_serialization::ensure_custom_data_registered::<HyperliquidOpenInterest>();
        nautilus_serialization::ensure_custom_data_registered::<HyperliquidPublicTrade>();
        nautilus_serialization::ensure_custom_data_registered::<HyperliquidTwapHistory>();
        nautilus_serialization::ensure_custom_data_registered::<HyperliquidTwapSliceFill>();
    }

    #[cfg(not(feature = "arrow"))]
    {
        let _ = nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidAllMids>();
        let _ =
            nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidOpenInterest>();
        let _ =
            nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidPublicTrade>();
        let _ =
            nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidTwapHistory>();
        let _ =
            nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidTwapSliceFill>();
    }

    let _ =
        nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidAllDexsAssetCtxs>();
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    #[rstest]
    fn test_register_hyperliquid_custom_data_is_idempotent() {
        register_hyperliquid_custom_data();
        register_hyperliquid_custom_data();
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_all_mids_arrow_schema() {
        use arrow::datatypes::DataType;
        use nautilus_serialization::arrow::ArrowSchemaProvider;

        let schema = HyperliquidAllMids::get_schema(None);

        assert_eq!(schema.fields().len(), 3);
        assert_eq!(schema.field(0).name(), "mids");
        assert_eq!(schema.field(0).data_type(), &DataType::Utf8);
        assert_eq!(schema.field(1).name(), "ts_event");
        assert_eq!(schema.field(1).data_type(), &DataType::UInt64);
        assert_eq!(schema.field(2).name(), "ts_init");
        assert_eq!(schema.field(2).data_type(), &DataType::UInt64);
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_open_interest_arrow_schema() {
        use arrow::datatypes::DataType;
        use nautilus_serialization::arrow::ArrowSchemaProvider;

        let schema = HyperliquidOpenInterest::get_schema(None);

        assert_eq!(schema.fields().len(), 4);
        assert_eq!(schema.field(0).name(), "instrument_id");
        assert!(matches!(
            schema.field(0).data_type(),
            DataType::Utf8 | DataType::Utf8View
        ));
        assert_eq!(schema.field(1).name(), "open_interest");
        assert!(matches!(
            schema.field(1).data_type(),
            DataType::Utf8 | DataType::Utf8View
        ));
        assert_eq!(schema.field(2).name(), "ts_event");
        assert_eq!(schema.field(2).data_type(), &DataType::UInt64);
        assert_eq!(schema.field(3).name(), "ts_init");
        assert_eq!(schema.field(3).data_type(), &DataType::UInt64);
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_open_interest_arrow_round_trip_preserves_decimal() {
        use std::str::FromStr;

        use nautilus_model::data::Data;
        use nautilus_serialization::arrow::{DecodeDataFromRecordBatch, EncodeToRecordBatch};

        let original = HyperliquidOpenInterest::new(
            InstrumentId::from("BTC-USD-PERP.HYPERLIQUID"),
            Decimal::from_str("123456.789012345678").unwrap(),
            UnixNanos::from(1),
            UnixNanos::from(2),
        );
        let metadata = EncodeToRecordBatch::metadata(&original);
        let batch =
            HyperliquidOpenInterest::encode_batch(&metadata, std::slice::from_ref(&original))
                .unwrap();
        let decoded = HyperliquidOpenInterest::decode_data_batch(&metadata, batch).unwrap();

        assert_eq!(decoded.len(), 1);
        match &decoded[0] {
            Data::Custom(custom) => {
                let open_interest = custom
                    .data
                    .as_any()
                    .downcast_ref::<HyperliquidOpenInterest>()
                    .expect("expected HyperliquidOpenInterest");
                assert_eq!(open_interest.instrument_id, original.instrument_id);
                assert_eq!(open_interest.open_interest, original.open_interest);
                assert_eq!(open_interest.ts_event, original.ts_event);
                assert_eq!(open_interest.ts_init, original.ts_init);
            }
            other => panic!("Expected Data::Custom, was {other:?}"),
        }
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_public_trade_arrow_round_trip_preserves_counterparties() {
        use nautilus_model::{
            data::Data,
            enums::AggressorSide,
            types::{Price, Quantity},
        };
        use nautilus_serialization::arrow::{DecodeDataFromRecordBatch, EncodeToRecordBatch};

        let original = HyperliquidPublicTrade::new(
            InstrumentId::from("BTC-USD-PERP.HYPERLIQUID"),
            Price::from("100000.50"),
            Quantity::from("0.123"),
            AggressorSide::Buy,
            "123456".to_string(),
            "0xbuyer".to_string(),
            "0xseller".to_string(),
            "0xhash".to_string(),
            UnixNanos::from(1),
            UnixNanos::from(2),
        );
        let metadata = EncodeToRecordBatch::metadata(&original);
        let batch =
            HyperliquidPublicTrade::encode_batch(&metadata, std::slice::from_ref(&original))
                .unwrap();
        let decoded = HyperliquidPublicTrade::decode_data_batch(&metadata, batch).unwrap();

        let Data::Custom(custom) = &decoded[0] else {
            panic!("Expected Data::Custom");
        };
        let trade = custom
            .data
            .as_any()
            .downcast_ref::<HyperliquidPublicTrade>()
            .expect("expected HyperliquidPublicTrade");
        assert_eq!(trade.buyer, original.buyer);
        assert_eq!(trade.seller, original.seller);
        assert_eq!(trade.hash, original.hash);
        assert_eq!(trade.price, original.price);
        assert_eq!(trade.size, original.size);
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_twap_history_arrow_schema() {
        use arrow::datatypes::DataType;
        use nautilus_serialization::arrow::ArrowSchemaProvider;

        let schema = HyperliquidTwapHistory::get_schema(None);
        let names: Vec<_> = schema.fields().iter().map(|f| f.name().as_str()).collect();

        // Serde-backed fields encode as non-null Utf8 JSON.
        assert!(names.contains(&"user"));
        assert!(names.contains(&"twap_id"));
        assert!(names.contains(&"instrument_id"));
        assert!(names.contains(&"size"));
        assert!(names.contains(&"executed_notional"));
        assert!(names.contains(&"status"));
        assert!(names.contains(&"is_snapshot"));
        assert!(names.contains(&"ts_event"));
        assert!(names.contains(&"ts_init"));

        for name in ["twap_id", "size", "status"] {
            assert!(matches!(
                schema.field_with_name(name).unwrap().data_type(),
                DataType::Utf8 | DataType::Utf8View
            ));
        }
        assert_eq!(
            schema.field_with_name("ts_init").unwrap().data_type(),
            &DataType::UInt64
        );
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_twap_history_arrow_round_trip_preserves_fields() {
        use std::str::FromStr;

        use nautilus_model::{data::Data, enums::OrderSide};
        use nautilus_serialization::arrow::{DecodeDataFromRecordBatch, EncodeToRecordBatch};

        let with_options = HyperliquidTwapHistory::new(
            "0xuser".to_string(),
            Some(7),
            "BTC".to_string(),
            Some(InstrumentId::from("BTC-USD-PERP.HYPERLIQUID")),
            OrderSide::Buy,
            Decimal::from_str("1.25").unwrap(),
            Decimal::from_str("0.5").unwrap(),
            Decimal::from_str("50000.123456789").unwrap(),
            15,
            false,
            true,
            HyperliquidTwapStatus::Finished,
            "finished".to_string(),
            UnixNanos::from(10),
            true,
            UnixNanos::from(20),
            UnixNanos::from(30),
        );
        let without_options = HyperliquidTwapHistory::new(
            "0xuser2".to_string(),
            None,
            "UNK".to_string(),
            None,
            OrderSide::Sell,
            Decimal::from_str("2").unwrap(),
            Decimal::ZERO,
            Decimal::ZERO,
            60,
            true,
            false,
            HyperliquidTwapStatus::Activated,
            "activated".to_string(),
            UnixNanos::from(40),
            false,
            UnixNanos::from(50),
            UnixNanos::from(60),
        );

        for original in [&with_options, &without_options] {
            let metadata = EncodeToRecordBatch::metadata(original);
            let batch =
                HyperliquidTwapHistory::encode_batch(&metadata, std::slice::from_ref(original))
                    .unwrap();
            let decoded = HyperliquidTwapHistory::decode_data_batch(&metadata, batch).unwrap();

            let Data::Custom(custom) = &decoded[0] else {
                panic!("Expected Data::Custom");
            };
            let history = custom
                .data
                .as_any()
                .downcast_ref::<HyperliquidTwapHistory>()
                .expect("expected HyperliquidTwapHistory");
            assert_eq!(history, original);
        }
    }

    #[cfg(feature = "arrow")]
    #[rstest]
    fn test_hyperliquid_twap_slice_fill_arrow_round_trip_preserves_decimals() {
        use std::str::FromStr;

        use nautilus_model::{data::Data, enums::OrderSide};
        use nautilus_serialization::arrow::{DecodeDataFromRecordBatch, EncodeToRecordBatch};

        let original = HyperliquidTwapSliceFill::new(
            "0xslice".to_string(),
            99,
            "ETH".to_string(),
            Some(InstrumentId::from("ETH-USD-PERP.HYPERLIQUID")),
            Decimal::from_str("3456.789012345678").unwrap(),
            Decimal::from_str("0.001").unwrap(),
            OrderSide::Buy,
            "0xhash".to_string(),
            111,
            222,
            true,
            Decimal::from_str("-0.0001").unwrap(),
            "USDC".to_string(),
            "Open Long".to_string(),
            Decimal::from_str("1.23").unwrap(),
            false,
            UnixNanos::from(1),
            UnixNanos::from(2),
        );
        let metadata = EncodeToRecordBatch::metadata(&original);
        let batch =
            HyperliquidTwapSliceFill::encode_batch(&metadata, std::slice::from_ref(&original))
                .unwrap();
        let decoded = HyperliquidTwapSliceFill::decode_data_batch(&metadata, batch).unwrap();

        let Data::Custom(custom) = &decoded[0] else {
            panic!("Expected Data::Custom");
        };
        let fill = custom
            .data
            .as_any()
            .downcast_ref::<HyperliquidTwapSliceFill>()
            .expect("expected HyperliquidTwapSliceFill");
        assert_eq!(fill, &original);
        assert_eq!(fill.price, original.price);
        assert_eq!(fill.fee, original.fee);
        assert_eq!(fill.closed_pnl, original.closed_pnl);
    }
}