/// <summary>
        /// Performs a validation if current type of margin trading is enabled globally and for the particular client
        /// (which is extracted from the action parameters).
        /// Skips validation if current action method is marked with <see cref="SkipMarginTradingEnabledCheckAttribute"/>.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Using this type of margin trading is restricted for client or
        /// a controller action has more then one ClientId in its parameters.
        /// </exception>
        private async Task ValidateMarginTradingEnabledAsync(ActionExecutingContext context)
        {
            var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return;
            }

            var cacheKey       = CacheKeyBuilder.Create(nameof(MarginTradingEnabledFilter), nameof(GetSingleClientIdGetter), controllerActionDescriptor.DisplayName);
            var clientIdGetter = _cacheProvider.Get(cacheKey, () => new CachableResult <ClientIdGetter>(GetSingleClientIdGetter(controllerActionDescriptor), CachingParameters.InfiniteCache));

            if (clientIdGetter != null)
            {
                var clientId = clientIdGetter(context.ActionArguments);
                if (string.IsNullOrWhiteSpace(clientId))
                {
                    await _log.WriteWarningAsync(nameof(MarginTradingEnabledFilter), nameof(ValidateMarginTradingEnabledAsync), context.ActionDescriptor.DisplayName, "ClientId is null but is expected. No validation will be performed");
                }
                else if (!await _marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId, _marginSettings.IsLive))
                {
                    throw new InvalidOperationException("Using this type of margin trading is restricted for client " + clientId);
                }
            }
        }
Exemple #2
0
        public async Task <InitDataLiveDemoClientResponse> InitData(string clientId)
        {
            var marginTradingEnabled = await _marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId);

            if (!marginTradingEnabled.Demo && !marginTradingEnabled.Live)
            {
                throw new Exception("Margin trading is not available");
            }

            var initData        = new InitDataLiveDemoClientResponse();
            var clientIdRequest = new ClientIdBackendRequest {
                ClientId = clientId
            };

            var initDataResponses = await _httpRequestService.RequestIfAvailableAsync <InitDataBackendResponse>(clientIdRequest, "init.data", () => null, marginTradingEnabled);

            initData.Live = initDataResponses.Live?.ToClientContract();
            initData.Demo = initDataResponses.Demo?.ToClientContract();

            var initAssetsResponses = await _httpRequestService.RequestIfAvailableAsync(clientIdRequest, "init.assets", Array.Empty <AssetPairBackendContract>, marginTradingEnabled);

            initData.Assets = initAssetsResponses.Live.Concat(initAssetsResponses.Demo).GroupBy(a => a.Id)
                              .Select(g => g.First().ToClientContract()).ToArray();

            var initPricesResponse =
                await _httpRequestService.RequestWithRetriesAsync <Dictionary <string, InstrumentBidAskPairContract> >(
                    clientIdRequest, "init.prices");

            initData.Prices = initPricesResponse.ToDictionary(p => p.Key, p => p.Value.ToClientContract());

            return(initData);
        }
Exemple #3
0
        private async Task <List <string> > GetAvailableAssetIds(string clientId)
        {
            var marginTradingEnabled = await _marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId);

            var responses = await _httpRequestService.RequestIfAvailableAsync(new ClientIdBackendRequest { ClientId = clientId },
                                                                              "init.availableassets",
                                                                              () => new List <string>(),
                                                                              marginTradingEnabled);

            return(responses.Live.Concat(responses.Demo).Distinct().ToList());
        }
Exemple #4
0
 public Task <bool> IsMarginTradingEnabled(string clientId)
 {
     return(_marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId, _dataReaderSettings.IsLive));
 }