@@ -525,7 +525,58 @@ func checkOps(requests [][]int, matches []*Match, valid func([]*types.Operation)
525
525
return nil
526
526
}
527
527
528
- // comparisonMatch ensures collections of *types.Operations
528
+ // compareOppositeMatches ensures collections of *types.Operation
529
+ // that may have opposite amounts contain valid matching amounts
530
+ func compareOppositeMatches (
531
+ amountPairs [][]int ,
532
+ matches []* Match ,
533
+ amountChecker func (* types.Operation , * types.Operation ) error ,
534
+ ) error {
535
+ for _ , amountMatch := range amountPairs {
536
+ if len (amountMatch ) != oppositesLength { // cannot have opposites without exactly 2
537
+ return fmt .Errorf ("cannot check opposites of %d operations" , len (amountMatch ))
538
+ }
539
+
540
+ // compare all possible pairs
541
+ if err := matchIndexValid (matches , amountMatch [0 ]); err != nil {
542
+ return fmt .Errorf ("%w: amounts comparison error" , err )
543
+ }
544
+ if err := matchIndexValid (matches , amountMatch [1 ]); err != nil {
545
+ return fmt .Errorf ("%w: amounts comparison error" , err )
546
+ }
547
+
548
+ match0Ops := matches [amountMatch [0 ]].Operations
549
+ match1Ops := matches [amountMatch [1 ]].Operations
550
+ if err := equalAmounts (match0Ops ); err != nil {
551
+ return fmt .Errorf (
552
+ "%w: amounts comparison error for match index %d" ,
553
+ err ,
554
+ amountMatch [0 ],
555
+ )
556
+ }
557
+ if err := equalAmounts (match1Ops ); err != nil {
558
+ return fmt .Errorf (
559
+ "%w: amounts comparison error for match index %d" ,
560
+ err ,
561
+ amountMatch [1 ],
562
+ )
563
+ }
564
+
565
+ // only need to check amount for the very first operation from each
566
+ // matched operations group since we made sure all amounts within the same
567
+ // matched operation group are the same
568
+ if err := amountChecker (
569
+ match0Ops [0 ],
570
+ match1Ops [0 ],
571
+ ); err != nil {
572
+ return fmt .Errorf ("%w: amounts do not match the amountChecker function" , err )
573
+ }
574
+ }
575
+
576
+ return nil
577
+ }
578
+
579
+ // comparisonMatch ensures collections of *types.Operation
529
580
// have either equal or opposite amounts.
530
581
func comparisonMatch (
531
582
descriptions * Descriptions ,
@@ -539,56 +590,11 @@ func comparisonMatch(
539
590
return fmt .Errorf ("%w: operation addresses not equal" , err )
540
591
}
541
592
542
- oppositeAmountMatchingOperations := [][][]int {
543
- descriptions .OppositeOrZeroAmounts ,
544
- descriptions .OppositeAmounts ,
593
+ if err := compareOppositeMatches (descriptions .OppositeAmounts , matches , oppositeAmounts ); err != nil {
594
+ return fmt .Errorf ("%w: operation amounts not opposite" , err )
545
595
}
546
- oppositeAmountCheckers := []func (* types.Operation , * types.Operation ) error {
547
- oppositeOrZeroAmounts ,
548
- oppositeAmounts ,
549
- }
550
-
551
- for i := range oppositeAmountMatchingOperations {
552
- for _ , amountMatch := range oppositeAmountMatchingOperations [i ] {
553
- if len (amountMatch ) != oppositesLength { // cannot have opposites without exactly 2
554
- return fmt .Errorf ("cannot check opposites of %d operations" , len (amountMatch ))
555
- }
556
-
557
- // compare all possible pairs
558
- if err := matchIndexValid (matches , amountMatch [0 ]); err != nil {
559
- return fmt .Errorf ("%w: opposite amounts comparison error" , err )
560
- }
561
- if err := matchIndexValid (matches , amountMatch [1 ]); err != nil {
562
- return fmt .Errorf ("%w: opposite amounts comparison error" , err )
563
- }
564
-
565
- match0Ops := matches [amountMatch [0 ]].Operations
566
- match1Ops := matches [amountMatch [1 ]].Operations
567
- if err := equalAmounts (match0Ops ); err != nil {
568
- return fmt .Errorf (
569
- "%w: opposite amounts comparison error for match index %d" ,
570
- err ,
571
- amountMatch [0 ],
572
- )
573
- }
574
- if err := equalAmounts (match1Ops ); err != nil {
575
- return fmt .Errorf (
576
- "%w: opposite amounts comparison error for match index %d" ,
577
- err ,
578
- amountMatch [1 ],
579
- )
580
- }
581
-
582
- // only need to check opposites amount for the very first operation from each
583
- // matched operations group since we made sure all amounts within the same
584
- // matched operation group are the same
585
- if err := oppositeAmountCheckers [i ](
586
- match0Ops [0 ],
587
- match1Ops [0 ],
588
- ); err != nil {
589
- return fmt .Errorf ("%w: amounts not opposites or both 0" , err )
590
- }
591
- }
596
+ if err := compareOppositeMatches (descriptions .OppositeOrZeroAmounts , matches , oppositeOrZeroAmounts ); err != nil {
597
+ return fmt .Errorf ("%w: both operation amounts not opposite and not zero" , err )
592
598
}
593
599
594
600
return nil
0 commit comments