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

Feature/integration tests #227

Merged
merged 4 commits into from
Jul 26, 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
25 changes: 25 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Integration Testing

on:
schedule:
- cron: "0 0 * * *"

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
- name: Set GitHub package source
run: dotnet nuget add source --username JKorf --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/JKorf/index.json"
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal -e INTEGRATION=1
2 changes: 1 addition & 1 deletion ByBit.Net/Bybit.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.9.0" />
<PackageReference Include="CryptoExchange.Net" Version="7.10.0" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions ByBit.Net/Clients/V5/BybitRestClientApiAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public async Task<WebCallResult<BybitResponse<BybitFeeRate>>> GetFeeRateAsync(

#endregion

#region Get Account Info
#region Get Margin Account Info

/// <inheritdoc />
public async Task<WebCallResult<BybitAccountInfo>> GetMarginAccountInfoAsync(CancellationToken ct = default)
Expand Down Expand Up @@ -1041,7 +1041,7 @@ public async Task<WebCallResult<IEnumerable<BybitConvertAsset>>> GetConvertAsset

#endregion

#region Get Convert Assets
#region Get Convert Quote

/// <inheritdoc />
public async Task<WebCallResult<BybitConvertQuote>> GetConvertQuoteAsync(ConvertAccountType accountType, string fromAsset, string toAsset, decimal quantity, string? clientOrderId = null, CancellationToken ct = default)
Expand Down
12 changes: 4 additions & 8 deletions ByBit.Net/Clients/V5/BybitRestClientApiExchangeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public async Task<WebCallResult<BybitResponse<BybitAnnouncement>>> GetAnnounceme
/// <inheritdoc />
public async Task<WebCallResult<BybitTime>> GetServerTimeAsync(CancellationToken ct = default)
{
// V5 doesn't have it's own server time endpoint (yet)
return await _baseClient.SendRequestAsync<BybitTime>(_baseClient.GetUrl("v5/market/time"), HttpMethod.Get, ct, null).ConfigureAwait(false);
}

Expand Down Expand Up @@ -169,7 +168,7 @@ public async Task<WebCallResult<BybitResponse<BybitOptionSymbol>>> GetOptionSymb

#endregion

#region Get Option symbols
#region Get Linear Inverse symbols

/// <inheritdoc />
public async Task<WebCallResult<BybitResponse<BybitLinearInverseSymbol>>> GetLinearInverseSymbolsAsync(
Expand Down Expand Up @@ -339,17 +338,14 @@ public async Task<WebCallResult<BybitResponse<BybitOpenInterest>>> GetOpenIntere

#endregion

#region Get Historic Volatility
#region Get Historical Volatility

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<BybitHistoricalVolatility>>> GetHistoricalVolatilityAsync(Category category, string? baseAsset = null, int? period = null, DateTime? startTime = null, DateTime? endTime = null, int? limit = null, string? cursor = null, CancellationToken ct = default)
public async Task<WebCallResult<IEnumerable<BybitHistoricalVolatility>>> GetHistoricalVolatilityAsync(string? baseAsset = null, int? period = null, DateTime? startTime = null, DateTime? endTime = null, int? limit = null, string? cursor = null, CancellationToken ct = default)
{
if (category != Category.Option)
throw new ArgumentException("Invalid category; should be Option");

var parameters = new Dictionary<string, object>()
{
{ "category", EnumConverter.GetString(category) },
{ "category", EnumConverter.GetString(Category.Option) },
};
parameters.AddOptionalParameter("startTime", DateTimeConverter.ConvertToMilliseconds(startTime));
parameters.AddOptionalParameter("endTime", DateTimeConverter.ConvertToMilliseconds(endTime));
Expand Down
8 changes: 2 additions & 6 deletions ByBit.Net/Clients/V5/BybitRestClientApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,13 @@ public async Task<WebCallResult<BybitResponse<BybitOrderId>>> CancelAllOrderAsyn

/// <inheritdoc />
public async Task<WebCallResult<BybitBorrowQuota>> GetBorrowQuotaAsync(
Category category,
string symbol,
OrderSide side,
CancellationToken ct = default)
{
if (category != Category.Spot)
throw new ArgumentException("Category should be spot");

var parameters = new Dictionary<string, object>()
{
{ "category", EnumConverter.GetString(category) },
{ "category", EnumConverter.GetString(Category.Spot) },
{ "symbol", symbol },
{ "side", EnumConverter.GetString(side) },
};
Expand Down Expand Up @@ -565,7 +561,7 @@ public async Task<WebCallResult<BybitResponse<BybitPosition>>> GetPositionsAsync

#endregion

#region Get Positions
#region Confirm Risk Limit

/// <inheritdoc />
public async Task<WebCallResult> ConfirmRiskLimitAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public interface IBybitRestClientApiExchangeData
/// Get historical volatility
/// <para><a href="https://bybit-exchange.github.io/docs/v5/market/iv" /></para>
/// </summary>
/// <param name="category">Category</param>
/// <param name="baseAsset">Filter by base asset</param>
/// <param name="period">Period</param>
/// <param name="startTime">Fitler by start time</param>
Expand All @@ -73,7 +72,7 @@ public interface IBybitRestClientApiExchangeData
/// <param name="cursor">Pagination cursor</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult<IEnumerable<BybitHistoricalVolatility>>> GetHistoricalVolatilityAsync(Category category, string? baseAsset = null, int? period = null, DateTime? startTime = null, DateTime? endTime = null, int? limit = null, string? cursor = null, CancellationToken ct = default);
Task<WebCallResult<IEnumerable<BybitHistoricalVolatility>>> GetHistoricalVolatilityAsync(string? baseAsset = null, int? period = null, DateTime? startTime = null, DateTime? endTime = null, int? limit = null, string? cursor = null, CancellationToken ct = default);

/// <summary>
/// Get index price klines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ Task<WebCallResult<BybitOrderId>> EditOrderAsync(
/// Get spot borrow quota
/// <para><a href="https://bybit-exchange.github.io/docs/v5/order/spot-borrow-quota" /></para>
/// </summary>
/// <param name="category">Category</param>
/// <param name="symbol">Symbol</param>
/// <param name="side">Side</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult<BybitBorrowQuota>> GetBorrowQuotaAsync(Category category, string symbol, OrderSide side, CancellationToken ct = default);
Task<WebCallResult<BybitBorrowQuota>> GetBorrowQuotaAsync(string symbol, OrderSide side, CancellationToken ct = default);

/// <summary>
/// Get delivery history
Expand Down
2 changes: 1 addition & 1 deletion ByBit.Net/Objects/Models/V5/BybitCollateralInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public record BybitCollateralInfo
/// Borrow usage rate
/// </summary>
[JsonProperty("borrowUsageRate")]
public decimal BorrowUsageRate { get; set; }
public decimal? BorrowUsageRate { get; set; }
}
}
116 changes: 116 additions & 0 deletions Bybit.UnitTests/BybitRestIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Bybit.Net.Clients;
using Bybit.Net.Objects;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Testing;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bybit.Net.UnitTests
{
[NonParallelizable]
internal class BybitRestIntegrationTests : RestIntergrationTest<BybitRestClient>
{
public override bool Run { get; set; }

public BybitRestIntegrationTests()
{
}

public override BybitRestClient GetClient(ILoggerFactory loggerFactory)
{
var key = Environment.GetEnvironmentVariable("APIKEY");
var sec = Environment.GetEnvironmentVariable("APISECRET");

Authenticated = key != null && sec != null;
return new BybitRestClient(null, loggerFactory, opts =>
{
opts.OutputOriginalData = true;
opts.ApiCredentials = Authenticated ? new ApiCredentials(key, sec) : null;
});
}

[Test]
public async Task TestErrorResponseParsing()
{
if (!ShouldRun())
return;

var result = await CreateClient().V5Api.ExchangeData.GetSpotTickersAsync("TST_TST", default);

Assert.That(result.Success, Is.False);
Assert.That(result.Error.Code, Is.EqualTo(10001));
}

[Test]
public async Task TestV5Account()
{
await RunAndCheckResult(client => client.V5Api.Account.GetBalancesAsync(Enums.AccountType.Unified, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetBorrowHistoryAsync(default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetCollateralInfoAsync(default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetAssetGreeksAsync(default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetFeeRateAsync(Enums.Category.Spot, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetMarginAccountInfoAsync(default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetTransactionHistoryAsync(default, default, default, default, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetAssetInfoAsync(default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetWithdrawalsAsync(default, default, default, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetDelayedWithdrawQuantityAsync("ETH", default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetApiKeyInfoAsync(default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetAccountTypesAsync(default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetSpotMarginStatusAndLeverageAsync(default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetSpotMarginDataAsync(default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetConvertAssetsAsync(Enums.ConvertAccountType.ConvertUta, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Account.GetConvertHistoryAsync(Enums.ConvertAccountType.ConvertUta, default, default, default), true);

// Only available to broker account
//await RunAndCheckResult(client => client.V5Api.Account.GetBrokerAccountInfoAsync(default), true);
//await RunAndCheckResult(client => client.V5Api.Account.GetBrokerEarningsAsync(default, default, default, default, default, default, default), true);
}

[Test]
public async Task TestV5ExchangeData()
{
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetServerTimeAsync(default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetKlinesAsync(Enums.Category.Spot, "ETHUSDT", Enums.KlineInterval.OneDay, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetMarkPriceKlinesAsync(Enums.Category.Linear, "ETHUSDT", Enums.KlineInterval.OneDay, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetIndexPriceKlinesAsync(Enums.Category.Linear, "ETHUSDT", Enums.KlineInterval.OneDay, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetPremiumIndexPriceKlinesAsync(Enums.Category.Linear, "ETHUSDT", Enums.KlineInterval.OneDay, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetSpotSymbolsAsync(default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetOptionSymbolsAsync(default, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetLinearInverseSymbolsAsync(default, default, default, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetOrderbookAsync(Enums.Category.Spot, "ETHUSDT", default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetSpotTickersAsync(default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetOptionTickersAsync(default, "BTC", default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetLinearInverseTickersAsync(Enums.Category.Linear, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetFundingRateHistoryAsync(Enums.Category.Linear, "ETHUSDT", default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetTradeHistoryAsync(Enums.Category.Linear, "ETHUSDT", default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetOpenInterestAsync(Enums.Category.Linear, "ETHUSDT", Enums.OpenInterestInterval.OneDay, default, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetHistoricalVolatilityAsync(default, default, default, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetInsuranceAsync("ETH", default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetRiskLimitAsync(Enums.Category.Linear, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetDeliveryPriceAsync(Enums.Category.Linear, default, default, default, default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetLeverageTokensAsync(default, default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetLeverageTokenMarketAsync("BTC3L", default), false);
await RunAndCheckResult(client => client.V5Api.ExchangeData.GetLongShortRatioAsync(Enums.Category.Linear, "ETHUSDT", Enums.DataPeriod.OneDay, default, default), false);
}

[Test]
public async Task TestV5Trading()
{
await RunAndCheckResult(client => client.V5Api.Trading.GetOrdersAsync(Enums.Category.Spot, default, default, default, default, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetOrderHistoryAsync(Enums.Category.Spot, default, default, default, default, default, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetBorrowQuotaAsync("ETHUSDT", Enums.OrderSide.Buy, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetDisconnectCancelAllConfigAsync(default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetUserTradesAsync(Enums.Category.Spot, default, default, default, default, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetPositionsAsync(Enums.Category.Linear, default, default, "USDT", default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetAssetExchangeHistoryAsync(default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetDeliveryHistoryAsync(Enums.Category.Linear, default, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetSettlementHistoryAsync(Enums.Category.Linear, default, default, default, default, default, default), true);
await RunAndCheckResult(client => client.V5Api.Trading.GetClosedProfitLossAsync(Enums.Category.Linear, default, default, default, default, default, default), true);
}
}
}
4 changes: 2 additions & 2 deletions Bybit.UnitTests/RestRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task ValidateSpotExchangeDataCalls()
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetFundingRateHistoryAsync(Enums.Category.Inverse, "ETHUSDT"), "GetFundingRateHistory");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetTradeHistoryAsync(Enums.Category.Inverse, "ETHUSDT"), "GetTradeHistory");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetOpenInterestAsync(Enums.Category.Inverse, "ETHUSDT", Enums.OpenInterestInterval.OneDay), "GetOpenInterest");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetHistoricalVolatilityAsync(Enums.Category.Option, "ETHUSDT"), "GetHistoricalVolatility");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetHistoricalVolatilityAsync("ETHUSDT"), "GetHistoricalVolatility");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetInsuranceAsync(), "GetInsurance");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetRiskLimitAsync(Enums.Category.Inverse), "GetRiskLimit");
await tester.ValidateAsync(client => client.V5Api.ExchangeData.GetDeliveryPriceAsync(Enums.Category.Inverse), "GetDeliveryPrice");
Expand All @@ -115,7 +115,7 @@ public async Task ValidateSpotTradingCalls()
await tester.ValidateAsync(client => client.V5Api.Trading.GetOrdersAsync(Enums.Category.Option, "ETHUSDT"), "GetOrders");
await tester.ValidateAsync(client => client.V5Api.Trading.CancelAllOrderAsync(Enums.Category.Option, "ETHUSDT"), "CancelAllOrder", ignoreProperties: new List<string> { "success" });
await tester.ValidateAsync(client => client.V5Api.Trading.GetOrderHistoryAsync(Enums.Category.Option, "ETHUSDT"), "GetOrderHistory");
await tester.ValidateAsync(client => client.V5Api.Trading.GetBorrowQuotaAsync(Enums.Category.Spot, "ETHUSDT", Enums.OrderSide.Buy), "GetBorrowQuota");
await tester.ValidateAsync(client => client.V5Api.Trading.GetBorrowQuotaAsync("ETHUSDT", Enums.OrderSide.Buy), "GetBorrowQuota");
await tester.ValidateAsync(client => client.V5Api.Trading.SetDisconnectCancelAllAsync(10), "SetDisconnectCancelAll");
await tester.ValidateAsync(client => client.V5Api.Trading.GetUserTradesAsync(Enums.Category.Option), "GetUserTrades");
await tester.ValidateAsync(client => client.V5Api.Trading.GetPositionsAsync(Enums.Category.Option), "GetPositions", ignoreProperties: new List<string> { "mmrSysUpdateTime" });
Expand Down
Loading