Skip to content

Commit

Permalink
[FIX] Update comparison match logic for opposite amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwu7 committed Jul 20, 2020
1 parent f929bfc commit d2c618a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ coverage.html
examples/server/server
examples/client/client
examples/fetcher/fetcher

.idea/
41 changes: 33 additions & 8 deletions parser/match_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,16 +437,41 @@ func comparisonMatch(
if err := matchIndexValid(matches, amountMatch[1]); err != nil {
return fmt.Errorf("%w: opposite amounts comparison error", err)
}
for _, op := range matches[amountMatch[0]].Operations {
for _, otherOp := range matches[amountMatch[1]].Operations {
if err := oppositeAmounts(
op,
otherOp,
); err != nil {
return fmt.Errorf("%w: amounts not opposites", err)
}

match1Ops := matches[amountMatch[0]].Operations
match2Ops := matches[amountMatch[1]].Operations

if len(match1Ops) != len(match2Ops) {
return errors.New("opposite amounts comparison error: unequal length between match amounts")
}
// continue if nothing to compare
if len(match1Ops) == 0 {
continue
}
// check if all operations within the same match slice have the same amount
for i := 1; i < len(match1Ops); i++ {
if err := equalAmounts([]*types.Operation{
match1Ops[i-1],
match1Ops[i],
}); err != nil {
return fmt.Errorf(
"%w: opposite amounts comparison error: amounts not equal within the same match amounts array", err)
}
if err := equalAmounts([]*types.Operation{
match2Ops[i-1],
match2Ops[i],
}); err != nil {
return fmt.Errorf(
"%w: opposite amounts comparison error: amounts not equal within the same match amounts array", err)
}
}
// check for opposites amount
if err := oppositeAmounts(
match1Ops[0],
match2Ops[0],
); err != nil {
return fmt.Errorf("%w: amounts not opposites", err)
}
}

return nil
Expand Down

0 comments on commit d2c618a

Please sign in to comment.