From 34ee99ab0c0d8a1d28bd33f73c74d900d63056dd Mon Sep 17 00:00:00 2001 From: TarasMi Date: Tue, 2 May 2023 11:33:20 +0000 Subject: [PATCH] [Bybit] Changed endpoint for unified account status 1. Different endpoint used to fetch unified account status. It's more reliable, doesn't need to handle API exception in case account is not unified. 2. Added a method to get API key expiration date. --- .../Exchanges/Bybit/ExchangeBybitV5Base.cs | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs b/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs index e065151f..9c4eb81a 100644 --- a/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs +++ b/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs @@ -205,37 +205,21 @@ protected override async Task OnGetOrderBookAsync(string mark /// Account status (is account Unified) needed in some private end-points (e.g. OnGetAmountsAvailableToTradeAsync or GetRecentOrderAsync). /// Better be set with constructor. If it's not set, this method will be used to get the account status. /// - public async Task GetAccountInfo() + public async Task GetAccountUnifiedStatusAsync() { - try - { - JObject result = await MakeJsonRequestAsync("/v5/account/info", null, await GetNoncePayloadAsync()); - int statusId = result["unifiedMarginStatus"].ConvertInvariant(); - IsUnifiedAccount = statusId switch - { - 1 => false, - 2 => MarketCategory == MarketCategory.Linear || MarketCategory == MarketCategory.Option, - 3 => MarketCategory == MarketCategory.Linear || MarketCategory == MarketCategory.Option || MarketCategory == MarketCategory.Spot, - _ => throw new ArgumentOutOfRangeException($"statusId is {statusId}"), - }; - } - catch (APIException e) - { - if (e.Message.Contains("3400026")) // for some reason bybit returns an {code:3400026, message:'account not exist'} error if account is not unified - { - IsUnifiedAccount = false; - } - else - { - throw; - } - } + JObject result = await MakeJsonRequestAsync("/v5/user/query-api", null, await GetNoncePayloadAsync()); + IsUnifiedAccount = result["unified"].ConvertInvariant() == 1 || result["uta"].ConvertInvariant() == 1; + } + public async Task GetAPIKeyExpirationDateAsync() + { + JObject result = await MakeJsonRequestAsync("/v5/user/query-api", null, await GetNoncePayloadAsync()); + return CryptoUtility.ParseTimestamp(result["expiredAt"], TimestampType.Iso8601UTC); } protected override async Task> OnGetAmountsAvailableToTradeAsync() { if (IsUnifiedAccount == null) { - await GetAccountInfo(); + await GetAccountUnifiedStatusAsync(); } var payload = await GetNoncePayloadAsync(); string accType = MarketCategory == MarketCategory.Inverse ? "CONTRACT" : @@ -388,7 +372,7 @@ public async Task GetRecentOrderAsync(string orderId, strin } if (IsUnifiedAccount == null) { - await GetAccountInfo(); + await GetAccountUnifiedStatusAsync(); } Dictionary payload = await GetNoncePayloadAsync(); payload.Add("symbol", marketSymbol);