code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/parties_amend_the_following_orders.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package steps
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/integration/stubs"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  
    26  	"github.com/cucumber/godog"
    27  )
    28  
    29  type OrderAmendmentError struct {
    30  	OrderAmendment types.OrderAmendment
    31  	OrderReference string
    32  	Err            error
    33  }
    34  
    35  func (o OrderAmendmentError) Error() string {
    36  	return fmt.Sprintf("%v: %v", o.OrderAmendment, o.Err)
    37  }
    38  
    39  func PartiesAmendTheFollowingOrders(
    40  	broker *stubs.BrokerStub,
    41  	exec Execution,
    42  	table *godog.Table,
    43  ) error {
    44  	for _, r := range parseAmendOrderTable(table) {
    45  		row := amendOrderRow{row: r}
    46  
    47  		o, err := broker.GetByReference(row.Party(), row.Reference())
    48  		if err != nil {
    49  			return errOrderNotFound(row.Reference(), row.Party(), err)
    50  		}
    51  
    52  		amend := types.OrderAmendment{
    53  			OrderID:      o.Id,
    54  			MarketID:     o.MarketId,
    55  			Price:        row.Price(),
    56  			SizeDelta:    row.SizeDelta(),
    57  			Size:         row.Size(),
    58  			ExpiresAt:    row.ExpirationDate(),
    59  			TimeInForce:  row.TimeInForce(),
    60  			PeggedOffset: row.PeggedOffset(),
    61  		}
    62  
    63  		_, err = exec.AmendOrder(context.Background(), &amend, o.PartyId)
    64  		if err := checkExpectedError(row, err, nil); err != nil {
    65  			return err
    66  		}
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  type amendOrderRow struct {
    73  	row RowWrapper
    74  }
    75  
    76  func parseAmendOrderTable(table *godog.Table) []RowWrapper {
    77  	return StrictParseTable(table, []string{
    78  		"party",
    79  		"reference",
    80  		"tif",
    81  	}, []string{
    82  		"price",
    83  		"size delta",
    84  		"size",
    85  		"error",
    86  		"expiration date",
    87  		"pegged offset",
    88  	})
    89  }
    90  
    91  func (r amendOrderRow) Party() string {
    92  	return r.row.MustStr("party")
    93  }
    94  
    95  func (r amendOrderRow) Reference() string {
    96  	return r.row.MustStr("reference")
    97  }
    98  
    99  func (r amendOrderRow) Price() *num.Uint {
   100  	return r.row.MaybeUint("price")
   101  }
   102  
   103  func (r amendOrderRow) SizeDelta() int64 {
   104  	if !r.row.HasColumn("size delta") {
   105  		return 0
   106  	}
   107  	return r.row.MustI64("size delta")
   108  }
   109  
   110  func (r amendOrderRow) Size() *uint64 {
   111  	return r.row.MaybeU64("size")
   112  }
   113  
   114  func (r amendOrderRow) TimeInForce() types.OrderTimeInForce {
   115  	return r.row.MustTIF("tif")
   116  }
   117  
   118  func (r amendOrderRow) ExpirationDate() *int64 {
   119  	if !r.row.HasColumn("expiration date") {
   120  		return nil
   121  	}
   122  
   123  	timeNano := r.row.MustTime("expiration date").UnixNano()
   124  	if timeNano == 0 {
   125  		return nil
   126  	}
   127  
   128  	return &timeNano
   129  }
   130  
   131  func (r amendOrderRow) Error() string {
   132  	return r.row.Str("error")
   133  }
   134  
   135  func (r amendOrderRow) ExpectError() bool {
   136  	return r.row.HasColumn("error")
   137  }
   138  
   139  func (r amendOrderRow) PeggedOffset() *num.Uint {
   140  	return r.row.MaybeUint("pegged offset")
   141  }