Exemple #1
0
        public void DepositArrivedStandaloneFailedTest_TestsIfTheOperationIsAbortedWhenAmountIshigherThanThresholdLimit_VerifiesThroughQueryingDatabaseIsntances()
        {
            // Tests the event handler DepositArrived by directly sending data
            IDepositApplicationService    depositApplicationService  = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"];
            IFundsPersistenceRepository   fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"];
            IBalanceRepository            balanceRepository          = (IBalanceRepository)ContextRegistry.GetContext()["BalanceRepository"];
            ILedgerRepository             ledgerRepository           = (ILedgerRepository)ContextRegistry.GetContext()["LedgerRepository"];
            IDepositRepository            depositRepository          = (IDepositRepository)ContextRegistry.GetContext()["DepositRepository"];
            IDepositLimitRepository       depositLimitRepository     = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"];
            StubTierLevelRetrievalService tierLevelRetrievalService  = (ITierLevelRetrievalService)ContextRegistry.GetContext()["TierLevelRetrievalService"] as StubTierLevelRetrievalService;

            Assert.IsNotNull(tierLevelRetrievalService);
            tierLevelRetrievalService.SetTierLevel(TierConstants.TierLevel1);
            AccountId      accountId      = new AccountId(123);
            Currency       currency       = new Currency("BTC", true);
            BitcoinAddress bitcoinAddress = new BitcoinAddress("bitcoinaddress1");
            TransactionId  transactionId  = new TransactionId("transactionid1");

            DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency(TierConstants.TierLevel1,
                                                                                              LimitsCurrency.Default.ToString());
            decimal amount   = depositLimit.DailyLimit + 0.001M;
            string  category = BitcoinConstants.ReceiveCategory;

            DepositAddress depositAddress = new DepositAddress(currency, bitcoinAddress, AddressStatus.New, DateTime.Now,
                                                               accountId);

            fundsPersistenceRepository.SaveOrUpdate(depositAddress);

            Balance balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);

            Assert.IsNull(balance);

            IList <Ledger> ledgers = ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);

            Assert.AreEqual(ledgers.Count, 0);

            List <Tuple <string, string, decimal, string> > transactionsList = new List <Tuple <string, string, decimal, string> >();

            transactionsList.Add(new Tuple <string, string, decimal, string>(bitcoinAddress.Value, transactionId.Value, amount, category));
            depositApplicationService.OnDepositArrival(currency.Name, transactionsList);

            Deposit deposit = depositRepository.GetDepositByTransactionId(transactionId);

            Assert.IsNotNull(deposit);
            Assert.AreEqual(amount, deposit.Amount);
            Assert.AreEqual(currency.Name, deposit.Currency.Name);
            Assert.AreEqual(accountId.Value, deposit.AccountId.Value);
            Assert.AreEqual(bitcoinAddress.Value, deposit.BitcoinAddress.Value);
            Assert.AreEqual(transactionId.Value, deposit.TransactionId.Value);
            Assert.AreEqual(TransactionStatus.Frozen, deposit.Status);

            // Confirm that the balance for the user has been frozen for this account
            balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);
            Assert.IsNotNull(balance);
            Assert.IsTrue(balance.IsFrozen);
        }
        /// <summary>
        /// Get ledgers for the given currency
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="currency"></param>
        /// <returns></returns>
        public IList <LedgerRepresentation> GetLedgersForCurrency(int accountId, string currency)
        {
            IList <LedgerRepresentation> ledgerRepresentations = new List <LedgerRepresentation>();
            IList <Ledger> ledgers = _ledgerRepository.GetLedgerByAccountIdAndCurrency(currency, new AccountId(accountId));

            foreach (var ledger in ledgers)
            {
                ledgerRepresentations.Add(new LedgerRepresentation(ledger.LedgerId, ledger.DateTime,
                                                                   ledger.LedgerType.ToString(), ledger.Currency.Name, ledger.Amount, ledger.AmountInUsd, ledger.Fee, ledger.Balance,
                                                                   ledger.TradeId, ledger.OrderId, ledger.WithdrawId, ledger.DepositId, ledger.AccountId.Value));
            }

            return(ledgerRepresentations);
        }
        /// <summary>
        /// Gets all the Deposit Ledger transactions
        /// </summary>
        /// <returns></returns>
        private IList <Ledger> GetDepositLedgers(Currency currency, AccountId accountId)
        {
            IList <Ledger> depositLedgers = new List <Ledger>();
            IList <Ledger> allLedgers     = _ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);

            foreach (var ledger in allLedgers)
            {
                if (ledger.LedgerType == LedgerType.Deposit)
                {
                    depositLedgers.Add(ledger);
                }
            }
            if (!depositLedgers.Any())
            {
                depositLedgers = null;
            }
            return(depositLedgers);
        }
        /// <summary>
        /// Gets the volume traded by a trader for a particular currency in the last 30 days in US Dollars
        /// </summary>
        /// <returns></returns>
        private decimal GetLast30DaysTradeVolume(string quoteCurrency, AccountId accountId)
        {
            decimal        tradesVolume = 0;
            IList <Ledger> allLedgers   = _ledgerRepository.GetLedgerByAccountIdAndCurrency(quoteCurrency, accountId);

            if (allLedgers.Any())
            {
                foreach (var ledger in allLedgers)
                {
                    if (ledger.LedgerType == LedgerType.Trade &&
                        ledger.DateTime >= DateTime.Now.AddDays(-30) &&
                        !ledger.IsBaseCurrencyInTrade)
                    {
                        tradesVolume += ledger.Amount;
                    }
                }
            }
            return(tradesVolume);
        }
Exemple #5
0
        public void DepositConfirmedTest_TestsIfTheOperationProceedsAsExpectedWhenADepositIsConfirmed_VerifiesThroughQueryingDatabaseIsntances()
        {
            IDepositApplicationService    depositApplicationService  = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"];
            IFundsPersistenceRepository   fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"];
            IBalanceRepository            balanceRepository          = (IBalanceRepository)ContextRegistry.GetContext()["BalanceRepository"];
            ILedgerRepository             ledgerRepository           = (ILedgerRepository)ContextRegistry.GetContext()["LedgerRepository"];
            IDepositRepository            depositRepository          = (IDepositRepository)ContextRegistry.GetContext()["DepositRepository"];
            IDepositAddressRepository     depositAddressRepository   = (IDepositAddressRepository)ContextRegistry.GetContext()["DepositAddressRepository"];
            StubTierLevelRetrievalService tierLevelRetrievalService  = (ITierLevelRetrievalService)ContextRegistry.GetContext()["TierLevelRetrievalService"] as StubTierLevelRetrievalService;

            if (tierLevelRetrievalService != null)
            {
                tierLevelRetrievalService.SetTierLevel(TierConstants.TierLevel1);
            }
            AccountId      accountId      = new AccountId(123);
            Currency       currency       = new Currency("BTC", true);
            BitcoinAddress bitcoinAddress = new BitcoinAddress("bitcoinaddress1");
            TransactionId  transactionId  = new TransactionId("transactionid1");
            decimal        amount         = 1.02m;
            string         category       = BitcoinConstants.ReceiveCategory;

            DepositAddress depositAddress = new DepositAddress(currency, bitcoinAddress, AddressStatus.New, DateTime.Now,
                                                               accountId);

            fundsPersistenceRepository.SaveOrUpdate(depositAddress);

            Balance balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);

            Assert.IsNull(balance);

            IList <Ledger> ledgers = ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);

            Assert.AreEqual(ledgers.Count, 0);

            List <Tuple <string, string, decimal, string> > transactionsList = new List <Tuple <string, string, decimal, string> >();

            transactionsList.Add(new Tuple <string, string, decimal, string>(bitcoinAddress.Value, transactionId.Value, amount, category));
            depositApplicationService.OnDepositArrival(currency.Name, transactionsList);

            Deposit deposit = depositRepository.GetDepositByTransactionId(transactionId);

            Assert.IsNotNull(deposit);
            Assert.AreEqual(deposit.Amount, amount);
            Assert.AreEqual(deposit.Currency.Name, currency.Name);
            Assert.IsTrue(deposit.Currency.IsCryptoCurrency);
            Assert.AreEqual(deposit.TransactionId.Value, transactionId.Value);
            Assert.AreEqual(deposit.BitcoinAddress.Value, bitcoinAddress.Value);
            Assert.AreEqual(TransactionStatus.Pending, deposit.Status);

            depositAddress = depositAddressRepository.GetDepositAddressByAddress(bitcoinAddress);
            Assert.IsNotNull(depositAddress);
            Assert.AreEqual(AddressStatus.Used, depositAddress.Status);

            balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);
            Assert.IsNull(balance);

            ledgers = ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);
            Assert.AreEqual(0, ledgers.Count);

            depositApplicationService.OnDepositConfirmed(transactionId.Value, 7);

            deposit = depositRepository.GetDepositByTransactionId(transactionId);
            Assert.IsNotNull(deposit);
            Assert.AreEqual(deposit.Status, TransactionStatus.Confirmed);

            balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);
            Assert.IsNotNull(balance);
            Assert.AreEqual(amount, balance.AvailableBalance);
            Assert.AreEqual(amount, balance.CurrentBalance);
            Assert.AreEqual(0, balance.PendingBalance);

            ledgers = ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);
            Assert.AreEqual(1, ledgers.Count);
            var ledger = ledgers.SingleOrDefault();

            Assert.IsNotNull(ledger);
            Assert.AreEqual(LedgerType.Deposit, ledger.LedgerType);
            Assert.AreEqual(accountId.Value, ledger.AccountId.Value);
            Assert.AreEqual(amount, ledger.Amount);
            Assert.AreEqual(deposit.DepositId, ledger.DepositId);
            Assert.AreEqual(0, ledger.Fee);
        }
Exemple #6
0
        public void DepositArrivedStandaloneTest_TestsIfTheOperationProceedsAsExpectedWHenANewDepositArrives_VerifiesThroughQueryingDatabaseIsntances()
        {
            // Tests the event handler DepositArrived by directly sending data
            IDepositApplicationService    depositApplicationService  = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"];
            IFundsPersistenceRepository   fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"];
            IBalanceRepository            balanceRepository          = (IBalanceRepository)ContextRegistry.GetContext()["BalanceRepository"];
            ILedgerRepository             ledgerRepository           = (ILedgerRepository)ContextRegistry.GetContext()["LedgerRepository"];
            IDepositRepository            depositRepository          = (IDepositRepository)ContextRegistry.GetContext()["DepositRepository"];
            IDepositAddressRepository     depositAddressRepository   = (IDepositAddressRepository)ContextRegistry.GetContext()["DepositAddressRepository"];
            IDepositLimitRepository       depositLimitRepository     = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"];
            StubTierLevelRetrievalService tierLevelRetrieval         = (StubTierLevelRetrievalService)ContextRegistry.GetContext()["TierLevelRetrievalService"];

            tierLevelRetrieval.SetTierLevel(TierConstants.TierLevel1);
            AccountId      accountId      = new AccountId(123);
            Currency       currency       = new Currency("BTC", true);
            BitcoinAddress bitcoinAddress = new BitcoinAddress("bitcoinaddress1");
            TransactionId  transactionId  = new TransactionId("transactionid1");
            DepositLimit   depositLimit   = depositLimitRepository.GetLimitByTierLevelAndCurrency(TierConstants.TierLevel1,
                                                                                                  LimitsCurrency.Default.ToString());
            decimal amount   = depositLimit.DailyLimit - 0.001M;
            string  category = BitcoinConstants.ReceiveCategory;

            DepositAddress depositAddress = new DepositAddress(currency, bitcoinAddress, AddressStatus.New, DateTime.Now,
                                                               accountId);

            fundsPersistenceRepository.SaveOrUpdate(depositAddress);

            Balance balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);

            Assert.IsNull(balance);

            IList <Ledger> ledgers = ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);

            Assert.AreEqual(ledgers.Count, 0);

            List <Tuple <string, string, decimal, string> > transactionsList = new List <Tuple <string, string, decimal, string> >();

            transactionsList.Add(new Tuple <string, string, decimal, string>(bitcoinAddress.Value, transactionId.Value, amount, category));
            depositApplicationService.OnDepositArrival(currency.Name, transactionsList);

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);

            manualResetEvent.WaitOne(2000);
            Deposit deposit = depositRepository.GetDepositByTransactionId(transactionId);

            Assert.IsNotNull(deposit);
            Assert.AreEqual(deposit.Amount, amount);
            Assert.AreEqual(deposit.Currency.Name, currency.Name);
            Assert.IsTrue(deposit.Currency.IsCryptoCurrency);
            Assert.AreEqual(deposit.TransactionId.Value, transactionId.Value);
            Assert.AreEqual(deposit.BitcoinAddress.Value, bitcoinAddress.Value);
            Assert.AreEqual(deposit.Status, TransactionStatus.Pending);

            depositAddress = depositAddressRepository.GetDepositAddressByAddress(bitcoinAddress);
            Assert.IsNotNull(depositAddress);
            Assert.AreEqual(depositAddress.Status, AddressStatus.Used);

            balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId);
            Assert.IsNull(balance);

            ledgers = ledgerRepository.GetLedgerByAccountIdAndCurrency(currency.Name, accountId);
            Assert.AreEqual(ledgers.Count, 0);
        }