Example #1
0
        public async Task Handle(ResumeLiquidationInternalCommand command,
                                 IEventPublisher publisher)
        {
            var executionInfo = await _operationExecutionInfoRepository.GetAsync <LiquidationOperationData>(
                operationName : LiquidationSaga.OperationName,
                id : command.OperationId);

            if (executionInfo?.Data == null)
            {
                await _log.WriteWarningAsync(nameof(LiquidationCommandsHandler),
                                             nameof(ResumeLiquidationInternalCommand),
                                             $"Unable to resume liquidation. Execution info was not found. Command: {command.ToJson()}");

                return;
            }

            await _log.WriteInfoAsync(nameof(LiquidationCommandsHandler),
                                      nameof(ResumeLiquidationInternalCommand),
                                      command.ToJson(),
                                      "Checking if position liquidation should be failed");

            var account = _accountsCache.Get(executionInfo.Data.AccountId);

            if (ShouldFailExecution(account.GetAccountLevel(), executionInfo.Data.LiquidationType))
            {
                await _log.WriteWarningAsync(
                    nameof(LiquidationCommandsHandler),
                    nameof(ResumeLiquidationInternalCommand),
                    new { accountId = account.Id, accountLevel = account.GetAccountLevel().ToString() }.ToJson(),
                    $"Unable to resume liquidation since account level is not {ValidAccountLevel.ToString()}.");

                await _failureExecutor.ExecuteAsync(publisher,
                                                    account.Id,
                                                    command.OperationId,
                                                    $"Account level is not {ValidAccountLevel.ToString()}.");

                return;
            }

            if (!command.IsCausedBySpecialLiquidation &&
                (!command.ResumeOnlyFailed || executionInfo.Data.State == LiquidationOperationState.Failed) ||
                executionInfo.Data.State == LiquidationOperationState.SpecialLiquidationStarted)
            {
                publisher.PublishEvent(new LiquidationResumedEvent
                {
                    OperationId  = command.OperationId,
                    CreationTime = _dateService.Now(),
                    Comment      = command.Comment,
                    IsCausedBySpecialLiquidation            = command.IsCausedBySpecialLiquidation,
                    PositionsLiquidatedBySpecialLiquidation = command.PositionsLiquidatedBySpecialLiquidation,
                    AccountId       = executionInfo.Data.AccountId,
                    AssetPairId     = executionInfo.Data.AssetPairId,
                    LiquidationType = executionInfo.Data.LiquidationType.ToType <LiquidationTypeContract>()
                });
            }
            else
            {
                await _log.WriteWarningAsync(
                    nameof(LiquidationCommandsHandler),
                    nameof(ResumeLiquidationInternalCommand),
                    null,
                    $"Unable to resume liquidation in state {executionInfo.Data.State}. Command: {command.ToJson()}");
            }
        }
Example #2
0
        public async Task Handle(ResumeLiquidationInternalCommand command,
                                 IEventPublisher publisher)
        {
            var executionInfo = await _operationExecutionInfoRepository.GetAsync <LiquidationOperationData>(
                operationName : LiquidationSaga.OperationName,
                id : command.OperationId);

            if (executionInfo?.Data == null)
            {
                await _log.WriteWarningAsync(nameof(LiquidationCommandsHandler),
                                             nameof(ResumeLiquidationInternalCommand),
                                             $"Unable to resume liquidation. Execution info was not found. Command: {command.ToJson()}");

                return;
            }

            if (!command.IsCausedBySpecialLiquidation ||
                executionInfo.Data.State == LiquidationOperationState.SpecialLiquidationStarted)
            {
                publisher.PublishEvent(new LiquidationResumedInternalEvent
                {
                    OperationId  = command.OperationId,
                    CreationTime = _dateService.Now(),
                    Comment      = command.Comment,
                    IsCausedBySpecialLiquidation            = command.IsCausedBySpecialLiquidation,
                    PositionsLiquidatedBySpecialLiquidation = command.PositionsLiquidatedBySpecialLiquidation
                });
            }
            else
            {
                await _log.WriteWarningAsync(nameof(LiquidationCommandsHandler),
                                             nameof(ResumeLiquidationInternalCommand),
                                             $"Unable to resume liquidation in state {executionInfo.Data.State}. Command: {command.ToJson()}");
            }
        }