Skip to content

Commit

Permalink
POST and DELETE methods for trade orders
Browse files Browse the repository at this point in the history
  • Loading branch information
tolyo committed Dec 7, 2023
1 parent 93729ae commit 641d9b5
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
container_name: exchange_db
image: postgres:15
ports:
- 5432:5432
- "5432:5432"
volumes:
- postgres-storage:/var/lib/postgresql/data
env_file:
Expand All @@ -25,4 +25,4 @@ services:
- ./pkg/conf/dev.env
- ./pkg/conf/docker.env
ports:
- 4000:4000
- "4000:4000"
79 changes: 75 additions & 4 deletions pkg/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ paths:
$ref: '#/components/schemas/Trade'
'404':
description: Error
delete:
tags:
- user
summary: Cancel trade
description: Cancels a trade order by id
operationId: deleteTradeById
parameters:
- $ref: '#/components/parameters/TradingAccountId'
- $ref: '#/components/parameters/TradeId'
responses:
'204':
description: Success
'404':
description: Error
/trades/{trading_account_id}:
get:
tags:
Expand Down Expand Up @@ -198,6 +212,38 @@ paths:
operationId: createTrade
parameters:
- $ref: '#/components/parameters/TradingAccountId'
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
instrument:
$ref: '#/components/schemas/InstrumentName'
side:
$ref: '#/components/schemas/TradeOrderSide'
type:
$ref: '#/components/schemas/TradeOrderType'
timeInForce:
$ref: '#/components/schemas/TradeOrderTimeInForce'
amount:
$ref: '#/components/schemas/MoneyAmount'
price:
$ref: '#/components/schemas/MoneyAmount'
required:
- instrument
- side
- type
- timeInForce
- amount
example:
instrument: BTC-EUR
side: SELL
type: LIMIT
timeInForce: GTC
amount: '100'
price: '100.00'
responses:
'200':
description: Success
Expand Down Expand Up @@ -382,6 +428,13 @@ components:
enum:
- SELL
- BUY
TradeOrderType:
type: string
enum:
- LIMIT
- MARKET
- STOPLOSS
- STOPLIMIT
TradeOrderTimeInForce:
type: string
enum:
Expand All @@ -396,6 +449,18 @@ components:
- OPEN
- REJECTED
- CANCELLED
- PARTIALLY_CANCELLED
- PARTIALLY_REJECTED
- FILLED
- REJECTED
MoneyAmount:
type: string
format: decimal
example: '100.50'
DateTime:
type: string
format: date-time
example: '2017-07-21T17:32:28Z'
TradeOrder:
type: object
properties:
Expand All @@ -405,18 +470,24 @@ components:
$ref: '#/components/schemas/InstrumentName'
side:
$ref: '#/components/schemas/TradeOrderSide'
type:
$ref: '#/components/schemas/TradeOrderType'
timeInForce:
$ref: '#/components/schemas/TradeOrderTimeInForce'
status:
$ref: '#/components/schemas/TradeOrderStatus'
price:
$ref: '#/components/schemas/MoneyAmount'
amount:
$ref: '#/components/schemas/MoneyAmount'
openAmount:
$ref: '#/components/schemas/MoneyAmount'
created:
$ref: '#/components/schemas/DateTime'
TradeOrderList:
type: array
items:
$ref: '#/components/schemas/TradeOrder'
MoneyAmount:
type: string
format: decimal
example: '100.50'
PaymentAccount:
type: object
description: Payment account available to user
Expand Down
32 changes: 32 additions & 0 deletions pkg/api/endpoints/user/trade_orders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ post:
operationId: createTrade
parameters:
- $ref: '../../models/trading_account.yaml#/components/parameters/TradingAccountId'
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
instrument:
$ref: '../../models/instrument.yaml#/components/schemas/InstrumentName'
side:
$ref: '../../models/trade_order.yaml#/components/schemas/TradeOrderSide'
type:
$ref: '../../models/trade_order.yaml#/components/schemas/TradeOrderType'
timeInForce:
$ref: '../../models/trade_order.yaml#/components/schemas/TradeOrderTimeInForce'
amount:
$ref: '../../models/shared.yaml#/components/schemas/MoneyAmount'
price:
$ref: '../../models/shared.yaml#/components/schemas/MoneyAmount'
required:
- instrument
- side
- type
- timeInForce
- amount
example:
instrument: "BTC-EUR"
side: "SELL"
type: "LIMIT"
timeInForce: "GTC"
amount: "100"
price: "100.00"

responses:
"200":
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/endpoints/user/trades_by_id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ get:
$ref: '../../models/trade.yaml#/components/schemas/Trade'
"404":
description: Error

delete:
tags:
- user
summary: Cancel trade
description: Cancels a trade order by id
operationId: deleteTradeById
parameters:
- $ref: '../../models/trading_account.yaml#/components/parameters/TradingAccountId'
- $ref: '../../models/trade.yaml#/components/parameters/TradeId'
responses:
"204":
description: Success

"404":
description: Error
6 changes: 6 additions & 0 deletions pkg/api/models/shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ components:
format: decimal
example: "100.50"

DateTime:
type: string
format: date-time
example: "2017-07-21T17:32:28Z"

parameters:
PaginationPage:
name: page
Expand All @@ -19,6 +24,7 @@ components:
type: integer
default: 1
example: 2

PaginationLimit:
name: limit
in: query
Expand Down
26 changes: 24 additions & 2 deletions pkg/api/models/trade_order.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@ components:
- FOK
- GTD
- GTT
TradeOrderType:
type: string
enum:
- LIMIT
- MARKET
- STOPLOSS
- STOPLIMIT

TradeOrderStatus:
type: string
enum:
- OPEN
- REJECTED
- CANCELLED
- PARTIALLY_CANCELLED
- PARTIALLY_REJECTED
- FILLED
- REJECTED

TradeOrderList:
type: array
Expand All @@ -32,15 +43,26 @@ components:
type: object
properties:
id:
$ref: ./shared.yaml#/components/schemas/Id
$ref: './shared.yaml#/components/schemas/Id'
instrument:
$ref: ./instrument.yaml#/components/schemas/InstrumentName
$ref: './instrument.yaml#/components/schemas/InstrumentName'
side:
$ref: '#/components/schemas/TradeOrderSide'
type:
$ref: '#/components/schemas/TradeOrderType'
timeInForce:
$ref: '#/components/schemas/TradeOrderTimeInForce'
status:
$ref: '#/components/schemas/TradeOrderStatus'
price:
$ref: './shared.yaml#/components/schemas/MoneyAmount'
amount:
$ref: './shared.yaml#/components/schemas/MoneyAmount'
openAmount:
$ref: './shared.yaml#/components/schemas/MoneyAmount'
created:
$ref: './shared.yaml#/components/schemas/DateTime'




Expand Down
5 changes: 4 additions & 1 deletion pkg/models/trade_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type TradeOrder struct {
OpenAmount float64 // Order amount available for trading
Status OrderStatus
TimeInForce OrderTimeInForce
Created string
}

const tradeOrderBaseQuery = `
Expand All @@ -80,7 +81,8 @@ const tradeOrderBaseQuery = `
t.amount,
t.open_amount,
t.status::text,
t.time_in_force::text
t.time_in_force::text,
t.created_at
FROM trade_order AS t
INNER JOIN trading_account ta
Expand All @@ -102,6 +104,7 @@ func GetTradeOrder(id TradeOrderId) TradeOrder {
&order.OpenAmount,
&order.Status,
&order.TimeInForce,
&order.Created,
)
return order
}
2 changes: 1 addition & 1 deletion pkg/rest/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
#
#**/api_currencies_service.go
**/api_public_service.go
#**/helpers.go
3 changes: 2 additions & 1 deletion pkg/rest/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ api/api.go
api/api_admin.go
api/api_admin_service.go
api/api_public.go
api/api_public_service.go
api/api_user.go
api/api_user_service.go
api/api_users.go
Expand All @@ -13,6 +12,7 @@ api/helpers.go
api/impl.go
api/logger.go
api/model_app_entity.go
api/model_create_trade_request.go
api/model_currency.go
api/model_currency_list.go
api/model_fx_instrument.go
Expand All @@ -28,4 +28,5 @@ api/model_trade_order.go
api/model_trade_order_side.go
api/model_trade_order_status.go
api/model_trade_order_time_in_force.go
api/model_trade_order_type.go
api/openapi.yaml
2 changes: 1 addition & 1 deletion pkg/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To see how to make this your own, look here:
[README](https://openapi-generator.tech)

- API version: 1.0.0
- Build date: 2023-12-07T01:13:40.551728+02:00[Europe/Riga]
- Build date: 2023-12-07T20:17:17.914519+02:00[Europe/Riga]


### Running the server
Expand Down
4 changes: 3 additions & 1 deletion pkg/rest/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type PublicAPIRouter interface {
// The UserAPIRouter implementation should parse necessary information from the http request,
// pass the data to a UserAPIServicer to perform the required actions, then write the service results to the http response.
type UserAPIRouter interface {
DeleteTradeById(http.ResponseWriter, *http.Request)
GetBookOrders(http.ResponseWriter, *http.Request)
GetPaymentAccounts(http.ResponseWriter, *http.Request)
GetTradeById(http.ResponseWriter, *http.Request)
Expand Down Expand Up @@ -79,6 +80,7 @@ type PublicAPIServicer interface {
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type UserAPIServicer interface {
DeleteTradeById(context.Context, interface{}, interface{}) (ImplResponse, error)
GetBookOrders(context.Context, interface{}) (ImplResponse, error)
GetPaymentAccounts(context.Context, interface{}) (ImplResponse, error)
GetTradeById(context.Context, interface{}, interface{}) (ImplResponse, error)
Expand All @@ -91,5 +93,5 @@ type UserAPIServicer interface {
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type UsersAPIServicer interface {
CreateTrade(context.Context, interface{}) (ImplResponse, error)
CreateTrade(context.Context, interface{}, CreateTradeRequest) (ImplResponse, error)
}
20 changes: 20 additions & 0 deletions pkg/rest/api/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) Router {
// Routes returns all the api routes for the UserAPIController
func (c *UserAPIController) Routes() Routes {
return Routes{
"DeleteTradeById": Route{
strings.ToUpper("Delete"),
"/trades/{trading_account_id}/id/{trade_id}",
c.DeleteTradeById,
},
"GetBookOrders": Route{
strings.ToUpper("Get"),
"/book_orders/{trading_account_id}",
Expand Down Expand Up @@ -77,6 +82,21 @@ func (c *UserAPIController) Routes() Routes {
}
}

// DeleteTradeById - Cancel trade
func (c *UserAPIController) DeleteTradeById(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tradingAccountIdParam := params["trading_account_id"]
tradeIdParam := params["trade_id"]
result, err := c.service.DeleteTradeById(r.Context(), tradingAccountIdParam, tradeIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}

// GetBookOrders - Get book orders
func (c *UserAPIController) GetBookOrders(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
Expand Down
Loading

0 comments on commit 641d9b5

Please sign in to comment.