public async Task ExecuteAsync(string assetId, decimal amount, string comment, bool allowChangeBalance,
                                       string userId)
        {
            var balanceOperation = new BalanceOperation
            {
                Time    = DateTime.UtcNow,
                AssetId = assetId,
                Type    = "Settlement",
                Amount  = amount,
                Comment = comment,
                UserId  = userId
            };

            if (allowChangeBalance)
            {
                string walletId = await _settingsService.GetWalletIdAsync();

                if (amount > 0)
                {
                    await _lykkeExchangeService.CashInAsync(walletId, assetId, Math.Abs(amount), userId, comment);
                }
                else
                {
                    await _lykkeExchangeService.CashOutAsync(walletId, assetId, Math.Abs(amount), userId, comment);
                }
            }

            await _balanceOperationService.AddAsync(balanceOperation);

            _log.InfoWithDetails("Settlement was executed", balanceOperation);
        }
        public async Task UpdateBalanceAsync(string assetId, decimal amount, string userId, string comment)
        {
            if (amount > 0)
            {
                await _lykkeExchangeService.CashInAsync(
                    _walletId,
                    assetId,
                    amount,
                    userId,
                    comment);
            }
            else if (amount < 0)
            {
                await _lykkeExchangeService.CashOutAsync(
                    _walletId,
                    assetId,
                    Math.Abs(amount),
                    userId,
                    comment);
            }

            await _primaryMarketRepository.CreateAsync(new PrimaryMarketHistoryItem
            {
                AssetId  = assetId,
                Amount   = amount,
                DateTime = DateTime.UtcNow,
                UserId   = userId,
                Comment  = comment
            });
        }
        public async Task UpdateAsync(string assetId, decimal amount, string comment, string userId)
        {
            Credit credit = await GetByAssetIdAsync(assetId);

            credit.Add(amount);

            string walletId = await _settingsService.GetWalletIdAsync();

            if (amount > 0)
            {
                await _lykkeExchangeService.CashInAsync(walletId, assetId, Math.Abs(amount), userId, comment);
            }
            else
            {
                await _lykkeExchangeService.CashOutAsync(walletId, assetId, Math.Abs(amount), userId, comment);
            }

            await _creditRepository.InsertOrReplaceAsync(credit);

            _cache.Set(credit);

            var balanceOperation = new BalanceOperation
            {
                Time    = DateTime.UtcNow,
                AssetId = assetId,
                Type    = "Credit",
                Amount  = amount,
                Comment = comment,
                UserId  = userId
            };

            await _balanceOperationService.AddAsync(balanceOperation);

            _log.InfoWithDetails("Credit was updated", balanceOperation);
        }
        private async Task <string> CashInAsync(string asset, string walletId, decimal amount, string comment)
        {
            string assetId = await GetAssetId(asset);

            string transactionId = await _lykkeExchangeService.CashInAsync(walletId, assetId, amount, "empty", comment);

            _log.InfoWithDetails("Cash in", new
            {
                Comment       = comment,
                Asset         = assetId,
                Amount        = amount,
                WalletId      = walletId,
                TransactionId = transactionId
            });

            return(transactionId);
        }
Exemple #5
0
        public async Task UpdateAmountAsync(string assetId, BalanceOperationType balanceOperationType, decimal amount,
                                            string comment, string userId)
        {
            await _semaphore.WaitAsync();

            try
            {
                Token previousToken = await GetAsync(assetId);

                Token currentToken = previousToken.Copy();

                string walletId = _settingsService.GetWalletId();

                string transactionId;

                switch (balanceOperationType)
                {
                case BalanceOperationType.CashIn:
                    currentToken.IncreaseAmount(amount);
                    transactionId = await _lykkeExchangeService
                                    .CashInAsync(walletId, assetId, amount, userId, comment);

                    break;

                case BalanceOperationType.CashOut:
                    currentToken.DecreaseAmount(amount);
                    try
                    {
                        transactionId = await _lykkeExchangeService
                                        .CashOutAsync(walletId, assetId, amount, userId, comment);
                    }
                    catch (NotEnoughFundsException)
                    {
                        throw new InvalidOperationException("No enough funds");
                    }

                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(balanceOperationType), (int)balanceOperationType,
                                                           typeof(BalanceOperationType));
                }

                await _tokenRepository.SaveAsync(currentToken);

                _cache.Set(currentToken);

                var balanceOperation = new BalanceOperation
                {
                    Timestamp     = DateTime.UtcNow,
                    AssetId       = assetId,
                    Type          = balanceOperationType,
                    Amount        = amount,
                    IsCredit      = false,
                    Comment       = comment,
                    UserId        = userId,
                    TransactionId = transactionId
                };

                await _balanceOperationService.AddAsync(balanceOperation);

                _log.InfoWithDetails("Token amount updated", new
                {
                    PreviuosToken    = previousToken,
                    CurrentToken     = currentToken,
                    BalanceOperation = balanceOperation
                });
            }
            catch (Exception exception)
            {
                _log.WarningWithDetails("An error occurred while updating token", exception, new
                {
                    AssetId = assetId,
                    Type    = balanceOperationType,
                    Amount  = amount,
                    Comment = comment,
                    UserId  = userId
                });

                throw;
            }
            finally
            {
                _semaphore.Release();
            }
        }
        public async Task UpdateAsync(string assetId, BalanceOperationType balanceOperationType, decimal amount,
                                      string comment, string userId)
        {
            try
            {
                string walletId = _settingsService.GetWalletId();

                string transactionId;

                switch (balanceOperationType)
                {
                case BalanceOperationType.CashIn:
                    transactionId =
                        await _lykkeExchangeService.CashInAsync(walletId, assetId, amount, userId, comment);

                    break;

                case BalanceOperationType.CashOut:
                    try
                    {
                        transactionId =
                            await _lykkeExchangeService.CashOutAsync(walletId, assetId, amount, userId, comment);
                    }
                    catch (NotEnoughFundsException)
                    {
                        throw new InvalidOperationException("No enough funds");
                    }

                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(balanceOperationType), (int)balanceOperationType,
                                                           typeof(BalanceOperationType));
                }

                var balanceOperation = new BalanceOperation
                {
                    Timestamp     = DateTime.UtcNow,
                    AssetId       = assetId,
                    Type          = balanceOperationType,
                    IsCredit      = false,
                    Amount        = amount,
                    Comment       = comment,
                    UserId        = userId,
                    TransactionId = transactionId
                };

                await _balanceOperationService.AddAsync(balanceOperation);

                _log.InfoWithDetails("Balance changed", balanceOperation);
            }
            catch (Exception exception)
            {
                _log.WarningWithDetails("An error occurred while changing balance", exception, new
                {
                    AssetId = assetId,
                    Type    = balanceOperationType,
                    Amount  = amount,
                    Comment = comment,
                    UserId  = userId
                });

                throw;
            }
        }
Exemple #7
0
        public async Task UpdateAsync(BalanceOperationType balanceOperationType, decimal amount, string comment,
                                      string userId)
        {
            await _semaphore.WaitAsync();

            try
            {
                Funding funding = (await GetAsync()).Copy();

                string walletId = _settingsService.GetWalletId();

                string transactionId;

                switch (balanceOperationType)
                {
                case BalanceOperationType.CashIn:
                    funding.Add(amount);
                    transactionId =
                        await _lykkeExchangeService.CashInAsync(walletId, AssetId, amount, userId, comment);

                    break;

                case BalanceOperationType.CashOut:
                    funding.Subtract(amount);
                    try
                    {
                        transactionId =
                            await _lykkeExchangeService.CashOutAsync(walletId, AssetId, amount, userId, comment);
                    }
                    catch (NotEnoughFundsException)
                    {
                        throw new InvalidOperationException("No enough funds");
                    }

                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(balanceOperationType), (int)balanceOperationType,
                                                           typeof(BalanceOperationType));
                }

                await _fundingRepository.InsertOrReplaceAsync(funding);

                _cache.Set(funding);

                var balanceOperation = new BalanceOperation
                {
                    Timestamp     = DateTime.UtcNow,
                    AssetId       = AssetId,
                    Type          = balanceOperationType,
                    IsCredit      = true,
                    Amount        = amount,
                    Comment       = comment,
                    UserId        = userId,
                    TransactionId = transactionId
                };

                await _balanceOperationService.AddAsync(balanceOperation);

                _log.InfoWithDetails("Funding amount updated", balanceOperation);
            }
            catch (Exception exception)
            {
                _log.WarningWithDetails("An error occurred while updating funding", exception, new
                {
                    AssetId,
                    Type    = balanceOperationType,
                    Amount  = amount,
                    Comment = comment,
                    UserId  = userId
                });

                throw;
            }
            finally
            {
                _semaphore.Release();
            }
        }