Skip to content

Commit

Permalink
add sub account optional flag (#414)
Browse files Browse the repository at this point in the history
* add subaccount optional flag

* added subaccount validation with optional param

* gen

* comments
  • Loading branch information
shrimalmadhur committed Jun 8, 2022
1 parent 18b4ab8 commit e2d4265
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 10 deletions.
44 changes: 35 additions & 9 deletions parser/match_operations.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2022 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -105,7 +105,11 @@ type MetadataDescription struct {

// AccountDescription is used to describe a *types.AccountIdentifier.
type AccountDescription struct {
Exists bool
Exists bool

// SubAccountOptional If this is true then SubAccountExists, SubAccountAddress,
// SubAccountMetadataKeys matching is ignored
SubAccountOptional bool
SubAccountExists bool
SubAccountAddress string
SubAccountMetadataKeys []*MetadataDescription
Expand Down Expand Up @@ -213,6 +217,16 @@ func accountMatch(req *AccountDescription, account *types.AccountIdentifier) err
return nil
}

if req.SubAccountOptional {
// Optionally can require a certain subaccount address if subaccount is present
if account.SubAccount != nil {
if err := verifySubAccountAddress(req.SubAccountAddress, account.SubAccount); err != nil {
return err
}
}
return nil
}

if account.SubAccount == nil {
if req.SubAccountExists {
return ErrAccountMatchSubAccountMissing
Expand All @@ -226,13 +240,8 @@ func accountMatch(req *AccountDescription, account *types.AccountIdentifier) err
}

// Optionally can require a certain subaccount address
if len(req.SubAccountAddress) > 0 && account.SubAccount.Address != req.SubAccountAddress {
return fmt.Errorf(
"%w: expected %s but got %s",
ErrAccountMatchUnexpectedSubAccountAddr,
req.SubAccountAddress,
account.SubAccount.Address,
)
if err := verifySubAccountAddress(req.SubAccountAddress, account.SubAccount); err != nil {
return err
}

if err := metadataMatch(req.SubAccountMetadataKeys, account.SubAccount.Metadata); err != nil {
Expand All @@ -242,6 +251,23 @@ func accountMatch(req *AccountDescription, account *types.AccountIdentifier) err
return nil
}

// verifySubAccountAddress verifies the sub-account address if
// sub-account is present.
func verifySubAccountAddress(
subAccountAddress string,
subAccount *types.SubAccountIdentifier,
) error {
if len(subAccountAddress) > 0 && subAccount.Address != subAccountAddress {
return fmt.Errorf(
"%w: expected %s but got %s",
ErrAccountMatchUnexpectedSubAccountAddr,
subAccountAddress,
subAccount.Address,
)
}
return nil
}

// amountMatch returns an error if a *types.Amount does not meet an
// *AmountDescription.
func amountMatch(req *AmountDescription, amount *types.Amount) error {
Expand Down
104 changes: 103 additions & 1 deletion parser/match_operations_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2022 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1408,6 +1408,108 @@ func TestMatchOperations(t *testing.T) {
matches: nil,
err: true,
},
"sub account optional - when sub-account exist": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr1",
SubAccount: &types.SubAccountIdentifier{
Address: "sub 2",
},
},
},
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
},
},
descriptions: &Descriptions{
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
SubAccountOptional: true,
SubAccountExists: true,
SubAccountAddress: "sub 2",
},
},
{
Account: &AccountDescription{
Exists: true,
SubAccountOptional: true,
SubAccountExists: true,
SubAccountAddress: "sub 1",
},
},
},
},
matches: []*Match{
{
Operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr1",
SubAccount: &types.SubAccountIdentifier{
Address: "sub 2",
},
},
},
},
Amounts: []*big.Int{nil},
},
{
Operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
},
},
Amounts: []*big.Int{nil},
},
},
err: false,
},
"sub account optional - when sub-account address is different": {
operations: []*types.Operation{
{
Account: &types.AccountIdentifier{
Address: "addr1",
SubAccount: &types.SubAccountIdentifier{
Address: "sub 2",
},
},
},
{
Account: &types.AccountIdentifier{
Address: "addr2",
},
},
},
descriptions: &Descriptions{
OperationDescriptions: []*OperationDescription{
{
Account: &AccountDescription{
Exists: true,
SubAccountOptional: true,
SubAccountExists: true,
SubAccountAddress: "sub 3",
},
},
{
Account: &AccountDescription{
Exists: true,
SubAccountOptional: true,
SubAccountExists: true,
SubAccountAddress: "sub 1",
},
},
},
},
matches: nil,
err: true,
},
"nil descriptions": {
operations: []*types.Operation{
{
Expand Down

0 comments on commit e2d4265

Please sign in to comment.