Skip to content

Commit

Permalink
Clean up the order submission
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinTrinque committed May 3, 2021
1 parent d4df24e commit 6c1adc0
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 256 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ vendor/
.vscode/
proto/**.orig
*.pb.go-re
gomock_reflect_*
1 change: 0 additions & 1 deletion GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ git clone git@github.com:vegaprotocol/vega.git
cd vega
git status # On branch develop, Your branch is up to date with 'origin/develop'.

make gettools_build # get the build tools
make deps # get the source dependencies
make install # build the binaries and put them in $GOPATH/bin

Expand Down
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ race: ## Run data race detector

.PHONY: mocks
mocks: ## Make mocks
@[ -d vendor ] && mv vendor vendor.tmp
@go generate ./...
@[ -d vendor.tmp ] && mv vendor.tmp vendor
@./script/build.sh -a mocks

.PHONY: msan
msan: ## Run memory sanitizer
Expand Down Expand Up @@ -125,10 +123,6 @@ print_check: ## Check for fmt.Print functions in Go code
rm -f "$$f" && \
if test "$$count" -gt 0 ; then exit 1 ; fi

.PHONY: gettools_build
gettools_build:
@./script/gettools.sh build

.PHONY: gettools_develop
gettools_develop:
@./script/gettools.sh develop
Expand Down
21 changes: 15 additions & 6 deletions execution/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,24 @@ func (e *Engine) removeMarket(mktID string) {
}

// SubmitOrder checks the incoming order and submits it to a Vega market.
func (e *Engine) SubmitOrder(ctx context.Context, order *types.Order) (*types.OrderConfirmation, error) {
func (e *Engine) SubmitOrder(ctx context.Context, orderSubmission *commandspb.OrderSubmission, party string) (confirmation *types.OrderConfirmation, returnedErr error) {
defer func() {
e.notifyFailureOnError(ctx, returnedErr, orderSubmission, party)
}()

if e.log.IsDebug() {
e.log.Debug("submit order", logging.Order(*order))
e.log.Debug("submit order", logging.OrderSubmission(orderSubmission))
}

timer := metrics.NewTimeCounter(order.MarketId, "execution", "SubmitOrder")
timer := metrics.NewTimeCounter(orderSubmission.MarketId, "execution", "SubmitOrder")

order := orderSubmission.IntoOrder(party)

mkt, ok := e.markets[order.MarketId]
mkt, ok := e.markets[orderSubmission.MarketId]
if !ok {
// FIXME(jeremy) We shouldn't define an ID on a failed submission order
// but this is a dirty fix for storage purpose. We'll improve this
// later on.
e.idgen.SetID(order)

// adding rejected order to the buf
Expand Down Expand Up @@ -466,7 +475,7 @@ func (e *Engine) SubmitOrder(ctx context.Context, order *types.Order) (*types.Or
// if it exists and is in a editable state.
func (e *Engine) AmendOrder(ctx context.Context, orderAmendment *commandspb.OrderAmendment) (confirmation *types.OrderConfirmation, returnedErr error) {
defer func() {
e.notifyFailureOnError(ctx, returnedErr, orderAmendment.PartyId, orderAmendment)
e.notifyFailureOnError(ctx, returnedErr, orderAmendment, orderAmendment.PartyId)
}()

if e.log.IsDebug() {
Expand Down Expand Up @@ -899,7 +908,7 @@ func (e *Engine) OnMarketProbabilityOfTradingTauScalingUpdate(ctx context.Contex
return nil
}

func (e *Engine) notifyFailureOnError(ctx context.Context, err error, partyID string, tx interface{}) {
func (e *Engine) notifyFailureOnError(ctx context.Context, err error, tx interface{}, partyID string) {
if err != nil {
e.broker.Send(events.NewTxErrEvent(ctx, err, partyID, tx))
}
Expand Down
3 changes: 3 additions & 0 deletions execution/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,12 +1149,15 @@ func (m *Market) releaseMarginExcess(ctx context.Context, partyID string) {

// SubmitOrder submits the given order
func (m *Market) SubmitOrder(ctx context.Context, order *types.Order) (*types.OrderConfirmation, error) {
order.CreatedAt = m.currentTime.UnixNano()

if !m.canTrade() {
order.Status = types.Order_STATUS_REJECTED
order.Reason = types.OrderError_ORDER_ERROR_MARKET_CLOSED
m.broker.Send(events.NewOrderEvent(ctx, order))
return nil, ErrTradingNotAllowed
}

conf, err := m.submitOrder(ctx, order, true)
if err != nil {
return nil, err
Expand Down
66 changes: 28 additions & 38 deletions gateway/graphql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 13 additions & 17 deletions gateway/graphql/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,10 +1663,10 @@ func (r *myMutationResolver) SubmitTransaction(ctx context.Context, data string,
}, nil
}

func (r *myMutationResolver) PrepareOrderSubmit(ctx context.Context, market, party string, price *string, size string, side Side,
func (r *myMutationResolver) PrepareOrderSubmit(ctx context.Context, market string, price *string, size string, side Side,
timeInForce OrderTimeInForce, expiration *string, ty OrderType, reference *string, po *PeggedOrderInput) (*PreparedSubmitOrder, error) {

order := &commandspb.OrderSubmission{}
orderSubmission := &commandspb.OrderSubmission{}

var (
p uint64
Expand All @@ -1680,28 +1680,24 @@ func (r *myMutationResolver) PrepareOrderSubmit(ctx context.Context, market, par
return nil, err
}
}
order.Price = p
orderSubmission.Price = p
s, err := safeStringUint64(size)
if err != nil {
return nil, err
}
order.Size = s
orderSubmission.Size = s
if len(market) <= 0 {
return nil, errors.New("market missing or empty")
}
order.MarketId = market
if len(party) <= 0 {
return nil, errors.New("party missing or empty")
}
orderSubmission.MarketId = market

order.PartyId = party
if order.TimeInForce, err = convertOrderTimeInForceToProto(timeInForce); err != nil {
if orderSubmission.TimeInForce, err = convertOrderTimeInForceToProto(timeInForce); err != nil {
return nil, err
}
if order.Side, err = convertSideToProto(side); err != nil {
if orderSubmission.Side, err = convertSideToProto(side); err != nil {
return nil, err
}
if order.Type, err = convertOrderTypeToProto(ty); err != nil {
if orderSubmission.Type, err = convertOrderTypeToProto(ty); err != nil {
return nil, err
}

Expand All @@ -1714,27 +1710,27 @@ func (r *myMutationResolver) PrepareOrderSubmit(ctx context.Context, market, par
if err != nil {
return nil, err
}
order.PeggedOrder = &types.PeggedOrder{Reference: pegreference,
orderSubmission.PeggedOrder = &types.PeggedOrder{Reference: pegreference,
Offset: offset}
}

// GTT must have an expiration value
if order.TimeInForce == types.Order_TIME_IN_FORCE_GTT && expiration != nil {
if orderSubmission.TimeInForce == types.Order_TIME_IN_FORCE_GTT && expiration != nil {
var expiresAt time.Time
expiresAt, err = vegatime.Parse(*expiration)
if err != nil {
return nil, fmt.Errorf("cannot parse expiration time: %s - invalid format sent to create order (example: 2018-01-02T15:04:05Z)", *expiration)
}

// move to pure timestamps or convert an RFC format shortly
order.ExpiresAt = expiresAt.UnixNano()
orderSubmission.ExpiresAt = expiresAt.UnixNano()
}
if reference != nil {
order.Reference = *reference
orderSubmission.Reference = *reference
}

req := protoapi.PrepareSubmitOrderRequest{
Submission: order,
Submission: orderSubmission,
}

// Pass the order over for consensus (service layer will use RPC client internally and handle errors etc)
Expand Down
2 changes: 0 additions & 2 deletions gateway/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ type Mutation {
prepareOrderSubmit(
"ID of the market to place the order"
marketId: ID!
"ID of the party placing the order"
partyId: ID!
"Price of the asset"
price: String
"Size of the order"
Expand Down
2 changes: 1 addition & 1 deletion integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func FeatureContext(s *godog.Suite) {
return steps.TradersAmendTheFollowingOrders(execsetup.errorHandler, execsetup.broker, execsetup.executionEngine, table)
})
s.Step(`^the traders place the following pegged orders:$`, func(table *gherkin.DataTable) error {
return steps.TradersPlacePeggedOrders(execsetup.executionEngine, table)
return steps.TradersPlaceTheFollowingPeggedOrders(execsetup.executionEngine, table)
})
s.Step(`^the traders deposit on asset's general account the following amount:$`, func(table *gherkin.DataTable) error {
return steps.TradersDepositAssets(execsetup.collateralEngine, execsetup.broker, table)
Expand Down
2 changes: 1 addition & 1 deletion integration/steps/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *CancelOrderError) Unwrap() error { return c.Err }

type SubmitOrderError struct {
reference string
request types.Order
request commandspb.OrderSubmission
Err error
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func parseMarkPrice(markPriceStr string) uint64 {
}

func errWrongMarkPrice(market string, markPrice uint64, marketData types.MarketData) error {
return fmt.Errorf("mark price if wrong for market(%v), expected(%v) got(%v)",
return fmt.Errorf("wrong mark price for market(%v), expected(%v) got(%v)",
market, markPrice, marketData.MarkPrice,
)
}
Expand Down

0 comments on commit 6c1adc0

Please sign in to comment.