Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CryptoExchange.Net testing update #210

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ByBit.Net/Bybit.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -48,10 +48,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.4.0" />
</ItemGroup>
</Project>
8 changes: 2 additions & 6 deletions ByBit.Net/BybitAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ public BybitAuthenticationProvider(ApiCredentials credentials) : base(credential
{
}

public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary<string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, RequestBodyFormat bodyFormat, out SortedDictionary<string, object> uriParameters, out SortedDictionary<string, object> bodyParameters, out Dictionary<string, string> headers)
public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, IDictionary<string, object> uriParams, IDictionary<string, object> bodyParams, Dictionary<string, string> headers, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, RequestBodyFormat bodyFormat)
{
uriParameters = parameterPosition == HttpMethodParameterPosition.InUri ? new SortedDictionary<string, object>(providedParameters, new BybitComparer()) : new SortedDictionary<string, object>();
bodyParameters = parameterPosition == HttpMethodParameterPosition.InBody ? new SortedDictionary<string, object>(providedParameters, new BybitComparer()) : new SortedDictionary<string, object>();
headers = new Dictionary<string, string>();

if (!auth)
return;

var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParameters : bodyParameters;
var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParams : bodyParams;
var timestamp = DateTimeConverter.ConvertToMilliseconds(GetTimestamp(apiClient).AddMilliseconds(-1000)).Value.ToString(CultureInfo.InvariantCulture);
if (apiClient is BybitRestClientCopyTradingApi || apiClient is BybitRestClientApi)
{
Expand Down
58 changes: 57 additions & 1 deletion Bybit.UnitTests/BybitRestClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using Bybit.Net.Clients;
using Bybit.Net;
using Bybit.Net.Clients;
using Bybit.Net.Objects.Internal;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using Newtonsoft.Json;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using CryptoExchange.Net.Converters.JsonNet;

namespace Bybit.UnitTests
{
Expand Down Expand Up @@ -73,5 +78,56 @@ public async Task ReceivingHttpErrorWithJsonError_Should_ReturnErrorAndNotSucces
Assert.That(result.Error!.Code == 400001);
Assert.That(result.Error.Message == "Error occured");
}

[Test]
public void CheckSignatureExample1()
{
var authProvider = new BybitAuthenticationProvider(new ApiCredentials("XXXXXXXXXX", "XXXXXXXXXX"));
var client = (RestApiClient)new BybitRestClient().V5Api;

CryptoExchange.Net.Testing.TestHelpers.CheckSignature(
client,
authProvider,
HttpMethod.Get,
"",
(uriParams, bodyParams, headers) =>
{
return headers["X-BAPI-SIGN"].ToString();
},
"B3A13CBD6F9DE0AD7ECE696645A98000281BE3558716624CFCA03DEBD2F24574",
new Dictionary<string, object>
{
{ "category", "option" },
{ "symbol", "BTC-29JUL22-25000-C" },
},
DateTimeConverter.ParseFromLong(1658384314791),
true,
false);
}

[Test]
public void CheckSignatureExample2()
{
var authProvider = new BybitAuthenticationProvider(new ApiCredentials("XXXXXXXXXX", "XXXXXXXXXX"));
var client = (RestApiClient)new BybitRestClient().V5Api;

CryptoExchange.Net.Testing.TestHelpers.CheckSignature(
client,
authProvider,
HttpMethod.Post,
"",
(uriParams, bodyParams, headers) =>
{
return headers["X-BAPI-SIGN"].ToString();
},
"A04DEA33A62E644897B14C6FF458DB428B9C2424B5CB785C9586490CCACA24AF",
new Dictionary<string, object>
{
{ "category", "option" }
},
DateTimeConverter.ParseFromLong(1658385579423),
true,
false);
}
}
}
Loading