private async Task Handle(ResumePausedSpecialLiquidationCommand command, IEventPublisher publisher)
        {
            var executionInfo = await _operationExecutionInfoRepository.GetAsync <SpecialLiquidationOperationData>(
                operationName : SpecialLiquidationSaga.OperationName,
                id : command.OperationId);

            if (executionInfo?.Data == null)
            {
                return;
            }

            var pause = await _rfqPauseService.GetCurrentAsync(command.OperationId);

            if (pause?.State == PauseState.PendingCancellation)
            {
                _chaosKitty.Meow(command.OperationId);

                var cancellationAcknowledged = await _rfqPauseService.AcknowledgeCancellationAsync(command.OperationId);

                if (cancellationAcknowledged)
                {
                    publisher.PublishEvent(new ResumePausedSpecialLiquidationSucceededEvent
                    {
                        OperationId  = command.OperationId,
                        CreationTime = _dateService.Now()
                    });

                    return;
                }

                publisher.PublishEvent(new ResumePausedSpecialLiquidationFailedEvent
                {
                    OperationId = command.OperationId,
                    Reason      = SpecialLiquidationResumePausedFailureReason.AcknowledgeError
                });

                return;
            }

            publisher.PublishEvent(new ResumePausedSpecialLiquidationFailedEvent
            {
                OperationId = command.OperationId,
                Reason      = SpecialLiquidationResumePausedFailureReason.NoActivePause
            });
        }
Exemplo n.º 2
0
        public async Task <RfqPauseInfoContract> GetPauseInfoAsync(string id)
        {
            var pause = await _rfqPauseService.GetCurrentAsync(id);

            if (pause == null)
            {
                return(null);
            }

            return(new RfqPauseInfoContract
            {
                State = pause?.State.ToString(),
                CreatedAt = pause.CreatedAt,
                EffectiveSince = pause.EffectiveSince,
                Initiator = pause.Initiator,
                Source = pause.Source.ToString()
            });
        }
Exemplo n.º 3
0
        private async Task Handle(PriceForSpecialLiquidationCalculatedEvent e, ICommandSender sender)
        {
            var executionInfo = await _operationExecutionInfoRepository.GetAsync <SpecialLiquidationOperationData>(
                operationName : OperationName,
                id : e.OperationId);

            if (executionInfo?.Data == null)
            {
                return;
            }

            var pause = await _rfqPauseService.GetCurrentAsync(e.OperationId);

            if (pause?.State == PauseState.Active)
            {
                return;
            }

            if (executionInfo.Data.SwitchState(SpecialLiquidationOperationState.PriceRequested,
                                               SpecialLiquidationOperationState.PriceReceived))
            {
                //validate that volume didn't changed to peek either to execute order or request the price again
                var currentVolume = GetNetPositionCloseVolume(executionInfo.Data.PositionIds, executionInfo.Data.AccountId);
                if (currentVolume != 0 && currentVolume != executionInfo.Data.Volume)
                {
                    // if RFQ is paused we will not continue
                    var pauseAcknowledged = await _rfqPauseService.AcknowledgeAsync(e.OperationId);

                    if (pauseAcknowledged)
                    {
                        return;
                    }

                    executionInfo.Data.RequestNumber++;
                    executionInfo.Data.Volume = currentVolume;

                    RequestPrice(sender, executionInfo);

                    await _operationExecutionInfoRepository.Save(executionInfo);

                    return;//wait for the new price
                }

                await _rfqPauseService.StopPendingAsync(e.OperationId, PauseCancellationSource.PriceReceived, nameof(SpecialLiquidationSaga));

                executionInfo.Data.Price = e.Price;

                //execute order in Gavel by API
                sender.SendCommand(new ExecuteSpecialLiquidationOrderCommand
                {
                    OperationId  = e.OperationId,
                    CreationTime = _dateService.Now(),
                    Instrument   = executionInfo.Data.Instrument,
                    Volume       = executionInfo.Data.Volume,
                    Price        = e.Price,
                }, _cqrsContextNamesSettings.TradingEngine);

                _chaosKitty.Meow(e.OperationId);

                await _operationExecutionInfoRepository.Save(executionInfo);
            }
        }