Пример #1
0
        public Task AccountHistory(string transactionId, string accountId, string clientId, decimal amount, decimal balance,
                                   decimal withdrawTransferLimit, AccountHistoryType type, string comment = null, string eventSourceId = null,
                                   string auditLog = null)
        {
            var record = new MarginTradingAccountHistory
            {
                Id                    = transactionId,
                AccountId             = accountId,
                ClientId              = clientId,
                Type                  = type,
                Amount                = amount,
                Balance               = balance,
                WithdrawTransferLimit = withdrawTransferLimit,
                Date                  = DateTime.UtcNow,
                Comment               = comment,
                OrderId               = type == AccountHistoryType.OrderClosed ? eventSourceId : null,
                AuditLog              = auditLog
            };

            return(TryProduceMessageAsync(_settings.RabbitMqQueues.AccountHistory.ExchangeName, record.ToBackendContract()));
        }
Пример #2
0
        public async Task <string> UpdateBalanceAsync(IMarginTradingAccount account, decimal amount, AccountHistoryType historyType,
                                                      string comment, string eventSourceId = null, bool changeTransferLimit = false, string auditLog = null)
        {
            if (historyType == AccountHistoryType.Deposit && changeTransferLimit)
            {
                CheckDepositLimits(account, amount);
            }

            if (changeTransferLimit)
            {
                CheckTransferLimits(account, amount);
            }

            var semaphore = GetSemaphore(account);

            await semaphore.WaitAsync();

            try
            {
                var updatedAccount =
                    await _repository.UpdateBalanceAsync(account.ClientId, account.Id, amount, changeTransferLimit);

                _acountBalanceChangedEventChannel.SendEvent(this, new AccountBalanceChangedEventArgs(updatedAccount));
                //todo: move to separate event consumers
                _accountsCacheService.UpdateBalance(updatedAccount);
                _clientNotifyService.NotifyAccountUpdated(updatedAccount);

                var transactionId = Guid.NewGuid().ToString("N");

                await _rabbitMqNotifyService.AccountHistory(
                    transactionId,
                    account.Id,
                    account.ClientId,
                    amount,
                    updatedAccount.Balance,
                    updatedAccount.WithdrawTransferLimit,
                    historyType,
                    comment,
                    eventSourceId,
                    auditLog);

                return(transactionId);
            }
            finally
            {
                semaphore.Release();
            }
        }