Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Parser] Update parser.MatchOperations to do non-negative and non-positive matching #305

Merged

Conversation

KNWR
Copy link
Contributor

@KNWR KNWR commented Mar 10, 2021

Motivation

There are several cases where the matcher may expect opposite amounts but zero amounts are sent. Instead of having clients implement additional fallback logic, we want to update the sdk to be able to handle non-negative or non-positive scenarios when expecting opposite amounts.

Solution

Add a new OppositeOrZeroAmounts matcher and corresponding matching logic instead of replacing the existing OppositeAmounts matcher. This way, this isn't a breaking change for other users of the library.

Add PositiveAmountSignOrZero and NegativeAmountSignOrZero amount signs for amount matching when using OppositeOrZeroAmounts. We could just use AnyAmountSign, but these additional signs let us get the same order of matches as with OppositeAmounts.

Open questions

  1. Does the order of elements in the Matches array returned by MatchOperations matter?

@KNWR KNWR changed the title Update parser.MatchOperations to do non-negative and non-positive matching [Parser] Update parser.MatchOperations to do non-negative and non-positive matching Mar 11, 2021
@KNWR KNWR marked this pull request as ready for review March 12, 2021 23:58
Copy link
Contributor

@septerr septerr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great! left some smoll comments! 🙏

PositiveAmountSign = 2

// PositiveOrZeroAmountSign is a positive or zero amount.
PositiveAmountSignOrZero = 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment (PositiveOrZeroAmountSign ) and constant names (PositiveAmountSignOrZero ) don't match.

I would personally prefer PositiveOrZeroAmountSign, for naming consistency, even though zero is not a sign.

PositiveAmountSignOrZero = 3

// NegativeOrZeroAmountSign is a positive or zero amount.
NegativeAmountSignOrZero = 4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before :)

// OppositeZeroAmounts are specified using the operation indices of
// OperationDescriptions to handle out of order matches. MatchOperations
// will error if all groups of operations aren't 0 or opposites.
OppositeOrZeroAmounts [][]int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name in comment (OppositeZeroAmounts ) and constant name (OppositeOrZeroAmounts ) do not match.

oppositeAmountCheckers := []func(*types.Operation, *types.Operation) error{
oppositeOrZeroAmounts,
oppositeAmounts,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 3d array makes grasping the code kinda difficult (this file is pretty difficult to read as is :( ).

Can we instead utilize a function to which we pass amountPairs, amountChecker and anything else that is needed (matches) and call it twice. Once for OppositeAmounts and once for OppositeOrZeroAmounts ? 🙏

@KNWR KNWR requested a review from septerr March 17, 2021 11:25
septerr
septerr previously approved these changes Mar 17, 2021
@KNWR KNWR force-pushed the add-nonnegative-and-non-positive-matching branch 2 times, most recently from cd25d86 to fa48c1d Compare March 17, 2021 22:24
septerr
septerr previously approved these changes Mar 17, 2021
@KNWR KNWR force-pushed the add-nonnegative-and-non-positive-matching branch from fa48c1d to da643b1 Compare March 17, 2021 23:47
@septerr septerr merged commit 9ba330f into coinbase:master Mar 17, 2021
@KNWR KNWR deleted the add-nonnegative-and-non-positive-matching branch March 17, 2021 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants