Exemplo n.º 1
0
        public async Task TestAccountBalanceUpdate_Success(string accountId, decimal changeAmount,
                                                           AccountBalanceChangeReasonTypeContract balanceChangeReasonType)
        {
            var account = Accounts.Single(x => x.Id == accountId);
            var time    = DateService.Now().AddMinutes(1);

            var accountsProjection = AssertEnv(accountId: accountId);

            var updatedContract = new AccountContract(accountId, account.ClientId, account.TradingConditionId,
                                                      account.BaseAssetId, changeAmount, account.WithdrawTransferLimit, account.LegalEntity,
                                                      account.IsDisabled, account.ModificationTimestamp, account.IsWithdrawalDisabled, false);

            await accountsProjection.Handle(new AccountChangedEvent(time, "test",
                                                                    updatedContract, AccountChangedEventTypeContract.BalanceUpdated,
                                                                    new AccountBalanceChangeContract("test", time, accountId, account.ClientId, changeAmount,
                                                                                                     account.Balance + changeAmount, account.WithdrawTransferLimit, "test", balanceChangeReasonType,
                                                                                                     "test", "Default", null, null, time)));

            var resultedAccount = _accountsCacheService.Get(accountId);

            Assert.AreEqual(account.Balance + changeAmount, resultedAccount.Balance);

            if (balanceChangeReasonType == AccountBalanceChangeReasonTypeContract.Withdraw)
            {
                _accountUpdateServiceMock.Verify(s => s.UnfreezeWithdrawalMargin(accountId, "test"), Times.Once);
            }

            if (balanceChangeReasonType == AccountBalanceChangeReasonTypeContract.UnrealizedDailyPnL)
            {
                _fakePosition.Verify(s => s.ChargePnL("test", changeAmount), Times.Once);
            }

            _accountBalanceChangedEventChannelMock.Verify(s => s.SendEvent(It.IsAny <object>(),
                                                                           It.IsAny <AccountBalanceChangedEventArgs>()), Times.Once);
        }
 public AccountBalanceChangeLightContract(decimal changeAmount, decimal balance, AccountBalanceChangeReasonTypeContract reasonType, DateTime changeTimestamp)
 {
     ChangeAmount    = changeAmount;
     Balance         = balance;
     ReasonType      = reasonType;
     ChangeTimestamp = changeTimestamp;
 }
Exemplo n.º 3
0
        public async Task TestAccountBalanceUpdate_Success(string accountId, decimal changeAmount,
                                                           AccountBalanceChangeReasonTypeContract balanceChangeReasonType, string auditLog)
        {
            var account = Accounts.Single(x => x.Id == accountId);
            var time    = DateService.Now().AddMinutes(1);

            var accountsProjection = AssertEnv(accountId: accountId);

            var updatedContract = new AccountContract()
            {
                Id                    = accountId,
                ClientId              = account.ClientId,
                TradingConditionId    = account.TradingConditionId,
                BaseAssetId           = account.BaseAssetId,
                Balance               = account.Balance,
                WithdrawTransferLimit = account.WithdrawTransferLimit,
                LegalEntity           = account.LegalEntity,
                IsDisabled            = account.IsDisabled,
                ModificationTimestamp = account.ModificationTimestamp,
                IsWithdrawalDisabled  = account.IsWithdrawalDisabled,
                IsDeleted             = false,
                AdditionalInfo        = "{}"
            };

            await accountsProjection.Handle(new AccountChangedEvent(time, "test",
                                                                    updatedContract, AccountChangedEventTypeContract.BalanceUpdated,
                                                                    new AccountBalanceChangeContract("test", time, accountId, account.ClientId, changeAmount,
                                                                                                     account.Balance + changeAmount, account.WithdrawTransferLimit, "test", balanceChangeReasonType,
                                                                                                     "test", "Default", auditLog, null, time)));

            var resultedAccount = _accountsCacheService.Get(accountId);

            Assert.AreEqual(account.Balance + changeAmount, resultedAccount.Balance);

            if (balanceChangeReasonType == AccountBalanceChangeReasonTypeContract.Withdraw)
            {
                _accountUpdateServiceMock.Verify(s => s.UnfreezeWithdrawalMargin(accountId, "test"), Times.Once);
            }

            if (balanceChangeReasonType == AccountBalanceChangeReasonTypeContract.UnrealizedDailyPnL)
            {
                var metadata = auditLog?.DeserializeJson <UnrealizedPnlMetadataContract>();

                if (metadata == null || metadata.RawTotalPnl == 0)
                {
                    _fakePosition.Verify(s => s.ChargePnL("test", changeAmount), Times.Once);
                }
                else
                {
                    _fakePosition.Verify(s => s.SetChargedPnL("test", metadata.RawTotalPnl), Times.Once);
                }
            }

            _accountBalanceChangedEventChannelMock.Verify(s => s.SendEvent(It.IsAny <object>(),
                                                                           It.IsAny <AccountBalanceChangedEventArgs>()), Times.Once);
        }
 /// <inheritdoc />
 public ChangeBalanceCommand([NotNull] string operationId, [CanBeNull] string clientId, [NotNull] string accountId,
                             decimal amount, AccountBalanceChangeReasonTypeContract reasonType, [NotNull] string reason,
                             [CanBeNull] string auditLog, [CanBeNull] string eventSourceId, [CanBeNull] string assetPairId,
                             DateTime tradingDay)
 {
     OperationId   = operationId ?? throw new ArgumentNullException(nameof(operationId));
     ClientId      = clientId;
     AccountId     = accountId ?? throw new ArgumentNullException(nameof(accountId));
     Amount        = amount;
     ReasonType    = reasonType;
     Reason        = reason;
     AuditLog      = auditLog;
     EventSourceId = eventSourceId;
     AssetPairId   = assetPairId;
     TradingDay    = tradingDay == default ? DateTime.UtcNow : tradingDay;
 }
 public AccountBalanceChangeContract([NotNull] string id, DateTime changeTimestamp, [NotNull] string accountId,
                                     [NotNull] string clientId, decimal changeAmount, decimal balance, decimal withdrawTransferLimit,
                                     [NotNull] string comment, AccountBalanceChangeReasonTypeContract reasonType, [NotNull] string eventSourceId,
                                     [NotNull] string legalEntity, string auditLog, string instrument, DateTime tradingDate)
 {
     Id = id ?? throw new ArgumentNullException(nameof(id));
     ChangeTimestamp       = changeTimestamp;
     AccountId             = accountId ?? throw new ArgumentNullException(nameof(accountId));
     ClientId              = clientId ?? throw new ArgumentNullException(nameof(clientId));
     ChangeAmount          = changeAmount;
     Balance               = balance;
     WithdrawTransferLimit = withdrawTransferLimit;
     Comment               = comment;
     ReasonType            = reasonType;
     EventSourceId         = eventSourceId;
     LegalEntity           = legalEntity;
     AuditLog              = auditLog;
     Instrument            = instrument;
     TradingDate           = tradingDate == DateTime.MinValue ? changeTimestamp.Date : tradingDate.Date;
 }
Exemplo n.º 6
0
        public static decimal GetTotalByType(this IEnumerable <AccountBalanceChangeLightContract> items, AccountBalanceChangeReasonTypeContract type)
        {
            if (items == null || !items.Any())
            {
                return(0);
            }

            return(items
                   .Where(x => x.ReasonType == type)
                   .Sum(x => x.ChangeAmount));
        }