Skip to content

Commit

Permalink
Order book resource implmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tolyo committed Dec 10, 2023
1 parent 3531706 commit bc96f3c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ func main() {
models.LoadFees(fees)
}

// Sample seed for debugging
//_, tradingAccount1 := services.Acc("test")
//services.ProcessTradeOrder(tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10.00, 1, "GTC")

server := rest.NewServer()
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}

}
27 changes: 18 additions & 9 deletions pkg/rest/api/api_public_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"errors"
"net/http"
"open-outcry/pkg/models"
"open-outcry/pkg/services"
"open-outcry/pkg/utils"
)

// PublicAPIService is a service that implements the logic for the PublicAPIServicer
Expand Down Expand Up @@ -72,14 +74,21 @@ func (s *PublicAPIService) GetInstruments(ctx context.Context) (ImplResponse, er

// GetOrderBook - Get order book
func (s *PublicAPIService) GetOrderBook(ctx context.Context, instrumentName string) (ImplResponse, error) {
// TODO - update GetOrderBook with the required logic for this service method.
// Add api_public_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

// TODO: Uncomment the next line to return response Response(200, OrderBook{}) or use other options such as http.Ok ...
// return Response(200, OrderBook{}), nil

// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil
res := services.GetOrderBook(models.InstrumentName(instrumentName))
orderBook := OrderBook{
Sell: utils.Map[models.PriceVolume, PriceVolume](res.SellSide, func(v models.PriceVolume) PriceVolume {
return PriceVolume{
Price: float32(v.Price),
Volume: float32(v.Volume),
}
}),
Buy: utils.Map[models.PriceVolume, PriceVolume](res.BuySide, func(v models.PriceVolume) PriceVolume {
return PriceVolume{
Price: float32(v.Price),
Volume: float32(v.Volume),
}
}),
}

return Response(http.StatusNotImplemented, nil), errors.New("GetOrderBook method not implemented")
return Response(http.StatusOK, orderBook), nil
}
4 changes: 2 additions & 2 deletions pkg/rest/api/model_order_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
package api

type OrderBook struct {
Sell []PriceVolume `json:"sell,omitempty"`
Sell []PriceVolume `json:"sell"`

Buy *interface{} `json:"buy,omitempty"`
Buy []PriceVolume `json:"buy"`
}

// AssertOrderBookRequired checks if the required fields are not zero-ed
Expand Down
5 changes: 4 additions & 1 deletion pkg/services/order_book_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func GetOrderBook(instrumentName models.InstrumentName) models.OrderBook {
ORDER BY price ASC, side DESC
`, instrumentName)

orderBook := models.OrderBook{}
orderBook := models.OrderBook{
SellSide: make([]models.PriceVolume, 0),
BuySide: make([]models.PriceVolume, 0),
}
for _, entry := range res {
switch entry.Side {
case models.Sell:
Expand Down
19 changes: 18 additions & 1 deletion pkg/services/order_book_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,24 @@ func (assert *ServiceTestSuite) TestGetOrderBook() {
{Price: 1.6, Volume: 10},
{Price: 1.4, Volume: 10},
},
SellSide: nil,
SellSide: []models.PriceVolume{},
}, GetOrderBook("BTC_EUR"))

ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10.7, 100, "GTC")
ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10.6, 100, "GTC")
ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10.7, 100, "GTC")
ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10.4, 100, "GTC")

assert.Equal(models.OrderBook{
BuySide: []models.PriceVolume{
{Price: 1.7, Volume: 20},
{Price: 1.6, Volume: 10},
{Price: 1.4, Volume: 10},
},
SellSide: []models.PriceVolume{
{Price: 10.4, Volume: 100},
{Price: 10.6, Volume: 100},
{Price: 10.7, Volume: 200},
},
}, GetOrderBook("BTC_EUR"))
}

0 comments on commit bc96f3c

Please sign in to comment.