private async Task Handle(PriceForSpecialLiquidationCalculatedEvent e, ICommandSender sender) { var executionInfo = await _operationExecutionInfoRepository.GetAsync <SpecialLiquidationOperationData>( operationName : OperationName, id : e.OperationId); if (executionInfo?.Data == null) { 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 != e.Volume) { sender.SendCommand(new GetPriceForSpecialLiquidationCommand { OperationId = e.OperationId, CreationTime = _dateService.Now(), Instrument = executionInfo.Data.Instrument, Volume = currentVolume, RequestNumber = e.RequestNumber++, AccountId = executionInfo.Data.AccountId, }, _cqrsContextNamesSettings.Gavel); executionInfo.Data.Volume = currentVolume; await _operationExecutionInfoRepository.Save(executionInfo); return;//wait for the new price } 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); } }
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); } }