Skip to content

Commit

Permalink
CryptoExchange.Net testing update (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed May 1, 2024
1 parent 52bd39d commit 0ec4e61
Show file tree
Hide file tree
Showing 388 changed files with 3,824 additions and 13,790 deletions.
6 changes: 6 additions & 0 deletions Binance.Net.UnitTests/Binance.Net.UnitTests.csproj
Expand Up @@ -17,4 +17,10 @@
<ProjectReference Include="..\Binance.Net\Binance.Net.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Endpoints\CoinFutures\Trading\" />
<Folder Include="Endpoints\CoinFutures\ExchangeData\" />
<Folder Include="Endpoints\CoinFutures\Account\" />
</ItemGroup>

</Project>
114 changes: 71 additions & 43 deletions Binance.Net.UnitTests/BinanceClientTests.cs
@@ -1,6 +1,4 @@
using Binance.Net.Objects;
using Binance.Net.UnitTests.TestImplementations;
using CryptoExchange.Net;
using Binance.Net.UnitTests.TestImplementations;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Requests;
using Moq;
Expand All @@ -12,18 +10,13 @@
using System.Net;
using System.Net.Http;
using CryptoExchange.Net.Objects;
using Binance.Net.Enums;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System.Reflection;
using System.Diagnostics;
using Binance.Net.Objects.Models.Spot;
using Binance.Net.Clients;
using Binance.Net.Clients.SpotApi;
using Binance.Net.ExtensionMethods;
using Binance.Net.Objects.Options;
using CryptoExchange.Net.Objects.Sockets;
using NUnit.Framework.Legacy;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters.JsonNet;

namespace Binance.Net.UnitTests
{
Expand Down Expand Up @@ -163,52 +156,87 @@ public void AddingAuthToRequest_Should_AddApiKeyHeader()

// act
var headers = new Dictionary<string, string>();
authProvider.AuthenticateRequest(new BinanceRestApiClient(new TraceLogger(), new BinanceRestOptions(), new BinanceRestOptions().SpotOptions), request.Uri, HttpMethod.Get, new Dictionary<string, object>(), true, ArrayParametersSerialization.MultipleValues,
HttpMethodParameterPosition.InUri, RequestBodyFormat.Json, out var uriParameters, out var bodyParameters, out headers);
authProvider.AuthenticateRequest(
new BinanceRestApiClient(new TraceLogger(), new BinanceRestOptions(), new BinanceRestOptions().SpotOptions),
request.Uri,
HttpMethod.Get,
new SortedDictionary<string, object>(),
new SortedDictionary<string, object>(),
headers,
true, ArrayParametersSerialization.MultipleValues,
HttpMethodParameterPosition.InUri, RequestBodyFormat.Json);

// assert
Assert.That(headers.First().Key == "X-MBX-APIKEY" && headers.First().Value == "TestKey");
}

[Test]
public void CheckRestInterfaces()
public void CheckSignatureExample1()
{
var assembly = Assembly.GetAssembly(typeof(BinanceRestClient));
var ignore = new string[] { "IBinanceClientUsdFuturesApi", "IBinanceClientCoinFuturesApi", "IBinanceClientSpotApi" };
var clientInterfaces = assembly.GetTypes().Where(t => t.Name.StartsWith("IBinanceClient") && !ignore.Contains(t.Name));

foreach(var clientInterface in clientInterfaces)
{
var implementation = assembly.GetTypes().Single(t => t.IsAssignableTo(clientInterface) && t != clientInterface);
int methods = 0;
foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task))))
var authProvider = new BinanceAuthenticationProvider(new ApiCredentials("vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A", "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"));
var client = (RestApiClient)new BinanceRestClient().SpotApi;

CryptoExchange.Net.Testing.TestHelpers.CheckSignature(
client,
authProvider,
HttpMethod.Post,
"/api/v3/order",
(uriParams, bodyParams, headers) =>
{
var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray());
ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}");
methods++;
}
Debug.WriteLine($"{clientInterface.Name} {methods} methods validated");
}
return bodyParams["signature"].ToString();
},
"c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71",
new Dictionary<string, object>
{
{ "symbol", "LTCBTC" },
{ "side", "BUY" },
{ "type", "LIMIT" },
{ "timeInForce", "GTC" },
{ "quantity", "1" },
{ "price", "0.1" },
{ "recvWindow", "5000" },
},
DateTimeConverter.ParseFromLong(1499827319559),
true,
false);
}

[Test]
public void CheckSocketInterfaces()
public void CheckSignatureExample2()
{
var assembly = Assembly.GetAssembly(typeof(BinanceSocketClientSpotApi));
var clientInterfaces = assembly.GetTypes().Where(t => t.Name.StartsWith("IBinanceSocketClient"));

foreach (var clientInterface in clientInterfaces)
{
var implementation = assembly.GetTypes().Single(t => t.IsAssignableTo(clientInterface) && t != clientInterface);
int methods = 0;
foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task<CallResult<UpdateSubscription>>))))
var authProvider = new BinanceAuthenticationProvider(new ApiCredentials("vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A", "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"));
var client = (RestApiClient)new BinanceRestClient().SpotApi;
client.ParameterPositions[HttpMethod.Post] = HttpMethodParameterPosition.InUri;
CryptoExchange.Net.Testing.TestHelpers.CheckSignature(
client,
authProvider,
HttpMethod.Post,
"/api/v3/order",
(uriParams, bodyParams, headers) =>
{
var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray());
ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.GetType().Name}");
methods++;
}
Debug.WriteLine($"{clientInterface.Name} {methods} methods validated");
}
return uriParams["signature"].ToString();
},
"c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71",
new Dictionary<string, object>
{
{ "symbol", "LTCBTC" },
{ "side", "BUY" },
{ "type", "LIMIT" },
{ "timeInForce", "GTC" },
{ "quantity", "1" },
{ "price", "0.1" },
{ "recvWindow", "5000" },
},
DateTimeConverter.ParseFromLong(1499827319559),
true,
false);
}

[Test]
public void CheckInterfaces()
{
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<BinanceRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<BinanceSocketClient>();
}
}
}
@@ -1,3 +1,6 @@
POST
/sapi/v1/asset/convert-transfer
true
{
"tranId": 118263407119,
"status": "S"
Expand Down
@@ -0,0 +1,6 @@
POST
/sapi/v1/margin/max-leverage
true
{
"success": true
}
@@ -0,0 +1,3 @@
POST
/sapi/v1/margin/exchange-small-liability
true
@@ -0,0 +1,7 @@
DELETE
/sapi/v1/margin/isolated/account
true
{
"success": true,
"symbol": "BTCUSDT"
}
@@ -1,3 +1,6 @@
POST
/sapi/v1/asset/dust
true
{
"totalServiceCharge":"0.02102542",
"totalTransfered":"1.05127099",
Expand Down
@@ -0,0 +1,7 @@
POST
/sapi/v1/margin/isolated/account
true
{
"success": true,
"symbol": "BTCUSDT"
}
@@ -0,0 +1,16 @@
GET
/sapi/v1/account/apiRestrictions
true
{
"ipRestrict":false,
"createTime":1698645219000,
"enableInternalTransfer":false, // This option authorizes this key to transfer funds between your master account and your sub account instantly
"enableFutures":false, // The Futures API cannot be used if the API key was created before the Futures account was opened, or if you have enabled portfolio margin.
"enablePortfolioMarginTrading":true, // API Key created before your activate portfolio margin does not support portfolio margin API service
"enableVanillaOptions":false, // Authorizes this key to Vanilla options trading
"permitsUniversalTransfer":false, // Authorizes this key to be used for a dedicated universal transfer API to transfer multiple supported currencies. Each business's own transfer API rights are not affected by this authorization
"enableReading":true,
"enableSpotAndMarginTrading":false, // Spot and margin trading
"enableWithdrawals":false, // This option allows you to withdraw via API. You must apply the IP Access Restriction filter in order to enable withdrawals
"enableMargin":false // This option can be adjusted after the Cross Margin account transfer is completed
}
@@ -1,11 +1,23 @@
GET
/api/v3/account
true
{
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"commissionRates": {
"maker": "0.00150000",
"taker": "0.00150000",
"buyer": "0.00000000",
"seller": "0.00000000"
},
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"brokered": false,
"requireSelfTradePrevention": false,
"preventSor": false,
"updateTime": 123456789,
"accountType": "SPOT",
"balances": [
Expand All @@ -22,5 +34,6 @@
],
"permissions": [
"SPOT"
]
],
"uid": 354937868
}
@@ -0,0 +1,6 @@
GET
/sapi/v1/account/status
true
{
"data": "Normal"
}
@@ -1,3 +1,6 @@
GET
/sapi/v1/asset/assetDividend
true
{
"rows":[
{
Expand Down
@@ -1,12 +1,15 @@
POST
/sapi/v1/asset/dust-btc
true
{
"details": [
{
"asset": "ADA",
"assetFullName": "ADA",
"amountFree": "6.21", //Convertible amount
"toBTC": "0.00016848", //BTC amount
"toBNB": "0.01777302", //BNB amount(Not deducted commission fee)
"toBNBOffExchange": "0.01741756", //BNB amount(Deducted commission fee)
"toBNB": "0.01777302", //BNB amount
"toBNBOffExchange": "0.01741756", //BNB amount
"exchange": "0.00035546" //Commission fee
}
],
Expand Down
@@ -0,0 +1,16 @@
GET
/sapi/v1/capital/contract/convertible-coins
true
{
"convertEnabled": true,
"coins": [
"USDC",
"USDP",
"TUSD"
],
"exchangeRates": {
"USDC": "1",
"TUSD": "1",
"USDP": "1"
}
}
@@ -1,3 +1,6 @@
POST
/sapi/v3/asset/getUserAsset
true
[
{
"asset": "AVAX",
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/bnbBurn
true
{
"spotBNBBurn":true,
"interestBNBBurn": false
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/asset/convert-transfer/queryByPage
true
{
"total":3,
"rows":
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/asset/ledger-transfer/cloud-mining/queryByPage
true
{
"total":5,
"rows":[
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/margin/exchange-small-liability
true
[
{
"asset": "ETH",
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/margin/exchange-small-liability-history
true
{
"total": 1,
"rows": [
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/accountSnapshot
true
{
"code":200, // 200 for success; others are error codes
"msg":"", // error message
Expand All @@ -7,7 +10,7 @@
"assets":[
{
"asset":"USDT",
"marginBalance":"118.99782335",
"marginBalance":"118.99782335", // Not real-time data, can ignore
"walletBalance":"120.23811389"
}
],
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/accountSnapshot
true
{
"code":200, // 200 for success; others are error codes
"msg":"", // error message
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/accountSnapshot
true
{
"code":200, // 200 for success; others are error codes
"msg":"", // error message
Expand Down
@@ -1,3 +1,6 @@
GET
/sapi/v1/capital/deposit/address
true
{
"address": "1HPn8Rx2y6nNSfagQBKy27GB99Vbzg89wv",
"coin": "BTC",
Expand Down

0 comments on commit 0ec4e61

Please sign in to comment.