signet-types 0.16.2

A collection of types used in Signet.
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use crate::AggregateOrders;
use crate::MarketError;
use crate::SignedFill;
use alloy::primitives::{Address, U256};
use serde::{Deserialize, Serialize};
use signet_zenith::RollupOrders;
use std::collections::HashMap;

/// The aggregate fills, to be populated via block extracts. Generally used to
/// hold a **running** total of fills for a given user and asset across a block
/// or set of transactions.
///
/// We use the following terminology:
/// - Add: push outputs from [`RollupOrders::Filled`] into the context.
///   [`Self::add_fill`] is called when the filler transfers assets to the
///   recipient specified in an order.
/// - Remove: pull outputs from [`RollupOrders::Order`] from the context. These
///   are called when an order event is emitted by the rollup orders contract.
///   All `Orders` should be aggregated into a single [`AggregateOrders`] before
///   calling [`Self::checked_remove_aggregate`] or
///   [`Self::unchecked_remove_aggregate`].
///
/// ## Example
///
/// ```
/// # use alloy::primitives::{Address, U256};
/// # use signet_zenith::RollupOrders;
/// # use signet_types::{AggregateFills, AggregateOrders};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let fill = RollupOrders::Filled {
/// #   outputs: vec![],
/// # };
/// # let order = RollupOrders::Order {
/// #   deadline: U256::ZERO,
/// #   inputs: vec![],
/// #   outputs: vec![],
/// # };
/// let mut context = AggregateFills::default();
/// // The first argument is the chain ID of the chain that emitted the event
/// // in this case, Ethereum.
/// context.add_fill(1, &fill);
/// context.checked_remove_order(&order)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AggregateFills {
    /// Outputs to be transferred to the user. These may be on the rollup or
    /// the host or potentially elsewhere in the future.
    fills: HashMap<(u64, Address), HashMap<Address, U256>>,
}

impl AggregateFills {
    /// Create a new aggregate.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the fill balance a specific asset for a specific user.
    pub fn filled(&self, output_asset: &(u64, Address), recipient: Address) -> U256 {
        self.fills.get(output_asset).and_then(|m| m.get(&recipient)).copied().unwrap_or_default()
    }

    /// Check if the context has enough filled for the asset, recipient, and
    /// amount.
    pub fn check_filled(
        &self,
        output_asset: &(u64, Address),
        recipient: Address,
        amount: U256,
    ) -> Result<(), MarketError> {
        if self.filled(output_asset, recipient) < amount {
            return Err(MarketError::InsufficientBalance {
                chain_id: output_asset.0,
                asset: output_asset.1,
                recipient,
                amount,
            });
        }
        Ok(())
    }

    /// Add an unstructured fill to the context. The `chain_id` is the ID of
    /// of the chain on which the fill occurred.
    pub fn add_raw_fill(
        &mut self,
        chain_id: u64,
        asset: Address,
        recipient: Address,
        amount: U256,
    ) {
        let entry = self.fills.entry((chain_id, asset)).or_default().entry(recipient).or_default();
        *entry = entry.saturating_add(amount);
    }

    /// Add the amount filled to context.
    fn add_fill_output(&mut self, chain_id: u64, output: &RollupOrders::Output) {
        self.add_raw_fill(chain_id, output.token, output.recipient, output.amount)
    }

    /// Ingest a new fill into the aggregate. The chain_id is the ID
    /// of the chain which emitted the event.
    ///
    /// # Note:
    ///
    /// This uses saturating arithmetic to avoid panics. If filling more than
    /// [`U256::MAX`], re-examine life choices and don't do that.
    pub fn add_fill(&mut self, chain_id: u64, fill: &RollupOrders::Filled) {
        fill.outputs.iter().for_each(|o| self.add_fill_output(chain_id, o));
    }

    /// Ingest a [`SignedFill`] into the aggregate. The chain_id is the ID
    /// of the chain which emitted the event.
    ///
    /// # Note:
    ///
    /// This uses saturating arithmetic to avoid panics. If filling more than
    /// [`U256::MAX`], re-examine life choices and don't do that.
    pub fn add_signed_fill(&mut self, chain_id: u64, fill: &SignedFill) {
        fill.outputs.iter().for_each(|o| self.add_fill_output(chain_id, o));
    }

    /// Absorb the fills from another context.
    pub fn absorb(&mut self, other: &Self) {
        for (output_asset, recipients) in other.fills.iter() {
            let context_recipients = self.fills.entry(*output_asset).or_default();
            for (recipient, value) in recipients {
                let filled = context_recipients.entry(*recipient).or_default();
                *filled = filled.saturating_add(*value);
            }
        }
    }

    /// Unabsorb the fills from another context.
    pub fn unchecked_unabsorb(&mut self, other: &Self) -> Result<(), MarketError> {
        for (output_asset, recipients) in other.fills.iter() {
            if let Some(context_recipients) = self.fills.get_mut(output_asset) {
                for (recipient, value) in recipients {
                    if let Some(filled) = context_recipients.get_mut(recipient) {
                        *filled =
                            filled.checked_sub(*value).ok_or(MarketError::InsufficientBalance {
                                chain_id: output_asset.0,
                                asset: output_asset.1,
                                recipient: *recipient,
                                amount: *value,
                            })?;
                    }
                }
            }
        }
        Ok(())
    }

    /// Check that the context can remove the aggregate.
    pub fn check_aggregate(&self, aggregate: &AggregateOrders) -> Result<(), MarketError> {
        for (output_asset, recipients) in aggregate.outputs.iter() {
            if !self.fills.contains_key(output_asset) {
                return Err(MarketError::MissingAsset {
                    chain_id: output_asset.0,
                    asset: output_asset.1,
                });
            };

            for (recipient, value) in recipients {
                self.check_filled(output_asset, *recipient, *value)?;
            }
        }
        Ok(())
    }

    /// Take the aggregate of some orders from the context, without checking
    /// in advance whether the context has sufficient fills to remove the
    /// aggregate. If the context does not have sufficient fills, the context
    /// will be left in a bad state after returning an error.
    pub fn unchecked_remove_aggregate(
        &mut self,
        aggregate: &AggregateOrders,
    ) -> Result<(), MarketError> {
        for (output_asset, recipients) in aggregate.outputs.iter() {
            let context_recipients =
                self.fills.get_mut(output_asset).ok_or(MarketError::MissingAsset {
                    chain_id: output_asset.0,
                    asset: output_asset.1,
                })?;

            for (recipient, amount) in recipients {
                let filled = context_recipients.get_mut(recipient).ok_or(
                    MarketError::InsufficientBalance {
                        chain_id: output_asset.0,
                        asset: output_asset.1,
                        recipient: *recipient,
                        amount: *amount,
                    },
                )?;
                *filled = filled.checked_sub(*amount).ok_or(MarketError::InsufficientBalance {
                    chain_id: output_asset.0,
                    asset: output_asset.1,
                    recipient: *recipient,
                    amount: *amount,
                })?;
            }
        }

        Ok(())
    }

    /// Remove the aggregate of some orders from the context, checking in
    /// advance that the context has sufficient fills to remove the aggregate.
    pub fn checked_remove_aggregate(
        &mut self,
        aggregate: &AggregateOrders,
    ) -> Result<(), MarketError> {
        self.check_aggregate(aggregate)?;

        for (output_asset, recipients) in aggregate.outputs.iter() {
            let context_recipients =
                self.fills.get_mut(output_asset).expect("checked in check_aggregate");

            for (recipient, amount) in recipients {
                let filled = context_recipients.get_mut(recipient).unwrap();
                *filled = filled.checked_sub(*amount).unwrap();
            }
        }

        Ok(())
    }

    /// Check that the context can take the order.
    pub fn check_order(&self, order: &RollupOrders::Order) -> Result<(), MarketError> {
        self.check_aggregate(&std::iter::once(order).collect())
    }

    /// Take the order from the context, checking in advance that the context
    /// has sufficient fills to remove the order.
    pub fn checked_remove_order(&mut self, order: &RollupOrders::Order) -> Result<(), MarketError> {
        let aggregate = std::iter::once(order).collect();
        self.check_aggregate(&aggregate)?;
        self.unchecked_remove_aggregate(&aggregate)
    }

    /// Take the order from the context, without checking in advance that the
    /// context has sufficient fills to remove the order. If the context does
    /// not have sufficient fills, the context will be left in a bad state
    /// after returning an error.
    pub fn unchecked_remove_order(
        &mut self,
        order: &RollupOrders::Order,
    ) -> Result<(), MarketError> {
        let aggregate = std::iter::once(order).collect();
        self.unchecked_remove_aggregate(&aggregate)
    }

    /// Borrow the current fill mapping.
    pub const fn fills(&self) -> &HashMap<(u64, Address), HashMap<Address, U256>> {
        &self.fills
    }

    /// Mutably borrow the current fill mapping
    pub const fn fills_mut(&mut self) -> &mut HashMap<(u64, Address), HashMap<Address, U256>> {
        &mut self.fills
    }

    /// Check the events emitted by a rollup transaction against the context.
    ///
    /// This will process all fills first, and all orders second.
    pub fn check_ru_tx_events(
        &self,
        fills: &AggregateFills,
        orders: &AggregateOrders,
    ) -> Result<(), MarketError> {
        // Check the aggregate against the combined contexts.
        let combined = CombinedContext { context: self, extra: fills };

        combined.check_aggregate(orders)?;

        Ok(())
    }

    /// Check and remove the events emitted by a rollup transaction. This
    /// function allows atomic ingestion of multiple Fills and Orders. If
    /// the check fails, the aggregate will not be mutated.
    ///
    /// This will process all fills first, and all orders second.
    pub fn checked_remove_ru_tx_events(
        &mut self,
        fills: &AggregateFills,
        orders: &AggregateOrders,
    ) -> Result<(), MarketError> {
        self.check_ru_tx_events(fills, orders)?;
        self.absorb(fills);
        self.unchecked_remove_aggregate(orders)
    }

    /// Check and remove the events emitted by a rollup transaction. This
    /// function allows atomic ingestion of multiple Fills and Orders. **If
    /// the check fails, the aggregate may be mutated.**
    pub fn unchecked_remove_ru_tx_events(
        &mut self,
        fills: &AggregateFills,
        orders: &AggregateOrders,
    ) -> Result<(), MarketError> {
        self.absorb(fills);
        self.unchecked_remove_aggregate(orders)
    }
}

/// A combined context for checking aggregates. This allows us to check with
/// fills, without mutating the context.
struct CombinedContext<'a, 'b> {
    context: &'a AggregateFills,
    extra: &'b AggregateFills,
}

impl CombinedContext<'_, '_> {
    /// Get the combined balance of the context and the extra context.
    fn balance(&self, output_asset: &(u64, Address), recipient: Address) -> U256 {
        self.context
            .filled(output_asset, recipient)
            .saturating_add(self.extra.filled(output_asset, recipient))
    }

    /// Check if the combined context has enough filled for the asset,
    /// recipient, and amount.
    fn check_filled(
        &self,
        output_asset: &(u64, Address),
        recipient: Address,
        amount: U256,
    ) -> Result<(), MarketError> {
        if self.balance(output_asset, recipient) < amount {
            return Err(MarketError::InsufficientBalance {
                chain_id: output_asset.0,
                asset: output_asset.1,
                recipient,
                amount,
            });
        }
        Ok(())
    }

    /// Check the aggregate against the combined context.
    fn check_aggregate(&self, aggregate: &AggregateOrders) -> Result<(), MarketError> {
        for (output_asset, recipients) in aggregate.outputs.iter() {
            for (recipient, amount) in recipients {
                self.check_filled(output_asset, *recipient, *amount)?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use signet_zenith::RollupOrders::{Filled, Order, Output};

    #[test]
    fn basic_fills() {
        let user_a = Address::with_last_byte(1);
        let user_b = Address::with_last_byte(2);

        let asset_a = Address::with_last_byte(3);
        let asset_b = Address::with_last_byte(4);

        // The orders contain the minimum amount for the fill.
        let a_to_a =
            Output { token: asset_a, amount: U256::from(100), recipient: user_a, chainId: 1 };
        let b_to_b =
            Output { token: asset_b, amount: U256::from(200), recipient: user_b, chainId: 1 };
        let a_to_b =
            Output { token: asset_a, amount: U256::from(300), recipient: user_b, chainId: 1 };

        let fill = Filled { outputs: vec![a_to_a, b_to_b, a_to_b] };

        let order =
            Order { deadline: U256::ZERO, inputs: vec![], outputs: vec![a_to_a, b_to_b, a_to_b] };

        let mut context = AggregateFills::default();
        context.add_fill(1, &fill);

        assert_eq!(context.fills().len(), 2);
        assert_eq!(
            context.fills().get(&(1, asset_a)).unwrap().get(&user_a).unwrap(),
            &U256::from(100)
        );
        assert_eq!(
            context.fills().get(&(1, asset_b)).unwrap().get(&user_b).unwrap(),
            &U256::from(200)
        );
        assert_eq!(
            context.fills().get(&(1, asset_a)).unwrap().get(&user_b).unwrap(),
            &U256::from(300)
        );

        context.checked_remove_order(&order).unwrap();
        assert_eq!(context.fills().len(), 2);
        assert_eq!(
            context.fills().get(&(1, asset_a)).unwrap().get(&user_a).unwrap(),
            &U256::from(0)
        );
        assert_eq!(
            context.fills().get(&(1, asset_b)).unwrap().get(&user_b).unwrap(),
            &U256::from(0)
        );
        assert_eq!(
            context.fills().get(&(1, asset_a)).unwrap().get(&user_b).unwrap(),
            &U256::from(0)
        );
    }

    // Empty removal should work
    #[test]
    fn empty_everything() {
        AggregateFills::default()
            .checked_remove_ru_tx_events(&Default::default(), &Default::default())
            .unwrap();
    }

    #[test]
    fn absorb_unabsorb() {
        let mut context_a = AggregateFills::default();
        let mut context_b = AggregateFills::default();
        let user = Address::with_last_byte(1);
        let asset = Address::with_last_byte(2);
        context_a.add_raw_fill(1, asset, user, U256::from(100));
        context_b.add_raw_fill(1, asset, user, U256::from(200));

        let pre_absorb = context_a.clone();
        context_a.absorb(&context_b);
        assert_eq!(context_a.filled(&(1, asset), user), U256::from(300));
        context_a.unchecked_unabsorb(&context_b).unwrap();
        assert_eq!(context_a, pre_absorb);
    }

    // === CombinedContext Overflow Tests ===

    #[test]
    fn combined_context_saturates_on_overflow() {
        // Verifies saturating addition prevents overflow
        let mut context = AggregateFills::default();
        let mut extra = AggregateFills::default();

        let user = Address::with_last_byte(1);
        let asset = Address::with_last_byte(2);

        // Context has near-max, extra has enough that would overflow with wrapping add
        context.add_raw_fill(1, asset, user, U256::MAX - U256::from(100));
        extra.add_raw_fill(1, asset, user, U256::from(200));

        let mut orders = AggregateOrders::new();
        orders.ingest_raw_output(1, asset, user, U256::from(150));

        // With saturating_add, balance saturates to U256::MAX, which is >= 150
        context.check_ru_tx_events(&extra, &orders).unwrap();
    }

    #[test]
    fn combined_context_near_max_no_overflow() {
        let mut context = AggregateFills::default();
        let mut extra = AggregateFills::default();

        let user = Address::with_last_byte(1);
        let asset = Address::with_last_byte(2);

        let half_max = U256::MAX / U256::from(2);
        context.add_raw_fill(1, asset, user, half_max);
        extra.add_raw_fill(1, asset, user, U256::from(100));

        let mut orders = AggregateOrders::new();
        orders.ingest_raw_output(1, asset, user, half_max + U256::from(50));

        context.check_ru_tx_events(&extra, &orders).unwrap();
    }

    // === Boundary Tests ===

    #[test]
    fn fill_saturates_at_max() {
        let mut context = AggregateFills::default();
        context.add_raw_fill(1, Address::ZERO, Address::ZERO, U256::MAX);
        context.add_raw_fill(1, Address::ZERO, Address::ZERO, U256::from(1));
        assert_eq!(context.filled(&(1, Address::ZERO), Address::ZERO), U256::MAX);
    }

    #[test]
    fn remove_max_from_max() {
        let mut context = AggregateFills::default();
        context.add_raw_fill(1, Address::ZERO, Address::ZERO, U256::MAX);

        let mut aggregate = AggregateOrders::new();
        aggregate.ingest_raw_output(1, Address::ZERO, Address::ZERO, U256::MAX);

        context.checked_remove_aggregate(&aggregate).unwrap();
        assert_eq!(context.filled(&(1, Address::ZERO), Address::ZERO), U256::ZERO);
    }

    #[test]
    fn absorb_saturates_at_max() {
        let mut a = AggregateFills::default();
        let mut b = AggregateFills::default();

        a.add_raw_fill(1, Address::ZERO, Address::ZERO, U256::MAX);
        b.add_raw_fill(1, Address::ZERO, Address::ZERO, U256::from(1000));

        a.absorb(&b);
        assert_eq!(a.filled(&(1, Address::ZERO), Address::ZERO), U256::MAX);
    }

    // === Panic/Error Path Tests ===

    #[test]
    fn unchecked_remove_aggregate_errors_on_missing_recipient() {
        let mut context = AggregateFills::default();
        let asset = Address::with_last_byte(1);

        // Add fill for asset but different recipient
        context.add_raw_fill(1, asset, Address::with_last_byte(99), U256::from(100));

        let mut aggregate = AggregateOrders::new();
        aggregate.ingest_raw_output(1, asset, Address::with_last_byte(2), U256::from(50));

        let result = context.unchecked_remove_aggregate(&aggregate);
        assert!(matches!(result, Err(MarketError::InsufficientBalance { .. })));
    }

    #[test]
    fn checked_remove_handles_missing_recipient() {
        let mut context = AggregateFills::default();
        let asset = Address::with_last_byte(1);

        context.add_raw_fill(1, asset, Address::with_last_byte(99), U256::from(100));

        let mut aggregate = AggregateOrders::new();
        aggregate.ingest_raw_output(1, asset, Address::with_last_byte(2), U256::from(50));

        let result = context.checked_remove_aggregate(&aggregate);
        assert!(matches!(result, Err(MarketError::InsufficientBalance { .. })));
    }

    #[test]
    fn insufficient_balance_error_fields() {
        let context = AggregateFills::default();
        let result = context.check_filled(
            &(42, Address::with_last_byte(1)),
            Address::with_last_byte(2),
            U256::from(100),
        );

        let err = result.unwrap_err();
        assert!(matches!(err, MarketError::InsufficientBalance { chain_id: 42, .. }));
    }

    #[test]
    fn missing_asset_error_fields() {
        let context = AggregateFills::default();
        let mut aggregate = AggregateOrders::new();
        aggregate.ingest_raw_output(42, Address::with_last_byte(1), Address::ZERO, U256::from(100));

        let result = context.check_aggregate(&aggregate);
        assert!(matches!(result, Err(MarketError::MissingAsset { chain_id: 42, .. })));
    }

    // === Multi-Chain Tests ===

    #[test]
    fn fills_across_multiple_chains() {
        let mut context = AggregateFills::default();
        let user = Address::with_last_byte(1);
        let asset = Address::with_last_byte(2);

        context.add_raw_fill(1, asset, user, U256::from(100));
        context.add_raw_fill(10, asset, user, U256::from(200));
        context.add_raw_fill(42161, asset, user, U256::from(300));

        assert_eq!(context.filled(&(1, asset), user), U256::from(100));
        assert_eq!(context.filled(&(10, asset), user), U256::from(200));
        assert_eq!(context.filled(&(42161, asset), user), U256::from(300));
    }

    #[test]
    fn remove_from_wrong_chain_fails() {
        let mut context = AggregateFills::default();
        let user = Address::with_last_byte(1);
        let asset = Address::with_last_byte(2);

        context.add_raw_fill(1, asset, user, U256::from(100));

        let mut aggregate = AggregateOrders::new();
        aggregate.ingest_raw_output(10, asset, user, U256::from(50));

        let result = context.checked_remove_aggregate(&aggregate);
        assert!(matches!(result, Err(MarketError::MissingAsset { chain_id: 10, .. })));
    }

    // === Clone/Equality Tests ===

    #[test]
    fn clone_equality() {
        let mut context = AggregateFills::default();
        context.add_raw_fill(
            1,
            Address::with_last_byte(1),
            Address::with_last_byte(2),
            U256::from(12345),
        );
        context.add_raw_fill(10, Address::with_last_byte(3), Address::with_last_byte(2), U256::MAX);

        let cloned = context.clone();
        assert_eq!(context, cloned);
    }
}

#[cfg(all(test, feature = "proptest"))]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    fn nonzero_u256() -> impl Strategy<Value = U256> {
        any::<U256>().prop_map(|x| if x == U256::ZERO { U256::from(1) } else { x })
    }

    proptest! {
        #[test]
        fn add_then_remove_returns_to_zero(
            chain_id in 0u64..100,
            asset_byte in 0u8..=255,
            recipient_byte in 0u8..=255,
            amount in nonzero_u256(),
        ) {
            let mut context = AggregateFills::default();
            let asset = Address::with_last_byte(asset_byte);
            let recipient = Address::with_last_byte(recipient_byte);

            context.add_raw_fill(chain_id, asset, recipient, amount);

            let mut aggregate = AggregateOrders::new();
            aggregate.ingest_raw_output(chain_id, asset, recipient, amount);

            context.checked_remove_aggregate(&aggregate).unwrap();
            prop_assert_eq!(context.filled(&(chain_id, asset), recipient), U256::ZERO);
        }

        #[test]
        fn fill_addition_is_commutative(
            chain_id in 0u64..100,
            asset_byte in 0u8..=255,
            recipient_byte in 0u8..=255,
            amount_a in any::<U256>(),
            amount_b in any::<U256>(),
        ) {
            let asset = Address::with_last_byte(asset_byte);
            let recipient = Address::with_last_byte(recipient_byte);

            let mut ab = AggregateFills::default();
            ab.add_raw_fill(chain_id, asset, recipient, amount_a);
            ab.add_raw_fill(chain_id, asset, recipient, amount_b);

            let mut ba = AggregateFills::default();
            ba.add_raw_fill(chain_id, asset, recipient, amount_b);
            ba.add_raw_fill(chain_id, asset, recipient, amount_a);

            prop_assert_eq!(ab, ba);
        }
    }
}