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

Clean up the order submission #3447

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
30 changes: 13 additions & 17 deletions execution/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,24 +423,22 @@ 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) {
timer := metrics.NewTimeCounter(orderSubmission.MarketId, "execution", "SubmitOrder")

defer func() {
timer.EngineTimeCounterAdd()
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")
order := orderSubmission.IntoOrder(party)

mkt, ok := e.markets[order.MarketId]
mkt, ok := e.markets[orderSubmission.MarketId]
if !ok {
e.idgen.SetID(order)

// adding rejected order to the buf
order.Status = types.Order_STATUS_REJECTED
order.Reason = types.OrderError_ORDER_ERROR_INVALID_MARKET_ID
evt := events.NewOrderEvent(ctx, order)
e.broker.Send(evt)

timer.EngineTimeCounterAdd()
return nil, types.ErrInvalidMarketID
}

Expand All @@ -450,23 +448,21 @@ func (e *Engine) SubmitOrder(ctx context.Context, order *types.Order) (*types.Or

conf, err := mkt.SubmitOrder(ctx, order)
if err != nil {
timer.EngineTimeCounterAdd()
return nil, err
}

if conf.Order.Status == types.Order_STATUS_FILLED {
metrics.OrderGaugeAdd(-1, order.MarketId)
}

timer.EngineTimeCounterAdd()
return conf, nil
}

// AmendOrder takes order amendment details and attempts to amend the order
// 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 +895,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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/pseudomuto/protoc-gen-doc v1.3.2 // indirect
github.com/rs/cors v1.7.0
github.com/satori/go.uuid v1.2.0
github.com/shopspring/decimal v1.2.0 // indirect
github.com/shopspring/decimal v1.2.0
github.com/spf13/afero v1.1.2
github.com/stretchr/testify v1.6.1
github.com/tendermint/tendermint v0.33.8
Expand Down