Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Apr 16, 2020
2 parents 2da2755 + 66618b4 commit 7b72bf8
Show file tree
Hide file tree
Showing 49 changed files with 1,754 additions and 5,093 deletions.
13 changes: 6 additions & 7 deletions Const.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
KLINE_PERIOD_60MIN
KLINE_PERIOD_1H
KLINE_PERIOD_2H
KLINE_PERIOD_3H
KLINE_PERIOD_4H
KLINE_PERIOD_6H
KLINE_PERIOD_8H
Expand Down Expand Up @@ -113,16 +114,16 @@ const (
)

var (
THIS_WEEK_CONTRACT = "this_week" //周合约
NEXT_WEEK_CONTRACT = "next_week" //次周合约
QUARTER_CONTRACT = "quarter" //季度合约
SWAP_CONTRACT = "swap" //永续合约
THIS_WEEK_CONTRACT = "this_week" //周合约
NEXT_WEEK_CONTRACT = "next_week" //次周合约
QUARTER_CONTRACT = "quarter" //季度合约
BI_QUARTER_CONTRACT = "bi_quarter" // NEXT QUARTER
SWAP_CONTRACT = "swap" //永续合约
)

//exchanges const
const (
KUCOIN = "kucoin.com"
OKCOIN_CN = "okcoin.cn"
OKCOIN_COM = "okcoin.com"
OKEX = "okex.com"
OKEX_V3 = "okex.com_v3"
Expand All @@ -142,9 +143,7 @@ const (
GATEIO = "gate.io"
BITTREX = "bittrex.com"
GDAX = "gdax.com"
WEX_NZ = "wex.nz"
BIGONE = "big.one"
COIN58 = "58coin.com"
FCOIN = "fcoin.com"
FCOIN_MARGIN = "fcoin.com_margin"
FMEX = "fmex.com"
Expand Down
30 changes: 19 additions & 11 deletions HttpUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,56 @@ import (
"time"
)

var fastHttpClient fasthttp.Client

func init() {
fastHttpClient.MaxConnsPerHost = 2
fastHttpClient.ReadTimeout = 10 * time.Second
fastHttpClient.WriteTimeout = 10 * time.Second
}
var (
fastHttpClient = &fasthttp.Client{
Name: "goex-http-utils",
MaxConnsPerHost: 16,
MaxIdleConnDuration: 20 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
socksDialer fasthttp.DialFunc
)

func NewHttpRequestWithFasthttp(client *http.Client, reqMethod, reqUrl, postData string, headers map[string]string) ([]byte, error) {
logger.Log.Debug("use fasthttp client")
transport := client.Transport

if transport != nil {
if proxy, err := transport.(*http.Transport).Proxy(nil); err == nil && proxy != nil {
proxyUrl := proxy.String()
logger.Log.Debug("proxy url: ", proxyUrl)
if proxy.Scheme != "socks5" {
logger.Log.Error("fasthttp only support the socks5 proxy")
} else {
fastHttpClient.Dial = fasthttpproxy.FasthttpSocksDialer(strings.TrimPrefix(proxyUrl, proxy.Scheme+"://"))
} else if socksDialer == nil {
socksDialer = fasthttpproxy.FasthttpSocksDialer(strings.TrimPrefix(proxyUrl, proxy.Scheme+"://"))
fastHttpClient.Dial = socksDialer
}
}
}

req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}()

for k, v := range headers {
req.Header.Set(k, v)
}
req.Header.SetMethod(reqMethod)
req.SetRequestURI(reqUrl)
req.SetBodyString(postData)
err := fastHttpClient.DoTimeout(req, resp, 10*time.Second)

err := fastHttpClient.Do(req, resp)
if err != nil {
return nil, err
}

if resp.StatusCode() != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode(), string(resp.Body())))
}

return resp.Body(), nil
}

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ goex项目是为了统一并标准化各个数字资产交易平台的接口而

[English](https://github.com/nntaoli-project/goex/blob/dev/README_en.md)

### goex已支持交易所 `22+`
### goex已支持交易所 `23+`

| 交易所 | 行情接口 | 交易接口 | 版本号 |
| --- | --- | --- | --- |
| hbg.com | Y | Y | 1 |
| hbdm.com | Y| Y | 1 |
| okex.com | Y | Y | 3 |
| binance.com | Y | Y | 1 |
| kucoin.com | Y | Y | 1 |
| bitstamp.net | Y | Y | 1 |
| bitfinex.com | Y | Y | 1 |
| zb.com | Y | Y | 1 |
Expand Down
3 changes: 2 additions & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ goex project is designed to unify and standardize the interfaces of each digital

[中文](https://github.com/nntaoli-project/goex/blob/dev/README.md)

### Exchanges are supported by goex `22+`
### Exchanges are supported by goex `23+`
| Exchange | Market API | Order API | Version |
| --- | --- | --- | --- |
| hbg.com | Y | Y | 1 |
| hbdm.com | Y (REST / WS)| Y | 1 |
| okex.com (spot/future)| Y (REST / WS) | Y | 1 |
| okex.com (swap future) | Y | Y | 2 |
| binance.com | Y | Y | 1 |
| kucoin.com | Y | Y | 1 |
| bitstamp.net | Y | Y | 1 |
| bitfinex.com | Y | Y | 1 |
| zb.com | Y | Y | 1 |
Expand Down
5 changes: 3 additions & 2 deletions Utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func FlateUnCompress(data []byte) ([]byte, error) {
return ioutil.ReadAll(flate.NewReader(bytes.NewReader(data)))
}

func UUID() string {
return strings.Replace(uuid.New().String(), "-", "", 32)
func GenerateOrderClientId(size int) string {
uuidStr := strings.Replace(uuid.New().String(), "-", "", 32)
return "goex-" + uuidStr[0:size-5]
}
4 changes: 4 additions & 0 deletions Utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ func TestFloatToString(t *testing.T) {
assert.Equal(t, "1.10231", FloatToString(1.10231000, 8))
assert.NotEqual(t, "1.10231000", FloatToString(1.10231000, 8))
}

func TestGenerateOrderClientId(t *testing.T) {
t.Log(len(GenerateOrderClientId(32)), GenerateOrderClientId(32))
}
38 changes: 10 additions & 28 deletions binance/Binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
)

const (
//API_BASE_URL = "https://api.binance.com/"
GLOBAL_API_BASE_URL = "https://api.binance.com"
US_API_BASE_URL = "https://api.binance.us"
JE_API_BASE_URL = "https://api.binance.je"
//API_V1 = API_BASE_URL + "api/v1/"
//API_V3 = API_BASE_URL + "api/v3/"

Expand Down Expand Up @@ -176,14 +178,14 @@ func (bn *Binance) buildParamsSigned(postForm *url.Values) error {
func New(client *http.Client, api_key, secret_key string) *Binance {
return NewWithConfig(&APIConfig{
HttpClient: client,
Endpoint: "https://api.binance.com",
Endpoint: GLOBAL_API_BASE_URL,
ApiKey: api_key,
ApiSecretKey: secret_key})
}

func NewWithConfig(config *APIConfig) *Binance {
if config.Endpoint == "" {
config.Endpoint = "https://api.binance.com"
config.Endpoint = GLOBAL_API_BASE_URL
}

bn := &Binance{
Expand Down Expand Up @@ -224,8 +226,7 @@ func (bn *Binance) setTimeOffset() error {
}

func (bn *Binance) GetTicker(currency CurrencyPair) (*Ticker, error) {
currency2 := bn.adaptCurrencyPair(currency)
tickerUri := bn.apiV3 + fmt.Sprintf(TICKER_URI, currency2.ToSymbol(""))
tickerUri := bn.apiV3 + fmt.Sprintf(TICKER_URI, currency.ToSymbol(""))
tickerMap, err := HttpGet(bn.httpClient, tickerUri)

if err != nil {
Expand Down Expand Up @@ -261,9 +262,8 @@ func (bn *Binance) GetDepth(size int, currencyPair CurrencyPair) (*Depth, error)
} else {
size = 1000
}
currencyPair2 := bn.adaptCurrencyPair(currencyPair)

apiUrl := fmt.Sprintf(bn.apiV3+DEPTH_URI, currencyPair2.ToSymbol(""), size)
apiUrl := fmt.Sprintf(bn.apiV3+DEPTH_URI, currencyPair.ToSymbol(""), size)
resp, err := HttpGet(bn.httpClient, apiUrl)
if err != nil {
return nil, err
Expand Down Expand Up @@ -309,7 +309,6 @@ func (bn *Binance) GetDepth(size int, currencyPair CurrencyPair) (*Depth, error)
}

func (bn *Binance) placeOrder(amount, price string, pair CurrencyPair, orderType, orderSide string) (*Order, error) {
pair = bn.adaptCurrencyPair(pair)
path := bn.apiV3 + ORDER_URI
params := url.Values{}
params.Set("symbol", pair.ToSymbol(""))
Expand Down Expand Up @@ -416,7 +415,6 @@ func (bn *Binance) MarketSell(amount, price string, currencyPair CurrencyPair) (
}

func (bn *Binance) CancelOrder(orderId string, currencyPair CurrencyPair) (bool, error) {
currencyPair = bn.adaptCurrencyPair(currencyPair)
path := bn.apiV3 + ORDER_URI
params := url.Values{}
params.Set("symbol", currencyPair.ToSymbol(""))
Expand Down Expand Up @@ -446,7 +444,6 @@ func (bn *Binance) CancelOrder(orderId string, currencyPair CurrencyPair) (bool,

func (bn *Binance) GetOneOrder(orderId string, currencyPair CurrencyPair) (*Order, error) {
params := url.Values{}
currencyPair = bn.adaptCurrencyPair(currencyPair)
params.Set("symbol", currencyPair.ToSymbol(""))
if orderId != "" {
params.Set("orderId", orderId)
Expand Down Expand Up @@ -508,7 +505,6 @@ func (bn *Binance) GetOneOrder(orderId string, currencyPair CurrencyPair) (*Orde

func (bn *Binance) GetUnfinishOrders(currencyPair CurrencyPair) ([]Order, error) {
params := url.Values{}
currencyPair = bn.adaptCurrencyPair(currencyPair)
params.Set("symbol", currencyPair.ToSymbol(""))

bn.buildParamsSigned(&params)
Expand Down Expand Up @@ -576,9 +572,8 @@ func (bn *Binance) GetAllUnfinishOrders() ([]Order, error) {
}

func (bn *Binance) GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error) {
currency2 := bn.adaptCurrencyPair(currency)
params := url.Values{}
params.Set("symbol", currency2.ToSymbol(""))
params.Set("symbol", currency.ToSymbol(""))
params.Set("interval", _INERNAL_KLINE_PERIOD_CONVERTER[period])
if since > 0 {
params.Set("startTime", strconv.Itoa(since))
Expand Down Expand Up @@ -614,7 +609,7 @@ func (bn *Binance) GetKlineRecords(currency CurrencyPair, period, size, since in
//注意:since is fromId
func (bn *Binance) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error) {
param := url.Values{}
param.Set("symbol", bn.adaptCurrencyPair(currencyPair).ToSymbol(""))
param.Set("symbol", currencyPair.ToSymbol(""))
param.Set("limit", "500")
if since > 0 {
param.Set("fromId", strconv.Itoa(int(since)))
Expand Down Expand Up @@ -648,8 +643,7 @@ func (bn *Binance) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, e

func (bn *Binance) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
params := url.Values{}
currency1 := bn.adaptCurrencyPair(currency)
params.Set("symbol", currency1.ToSymbol(""))
params.Set("symbol", currency.ToSymbol(""))

bn.buildParamsSigned(&params)
path := bn.apiV3 + "allOrders?" + params.Encode()
Expand Down Expand Up @@ -682,18 +676,6 @@ func (bn *Binance) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize

}

func (bn *Binance) adaptCurrencyPair(pair CurrencyPair) CurrencyPair {
if pair.CurrencyA.Eq(BCH) || pair.CurrencyA.Eq(BCC) {
return NewCurrencyPair(NewCurrency("BCHABC", ""), pair.CurrencyB).AdaptUsdToUsdt()
}

if pair.CurrencyA.Symbol == "BSV" {
return NewCurrencyPair(NewCurrency("BCHSV", ""), pair.CurrencyB).AdaptUsdToUsdt()
}

return pair.AdaptUsdToUsdt()
}

func (bn *Binance) toCurrencyPair(symbol string) CurrencyPair {
if bn.ExchangeInfo == nil {
var err error
Expand Down
34 changes: 19 additions & 15 deletions binance/Binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ import (
"time"
)

var ba = New(&http.Client{
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("socks5://127.0.0.1:1080")
return nil, nil
},
Dial: (&net.Dialer{
var ba = NewWithConfig(
&goex.APIConfig{
HttpClient: &http.Client{
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("socks5://127.0.0.1:1080")
return nil, nil
},
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
},
Timeout: 10 * time.Second,
}).Dial,
},
Timeout: 10 * time.Second,
}, "q6y6Gr7fF3jSJLncpfn2PmAA0xu4XRiRFHpFkyJy3d7K68WUxY0Gt8rrajCDUfbI",
"AP8C2kh4RyISN3fpRCFMZJddf233XbPcYWQ1S7gBan3pGjCQg2JnyQFSJrIaNzRh",
)
},
Endpoint: US_API_BASE_URL,
ApiKey: "q6y6Gr7fF3jSJLncpfn2PmAA0xu4XRiRFHpFkyJy3d7K68WUxY0Gt8rrajCDUfbI",
ApiSecretKey: "AP8C2kh4RyISN3fpRCFMZJddf233XbPcYWQ1S7gBan3pGjCQg2JnyQFSJrIaNzRh",
})

func TestBinance_GetTicker(t *testing.T) {
ticker, _ := ba.GetTicker(goex.LTC_BTC)
t.Log(ticker)
ticker, err := ba.GetTicker(goex.NewCurrencyPair2("USDT_USD"))
t.Log(ticker, err)
}

func TestBinance_LimitBuy(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions bitfinex/BitfinexLending.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
. "github.com/nntaoli-project/goex"
"io/ioutil"
"strconv"
"strings"

. "github.com/nntaoli-project/goex"
)

type LendBookItem struct {
Expand Down Expand Up @@ -84,7 +85,7 @@ func (bfx *Bitfinex) GetDepositWalletBalance() (*Account, error) {

func (bfx *Bitfinex) GetLendBook(currency Currency) (error, *LendBook) {
path := fmt.Sprintf("/lendbook/%s", currency.Symbol)
resp, err := bfx.httpClient.Get(BASE_URL + path)
resp, err := bfx.httpClient.Get(apiURLV1 + path)
if err != nil {
return err, nil
}
Expand Down
Loading

0 comments on commit 7b72bf8

Please sign in to comment.