Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from bzeron/master
Browse files Browse the repository at this point in the history
kucoin level3 sdk
  • Loading branch information
Ive20 authored Nov 4, 2019
2 parents da28ca4 + 37b8c87 commit de3419f
Show file tree
Hide file tree
Showing 30 changed files with 1,757 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# API_SKIP_VERIFY_TLS=1

API_BASE_URI=https://api.kucoin.com

# If open order book true otherwise false
ENABLE_ORDER_BOOK=true

# If open event watcher true otherwise false
ENABLE_EVENT_WATCHER=true

# Password for RPS calls. Pass the same when calling
RPC_TOKEN=market-token

REDIS_HOST=127.0.0.1:6379
REDIS_PASSWORD=
REDIS_DB=
16 changes: 16 additions & 0 deletions .env-sandbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# API_SKIP_VERIFY_TLS=1

API_BASE_URI=https://openapi-sandbox.kucoin.com

# If open order book true otherwise false
ENABLE_ORDER_BOOK=true

# If open event watcher true otherwise false
ENABLE_EVENT_WATCHER=true

# Password for RPS calls. Pass the same when calling
RPC_TOKEN=market-token

REDIS_HOST=127.0.0.1:6379
REDIS_PASSWORD=
REDIS_DB=
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea
vendor
kucoin_market
.env.local
__pycache__
go.sum
kucoin-market-for-linux
kucoin-market-for-mac
kucoin-market-for-windows.exe
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM golang:1.13-stretch as builder

RUN export GO111MODULE=on \
&& export GOPROXY=https://goproxy.io \
&& mkdir -p /go/src/github.com/Kucoin/kucoin-level3-sdk

COPY . /go/src/github.com/Kucoin/kucoin-level3-sdk

RUN cd /go/src/github.com/Kucoin/kucoin-level3-sdk \
&& CGO_ENABLED=0 go build -ldflags '-s -w' -o /go/bin/kucoin_market kucoin_market.go

FROM debian:stretch

RUN apt-get update \
&& apt-get install ca-certificates -y

COPY --from=builder /go/bin/kucoin_market /usr/local/bin/

# .env => /app/.env
WORKDIR /app
VOLUME /app

EXPOSE 9090

COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["kucoin_market", "-c", "/app/.env", "-symbol", "BTC-USDT", "-p", "9090", "-rpckey", "BTC-USDT"]
108 changes: 107 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
# Kucoin-Level3-sdk
# Kucoin Level3 market

## guide
[中文文档](README_CN.md)

## Installation

1. install dependencies

```
go get github.com/JetBlink/orderbook
go get github.com/go-redis/redis
go get github.com/gorilla/websocket
go get github.com/joho/godotenv
go get github.com/Kucoin/kucoin-go-sdk
go get github.com/shopspring/decimal
```

2. build

```
CGO_ENABLED=0 go build -ldflags '-s -w' -o kucoin_market kucoin_market.go
```

or you can download the latest available [release](https://github.com/Kucoin/kucoin-level3-sdk/releases)

## Usage

1. [vim .env](.env):
```
# API_SKIP_VERIFY_TLS=1
API_BASE_URI=https://api.kucoin.com
# If open order book true otherwise false
ENABLE_ORDER_BOOK=true
# If open event watcher true otherwise false
ENABLE_EVENT_WATCHER=true
# Password for RPS calls. Pass the same when calling
RPC_TOKEN=market-token
REDIS_HOST=127.0.0.1:6379
REDIS_PASSWORD=
REDIS_DB=
```
1. Run Command:
```
./kucoin_market -c .env -symbol BTC-USDT -p 9090 -rpckey BTC-USDT
```
## Docker Usage
1. Build docker image
```
docker build -t kucoin_market .
```
1. [vim .env](.env)
1. Run
```
docker run --rm -it -v $(pwd)/.env:/app/.env --net=host kucoin_market
```
## RPC Method
> endpoint : 127.0.0.1:9090
> the sdk rpc is based on golang jsonrpc 1.0 over tcp.
see:[python jsonrpc client demo](./demo/python-demo/level3/rpc.py)
* Get Part Order Book
```
{"method": "Server.GetPartOrderBook", "params": [{"token": "your-rpc-token", "number": 1}], "id": 0}
```
* Get Full Order Book
```
{"method": "Server.GetOrderBook", "params": [{"token": "your-rpc-token"}], "id": 0}
```
* Add Event ClientOids To Channels
```
{"method": "Server.AddEventClientOidsToChannels", "params": [{"token": "your-rpc-token", "data": {"clientOid": ["channel-1", "channel-2"]}}], "id": 0}
```
* Add Event OrderIds To Channels
```
{"method": "Server.AddEventOrderIdsToChannels", "params": [{"token": "your-rpc-token", "data": {"orderId": ["channel-1", "channel-2"]}}], "id": 0}
```
## Python-Demo
> the demo including orderbook display
see:[python use_level3 demo](./demo/python-demo/order_book_demo.py)
- Run order_book.py
```
command: python order_book.py
describe: display orderbook
```
90 changes: 90 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Kucoin Level3 market

## 入门文档
[英文文档](README.md)

## 安装

1. install dependencies

```
go get github.com/JetBlink/orderbook
go get github.com/go-redis/redis
go get github.com/gorilla/websocket
go get github.com/joho/godotenv
go get github.com/Kucoin/kucoin-go-sdk
go get github.com/shopspring/decimal
```

2. build

```
CGO_ENABLED=0 go build -ldflags '-s -w' -o kucoin_market kucoin_market.go
```

或者直接下载已经编译完成的二进制文件

## 用法

1. [vim .env](.env):
```
# API_SKIP_VERIFY_TLS=1
API_BASE_URI=https://api.kucoin.com
# If open order book true otherwise false
ENABLE_ORDER_BOOK=true
# If open event watcher true otherwise false
ENABLE_EVENT_WATCHER=true
# Password for RPS calls. Pass the same when calling
RPC_TOKEN=market-token
REDIS_HOST=127.0.0.1:6379
REDIS_PASSWORD=
REDIS_DB=
```
1. 运行命令:
```
./kucoin_market -c .env -symbol BTC-USDT -p 9090 -rpckey BTC-USDT
```
## RPC Method
> endpoint : 127.0.0.1:9090
> the sdk rpc is based on golang jsonrpc 1.0 over tcp.
see:[python jsonrpc client demo](./demo/python-demo/level3/rpc.py)
* Get Part Order Book
```
{"method": "Server.GetPartOrderBook", "params": [{"token": "your-rpc-token", "number": 1}], "id": 0}
```
* Get Full Order Book
```
{"method": "Server.GetOrderBook", "params": [{"token": "your-rpc-token"}], "id": 0}
```
* Add Event ClientOids To Channels
```
{"method": "Server.AddEventClientOidsToChannels", "params": [{"token": "your-rpc-token", "data": {"clientOid": ["channel-1", "channel-2"]}}], "id": 0}
```
* Add Event OrderIds To Channels
```
{"method": "Server.AddEventOrderIdsToChannels", "params": [{"token": "your-rpc-token", "data": {"orderId": ["channel-1", "channel-2"]}}], "id": 0}
```
## Python-Demo
> python的demo包含了一个本地orderbook的展示
see:[python use_level3 demo](./demo/python-demo/order_book_demo.py)
- Run order_book.py
```
command: python order_book.py
describe: display orderbook
```
102 changes: 102 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package api

import (
"encoding/json"
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"

"github.com/Kucoin/kucoin-level3-sdk/builder"
"github.com/Kucoin/kucoin-level3-sdk/events"
"github.com/Kucoin/kucoin-level3-sdk/utils/log"
)

//Server is api server
type Server struct {
level3Builder *builder.Builder
eventWatcher *events.Watcher

apiPort string
token string
}

//InitRpcServer init rpc server
func InitRpcServer(apiPort string, token string, level3Builder *builder.Builder, watcher *events.Watcher) {
if apiPort == "" || token == "" {
panic(fmt.Sprintf("missing configuration,apiPort: %s, token: %s", apiPort, token))
}

apiPort = ":" + apiPort
if err := rpc.Register(&Server{
level3Builder: level3Builder,
eventWatcher: watcher,

apiPort: apiPort,
token: token,
}); err != nil {
panic("api server run failed, error: %s" + err.Error())
}

log.Warn("start running rpc server, port: %s", apiPort)

listener, err := net.Listen("tcp", apiPort)
if err != nil {
panic("api server run failed, error: %s" + err.Error())
}

for {
conn, err := listener.Accept()
if err != nil {
continue
}

go jsonrpc.ServeConn(conn)
}
}

//TokenMessage is token type message
type TokenMessage struct {
Token string `json:"token"`
}

//Response is api response
type Response struct {
Code string `json:"code"`
Data interface{} `json:"data"`
Error string `json:"error"`
}

func (s *Server) checkToken(token string) string {
if token != s.token {
return s.failure(TokenErrorCode, "error token")
}

return ""
}

func (s *Server) success(data interface{}) string {
response, _ := json.Marshal(&Response{
Code: "0",
Data: data,
Error: "",
})

return string(response)
}

const (
ServerErrorCode = "10"
TokenErrorCode = "20"
TickerErrorCode = "30"
)

func (s *Server) failure(code string, err string) string {
response, _ := json.Marshal(&Response{
Code: code,
Data: "",
Error: err,
})

return string(response)
}
Loading

0 comments on commit de3419f

Please sign in to comment.