Skip to content

Commit

Permalink
Merge pull request #25 from binance-chain/rpc
Browse files Browse the repository at this point in the history
[R4R] support websocket RPC to fullnode
  • Loading branch information
unclezoro authored May 8, 2019
2 parents 327714a + fc67bbd commit e5e9977
Show file tree
Hide file tree
Showing 76 changed files with 3,849 additions and 1,056 deletions.
29 changes: 15 additions & 14 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,10 @@ Add "github.com/binance-chain/go-sdk" dependency into your go.mod file. Example:
require (
github.com/binance-chain/go-sdk latest
)
replace github.com/tendermint/go-amino => github.com/binance-chain/bnc-go-amino v0.14.1-binance.1
```

### Use go get

Use go get to install sdk into your `GOPATH`:
```bash
go get github.com/binance-chain/go-sdk
```

### Use dep
Add dependency to your Gopkg.toml file. Example:
```bash
[[override]]
name = "github.com/binance-chain/go-sdk"
```
**NOTE**: Please make sure you use binance-chain amino repo instead of tendermint amino.

## Usage

Expand Down Expand Up @@ -149,4 +138,16 @@ Create a `buy` order:
createOrderResult, err := client.CreateOrder(tradeSymbol, nativeSymbol, txmsg.OrderSide.BUY, 100000000, 100000000, true)
```

For more API usage documentation, please check the [wiki](https://github.com/binance-chain/go-sdk/wiki)..
For more API usage documentation, please check the [wiki](https://github.com/binance-chain/go-sdk/wiki)..

## RPC Client(Beta)
RPC endpoints may be used to interact with a node directly over HTTP or websockets. Using RPC, you may perform low-level
operations like executing ABCI queries, viewing network/consensus state or broadcasting a transaction against full node or
light client.

### Example
```go
nodeAddr := "tcp://127.0.0.1:27147"
testClientInstance := rpc.NewRPCClient(nodeAddr)
status, err := c.Status()
```
3 changes: 2 additions & 1 deletion client/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package basic
import (
"encoding/json"
"fmt"
"gopkg.in/resty.v1"
"net/http"
"net/url"
"time"

"gopkg.in/resty.v1"

"github.com/binance-chain/go-sdk/types"
"github.com/binance-chain/go-sdk/types/tx"
"github.com/gorilla/websocket"
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/binance-chain/go-sdk/client/query"
"github.com/binance-chain/go-sdk/client/transaction"
"github.com/binance-chain/go-sdk/client/websocket"
"github.com/binance-chain/go-sdk/common/types"
"github.com/binance-chain/go-sdk/keys"
"github.com/binance-chain/go-sdk/types"
)

// dexClient wrapper
Expand Down
19 changes: 0 additions & 19 deletions client/query/errors.go

This file was deleted.

25 changes: 4 additions & 21 deletions client/query/get_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,23 @@ package query

import (
"encoding/json"
"github.com/binance-chain/go-sdk/common/types"
)

// Account definition
type Account struct {
Number int64 `json:"account_number"`
Address string `json:"address"`
Balances []Coin `json:"balances"`
PublicKey []uint8 `json:"public_key"`
Sequence int64 `json:"sequence"`
}

// Coin def
type Coin struct {
Symbol string `json:"symbol"` // ex: BNB
Free string `json:"free"` // in decimal, ex: 0.00000000
Locked string `json:"locked"` // in decimal, ex: 0.00000000
Frozen string `json:"frozen"` // in decimal, ex: 0.00000000
}

// GetAccount returns list of trading pairs
func (c *client) GetAccount(address string) (*Account, error) {
func (c *client) GetAccount(address string) (*types.BalanceAccount, error) {
if address == "" {
return nil, AddressMissingError
return nil, types.AddressMissingError
}

qp := map[string]string{}
resp, err := c.baseClient.Get("/account/"+address, qp)
if err != nil {
return nil, err
}
var account Account
var account types.BalanceAccount
if err := json.Unmarshal(resp, &account); err != nil {
return nil, err
}

return &account, nil
}
86 changes: 3 additions & 83 deletions client/query/get_closed_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,11 @@ import (
"encoding/json"

"github.com/binance-chain/go-sdk/common"
"github.com/binance-chain/go-sdk/common/types"
)

const (
SideBuy = "BUY"
SideSell = "SELL"
)

type ClosedOrdersQuery struct {
SenderAddress string `json:"address"` // required
Symbol string `json:"symbol,omitempty"` //option
Offset *uint32 `json:"offset,omitempty,string"` //option
Limit *uint32 `json:"limit,omitempty,string"` //option
Start *int64 `json:"start,omitempty,string"` //option
End *int64 `json:"end,omitempty,string"` //option
Side string `json:"side,omitempty"` //option
Total int `json:"total,string"` //0 for not required and 1 for required; default not required, return total=-1 in response
}

func NewClosedOrdersQuery(senderAddress string, withTotal bool) *ClosedOrdersQuery {
totalQuery := 0
if withTotal {
totalQuery = 1
}
return &ClosedOrdersQuery{SenderAddress: senderAddress, Total: totalQuery}
}

func (param *ClosedOrdersQuery) Check() error {
if param.SenderAddress == "" {
return AddressMissingError
}
if param.Side != SideBuy && param.Side != SideSell && param.Side != "" {
return TradeSideMisMatchError
}
if param.Limit != nil && *param.Limit <= 0 {
return LimitOutOfRangeError
}
if param.Start != nil && *param.Start <= 0 {
return StartTimeOutOfRangeError
}
if param.End != nil && *param.End <= 0 {
return EndTimeOutOfRangeError
}
if param.Start != nil && param.End != nil && *param.Start > *param.End {
return EndTimeLessThanStartTimeError
}
return nil
}

func (param *ClosedOrdersQuery) WithSymbol(baseAssetSymbol, quoteAssetSymbol string) *ClosedOrdersQuery {
param.Symbol = common.CombineSymbol(baseAssetSymbol, quoteAssetSymbol)
return param
}

func (param *ClosedOrdersQuery) WithOffset(offset uint32) *ClosedOrdersQuery {
param.Offset = &offset
return param
}

func (param *ClosedOrdersQuery) WithLimit(limit uint32) *ClosedOrdersQuery {
param.Limit = &limit
return param
}

func (param *ClosedOrdersQuery) WithStart(start int64) *ClosedOrdersQuery {
param.Start = &start
return param
}

func (param *ClosedOrdersQuery) WithEnd(end int64) *ClosedOrdersQuery {
param.End = &end
return param
}

func (param *ClosedOrdersQuery) WithSide(side string) *ClosedOrdersQuery {
param.Side = side
return param
}

type CloseOrders struct {
Order []Order `json:"order"`
Total int `json:"total"`
}

// GetClosedOrders returns array of open orders
func (c *client) GetClosedOrders(query *ClosedOrdersQuery) (*CloseOrders, error) {
func (c *client) GetClosedOrders(query *types.ClosedOrdersQuery) (*types.CloseOrders, error) {
err := query.Check()
if err != nil {
return nil, err
Expand All @@ -102,7 +22,7 @@ func (c *client) GetClosedOrders(query *ClosedOrdersQuery) (*CloseOrders, error)
return nil, err
}

var orders CloseOrders
var orders types.CloseOrders
if err := json.Unmarshal(resp, &orders); err != nil {
return nil, err
}
Expand Down
37 changes: 3 additions & 34 deletions client/query/get_depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,11 @@ import (
"encoding/json"

"github.com/binance-chain/go-sdk/common"
"github.com/binance-chain/go-sdk/common/types"
)

// DepthQuery
type DepthQuery struct {
Symbol string `json:"symbol"`
Limit *uint32 `json:"limit,omitempty,string"`
}

func NewDepthQuery(baseAssetSymbol, quoteAssetSymbol string) *DepthQuery {
return &DepthQuery{Symbol: common.CombineSymbol(baseAssetSymbol, quoteAssetSymbol)}
}

func (param *DepthQuery) WithLimit(limit uint32) *DepthQuery {
param.Limit = &limit
return param
}

func (param *DepthQuery) Check() error {
if param.Symbol == "" {
return SymbolMissingError
}
if param.Limit != nil && *param.Limit <= 0 {
return LimitOutOfRangeError
}
return nil
}

// MarketDepth broad caste to the user
type MarketDepth struct {
Bids [][]string `json:"bids"` // "bids": [ [ "0.0024", "10" ] ]
Asks [][]string `json:"asks"` // "asks": [ [ "0.0024", "10" ] ]
Height int64 `json:"height"`
}

// GetDepth returns market depth records
func (c *client) GetDepth(query *DepthQuery) (*MarketDepth, error) {
func (c *client) GetDepth(query *types.DepthQuery) (*types.MarketDepth, error) {
err := query.Check()
if err != nil {
return nil, err
Expand All @@ -53,7 +22,7 @@ func (c *client) GetDepth(query *DepthQuery) (*MarketDepth, error) {
return nil, err
}

var MarketDepth MarketDepth
var MarketDepth types.MarketDepth
if err := json.Unmarshal(resp, &MarketDepth); err != nil {
return nil, err
}
Expand Down
70 changes: 4 additions & 66 deletions client/query/get_kline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,11 @@ import (
"fmt"

"github.com/binance-chain/go-sdk/common"
"github.com/binance-chain/go-sdk/common/types"
)

// KlineQuery def
type KlineQuery struct {
Symbol string `json:"symbol"` // required
Interval string `json:"interval"` // required, interval (5m, 1h, 1d, 1w, etc.)
Limit *uint32 `json:"limit,omitempty,string"`
StartTime *int64 `json:"start_time,omitempty,string"`
EndTime *int64 `json:"end_time,omitempty,string"`
}

func NewKlineQuery(baseAssetSymbol, quoteAssetSymbol, interval string) *KlineQuery {
return &KlineQuery{Symbol: common.CombineSymbol(baseAssetSymbol, quoteAssetSymbol), Interval: interval}
}

func (param *KlineQuery) WithStartTime(start int64) *KlineQuery {
param.StartTime = &start
return param
}

func (param *KlineQuery) WithEndTime(end int64) *KlineQuery {
param.EndTime = &end
return param
}

func (param *KlineQuery) WithLimit(limit uint32) *KlineQuery {
param.Limit = &limit
return param
}

func (param *KlineQuery) Check() error {
if param.Symbol == "" {
return SymbolMissingError
}
if param.Interval == "" {
return IntervalMissingError
}
if param.Limit != nil && *param.Limit <= 0 {
return LimitOutOfRangeError
}
if param.StartTime != nil && *param.StartTime <= 0 {
return StartTimeOutOfRangeError
}
if param.EndTime != nil && *param.EndTime <= 0 {
return EndTimeOutOfRangeError
}
if param.StartTime != nil && param.EndTime != nil && *param.StartTime > *param.EndTime {
return EndTimeLessThanStartTimeError
}
return nil
}

// Kline def
type Kline struct {
Close float64 `json:"close,string"`
CloseTime int64 `json:"closeTime"`
High float64 `json:"high,string"`
Low float64 `json:"low,string"`
NumberOfTrades int32 `json:"numberOfTrades"`
Open float64 `json:"open,string"`
OpenTime int64 `json:"openTime"`
QuoteAssetVolume float64 `json:"quoteAssetVolume,string"`
Volume float64 `json:"volume,string"`
}

// GetKlines returns transaction details
func (c *client) GetKlines(query *KlineQuery) ([]Kline, error) {
func (c *client) GetKlines(query *types.KlineQuery) ([]types.Kline, error) {
err := query.Check()
if err != nil {
return nil, err
Expand All @@ -90,10 +28,10 @@ func (c *client) GetKlines(query *KlineQuery) ([]Kline, error) {
if err := json.Unmarshal(resp, &iklines); err != nil {
return nil, err
}
klines := make([]Kline, len(iklines))
klines := make([]types.Kline, len(iklines))
// Todo
for index, ikline := range iklines {
kl := Kline{}
kl := types.Kline{}
imap := make(map[string]interface{}, 9)
if len(ikline) >= 9 {
imap["openTime"] = ikline[0]
Expand Down
Loading

0 comments on commit e5e9977

Please sign in to comment.