Skip to content

Commit

Permalink
v0.2.0 Release
Browse files Browse the repository at this point in the history
* v0.2.0 Release
  • Loading branch information
jonte-z authored Apr 21, 2023
1 parent a07fff4 commit c9ecd29
Show file tree
Hide file tree
Showing 25 changed files with 565 additions and 315 deletions.
38 changes: 31 additions & 7 deletions .github/workflows/connector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,41 @@ on:
- rc-**

jobs:
Checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: './go.mod'
- name: Check Formatting
run: go fmt ./...
- name: Vet Project
run: go vet ./...
- name: Initialise StaticCheck
run: go get honnef.co/go/tools/cmd/staticcheck@latest && go install honnef.co/go/tools/cmd/staticcheck@latest
- name: StaticCheck - Check for unused functions, duplicate imports + more
run: staticcheck ./...
- name: Initialise DeadCode
run: go get github.com/remyoudompheng/go-misc/deadcode && go install github.com/remyoudompheng/go-misc/deadcode
- name: DeadCode - Check for unused variables
run: deadcode -test
- name: Initialise ErrCheck
run: go get github.com/kisielk/errcheck@latest && go install github.com/kisielk/errcheck@latest
- name: ErrCheck - Detect any unchecked errors
run: errcheck ./...
- name: Install GoSec
run: go get github.com/securego/gosec/v2/cmd/gosec@latest && go install github.com/securego/gosec/v2/cmd/gosec@latest
- name: GoSec - Scan for potential security issues with gosec
run: gosec -tests -confidence high ./...
UnitTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version-file: './go.mod'
- name: Format
run: ./scripts/checks.sh format
- name: Vet
run: ./scripts/checks.sh vet
- name: UnitTest
run: go test -v .
- name: Run Unit Tests
run: go test -v -race -covermode=atomic .
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# Change log

## v0.2.0 - 2023-04-21

### Added
- WebsocketStreamClient
- Websocket Client can be initialized with 2 parameters, `NewWebsocketStreamClient(isCombined, baseURL)`:
- `isCombined` is a MANDATORY boolean value that specifies whether you are calling a combined stream or not.
- If `isCombined` is set to `true`, `"/stream?streams="` will be appended to the `baseURL` to allow for Combining streams.
- Otherwise, if set to `false`, `"/ws/"` will be appended to the `baseURL`.
- `baseURL` is an OPTIONAL string value that determines the base URL to use for the websocket connection.
- If `baseURL` is not set, it will default to the Live Exchange URL: `"wss://stream.binance.com:9443"`.

### Fixed
- Order Response Types for `CreateOrderService` - `POST /api/v3/order`
- Added support for all 3 Order Response Types - `ACK`, `RESULT` and `FULL`
- Order Response Types for `MarginAccountNewOrderService` - `POST /sapi/v1/margin/order`
- Added support for all 3 Order Response Types - `ACK`, `RESULT` and `FULL`
- Different Response Types for `MarginIsolatedAccountInfoService` - `GET /sapi/v1/margin/isolated/account`
- Added support for both Response Types, depending on whether `symbols` is set or not.

### Changed
- Renamed `WsAllMiniMarketsStatServe` to `WsAllMarketMiniTickersStatServe`.
- Renamed `WsMarketsStatServe` to `WsMarketTickersStatServe`.
- Renamed `WsAllMarketsStatServe` to `WsAllMarketTickersStatServe`.
- Updated Github Action `UnitTest`.

### Removed
- Removed unused `setFormParam`, `setFormParams`, `WithRecvWindow`, `WithHeader` and `WithHeaders` functions from `request.go`.

## v0.1.0 - 2023-03-31

- First release
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ func main() {
Please find more examples for each supported endpoint in the `examples` folder.

## Websocket API
Initialising Websocket Client
- Websocket Client can be initialized with 2 parameters, `NewWebsocketStreamClient(isCombined, baseURL)`:
- `isCombined` is a MANDATORY boolean value that specifies whether you are calling a combined stream or not.
- If `isCombined` is set to `true`, `"/stream?streams="` will be appended to the `baseURL` to allow for Combining streams.
- Otherwise, if set to `false`, `"/ws/"` will be appended to the `baseURL`.
- `baseURL` is an OPTIONAL string value that determines the base URL to use for the websocket connection.
- If `baseURL` is not set, it will default to the Live Exchange URL: `"wss://stream.binance.com:9443"`.

```go
// Initialise Websocket Client with Production baseURL and false for "isCombined" parameter

websocketStreamClient := binance_connector.NewWebsocketStreamClient(false, "wss://testnet.binance.vision")

// Initialise Websocket Client with Production baseURL and true for "isCombined" parameter

websocketStreamClient := binance_connector.NewWebsocketStreamClient(true)
```

Diff. Depth Stream Example

Expand All @@ -91,6 +108,9 @@ import (
)

func main() {
// Initialise Websocket Client with Testnet BaseURL and false for "isCombined" parameter
websocketStreamClient := binance_connector.NewWebsocketStreamClient(false, "wss://testnet.binance.vision")

wsDepthHandler := func(event *binance_connector.WsDepthEvent) {
fmt.Println(binance_connector.PrettyPrint(event))
}
Expand All @@ -100,21 +120,57 @@ func main() {
}

// Depth stream subscription
doneCh, stopCh, err := binance_connector.WsDepthServe("BNBUSDT", wsDepthHandler, errHandler)
doneCh, stopCh, err := websocketStreamClient.WsDepthServe("BNBUSDT", wsDepthHandler, errHandler)
if err != nil {
fmt.Println(err)
return
}

go func() {
time.Sleep(30 * time.Second)
stopCh <- struct{}{} // use stopC to stop streaming
stopCh <- struct{}{} // use stopCh to stop streaming
}()

<-doneCh
}
```

Combined Diff. Depth Stream Example

```go
package main

import (
"fmt"
"time"

binance_connector "github.com/binance/binance-connector-go"
)

func main() {
// Set isCombined parameter to true as we are using Combined Depth Stream
websocketStreamClient := binance_connector.NewWebsocketStreamClient(true)

wsCombinedDepthHandler := func(event *binance_connector.WsDepthEvent) {
fmt.Println(binance_connector.PrettyPrint(event))
}
errHandler := func(err error) {
fmt.Println(err)
}
// Use WsCombinedDepthServe to subscribe to multiple streams
doneCh, stopCh, err := websocketStreamClient.WsCombinedDepthServe([]string{"LTCBTC", "BTCUSDT", "MATICUSDT"}, wsCombinedDepthHandler, errHandler)
if err != nil {
fmt.Println(err)
return
}
go func() {
time.Sleep(5 * time.Second)
stopCh <- struct{}{}
}()
<-doneCh
}
```

## Base URL
- Binance provides alternative Production URLs in case of performance issues:
- https://api1.binance.com
Expand Down
92 changes: 83 additions & 9 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,15 @@ func (s *CreateOrderService) SelfTradePreventionMode(selfTradePreventionMode str
return s
}

const (
ACK = 1
RESULT = 2
FULL = 3
)

// Do send request
func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res *CreateOrderResponse, err error) {
func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res interface{}, err error) {
respType := ACK
r := &request{
method: http.MethodPost,
endpoint: "/api/v3/order",
Expand All @@ -276,6 +283,12 @@ func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res
r.setParam("symbol", s.symbol)
r.setParam("side", s.side)
r.setParam("type", s.orderType)
switch s.orderType {
case "MARKET":
respType = FULL
case "LIMIT":
respType = FULL
}
if s.timeInForce != nil {
r.setParam("timeInForce", *s.timeInForce)
}
Expand Down Expand Up @@ -305,6 +318,15 @@ func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res
}
if s.newOrderRespType != nil {
r.setParam("newOrderRespType", *s.newOrderRespType)
switch *s.newOrderRespType {
case "ACK":
respType = ACK
case "RESULT":
respType = RESULT
case "FULL":
respType = FULL
}

}
if s.selfTradePreventionMode != nil {
r.setParam("selfTradePreventionMode", *s.selfTradePreventionMode)
Expand All @@ -313,21 +335,73 @@ func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res
if err != nil {
return nil, err
}
res = new(CreateOrderResponse)
switch respType {
case ACK:
res = new(CreateOrderResponseACK)
case RESULT:
res = new(CreateOrderResponseRESULT)
case FULL:
res = new(CreateOrderResponseFULL)
}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}

// Create CreateOrderResponse
type CreateOrderResponse struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
OrderIdList []int64 `json:"orderIdList"`
ClientOrderId string `json:"clientOrderId"`
TransactTime uint64 `json:"transactTime"`
// Create CreateOrderResponseACK
type CreateOrderResponseACK struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
OrderListId int64 `json:"orderListId"`
ClientOrderId string `json:"clientOrderId"`
TransactTime uint64 `json:"transactTime"`
}

// Create CreateOrderResponseRESULT
type CreateOrderResponseRESULT struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
OrderListId int64 `json:"orderListId"`
ClientOrderId string `json:"clientOrderId"`
TransactTime uint64 `json:"transactTime"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
CumulativeQuoteQty string `json:"cummulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
WorkingTime uint64 `json:"workingTime"`
SelfTradePreventionMode string `json:"selfTradePreventionMode"`
}

// Create CreateOrderResponseFULL
type CreateOrderResponseFULL struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
OrderListId int64 `json:"orderListId"`
ClientOrderId string `json:"clientOrderId"`
TransactTime uint64 `json:"transactTime"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
CumulativeQuoteQty string `json:"cummulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
WorkingTime uint64 `json:"workingTime"`
SelfTradePreventionMode string `json:"selfTradePreventionMode"`
Fills []struct {
Price string `json:"price"`
Qty string `json:"qty"`
Commission string `json:"commission"`
CommissionAsset string `json:"commissionAsset"`
TradeId int64 `json:"tradeId"`
} `json:"fills"`
}

// Binance Cancel Order endpoint (DELETE /api/v3/order)
Expand Down
6 changes: 3 additions & 3 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ func (s *accountTestSuite) TestNewOrder() {
Do(ctx)

s.r().NoError(err)
e := &CreateOrderResponse{
e := &CreateOrderResponseFULL{
Symbol: "BTCUSDT",
OrderId: 28,
}
s.assertCreateOrderResponseEqual(e, res)
s.assertCreateOrderResponseEqual(e, res.(*CreateOrderResponseFULL))
}

func (s *accountTestSuite) assertCreateOrderResponseEqual(e, a *CreateOrderResponse) {
func (s *accountTestSuite) assertCreateOrderResponseEqual(e, a *CreateOrderResponseFULL) {
r := s.r()
r.Equal(e.Symbol, a.Symbol, "Symbol")
r.Equal(e.OrderId, a.OrderId, "OrderId")
Expand Down
12 changes: 5 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -40,11 +40,9 @@ type doFunc func(req *http.Request) (*http.Response, error)

// Globals
const (
baseAPIMainURL = "https://api.binance.com"
baseAPITestnetURL = "https://testnet.binance.vision"
timestampKey = "timestamp"
signatureKey = "signature"
recvWindowKey = "recvWindow"
timestampKey = "timestamp"
signatureKey = "signature"
recvWindowKey = "recvWindow"
)

func currentTimestamp() int64 {
Expand Down Expand Up @@ -162,7 +160,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
if err != nil {
return []byte{}, err
}
data, err = ioutil.ReadAll(res.Body)
data, err = io.ReadAll(res.Body)
if err != nil {
return []byte{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package binance_connector

const Name = "binance-connector-go"

const Version = "0.1.0"
const Version = "0.2.0"
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ func main() {
}

func WsAllMarketMiniTickers() {
wsAllMarketMiniTickersHandler := func(event binance_connector.WsAllMiniMarketsStatEvent) {
websocketStreamClient := binance_connector.NewWebsocketStreamClient(false)
wsAllMarketMiniTickersHandler := func(event binance_connector.WsAllMarketMiniTickersStatEvent) {
fmt.Println(binance_connector.PrettyPrint(event))
}
errHandler := func(err error) {
fmt.Println(err)
}
doneCh, _, err := binance_connector.WsAllMiniMarketsStatServe(wsAllMarketMiniTickersHandler, errHandler)
doneCh, _, err := websocketStreamClient.WsAllMarketMiniTickersStatServe(wsAllMarketMiniTickersHandler, errHandler)
if err != nil {
fmt.Println(err)
return
Expand Down
5 changes: 3 additions & 2 deletions examples/websocket/AllMarketTickers/AllMarketTickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ func main() {
}

func WsAllMarketTickersExample() {
wsAllMarketTickersHandler := func(event binance_connector.WsAllMarketsStatEvent) {
websocketStreamClient := binance_connector.NewWebsocketStreamClient(false)
wsAllMarketTickersHandler := func(event binance_connector.WsAllMarketTickersStatEvent) {
fmt.Println(binance_connector.PrettyPrint(event))
}
errHandler := func(err error) {
fmt.Println(err)
}
doneCh, _, err := binance_connector.WsAllMarketsStatServe(wsAllMarketTickersHandler, errHandler)
doneCh, _, err := websocketStreamClient.WsAllMarketTickersStatServe(wsAllMarketTickersHandler, errHandler)
if err != nil {
fmt.Println(err)
return
Expand Down
Loading

0 comments on commit c9ecd29

Please sign in to comment.