Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
2pd committed Mar 31, 2023
0 parents commit a07fff4
Show file tree
Hide file tree
Showing 187 changed files with 24,058 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/connector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Run Tests

on:
push:
branches:
- master
pull_request:
branches:
- master
- rc-**

jobs:
UnitTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
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 .
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage.txt
*.DS_Store
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change log

## v0.1.0 - 2023-03-31

- First release
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2023 Binance

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
217 changes: 217 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# Binance Spot Go Connector

This is a lightweight library that works as a connector to [Binance public API](https://github.com/binance/binance-spot-api-docs)

## Supported API Endpoints:
- Account/Trade: `account.go`
- Wallet: `wallet.go`
- Margin Account/Trade: `margin.go`
- Market Data: `market.go`
- Sub-Accounts: `subaccount.go`
- Websocket Market/User Data Stream: `websocket.go`
- Websocket User Data Stream: `user_stream.go`

## Installation
```shell
go get github.com/binance/binance-connector-go
```

To reference the package in your code, use the following import statement:
```golang
import (
"github.com/binance/binance-connector-go"
)
```
## Authentication
```go
// The Client can be initiated with apiKey, secretKey and baseURL.
// The baseURL is optional. If not specified, it will default to "https://api.binance.com".
client := binance_connector.NewClient("yourApiKey", "yourSecretKey")
```

## Extra Options
```go
client := binance_connector.NewClient("yourApiKey", "yourSecretKey", "https://api.binance.com")

// Debug Mode
client.Debug = true

// TimeOffset (in milliseconds) - used to adjust the request timestamp by subtracting/adding the current time with it:
client.TimeOffset = -1000 // implies adding: request timestamp = current time - (-1000)
```

## REST API

Create an order example

```go
package main

import (
"context"
"fmt"

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

func main() {
apiKey := "yourApiKey"
secretKey := "yourSecretKey"
baseURL := "https://testnet.binance.vision"

// Initialise the client
client := binance_connector.NewClient(apiKey, secretKey, baseURL)

// Create new order
newOrder, err := client.NewCreateOrderService().Symbol("BTCUSDT").
Side("BUY").Type("MARKET").Quantity(0.001).
Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(binance_connector.PrettyPrint(newOrder))
}
```

Please find more examples for each supported endpoint in the `examples` folder.

## Websocket API

Diff. Depth Stream Example

```go
package main

import (
"fmt"
"time"

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

func main() {
wsDepthHandler := func(event *binance_connector.WsDepthEvent) {
fmt.Println(binance_connector.PrettyPrint(event))
}

errHandler := func(err error) {
fmt.Println(err)
}

// Depth stream subscription
doneCh, stopCh, err := binance_connector.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
}()

<-doneCh
}
```

## Base URL
- Binance provides alternative Production URLs in case of performance issues:
- https://api1.binance.com
- https://api2.binance.com
- https://api3.binance.com

## Testnet Support
- In order to use the Testnet, simply set the `baseURL` to "https://testnet.binance.vision"
- You can find step-by-step instructions on how to use the get a Testnet API and Secret Key [here](https://dev.binance.vision/t/binance-testnet-environments/99)

## Pretty Print vs PrintLn
- The `fmt.Println(<response>)` function will print the struct in a single line, which is not very readable.
- The `fmt.Println(binance_connector.PrettyPrint(<response>))` function will print the struct, including both the key and value, in a multi-line format which is more easily readable.

### Regular PrintLn Example Output
```bash
&{depthUpdate 1680092520368 LTCBTC 1989614201 1989614210 [{0.00322300 70.96700000} {0.00322200 52.57100000} {0.00322000 248.64000000} {0.00321900 34.98300000}] [{0.00322600 71.52600000} {0.00323400 53.88900000} {0.00323500 27.37000000}]}
&{depthUpdate 1680092521368 LTCBTC 1989614211 1989614212 [{0.00320700 197.10100000} {0.00320100 15.76800000}] []}
&{depthUpdate 1680092522368 LTCBTC 1989614213 1989614224 [{0.00322300 86.15400000} {0.00322200 37.38400000} {0.00322100 252.53900000} {0.00322000 60.01300000}] [{0.00322800 75.48400000} {0.00322900 254.84500000} {0.00323000 8.74700000} {0.00323100 37.42800000}]}
&{depthUpdate 1680092523369 LTCBTC 1989614225 1989614226 [{0.00322300 103.57400000}] [{0.00399500 11.75400000}]}
&{depthUpdate 1680092524369 LTCBTC 1989614227 1989614276 [{0.00322500 0.00000000} {0.00322400 101.32700000} {0.00322300 138.82600000} {0.00322200 58.49100000} {0.00322100 249.65400000} {0.00321900 47.34800000} {0.00317800 16.08500000} {0.00317500 38.36500000}] [{0.00322500 75.14300000} {0.00322600 48.19100000} {0.00322700 44.97900000} {0.00322800 242.74300000} {0.00322900 20.73400000} {0.00532700 0.18900000} {0.00779700 0.05600000}]}
```

### binance_connector.PrettyPrint Example Output
```bash
{
"e": "depthUpdate",
"E": 1680092041346,
"s": "LTCBTC",
"U": 1989606566,
"u": 1989606596,
"b": [
{
"Price": "0.00322800",
"Quantity": "83.05100000"
},
{
"Price": "0.00322700",
"Quantity": "12.50200000"
},
{
"Price": "0.00322500",
"Quantity": "48.53700000"
},
{
"Price": "0.00322400",
"Quantity": "244.13500000"
}
],
"a": [
{
"Price": "0.00322900",
"Quantity": "79.52900000"
},
{
"Price": "0.00323000",
"Quantity": "42.68400000"
},
{
"Price": "0.00323100",
"Quantity": "68.75500000"
}
]
}
{
"e": "depthUpdate",
"E": 1680092042346,
"s": "LTCBTC",
"U": 1989606597,
"u": 1989606611,
"b": [
{
"Price": "0.00321400",
"Quantity": "0.24700000"
},
{
"Price": "0.00318000",
"Quantity": "1.91600000"
}
],
"a": [
{
"Price": "0.00322900",
"Quantity": "79.27900000"
}
]
}
```

## Limitations
Futures and European Options APIs are not supported:
- /fapi/*
- /dapi/*
- /vapi/*
- Associated Websocket Market and User Data Streams

## Contributing
Contributions are welcome.<br/>
If you've found a bug within this project, please open an issue to discuss what you would like to change.<br/>
If it's an issue with the API, please open a topic at [Binance Developer Community](https://dev.binance.vision)
Loading

0 comments on commit a07fff4

Please sign in to comment.