mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-19 15:10:05 +00:00
* 修复火币Post REST API方法不正确的问题,同时增加火币海带丝交易所 * add vendor folder * 修改命名空间依赖 * 第一次提交分支 * 增加取消订单功能 * 修复binance.GetAccount方法 * 更新readme.md * 增加 Gateio 交易所的支持,支持获取K线、支持的交易对、交易市场参数 * 替换HuobiHadax的参数 * 买/卖订单、取消订单 * OKEX 币币交易:增加获取用户信息,下订单,取消订单 * 测试ok kline * 修复 Bitfinex 的 GetAccountInfo 方法 * 做一些不必要的删减 * 修复binfinex不返回错误的bug * 统一我修改交易所的Kline获取方式 * Bitfinex 增加获取最新价格 * update main.go * 更新GetSymbol方法 * 修改火币和海带丝的Kline编号ID类型 * 修改海带丝的默认配置大小写 * okex增加获取最新价格 * 调整okex的参数判断 * 调整比特儿的参数名称 * 修改火币、火币Hadax的参数全名 * 更新海带丝的配置名称 * 修改bintfinex的GetAccountInfo方法 * 去掉一行注释 * 支持zb交易所的部分功能 * 修复获取K线时没有设置参数的错误 * 增加 Binance 取消订单的方法,获取订单状态,获取所有打开的状态以及所有订单 * 修改获取深度和历史订单的数据 * 修改币安获取深度的参数 * 修改火币获取市场深度的参数 * 修改okex获取市场深度的参数 * 修改币安、OKex获取历史订单的参数 * 修复币安提交参数错误的问题 * merge upstrem * merge后,调整一部分命名空间 * 修改ZB时间参数的命名方式 * 继续替换命名空间 * 命名空间的替换 * 继续命名空间的替换 * 测试 * Port code from idoall's PR * Drop errors dep * Start amending PR * Fix commented code * Translate text from Chinese to English (except for ZB). The reasning behind this is that it's a Chinese exchange and the structs are self explanatory in English, but would for other developers in China * Translate Chinese text, basic formatting changes * Remove commented lines and address feedback on PR
299 lines
6.5 KiB
Go
299 lines
6.5 KiB
Go
package huobi
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"io/ioutil"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/thrasher-/gocryptotrader/common"
|
|
"github.com/thrasher-/gocryptotrader/config"
|
|
)
|
|
|
|
// Please supply you own test keys here for due diligence testing.
|
|
const (
|
|
apiKey = ""
|
|
apiSecret = ""
|
|
)
|
|
|
|
var h HUOBI
|
|
|
|
// getDefaultConfig returns a default huobi config
|
|
func getDefaultConfig() config.ExchangeConfig {
|
|
return config.ExchangeConfig{
|
|
Name: "Huobi",
|
|
Enabled: true,
|
|
Verbose: true,
|
|
Websocket: false,
|
|
UseSandbox: false,
|
|
RESTPollingDelay: 10,
|
|
HTTPTimeout: 15000000000,
|
|
AuthenticatedAPISupport: true,
|
|
APIKey: "",
|
|
APISecret: "",
|
|
ClientID: "",
|
|
AvailablePairs: "BTC-USDT,BCH-USDT",
|
|
EnabledPairs: "BTC-USDT",
|
|
BaseCurrencies: "USD",
|
|
AssetTypes: "SPOT",
|
|
SupportsAutoPairUpdates: false,
|
|
ConfigCurrencyPairFormat: &config.CurrencyPairFormatConfig{
|
|
Uppercase: true,
|
|
Delimiter: "-",
|
|
},
|
|
RequestCurrencyPairFormat: &config.CurrencyPairFormatConfig{
|
|
Uppercase: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestSetDefaults(t *testing.T) {
|
|
h.SetDefaults()
|
|
}
|
|
|
|
func TestSetup(t *testing.T) {
|
|
cfg := config.GetConfig()
|
|
cfg.LoadConfig("../../testdata/configtest.json")
|
|
hConfig, err := cfg.GetExchangeConfig("Huobi")
|
|
if err != nil {
|
|
t.Error("Test Failed - Huobi Setup() init error")
|
|
}
|
|
|
|
hConfig.AuthenticatedAPISupport = true
|
|
hConfig.APIKey = apiKey
|
|
hConfig.APISecret = apiSecret
|
|
|
|
h.Setup(hConfig)
|
|
}
|
|
|
|
func TestGetFee(t *testing.T) {
|
|
t.Parallel()
|
|
if h.GetFee() != 0 {
|
|
t.Errorf("test failed - Huobi GetFee() error")
|
|
}
|
|
}
|
|
|
|
func TestGetSpotKline(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetSpotKline(KlinesRequestParams{
|
|
Symbol: "btcusdt",
|
|
Period: TimeIntervalHour,
|
|
Size: 0,
|
|
})
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetSpotKline: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetMarketDetailMerged(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetMarketDetailMerged("btcusdt")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetMarketDetailMerged: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetDepth(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetDepth(OrderBookDataRequestParams{
|
|
Symbol: "btcusdt",
|
|
Type: OrderBookDataRequestParamsTypeStep1,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetDepth: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetTrades(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetTrades("btcusdt")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetTrades: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetLatestSpotPrice(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetLatestSpotPrice("btcusdt")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi GetLatestSpotPrice: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetTradeHistory(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetTradeHistory("btcusdt", "50")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetTradeHistory: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetMarketDetail(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetMarketDetail("btcusdt")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetTradeHistory: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetSymbols(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetSymbols()
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetSymbols: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetCurrencies(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetCurrencies()
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetCurrencies: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetTimestamp(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := h.GetTimestamp()
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetTimestamp: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetAccounts(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if h.APIKey == "" || h.APISecret == "" || h.APIAuthPEMKey == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
_, err := h.GetAccounts()
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi GetAccounts: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetAccountBalance(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if h.APIKey == "" || h.APISecret == "" || h.APIAuthPEMKey == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
result, err := h.GetAccounts()
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi GetAccounts: %s", err)
|
|
}
|
|
|
|
userID := strconv.FormatInt(result[0].ID, 10)
|
|
_, err = h.GetAccountBalance(userID)
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi GetAccountBalance: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestSpotNewOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if h.APIKey == "" || h.APISecret == "" || h.APIAuthPEMKey == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
arg := SpotNewOrderRequestParams{
|
|
Symbol: "btcusdt",
|
|
AccountID: 000000,
|
|
Amount: 0.01,
|
|
Price: 10.1,
|
|
Type: SpotNewOrderRequestTypeBuyLimit,
|
|
}
|
|
|
|
_, err := h.SpotNewOrder(arg)
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi SpotNewOrder: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestCancelOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := h.CancelOrder(1337)
|
|
if err == nil {
|
|
t.Error("Test failed - Huobi TestCancelOrder: Invalid orderID returned true")
|
|
}
|
|
}
|
|
|
|
func TestGetOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := h.GetOrder(1337)
|
|
if err == nil {
|
|
t.Error("Test failed - Huobi TestCancelOrder: Invalid orderID returned true")
|
|
}
|
|
}
|
|
|
|
func TestGetMarginLoanOrders(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if h.APIKey == "" || h.APISecret == "" || h.APIAuthPEMKey == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
_, err := h.GetMarginLoanOrders("btcusdt", "", "", "", "", "", "", "")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetMarginLoanOrders: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetMarginAccountBalance(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if h.APIKey == "" || h.APISecret == "" || h.APIAuthPEMKey == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
_, err := h.GetMarginAccountBalance("btcusdt")
|
|
if err != nil {
|
|
t.Errorf("Test failed - Huobi TestGetMarginAccountBalance: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestCancelWithdraw(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := h.CancelWithdraw(1337)
|
|
if err == nil {
|
|
t.Error("Test failed - Huobi TestCancelWithdraw: Invalid withdraw-ID was valid")
|
|
}
|
|
}
|
|
|
|
func TestPEMLoadAndSign(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pemKey := strings.NewReader(h.APIAuthPEMKey)
|
|
pemBytes, err := ioutil.ReadAll(pemKey)
|
|
if err != nil {
|
|
t.Fatalf("Test Failed. TestPEMLoadAndSign Unable to ioutil.ReadAll PEM key: %s", err)
|
|
}
|
|
|
|
block, _ := pem.Decode(pemBytes)
|
|
if block == nil {
|
|
t.Fatalf("Test Failed. TestPEMLoadAndSign Block is nil")
|
|
}
|
|
|
|
x509Encoded := block.Bytes
|
|
privKey, err := x509.ParseECPrivateKey(x509Encoded)
|
|
if err != nil {
|
|
t.Fatalf("Test Failed. TestPEMLoadAndSign Unable to ParseECPrivKey: %s", err)
|
|
}
|
|
|
|
_, _, err = ecdsa.Sign(rand.Reader, privKey, common.GetSHA256([]byte("test")))
|
|
if err != nil {
|
|
t.Fatalf("Test Failed. TestPEMLoadAndSign Unable to sign: %s", err)
|
|
}
|
|
}
|