JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.03k stars 425 forks source link

testnet.binancefuture.com connection issue #576

Closed wil70 closed 3 years ago

wil70 commented 3 years ago

Describe the bug Hello, I'm not able to get order working on testnet (https://testnet.binancefuture.com/en/delivery/BTCUSD_QUARTER?theme=dark) I'm able to get the data feed (but from binance.com, not from the testnet), and I do not get the balance or the order. So my guess is it is still going to binance.com instead of testnet.binancefuture.com

I also tried to add BaseAddress = "https://testnet.binancefuture.com", to the clientOptions but that didn't work To Reproduce CryptoExchange.Net.Authentication.ApiCredentials apiCredential = new CryptoExchange.Net.Authentication.ApiCredentials( "my key from https://testnet.binancefuture.com/en/delivery/BTCUSD_QUARTER?theme=dark", "my secret key from https://testnet.binancefuture.com/en/delivery/BTCUSD_QUARTER?theme=dark" );//"BinanceKey", "BinanceSecret");

            // ref: https://github.com/JKorf/Binance.Net/issues/545
            var clientOptions = new BinanceClientOptions
            {
                BaseAddressUsdtFutures = "https://testnet.binancefuture.com",
                BaseAddressCoinFutures = "https://testnet.binancefuture.com"
            };
            clientOptions.ApiCredentials = apiCredential;

            var socketClientOptions = new BinanceSocketClientOptions
            {
                BaseAddress = "wss://stream.binancefuture.com",
                BaseAddressUsdtFutures = "wss://stream.binancefuture.com",
                BaseAddressCoinFutures = "wss://stream.binancefuture.com",
            };
            socketClientOptions.ApiCredentials = apiCredential;

            _socketExchange = new BinanceClient(clientOptions);
            _socketClient = new BinanceSocketClient(socketClientOptions);// new Binance.Net.Objects.Spot.BinanceSocketClientOptions();

            _isConnected = true;

            var symbols = await _socketExchange.GetSymbolsAsync();
            Console.WriteLine($"{((CryptoExchange.Net.RestClient)_socketExchange).ClientName}: {symbols.Data?.Count()} symbols returned, first = {symbols.Data?.First().CommonName}");

            var balances = await _socketExchange.GetBalancesAsync();
            var btcBalance = balances.Data?.Where(b => b.CommonAsset.ToUpperInvariant() == "BTC").FirstOrDefault();
            Console.WriteLine($"{((CryptoExchange.Net.RestClient)_socketExchange).ClientName}: {balances.Data?.Count()} balance returned, BTC balance = {btcBalance?.CommonTotal}");

            var symbolName = _socketExchange.GetSymbolName("BTC", "USDT");
            var klines = await _socketExchange.GetKlinesAsync(symbolName, TimeSpan.FromHours(1));
            Console.WriteLine($"{((CryptoExchange.Net.RestClient)_socketExchange).ClientName}: {klines.Data?.Count()} klines returned, first klines @ {klines.Data?.First().CommonClose}");

            var orderResult = await _socketExchange.PlaceOrderAsync(
"BTCUSDT",
CryptoExchange.Net.ExchangeInterfaces.IExchangeClient.OrderSide.Buy,
CryptoExchange.Net.ExchangeInterfaces.IExchangeClient.OrderType.Market,
(decimal).2,
null,
null
);

Expected behavior [2021.01.29 8:20:40] Binance: 1273 symbols returned, first = ETHBTC [2021.01.29 8:20:42] Binance: balance returned, BTC balance = 10000000 [2021.01.29 8:20:44] Binance: 500 klines returned, first klines @ 40075.82000000

Debug logging The error is -2015 "Invalid API-key, IP, or permissions for action." Unauthorized so it means something is not align between the calls I do and my keys

thanks!

wil70 commented 3 years ago

For testnet, with perpertual future conract, It doesn't seem to be using the "fapi" and "v1" The BinanceClientFutureUsdt is created within BinanceClient and it uses "api" and "v3" Is there a way to make it work with testnet and perpetual future contrats?

JKorf commented 3 years ago

Hi, you seem to be using the IExchangeClient interface, which is currently only suited for Spot trading. If you want to use the futures (testnet) you can use the IBinanceClient interface instead.

 IExchangeClient _socketExchange; // <- only supports a selection of common functions
 IBinanceClient _socketExchange; // <- the full binance library

To place an order you can then use _ socketExchange.FuturesUsdt.Order.PlaceOrder() to place an order, and _socketExchange.FuturesUsdt.Market.xx to get market info.

wil70 commented 3 years ago

Working - thank you @JKorf!

I'm having difficulty login into the testnet website itself today; it was working well before. https://testnet.binancefuture.com/en/futures/BCH_USDT image

Are you experiencing the samething?

Mafyou commented 3 years ago

It's half fixed now lol! You can login but when go to chart, it's seems gone.

wil70 commented 3 years ago

;) Do you experience this too? How do we fix this? I entered a ticket at Binance but they do not seem super responsive.

JKorf commented 3 years ago

Binance Testnet availability is out of scope for this library