mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* port orderbook binance management from draft singular asset (spot) processing add additional updates to buffer management * integrate port * shifted burden of proof to exchange and remove repairing techniques that obfuscate issues and could caause artifacts * WIP * Update exchanges, update tests, update configuration so we can default off on buffer util. * Add buffer enabled switching to all exchanges and some that are missing, default to off. * lbtc set not aggregate books * Addr linter issues * EOD wip * optimization and bug fix pass * clean before test and benchmarking * add testing/benchmarks to sorting/reversing functions, dropped pointer to slice as we aren't changing slice len or cap * Add tests and removed ptr for main book as we just ammend amount * addr exchange test issues * ci issues * addr glorious issues * Addr MCB nits, fixed funding rate book for bitfinex and fixed potential panic on nil book return * addr linter issues * updated mistakes * Fix more tests * revert bypass * Addr mcb nits * fix zero price bug caused by exchange. Filted out bid result rather then unsubscribing. Updated orderbook to L2 so there is no aggregation. * Allow for zero bid and ask books to be loaded and warn if found. * remove authentication subscription conflicts as they do not have a channel ID return * WIP - Batching outbound requests for kraken as they do not give you the partial if you subscribe to do many things. * finalised outbound request for kraken * filter zero value due to invalid returned data from exchange, add in max subscription amount and increased outbound batch limit * expand to max allowed book length & fix issue where they were sending a zero length ask side when we sent a depth of zero * Updated function comments and added in more realistic book sizing for sort cases * change map ordering * amalgamate maps in buffer * Rm ln * fix kraken linter issues * add in buffer initialisation * increase timout by 30seconds * Coinbene: Add websocket orderbook length check. * Engine: Improve switch statement for orderbook summary dissplay. * Binance: Added tests, remove deadlock * Exchanges: Change orderbook field -> IsFundingRate * Orderbook Buffer: Added method to orderbookHolder * Kraken: removed superfluous integer for sleep * Bitmex: fixed error return * cmd/gctcli: force 8 decimal place usage for orderbook streaming * Kraken: Add checksum and fix bug where we were dropping returned data which was causing artifacts * Kraken: As per orderbook documentation added in maxdepth field to update to filter depth that goes beyond current scope * Bitfinex: Tracking down bug on margin-funding, added sequence and checksum validation websocket config on connect (WIP) * Bitfinex: Complete implementation of checksum * Bitfinex: Fix funding book insertion and checksum - Dropped updates and deleting items not on book are continuously occuring from stream * Bitfinex: Fix linter issues * Bitfinex: Fix even more linter issues. * Bitmex: Populate orderbook base identification fields to be passed back when error occurrs * OkGroup: Populate orderbook base identification fields to be passed back when error occurrs * BTSE: Change string check to 'connect success' to capture multiple user successful strings * Bitfinex: Updated handling of funding tickers * Bitfinex: Fix undocumented alignment bug for funding rates * Bitfinex: Updated error return with more information * Bitfinex: Change REST fetching to Raw book to keep it in line with websocket implementation. Fix woopsy. * Localbitcoins: Had to impose a rate limiter to stop errors, fixed return for easier error identification. * Exchanges: Update failing tests * LocalBitcoins: Addr nit and bumped time by 1 second for fetching books * Kraken: Dynamically scale precision based on str return for checksum calculations * Kraken: Add pair and asset type to validateCRC32 error reponse * BTSE: Filter out zero amount orderbook price levels in websocket return * Exchanges: Update orderbook functions to return orderbook base to differentiate errors. * BTSE: Fix spelling * Bitmex: Fix error return string * BTSE: Add orderbook filtering function * Coinbene: Change wording * BTSE: Add test for filtering * Binance: Addr nits, added in variables for buffers and worker amounts and fixed error log messages * GolangCI: Remove excess 0 * Binance: Reduces double ups on asset and pair in errors * Binance: Fix error checking
1236 lines
33 KiB
Go
1236 lines
33 KiB
Go
package binance
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/core"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
|
)
|
|
|
|
// Please supply your own keys here for due diligence testing
|
|
const (
|
|
apiKey = ""
|
|
apiSecret = ""
|
|
canManipulateRealOrders = false
|
|
)
|
|
|
|
var b Binance
|
|
|
|
func areTestAPIKeysSet() bool {
|
|
return b.ValidateAPICredentials()
|
|
}
|
|
|
|
func setFeeBuilder() *exchange.FeeBuilder {
|
|
return &exchange.FeeBuilder{
|
|
Amount: 1,
|
|
FeeType: exchange.CryptocurrencyTradeFee,
|
|
Pair: currency.NewPair(currency.BTC, currency.LTC),
|
|
PurchasePrice: 1,
|
|
}
|
|
}
|
|
|
|
func TestGetExchangeInfo(t *testing.T) {
|
|
t.Parallel()
|
|
info, err := b.GetExchangeInfo()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if mockTests {
|
|
serverTime := time.Date(2020, 4, 15, 23, 44, 38, int(861*time.Millisecond), time.UTC)
|
|
if !info.Servertime.Equal(serverTime) {
|
|
t.Errorf("Expected %v, got %v", serverTime, info.Servertime)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFetchTradablePairs(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.FetchTradablePairs(asset.Spot)
|
|
if err != nil {
|
|
t.Error("Binance FetchTradablePairs(asset asets.AssetType) error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetOrderBook(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := b.GetOrderBook(OrderBookDataRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Limit: 1000,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Error("Binance GetOrderBook() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetMostRecentTrades(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetMostRecentTrades(RecentTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Limit: 15,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Error("Binance GetMostRecentTrades() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetHistoricalTrades(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetHistoricalTrades("BTCUSDT", 5, 0)
|
|
if !mockTests && err == nil {
|
|
t.Error("Binance GetHistoricalTrades() expecting error")
|
|
}
|
|
if mockTests && err == nil {
|
|
t.Error("Binance GetHistoricalTrades() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetAggregatedTrades(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := b.GetAggregatedTrades(&AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Limit: 5,
|
|
})
|
|
if err != nil {
|
|
t.Error("Binance GetAggregatedTrades() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetSpotKline(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := b.GetSpotKline(&KlinesRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Interval: kline.FiveMin.Short(),
|
|
Limit: 24,
|
|
StartTime: time.Unix(1577836800, 0),
|
|
EndTime: time.Unix(1580515200, 0),
|
|
})
|
|
if err != nil {
|
|
t.Error("Binance GetSpotKline() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetAveragePrice(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetAveragePrice(currency.NewPair(currency.BTC, currency.USDT))
|
|
if err != nil {
|
|
t.Error("Binance GetAveragePrice() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetPriceChangeStats(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetPriceChangeStats(currency.NewPair(currency.BTC, currency.USDT))
|
|
if err != nil {
|
|
t.Error("Binance GetPriceChangeStats() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetTickers(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetTickers()
|
|
if err != nil {
|
|
t.Error("Binance TestGetTickers error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetLatestSpotPrice(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetLatestSpotPrice(currency.NewPair(currency.BTC, currency.USDT))
|
|
if err != nil {
|
|
t.Error("Binance GetLatestSpotPrice() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetBestPrice(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetBestPrice(currency.NewPair(currency.BTC, currency.USDT))
|
|
if err != nil {
|
|
t.Error("Binance GetBestPrice() error", err)
|
|
}
|
|
}
|
|
|
|
func TestQueryOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.QueryOrder(currency.NewPair(currency.BTC, currency.USDT), "", 1337)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("QueryOrder() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("QueryOrder() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock QueryOrder() error", err)
|
|
}
|
|
}
|
|
|
|
func TestOpenOrders(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
p := currency.NewPair(currency.BTC, currency.USDT)
|
|
_, err := b.OpenOrders(&p)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("OpenOrders() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("OpenOrders() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock OpenOrders() error", err)
|
|
}
|
|
}
|
|
|
|
func TestAllOrders(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.AllOrders(currency.NewPair(currency.BTC, currency.USDT), "", "")
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("AllOrders() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("AllOrders() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock AllOrders() error", err)
|
|
}
|
|
}
|
|
|
|
// TestGetFeeByTypeOfflineTradeFee logic test
|
|
func TestGetFeeByTypeOfflineTradeFee(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var feeBuilder = setFeeBuilder()
|
|
b.GetFeeByType(feeBuilder)
|
|
if !areTestAPIKeysSet() || mockTests {
|
|
if feeBuilder.FeeType != exchange.OfflineTradeFee {
|
|
t.Errorf("Expected %v, received %v", exchange.OfflineTradeFee, feeBuilder.FeeType)
|
|
}
|
|
} else {
|
|
if feeBuilder.FeeType != exchange.CryptocurrencyTradeFee {
|
|
t.Errorf("Expected %v, received %v", exchange.CryptocurrencyTradeFee, feeBuilder.FeeType)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetFee(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var feeBuilder = setFeeBuilder()
|
|
|
|
if areTestAPIKeysSet() || mockTests {
|
|
// CryptocurrencyTradeFee Basic
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0.1) || err != nil {
|
|
t.Error(err)
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0), resp)
|
|
}
|
|
|
|
// CryptocurrencyTradeFee High quantity
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.Amount = 1000
|
|
feeBuilder.PurchasePrice = 1000
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(100000) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(100000), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CryptocurrencyTradeFee IsMaker
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.IsMaker = true
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0.1) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0.1), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CryptocurrencyTradeFee Negative purchase price
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.PurchasePrice = -1000
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
// CryptocurrencyWithdrawalFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.CryptocurrencyWithdrawalFee
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0.0005) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0.0005), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// CyptocurrencyDepositFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.CyptocurrencyDepositFee
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// InternationalBankDepositFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.InternationalBankDepositFee
|
|
feeBuilder.FiatCurrency = currency.HKD
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
|
|
// InternationalBankWithdrawalFee Basic
|
|
feeBuilder = setFeeBuilder()
|
|
feeBuilder.FeeType = exchange.InternationalBankWithdrawalFee
|
|
feeBuilder.FiatCurrency = currency.HKD
|
|
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
|
|
t.Errorf("GetFee() error. Expected: %f, Received: %f", float64(0), resp)
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestFormatWithdrawPermissions(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
expectedResult := exchange.AutoWithdrawCryptoText + " & " + exchange.NoFiatWithdrawalsText
|
|
withdrawPermissions := b.FormatWithdrawPermissions()
|
|
if withdrawPermissions != expectedResult {
|
|
t.Errorf("Expected: %s, Received: %s", expectedResult, withdrawPermissions)
|
|
}
|
|
}
|
|
|
|
func TestGetActiveOrders(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var getOrdersRequest = order.GetOrdersRequest{
|
|
Type: order.AnyType,
|
|
}
|
|
_, err := b.GetActiveOrders(&getOrdersRequest)
|
|
if err == nil {
|
|
t.Error("Expected: 'At least one currency is required to fetch order history'. received nil")
|
|
}
|
|
|
|
getOrdersRequest.Pairs = []currency.Pair{
|
|
currency.NewPair(currency.LTC, currency.BTC),
|
|
}
|
|
|
|
_, err = b.GetActiveOrders(&getOrdersRequest)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("GetActiveOrders() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("GetActiveOrders() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock GetActiveOrders() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetOrderHistory(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var getOrdersRequest = order.GetOrdersRequest{
|
|
Type: order.AnyType,
|
|
}
|
|
|
|
_, err := b.GetOrderHistory(&getOrdersRequest)
|
|
if err == nil {
|
|
t.Error("Expected: 'At least one currency is required to fetch order history'. received nil")
|
|
}
|
|
|
|
getOrdersRequest.Pairs = []currency.Pair{
|
|
currency.NewPair(currency.LTC,
|
|
currency.BTC)}
|
|
|
|
_, err = b.GetOrderHistory(&getOrdersRequest)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("GetOrderHistory() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("GetOrderHistory() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock GetOrderHistory() error", err)
|
|
}
|
|
}
|
|
|
|
func TestNewOrderTest(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
req := &NewOrderRequest{
|
|
Symbol: currency.NewPair(currency.LTC, currency.BTC),
|
|
Side: order.Buy.String(),
|
|
TradeType: BinanceRequestParamsOrderLimit,
|
|
Price: 0.0025,
|
|
Quantity: 100000,
|
|
TimeInForce: BinanceRequestParamsTimeGTC,
|
|
}
|
|
|
|
err := b.NewOrderTest(req)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("NewOrderTest() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("NewOrderTest() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock NewOrderTest() error", err)
|
|
}
|
|
|
|
req = &NewOrderRequest{
|
|
Symbol: currency.NewPair(currency.LTC, currency.BTC),
|
|
Side: order.Sell.String(),
|
|
TradeType: BinanceRequestParamsOrderMarket,
|
|
Price: 0.0045,
|
|
QuoteOrderQty: 10,
|
|
}
|
|
|
|
err = b.NewOrderTest(req)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("NewOrderTest() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("NewOrderTest() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock NewOrderTest() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetHistoricTrades(t *testing.T) {
|
|
t.Parallel()
|
|
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start, err := time.Parse(time.RFC3339, "2020-01-02T15:04:05Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := b.GetHistoricTrades(currencyPair, asset.Spot, start, start.Add(15*time.Minute))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
var expected int
|
|
if mockTests {
|
|
expected = 5
|
|
} else {
|
|
expected = 2134
|
|
}
|
|
if len(result) != expected {
|
|
t.Errorf("GetHistoricTrades() expected %v entries, got %v", expected, len(result))
|
|
}
|
|
}
|
|
|
|
func TestGetAggregatedTradesBatched(t *testing.T) {
|
|
t.Parallel()
|
|
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start, err := time.Parse(time.RFC3339, "2020-01-02T15:04:05Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mockExpectTime, err := time.Parse(time.RFC3339, "2020-01-02T16:19:04.8Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
expectTime, err := time.Parse(time.RFC3339Nano, "2020-01-02T16:19:04.831Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
// mock test or live test
|
|
mock bool
|
|
args *AggregatedTradeRequestParams
|
|
numExpected int
|
|
lastExpected time.Time
|
|
}{
|
|
{
|
|
name: "mock batch with timerange",
|
|
mock: true,
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currencyPair,
|
|
StartTime: start,
|
|
EndTime: start.Add(75 * time.Minute),
|
|
},
|
|
numExpected: 3,
|
|
lastExpected: mockExpectTime,
|
|
},
|
|
{
|
|
name: "batch with timerange",
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currencyPair,
|
|
StartTime: start,
|
|
EndTime: start.Add(75 * time.Minute),
|
|
},
|
|
numExpected: 4303,
|
|
lastExpected: expectTime,
|
|
},
|
|
{
|
|
name: "mock custom limit with start time set, no end time",
|
|
mock: true,
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
StartTime: start,
|
|
Limit: 1001,
|
|
},
|
|
numExpected: 4,
|
|
lastExpected: time.Date(2020, 1, 2, 16, 19, 5, int(200*time.Millisecond), time.UTC),
|
|
},
|
|
{
|
|
name: "custom limit with start time set, no end time",
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
StartTime: time.Date(2020, 11, 18, 12, 0, 0, 0, time.UTC),
|
|
Limit: 1001,
|
|
},
|
|
numExpected: 1001,
|
|
lastExpected: time.Date(2020, 11, 18, 13, 0, 0, int(34*time.Millisecond), time.UTC),
|
|
},
|
|
{
|
|
name: "mock recent trades",
|
|
mock: true,
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Limit: 3,
|
|
},
|
|
numExpected: 3,
|
|
lastExpected: time.Date(2020, 1, 2, 16, 19, 5, int(200*time.Millisecond), time.UTC),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.mock != mockTests {
|
|
t.Skip()
|
|
}
|
|
result, err := b.GetAggregatedTrades(tt.args)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if len(result) != tt.numExpected {
|
|
t.Errorf("GetAggregatedTradesBatched() expected %v entries, got %v", tt.numExpected, len(result))
|
|
}
|
|
lastTradeTime := result[len(result)-1].TimeStamp
|
|
if !lastTradeTime.Equal(tt.lastExpected) {
|
|
t.Errorf("last trade expected %v, got %v", tt.lastExpected, lastTradeTime)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetAggregatedTradesErrors(t *testing.T) {
|
|
t.Parallel()
|
|
start, err := time.Parse(time.RFC3339, "2020-01-02T15:04:05Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args *AggregatedTradeRequestParams
|
|
}{
|
|
{
|
|
name: "get recent trades does not support custom limit",
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Limit: 1001,
|
|
},
|
|
},
|
|
{
|
|
name: "start time and fromId cannot be both set",
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
StartTime: start,
|
|
EndTime: start.Add(75 * time.Minute),
|
|
FromID: 2,
|
|
},
|
|
},
|
|
{
|
|
name: "can't get most recent 5000 (more than 1000 not allowed)",
|
|
args: &AggregatedTradeRequestParams{
|
|
Symbol: currency.NewPair(currency.BTC, currency.USDT),
|
|
Limit: 5000,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := b.GetAggregatedTrades(tt.args)
|
|
if err == nil {
|
|
t.Errorf("Binance.GetAggregatedTrades() error = %v, wantErr true", err)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Any tests below this line have the ability to impact your orders on the exchange. Enable canManipulateRealOrders to run them
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
|
|
|
func TestSubmitOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if areTestAPIKeysSet() && !canManipulateRealOrders && !mockTests {
|
|
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
|
}
|
|
|
|
var orderSubmission = &order.Submit{
|
|
Pair: currency.Pair{
|
|
Delimiter: "_",
|
|
Base: currency.LTC,
|
|
Quote: currency.BTC,
|
|
},
|
|
Side: order.Buy,
|
|
Type: order.Limit,
|
|
Price: 1,
|
|
Amount: 1000000000,
|
|
ClientID: "meowOrder",
|
|
AssetType: asset.Spot,
|
|
}
|
|
|
|
_, err := b.SubmitOrder(orderSubmission)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("SubmitOrder() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("SubmitOrder() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock SubmitOrder() error", err)
|
|
}
|
|
}
|
|
|
|
func TestCancelExchangeOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if areTestAPIKeysSet() && !canManipulateRealOrders && !mockTests {
|
|
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
|
}
|
|
var orderCancellation = &order.Cancel{
|
|
ID: "1",
|
|
WalletAddress: core.BitcoinDonationAddress,
|
|
AccountID: "1",
|
|
Pair: currency.NewPair(currency.LTC, currency.BTC),
|
|
AssetType: asset.Spot,
|
|
}
|
|
|
|
err := b.CancelOrder(orderCancellation)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("CancelExchangeOrder() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("CancelExchangeOrder() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock CancelExchangeOrder() error", err)
|
|
}
|
|
}
|
|
|
|
func TestCancelAllExchangeOrders(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if areTestAPIKeysSet() && !canManipulateRealOrders && !mockTests {
|
|
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
|
}
|
|
var orderCancellation = &order.Cancel{
|
|
ID: "1",
|
|
WalletAddress: core.BitcoinDonationAddress,
|
|
AccountID: "1",
|
|
Pair: currency.NewPair(currency.LTC, currency.BTC),
|
|
AssetType: asset.Spot,
|
|
}
|
|
|
|
_, err := b.CancelAllOrders(orderCancellation)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("CancelAllExchangeOrders() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("CancelAllExchangeOrders() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock CancelAllExchangeOrders() error", err)
|
|
}
|
|
}
|
|
|
|
func TestGetAccountInfo(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.UpdateAccountInfo()
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("GetAccountInfo() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("GetAccountInfo() expecting an error when no keys are set")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock GetAccountInfo() error", err)
|
|
}
|
|
}
|
|
|
|
func TestModifyOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.ModifyOrder(&order.Modify{AssetType: asset.Spot})
|
|
if err == nil {
|
|
t.Error("ModifyOrder() error cannot be nil")
|
|
}
|
|
}
|
|
|
|
func TestWithdraw(t *testing.T) {
|
|
t.Parallel()
|
|
if areTestAPIKeysSet() && !canManipulateRealOrders && !mockTests {
|
|
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
|
}
|
|
|
|
withdrawCryptoRequest := withdraw.Request{
|
|
Exchange: b.Name,
|
|
Amount: 0,
|
|
Currency: currency.BTC,
|
|
Description: "WITHDRAW IT ALL",
|
|
Crypto: withdraw.CryptoRequest{
|
|
Address: core.BitcoinDonationAddress,
|
|
},
|
|
}
|
|
|
|
_, err := b.WithdrawCryptocurrencyFunds(&withdrawCryptoRequest)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("Withdraw() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("Withdraw() expecting an error when no keys are set")
|
|
case mockTests && err == nil:
|
|
t.Error("Mock Withdraw() error cannot be nil")
|
|
}
|
|
}
|
|
|
|
func TestWithdrawHistory(t *testing.T) {
|
|
t.Parallel()
|
|
if areTestAPIKeysSet() && !canManipulateRealOrders && !mockTests {
|
|
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
|
}
|
|
|
|
_, err := b.GetWithdrawalsHistory(currency.XBT)
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("GetWithdrawalsHistory() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("GetWithdrawalsHistory() expecting an error when no keys are set")
|
|
}
|
|
}
|
|
|
|
func TestWithdrawFiat(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.WithdrawFiatFunds(&withdraw.Request{})
|
|
if err != common.ErrFunctionNotSupported {
|
|
t.Errorf("Expected '%v', received: '%v'", common.ErrFunctionNotSupported, err)
|
|
}
|
|
}
|
|
|
|
func TestWithdrawInternationalBank(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.WithdrawFiatFundsToInternationalBank(&withdraw.Request{})
|
|
if err != common.ErrFunctionNotSupported {
|
|
t.Errorf("Expected '%v', received: '%v'", common.ErrFunctionNotSupported, err)
|
|
}
|
|
}
|
|
|
|
func TestGetDepositAddress(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := b.GetDepositAddress(currency.BTC, "")
|
|
switch {
|
|
case areTestAPIKeysSet() && err != nil:
|
|
t.Error("GetDepositAddress() error", err)
|
|
case !areTestAPIKeysSet() && err == nil && !mockTests:
|
|
t.Error("GetDepositAddress() error cannot be nil")
|
|
case mockTests && err != nil:
|
|
t.Error("Mock GetDepositAddress() error", err)
|
|
}
|
|
}
|
|
|
|
func TestWSSubscriptionHandling(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{
|
|
"method": "SUBSCRIBE",
|
|
"params": [
|
|
"btcusdt@aggTrade",
|
|
"btcusdt@depth"
|
|
],
|
|
"id": 1
|
|
}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWSUnsubscriptionHandling(t *testing.T) {
|
|
pressXToJSON := []byte(`{
|
|
"method": "UNSUBSCRIBE",
|
|
"params": [
|
|
"btcusdt@depth"
|
|
],
|
|
"id": 312
|
|
}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsOrderUpdateHandling(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{
|
|
"e": "executionReport",
|
|
"E": 1499405658658,
|
|
"s": "BTCUSDT",
|
|
"c": "mUvoqJxFIILMdfAW5iGSOW",
|
|
"S": "BUY",
|
|
"o": "LIMIT",
|
|
"f": "GTC",
|
|
"q": "1.00000000",
|
|
"p": "0.10264410",
|
|
"P": "0.00000000",
|
|
"F": "0.00000000",
|
|
"g": -1,
|
|
"C": null,
|
|
"x": "NEW",
|
|
"X": "NEW",
|
|
"r": "NONE",
|
|
"i": 4293153,
|
|
"l": "0.00000000",
|
|
"z": "0.00000000",
|
|
"L": "0.00000000",
|
|
"n": "0",
|
|
"N": null,
|
|
"T": 1499405658657,
|
|
"t": -1,
|
|
"I": 8641984,
|
|
"w": true,
|
|
"m": false,
|
|
"M": false,
|
|
"O": 1499405658657,
|
|
"Z": "0.00000000",
|
|
"Y": "0.00000000",
|
|
"Q": "0.00000000"
|
|
}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsOutboundAccountPosition(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{
|
|
"e": "outboundAccountPosition",
|
|
"E": 1564034571105,
|
|
"u": 1564034571073,
|
|
"B": [
|
|
{
|
|
"a": "ETH",
|
|
"f": "10000.000000",
|
|
"l": "0.000000"
|
|
}
|
|
]
|
|
}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsTickerUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{"stream":"btcusdt@ticker","data":{"e":"24hrTicker","E":1580254809477,"s":"BTCUSDT","p":"420.97000000","P":"4.720","w":"9058.27981278","x":"8917.98000000","c":"9338.96000000","Q":"0.17246300","b":"9338.03000000","B":"0.18234600","a":"9339.70000000","A":"0.14097600","o":"8917.99000000","h":"9373.19000000","l":"8862.40000000","v":"72229.53692000","q":"654275356.16896672","O":1580168409456,"C":1580254809456,"F":235294268,"L":235894703,"n":600436}}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsKlineUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{"stream":"btcusdt@kline_1m","data":{
|
|
"e": "kline",
|
|
"E": 123456789,
|
|
"s": "BNBBTC",
|
|
"k": {
|
|
"t": 123400000,
|
|
"T": 123460000,
|
|
"s": "BNBBTC",
|
|
"i": "1m",
|
|
"f": 100,
|
|
"L": 200,
|
|
"o": "0.0010",
|
|
"c": "0.0020",
|
|
"h": "0.0025",
|
|
"l": "0.0015",
|
|
"v": "1000",
|
|
"n": 100,
|
|
"x": false,
|
|
"q": "1.0000",
|
|
"V": "500",
|
|
"Q": "0.500",
|
|
"B": "123456"
|
|
}
|
|
}}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsTradeUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{"stream":"btcusdt@trade","data":{
|
|
"e": "trade",
|
|
"E": 123456789,
|
|
"s": "BNBBTC",
|
|
"t": 12345,
|
|
"p": "0.001",
|
|
"q": "100",
|
|
"b": 88,
|
|
"a": 50,
|
|
"T": 123456785,
|
|
"m": true,
|
|
"M": true
|
|
}}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsDepthUpdate(t *testing.T) {
|
|
b.setupOrderbookManager()
|
|
seedLastUpdateID := int64(161)
|
|
book := OrderBook{
|
|
Asks: []OrderbookItem{
|
|
{Price: 6621.80000000, Quantity: 0.00198100},
|
|
{Price: 6622.14000000, Quantity: 4.00000000},
|
|
{Price: 6622.46000000, Quantity: 2.30000000},
|
|
{Price: 6622.47000000, Quantity: 1.18633300},
|
|
{Price: 6622.64000000, Quantity: 4.00000000},
|
|
{Price: 6622.73000000, Quantity: 0.02900000},
|
|
{Price: 6622.76000000, Quantity: 0.12557700},
|
|
{Price: 6622.81000000, Quantity: 2.08994200},
|
|
{Price: 6622.82000000, Quantity: 0.01500000},
|
|
{Price: 6623.17000000, Quantity: 0.16831300},
|
|
},
|
|
Bids: []OrderbookItem{
|
|
{Price: 6621.55000000, Quantity: 0.16356700},
|
|
{Price: 6621.45000000, Quantity: 0.16352600},
|
|
{Price: 6621.41000000, Quantity: 0.86091200},
|
|
{Price: 6621.25000000, Quantity: 0.16914100},
|
|
{Price: 6621.23000000, Quantity: 0.09193600},
|
|
{Price: 6621.22000000, Quantity: 0.00755100},
|
|
{Price: 6621.13000000, Quantity: 0.08432000},
|
|
{Price: 6621.03000000, Quantity: 0.00172000},
|
|
{Price: 6620.94000000, Quantity: 0.30506700},
|
|
{Price: 6620.93000000, Quantity: 0.00200000},
|
|
},
|
|
LastUpdateID: seedLastUpdateID,
|
|
}
|
|
|
|
update1 := []byte(`{"stream":"btcusdt@depth","data":{
|
|
"e": "depthUpdate",
|
|
"E": 123456788,
|
|
"s": "BTCUSDT",
|
|
"U": 157,
|
|
"u": 160,
|
|
"b": [
|
|
["6621.45", "0.3"]
|
|
],
|
|
"a": [
|
|
["6622.46", "1.5"]
|
|
]
|
|
}}`)
|
|
|
|
p := currency.NewPairWithDelimiter("BTC", "USDT", "-")
|
|
if err := b.SeedLocalCacheWithBook(p, &book); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if err := b.wsHandleData(update1); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
ob := b.Websocket.Orderbook.GetOrderbook(p, asset.Spot)
|
|
if exp, got := seedLastUpdateID, ob.LastUpdateID; got != exp {
|
|
t.Fatalf("Unexpected Last update id of orderbook for old update. Exp: %d, got: %d", exp, got)
|
|
}
|
|
if exp, got := 2.3, ob.Asks[2].Amount; got != exp {
|
|
t.Fatalf("Ask altered by outdated update. Exp: %f, got %f", exp, got)
|
|
}
|
|
if exp, got := 0.163526, ob.Bids[1].Amount; got != exp {
|
|
t.Fatalf("Bid altered by outdated update. Exp: %f, got %f", exp, got)
|
|
}
|
|
|
|
update2 := []byte(`{"stream":"btcusdt@depth","data":{
|
|
"e": "depthUpdate",
|
|
"E": 123456789,
|
|
"s": "BTCUSDT",
|
|
"U": 161,
|
|
"u": 165,
|
|
"b": [
|
|
["6621.45", "0.163526"]
|
|
],
|
|
"a": [
|
|
["6622.46", "2.3"],
|
|
["6622.47", "1.9"]
|
|
]
|
|
}}`)
|
|
|
|
if err := b.wsHandleData(update2); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
ob = b.Websocket.Orderbook.GetOrderbook(p, asset.Spot)
|
|
if exp, got := int64(165), ob.LastUpdateID; got != exp {
|
|
t.Fatalf("Unexpected Last update id of orderbook for new update. Exp: %d, got: %d", exp, got)
|
|
}
|
|
if exp, got := 2.3, ob.Asks[2].Amount; got != exp {
|
|
t.Fatalf("Unexpected Ask amount. Exp: %f, got %f", exp, got)
|
|
}
|
|
if exp, got := 1.9, ob.Asks[3].Amount; got != exp {
|
|
t.Fatalf("Unexpected Ask amount. Exp: %f, got %f", exp, got)
|
|
}
|
|
if exp, got := 0.163526, ob.Bids[1].Amount; got != exp {
|
|
t.Fatalf("Unexpected Bid amount. Exp: %f, got %f", exp, got)
|
|
}
|
|
}
|
|
|
|
func TestWsBalanceUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{
|
|
"e": "balanceUpdate",
|
|
"E": 1573200697110,
|
|
"a": "BTC",
|
|
"d": "100.00000000",
|
|
"T": 1573200697068
|
|
}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestWsOCO(t *testing.T) {
|
|
t.Parallel()
|
|
pressXToJSON := []byte(`{
|
|
"e": "listStatus",
|
|
"E": 1564035303637,
|
|
"s": "ETHBTC",
|
|
"g": 2,
|
|
"c": "OCO",
|
|
"l": "EXEC_STARTED",
|
|
"L": "EXECUTING",
|
|
"r": "NONE",
|
|
"C": "F4QN4G8DlFATFlIUQ0cjdD",
|
|
"T": 1564035303625,
|
|
"O": [
|
|
{
|
|
"s": "ETHBTC",
|
|
"i": 17,
|
|
"c": "AJYsMjErWJesZvqlJCTUgL"
|
|
},
|
|
{
|
|
"s": "ETHBTC",
|
|
"i": 18,
|
|
"c": "bfYPSQdLoqAJeNrOr9adzq"
|
|
}
|
|
]
|
|
}`)
|
|
err := b.wsHandleData(pressXToJSON)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestGetWsAuthStreamKey(t *testing.T) {
|
|
key, err := b.GetWsAuthStreamKey()
|
|
switch {
|
|
case mockTests && err != nil,
|
|
!mockTests && areTestAPIKeysSet() && err != nil:
|
|
t.Fatal(err)
|
|
case !mockTests && !areTestAPIKeysSet() && err == nil:
|
|
t.Fatal("Expected error")
|
|
}
|
|
|
|
if key == "" {
|
|
t.Error("Expected key")
|
|
}
|
|
}
|
|
|
|
func TestMaintainWsAuthStreamKey(t *testing.T) {
|
|
err := b.MaintainWsAuthStreamKey()
|
|
switch {
|
|
case mockTests && err != nil,
|
|
!mockTests && areTestAPIKeysSet() && err != nil:
|
|
t.Fatal(err)
|
|
case !mockTests && !areTestAPIKeysSet() && err == nil:
|
|
t.Fatal("Expected error")
|
|
}
|
|
}
|
|
|
|
func TestExecutionTypeToOrderStatus(t *testing.T) {
|
|
type TestCases struct {
|
|
Case string
|
|
Result order.Status
|
|
}
|
|
testCases := []TestCases{
|
|
{Case: "NEW", Result: order.New},
|
|
{Case: "CANCELLED", Result: order.Cancelled},
|
|
{Case: "REJECTED", Result: order.Rejected},
|
|
{Case: "TRADE", Result: order.PartiallyFilled},
|
|
{Case: "EXPIRED", Result: order.Expired},
|
|
{Case: "LOL", Result: order.UnknownStatus},
|
|
}
|
|
for i := range testCases {
|
|
result, _ := stringToOrderStatus(testCases[i].Case)
|
|
if result != testCases[i].Result {
|
|
t.Errorf("Exepcted: %v, received: %v", testCases[i].Result, result)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetHistoricCandles(t *testing.T) {
|
|
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
startTime := time.Unix(1546300800, 0)
|
|
end := time.Unix(1577836799, 0)
|
|
_, err = b.GetHistoricCandles(currencyPair, asset.Spot, startTime, end, kline.OneDay)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = b.GetHistoricCandles(currencyPair, asset.Spot, startTime, end, kline.Interval(time.Hour*7))
|
|
if err == nil {
|
|
t.Fatal("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestGetHistoricCandlesExtended(t *testing.T) {
|
|
currencyPair, err := currency.NewPairFromString("BTC-USDT")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
startTime := time.Unix(1546300800, 0)
|
|
end := time.Unix(1577836799, 0)
|
|
_, err = b.GetHistoricCandlesExtended(currencyPair, asset.Spot, startTime, end, kline.OneDay)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = b.GetHistoricCandlesExtended(currencyPair, asset.Spot, startTime, end, kline.Interval(time.Hour*7))
|
|
if err == nil {
|
|
t.Fatal("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestBinance_FormatExchangeKlineInterval(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
interval kline.Interval
|
|
output string
|
|
}{
|
|
{
|
|
"OneMin",
|
|
kline.OneMin,
|
|
"1m",
|
|
},
|
|
{
|
|
"OneDay",
|
|
kline.OneDay,
|
|
"1d",
|
|
},
|
|
{
|
|
"OneMonth",
|
|
kline.OneMonth,
|
|
"1M",
|
|
},
|
|
}
|
|
|
|
for x := range testCases {
|
|
test := testCases[x]
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
ret := b.FormatExchangeKlineInterval(test.interval)
|
|
|
|
if ret != test.output {
|
|
t.Fatalf("unexpected result return expected: %v received: %v", test.output, ret)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetRecentTrades(t *testing.T) {
|
|
t.Parallel()
|
|
currencyPair, err := currency.NewPairFromString("BTCUSDT")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = b.GetRecentTrades(currencyPair, asset.Spot)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestSeedLocalCache(t *testing.T) {
|
|
t.Parallel()
|
|
err := b.SeedLocalCache(currency.NewPair(currency.BTC, currency.USDT))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestGenerateSubscriptions(t *testing.T) {
|
|
t.Parallel()
|
|
subs, err := b.GenerateSubscriptions()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(subs) != 4 {
|
|
t.Fatal("unexpected subscription length")
|
|
}
|
|
}
|
|
|
|
var websocketDepthUpdate = []byte(`{"E":1608001030784,"U":7145637266,"a":[["19455.19000000","0.59490200"],["19455.37000000","0.00000000"],["19456.11000000","0.00000000"],["19456.16000000","0.00000000"],["19458.67000000","0.06400000"],["19460.73000000","0.05139800"],["19461.43000000","0.00000000"],["19464.59000000","0.00000000"],["19466.03000000","0.45000000"],["19466.36000000","0.00000000"],["19508.67000000","0.00000000"],["19572.96000000","0.00217200"],["24386.00000000","0.00256600"]],"b":[["19455.18000000","2.94649200"],["19453.15000000","0.01233600"],["19451.18000000","0.00000000"],["19446.85000000","0.11427900"],["19446.74000000","0.00000000"],["19446.73000000","0.00000000"],["19444.45000000","0.14937800"],["19426.75000000","0.00000000"],["19416.36000000","0.36052100"]],"e":"depthUpdate","s":"BTCUSDT","u":7145637297}`)
|
|
|
|
func TestProcessUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
p := currency.NewPair(currency.BTC, currency.USDT)
|
|
var depth WebsocketDepthStream
|
|
err := json.Unmarshal(websocketDepthUpdate, &depth)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = b.obm.stageWsUpdate(&depth, p, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = b.obm.fetchBookViaREST(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = b.obm.cleanup(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|