Esempio n. 1
0
        private async Task Handle(FreezeAmountForWithdrawalCommand command, IEventPublisher publisher)
        {
            var(executionInfo, _) = await _operationExecutionInfoRepository.GetOrAddAsync(
                operationName : OperationName,
                operationId : command.OperationId,
                factory : () => new OperationExecutionInfo <WithdrawalFreezeOperationData>(
                    operationName: OperationName,
                    id: command.OperationId,
                    lastModified: _dateService.Now(),
                    data: new WithdrawalFreezeOperationData
            {
                State     = OperationState.Initiated,
                AccountId = command.AccountId,
                Amount    = command.Amount,
            }
                    ));

            MarginTradingAccount account = null;

            try
            {
                account = _accountsCacheService.Get(command.AccountId);
            }
            catch
            {
                publisher.PublishEvent(new AmountForWithdrawalFreezeFailedEvent(command.OperationId, _dateService.Now(),
                                                                                command.AccountId, command.Amount, $"Failed to get account {command.AccountId}"));
                return;
            }

            if (executionInfo.Data.SwitchState(OperationState.Initiated, OperationState.Started))
            {
                if (account.GetFreeMargin() >= command.Amount)
                {
                    await _accountUpdateService.FreezeWithdrawalMargin(command.AccountId, command.OperationId,
                                                                       command.Amount);

                    _chaosKitty.Meow(command.OperationId);

                    publisher.PublishEvent(new AmountForWithdrawalFrozenEvent(command.OperationId, _dateService.Now(),
                                                                              command.AccountId, command.Amount, command.Reason));
                }
                else
                {
                    publisher.PublishEvent(new AmountForWithdrawalFreezeFailedEvent(command.OperationId,
                                                                                    _dateService.Now(),
                                                                                    command.AccountId, command.Amount, "Not enough free margin"));
                }

                _chaosKitty.Meow(command.OperationId);

                await _operationExecutionInfoRepository.Save(executionInfo);
            }
        }
Esempio n. 2
0
 public static MarginTradingAccountClientContract ToClientContract(this MarginTradingAccount src)
 {
     return(new MarginTradingAccountClientContract
     {
         Id = src.Id,
         TradingConditionId = src.TradingConditionId,
         BaseAssetId = src.BaseAssetId,
         Balance = src.Balance,
         WithdrawTransferLimit = src.WithdrawTransferLimit,
         MarginCall = src.GetMarginCallLevel(),
         StopOut = src.GetStopOutLevel(),
         TotalCapital = src.GetTotalCapital(),
         FreeMargin = src.GetFreeMargin(),
         MarginAvailable = src.GetMarginAvailable(),
         UsedMargin = src.GetUsedMargin(),
         MarginInit = src.GetMarginInit(),
         PnL = src.GetPnl(),
         OpenPositionsCount = src.GetOpenPositionsCount(),
         MarginUsageLevel = src.GetMarginUsageLevel()
     });
 }