Skip to content

Commit

Permalink
feat: added wss futures client and implemted 'order.place', 'order.ca… (
Browse files Browse the repository at this point in the history
adshao#609)

* feat: added wss futures client and implemted 'order.place', 'order.cancel' endpoints

* feat: added proxy env for webscoket connection, unit/integration tests, sync write method and support 3 sign functions

* feat: moved service constructors in client_ws.go (requested from PR)

---------

Co-authored-by: Artur Abelian <[email protected]>
Co-authored-by: ddxyq <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2024
1 parent a836b57 commit 5c80e99
Show file tree
Hide file tree
Showing 14 changed files with 2,010 additions and 1 deletion.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,101 @@ delivery.UseTestnet = true
BinanceClient = delivery.NewClient(ApiKey, SecretKey)
```

#### Websocket client
##### Order place
##### Async write/read
```go
func main() {
orderPlaceService, _ := futures.NewOrderPlaceWsService(apiKey, secretKey)

ctx, cancel := context.WithCancel(context.Background())

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
select {
case <-c:
cancel()
}
}()

request := futures.NewOrderPlaceWsRequest()
request.
Symbol("BTCUSDT").
Side(futures.SideTypeSell).
Type(futures.OrderTypeLimit).
Price("68198.00").
Quantity("0.002").
TimeInForce(futures.TimeInForceTypeGTC)

// sender
go func() {
for {
select {
case <-ctx.Done():
return
default:
err := orderPlaceService.Do("id", request)
if err != nil {
return
}
}
}
}()

wg := &sync.WaitGroup{}
wg.Add(1)
go listenOrderPlaceResponse(ctx, wg, orderPlaceService)
wg.Wait()

log.Println("exit")
}

func listenOrderPlaceResponse(ctx context.Context, wg *sync.WaitGroup, orderPlaceService *futures.OrderPlaceWsService) {
defer wg.Done()

go func() {
for msg := range orderPlaceService.GetReadChannel() {
log.Println("order place response", string(msg))
}
}()

go func() {
for err := range orderPlaceService.GetReadErrorChannel() {
log.Println("order place error", err)
}
}()

select {
case <-ctx.Done():
orderPlaceService.ReceiveAllDataBeforeStop(10 * time.Second)
}
}
```
##### Sync write/read
```go
func main() {
orderPlaceService, _ := futures.NewOrderPlaceWsService(apiKey, secretKey)

id := "some-id"
request := futures.NewOrderPlaceWsRequest()
request.
Symbol("BTCUSDT").
Side(futures.SideTypeSell).
Type(futures.OrderTypeLimit).
Price("68198.00").
Quantity("0.002").
TimeInForce(futures.TimeInForceTypeGTC)

response, err := orderPlaceService.SyncDo(id, request)
if err != nil {
log.Fatal(err)
}

// handle response
}
```




Loading

0 comments on commit 5c80e99

Please sign in to comment.