mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Changes method signature for cancelling all orders (experitmental). Implements cancelAllOrders wrapper for alphapoint, anx, binance * Implements cancel all wrapper for bitfinex, bitmex, bitstamp, bittrex, btcmarkets, coinbasepro and hilariously coinut * Changes method signature to only use one OrderCancellation type. Adds support for Exmo, gateio, gemini, itbit, lakebtc * Adds/updates support for hitbtc, huobi, hadax, itbit and kraken * Adds support for liqui, localbitcoins, okcoin, poloniex, wex and yobit. Splits up open order methods for poloniex * Adds bithumb, okex and zb support. BTCC for another PR * Updates bitflyer, bithumb, bitmex, coinut, okex and zb cancelAllOrders method to cancel via enabled currency pairs rather than a singular currency * Adds tests to all exchanges to test wrapper function CancelAllOrders * Fixes OKEX and huobi, btcmarkets, kraken, okCoin cancel order implementations * Fixes coinut, hitbtc and okex api for authenticated requests * Fixes comment and spacing * Changes the CancelAllOrders signature to return orderids and errors along with a generic error. * Fixes OKEX delimiter * Removes spacing and test verbosity * Removes more spacing * Removes space * Fixes okex rebasing issue. Also makes the maps instead of assuming they just work
501 lines
13 KiB
Go
501 lines
13 KiB
Go
package okex
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/thrasher-/gocryptotrader/config"
|
|
"github.com/thrasher-/gocryptotrader/currency/pair"
|
|
"github.com/thrasher-/gocryptotrader/currency/symbol"
|
|
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
|
)
|
|
|
|
var o OKEX
|
|
|
|
// Please supply you own test keys here for due diligence testing.
|
|
const (
|
|
apiKey = ""
|
|
apiSecret = ""
|
|
canManipulateRealOrders = false
|
|
)
|
|
|
|
func TestSetDefaults(t *testing.T) {
|
|
o.SetDefaults()
|
|
if o.GetName() != "OKEX" {
|
|
t.Error("Test Failed - Bittrex - SetDefaults() error")
|
|
}
|
|
}
|
|
|
|
func TestSetup(t *testing.T) {
|
|
cfg := config.GetConfig()
|
|
cfg.LoadConfig("../../testdata/configtest.json")
|
|
okexConfig, err := cfg.GetExchangeConfig("OKEX")
|
|
if err != nil {
|
|
t.Error("Test Failed - Okex Setup() init error")
|
|
}
|
|
|
|
okexConfig.AuthenticatedAPISupport = true
|
|
okexConfig.APIKey = apiKey
|
|
okexConfig.APISecret = apiSecret
|
|
|
|
o.Setup(okexConfig)
|
|
}
|
|
|
|
func TestGetSpotInstruments(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetSpotInstruments()
|
|
if err != nil {
|
|
t.Errorf("Test failed - okex GetSpotInstruments() failed: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractPrice(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractPrice("btc_usd", "this_week")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractPrice() error", err)
|
|
}
|
|
_, err = o.GetContractPrice("btc_bla", "123525")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractPrice() error", err)
|
|
}
|
|
_, err = o.GetContractPrice("btc_bla", "this_week")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractPrice() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractMarketDepth(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractMarketDepth("btc_usd", "this_week")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractMarketDepth() error", err)
|
|
}
|
|
_, err = o.GetContractMarketDepth("btc_bla", "123525")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractMarketDepth() error", err)
|
|
}
|
|
_, err = o.GetContractMarketDepth("btc_bla", "this_week")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractMarketDepth() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractTradeHistory(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractTradeHistory("btc_usd", "this_week")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractTradeHistory() error", err)
|
|
}
|
|
_, err = o.GetContractTradeHistory("btc_bla", "123525")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractTradeHistory() error", err)
|
|
}
|
|
_, err = o.GetContractTradeHistory("btc_bla", "this_week")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractTradeHistory() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractIndexPrice(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractIndexPrice("btc_usd")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractIndexPrice() error", err)
|
|
}
|
|
_, err = o.GetContractIndexPrice("lol123")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractTradeHistory() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractExchangeRate(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractExchangeRate()
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractExchangeRate() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractCandlestickData(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractCandlestickData("btc_usd", "1min", "this_week", 1, 2)
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractCandlestickData() error", err)
|
|
}
|
|
_, err = o.GetContractCandlestickData("btc_bla", "1min", "this_week", 1, 2)
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractCandlestickData() error", err)
|
|
}
|
|
_, err = o.GetContractCandlestickData("btc_usd", "min", "this_week", 1, 2)
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractCandlestickData() error", err)
|
|
}
|
|
_, err = o.GetContractCandlestickData("btc_usd", "1min", "this_wok", 1, 2)
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractCandlestickData() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractHoldingsNumber(t *testing.T) {
|
|
t.Parallel()
|
|
_, _, err := o.GetContractHoldingsNumber("btc_usd", "this_week")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractHoldingsNumber() error", err)
|
|
}
|
|
_, _, err = o.GetContractHoldingsNumber("btc_bla", "this_week")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractHoldingsNumber() error", err)
|
|
}
|
|
_, _, err = o.GetContractHoldingsNumber("btc_usd", "this_bla")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractHoldingsNumber() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractlimit(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetContractlimit("btc_usd", "this_week")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetContractlimit() error", err)
|
|
}
|
|
_, err = o.GetContractlimit("btc_bla", "this_week")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractlimit() error", err)
|
|
}
|
|
_, err = o.GetContractlimit("btc_usd", "this_bla")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractlimit() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractUserInfo(t *testing.T) {
|
|
t.Parallel()
|
|
err := o.GetContractUserInfo()
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractUserInfo() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractPosition(t *testing.T) {
|
|
t.Parallel()
|
|
err := o.GetContractPosition("btc_usd", "this_week")
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractPosition() error", err)
|
|
}
|
|
}
|
|
|
|
func TestPlaceContractOrders(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.PlaceContractOrders("btc_usd", "this_week", "1", 10, 1, 1, true)
|
|
if err == nil {
|
|
t.Error("Test failed - okex PlaceContractOrders() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetContractFuturesTradeHistory(t *testing.T) {
|
|
t.Parallel()
|
|
err := o.GetContractFuturesTradeHistory("btc_usd", "1972-01-01", 0)
|
|
if err == nil {
|
|
t.Error("Test failed - okex GetContractTradeHistory() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetLatestSpotPrice(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetLatestSpotPrice("ltc_btc")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetLatestSpotPrice() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetSpotTicker(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetSpotTicker("ltc_btc")
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetSpotTicker() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetSpotMarketDepth(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetSpotMarketDepth(ActualSpotDepthRequestParams{
|
|
Symbol: "eth_btc",
|
|
Size: 2,
|
|
})
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetSpotMarketDepth() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetSpotRecentTrades(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := o.GetSpotRecentTrades(ActualSpotTradeHistoryRequestParams{
|
|
Symbol: "ltc_btc",
|
|
Since: 0,
|
|
})
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetSpotRecentTrades() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetSpotKline(t *testing.T) {
|
|
t.Parallel()
|
|
arg := KlinesRequestParams{
|
|
Symbol: "ltc_btc",
|
|
Type: TimeIntervalFiveMinutes,
|
|
Size: 100,
|
|
}
|
|
_, err := o.GetSpotKline(arg)
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetSpotCandleStick() error", err)
|
|
}
|
|
}
|
|
|
|
func TestSpotNewOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if o.APIKey == "" || o.APISecret == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
_, err := o.SpotNewOrder(SpotNewOrderRequestParams{
|
|
Symbol: "ltc_btc",
|
|
Amount: 1.1,
|
|
Price: 10.1,
|
|
Type: SpotNewOrderRequestTypeBuy,
|
|
})
|
|
if err != nil {
|
|
t.Error("Test failed - okex SpotNewOrder() error", err)
|
|
}
|
|
}
|
|
|
|
func TestSpotCancelOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if o.APIKey == "" || o.APISecret == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
_, err := o.SpotCancelOrder("ltc_btc", 519158961)
|
|
if err != nil {
|
|
t.Error("Test failed - okex SpotCancelOrder() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetUserInfo(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if o.APIKey == "" || o.APISecret == "" {
|
|
t.Skip()
|
|
}
|
|
|
|
_, err := o.GetUserInfo()
|
|
if err != nil {
|
|
t.Error("Test failed - okex GetUserInfo() error", err)
|
|
}
|
|
}
|
|
|
|
func setFeeBuilder() exchange.FeeBuilder {
|
|
return exchange.FeeBuilder{
|
|
Amount: 1,
|
|
Delimiter: "-",
|
|
FeeType: exchange.CryptocurrencyTradeFee,
|
|
FirstCurrency: symbol.LTC,
|
|
SecondCurrency: symbol.BTC,
|
|
IsMaker: false,
|
|
PurchasePrice: 1,
|
|
CurrencyItem: symbol.USD,
|
|
BankTransactionType: exchange.WireTransfer,
|
|
}
|
|
}
|
|
|
|
func TestGetFee(t *testing.T) {
|
|
o.SetDefaults()
|
|
var feeBuilder = setFeeBuilder()
|
|
// CryptocurrencyTradeFee Basic
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0.0015) || err != nil {
|
|
t.Error(err)
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.0015), resp)
|
|
}
|
|
|
|
// CryptocurrencyTradeFee High quantity
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.Amount = 1000
|
|
feeBuilder.PurchasePrice = 1000
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(1500) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(1500), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CryptocurrencyTradeFee IsMaker
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.IsMaker = true
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0.001) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.001), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CryptocurrencyTradeFee Negative purchase price
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.PurchasePrice = -1000
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
// CryptocurrencyWithdrawalFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.CryptocurrencyWithdrawalFee
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0.001) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.001), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CryptocurrencyWithdrawalFee Invalid currency
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FirstCurrency = "hello"
|
|
feeBuilder.FeeType = exchange.CryptocurrencyWithdrawalFee
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CyptocurrencyDepositFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.CyptocurrencyDepositFee
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// InternationalBankDepositFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.InternationalBankDepositFee
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// InternationalBankWithdrawalFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.InternationalBankWithdrawalFee
|
|
feeBuilder.CurrencyItem = symbol.USD
|
|
if resp, err := o.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestFormatWithdrawPermissions(t *testing.T) {
|
|
// Arrange
|
|
o.SetDefaults()
|
|
expectedResult := exchange.AutoWithdrawCryptoText
|
|
// Act
|
|
withdrawPermissions := o.FormatWithdrawPermissions()
|
|
// Assert
|
|
if withdrawPermissions != expectedResult {
|
|
t.Errorf("Expected: %s, Recieved: %s", expectedResult, withdrawPermissions)
|
|
}
|
|
}
|
|
|
|
// Any tests below this line have the ability to impact your orders on the exchange. Enable canManipulateRealOrders to run them
|
|
// ----------------------------------------------------------------------------------------------------------------------------
|
|
func isRealOrderTestEnabled() bool {
|
|
if o.APIKey == "" || o.APISecret == "" ||
|
|
o.APIKey == "Key" || o.APISecret == "Secret" ||
|
|
!canManipulateRealOrders {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestSubmitOrder(t *testing.T) {
|
|
o.SetDefaults()
|
|
TestSetup(t)
|
|
|
|
if !isRealOrderTestEnabled() {
|
|
t.Skip()
|
|
}
|
|
|
|
var p = pair.CurrencyPair{
|
|
Delimiter: "",
|
|
FirstCurrency: symbol.BTC,
|
|
SecondCurrency: symbol.EUR,
|
|
}
|
|
response, err := o.SubmitOrder(p, exchange.Buy, exchange.Market, 1, 10, "hi")
|
|
if err != nil || !response.IsOrderPlaced {
|
|
t.Errorf("Order failed to be placed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCancelExchangeOrder(t *testing.T) {
|
|
// Arrange
|
|
o.SetDefaults()
|
|
TestSetup(t)
|
|
|
|
if !isRealOrderTestEnabled() {
|
|
t.Skip()
|
|
}
|
|
|
|
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
|
|
|
|
var orderCancellation = exchange.OrderCancellation{
|
|
OrderID: "1",
|
|
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
|
|
AccountID: "1",
|
|
CurrencyPair: currencyPair,
|
|
}
|
|
|
|
// Act
|
|
err := o.CancelOrder(orderCancellation)
|
|
|
|
// Assert
|
|
if err != nil {
|
|
t.Errorf("Could not cancel order: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestCancelAllExchangeOrders(t *testing.T) {
|
|
// Arrange
|
|
o.SetDefaults()
|
|
TestSetup(t)
|
|
|
|
if !isRealOrderTestEnabled() {
|
|
t.Skip()
|
|
}
|
|
|
|
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
|
|
|
|
var orderCancellation = exchange.OrderCancellation{
|
|
OrderID: "1",
|
|
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
|
|
AccountID: "1",
|
|
CurrencyPair: currencyPair,
|
|
}
|
|
|
|
// Act
|
|
resp, err := o.CancelAllOrders(orderCancellation)
|
|
|
|
// Assert
|
|
if err != nil {
|
|
t.Errorf("Could not cancel order: %s", err)
|
|
}
|
|
|
|
if len(resp.OrderStatus) > 0 {
|
|
t.Errorf("%v orders failed to cancel", len(resp.OrderStatus))
|
|
}
|
|
}
|
|
|
|
func TestGetAccountInfo(t *testing.T) {
|
|
if apiKey != "" || apiSecret != "" {
|
|
_, err := o.GetAccountInfo()
|
|
if err != nil {
|
|
t.Error("Test Failed - GetAccountInfo() error", err)
|
|
}
|
|
} else {
|
|
_, err := o.GetAccountInfo()
|
|
if err == nil {
|
|
t.Error("Test Failed - GetAccountInfo() error")
|
|
}
|
|
}
|
|
}
|