public void AssignDepositLimitsTest_ChecksThatDepositLimitsAreAssignedProperlyWhenLevel1IsNotVerifiedAndBalanceIsAlreadyPresent_VerifiesThroughReturnedValues() { IDepositApplicationService depositApplicationService = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"]; IDepositLimitRepository depositLimitRepository = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"]; StubTierLevelRetrievalService tierLevelRetrieval = (StubTierLevelRetrievalService)ContextRegistry.GetContext()["TierLevelRetrievalService"]; tierLevelRetrieval.SetTierLevel("Tier 0"); DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency("Tier 0", LimitsCurrency.Default.ToString()); Assert.IsNotNull(depositLimit); Assert.AreEqual(0, depositLimit.DailyLimit); Assert.AreEqual(0, depositLimit.MonthlyLimit); AccountId accountId = new AccountId(123); Currency currency = new Currency("BTC", true); DepositLimitThresholdsRepresentation depositLimitThresholds = depositApplicationService.GetThresholdLimits(accountId.Value, currency.Name); Assert.IsNotNull(depositLimitThresholds); Assert.AreEqual(depositLimit.DailyLimit, depositLimitThresholds.DailyLimit); Assert.AreEqual(depositLimit.MonthlyLimit, depositLimitThresholds.MonthlyLimit); Assert.AreEqual(0, depositLimitThresholds.DailyLimitUsed); Assert.AreEqual(0, depositLimitThresholds.MonthlyLimitUsed); Assert.AreEqual(0, depositLimitThresholds.CurrentBalance); Assert.AreEqual(0, depositLimitThresholds.MaximumDeposit); }
public void DepositArrivedAndConfirmedTest_ChecksThatANewDepositInstanceIsCreatedAndConfirmedAsExpected_VerifiesThroughDatabaseQuery() { IDepositApplicationService depositApplicationService = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"]; IFundsPersistenceRepository fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"]; IBalanceRepository balanceRepository = (IBalanceRepository)ContextRegistry.GetContext()["BalanceRepository"]; IDepositRepository depositRepository = (IDepositRepository)ContextRegistry.GetContext()["DepositRepository"]; IDepositLimitRepository depositLimitRepository = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"]; AccountId accountId = new AccountId(123); Currency currency = new Currency("BTC", true); BitcoinAddress bitcoinAddress = new BitcoinAddress("bitcoinaddress1"); TransactionId transactionId = new TransactionId("transactionid1"); string category = BitcoinConstants.ReceiveCategory; DepositAddress depositAddress = new DepositAddress(currency, bitcoinAddress, AddressStatus.New, DateTime.Now, accountId); fundsPersistenceRepository.SaveOrUpdate(depositAddress); DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency("Tier 1", "Default"); Assert.IsNotNull(depositLimit, "DepositLimit used initially to compare later"); List <Tuple <string, string, decimal, string> > transactionsList = new List <Tuple <string, string, decimal, string> >(); // Provide the amount which is greater than the daily limit transactionsList.Add(new Tuple <string, string, decimal, string>(bitcoinAddress.Value, transactionId.Value, depositLimit.DailyLimit - 0.01M, category)); depositApplicationService.OnDepositArrival(currency.Name, transactionsList); // Deposit Transaction first arrival Deposit deposit = depositRepository.GetDepositByTransactionId(transactionId); Assert.IsNotNull(deposit); Assert.AreEqual(depositLimit.DailyLimit - 0.01M, deposit.Amount); Assert.AreEqual(transactionId.Value, deposit.TransactionId.Value); Assert.AreEqual(accountId.Value, deposit.AccountId.Value); Assert.AreEqual(bitcoinAddress.Value, deposit.BitcoinAddress.Value); Assert.AreEqual(TransactionStatus.Pending, deposit.Status); Balance balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId); Assert.IsNull(balance); // Deposit Confirmation depositApplicationService.OnDepositConfirmed(transactionId.Value, 7); deposit = depositRepository.GetDepositByTransactionId(transactionId); Assert.IsNotNull(deposit); Assert.AreEqual(depositLimit.DailyLimit - 0.01M, deposit.Amount); Assert.AreEqual(transactionId.Value, deposit.TransactionId.Value); Assert.AreEqual(accountId.Value, deposit.AccountId.Value); Assert.AreEqual(bitcoinAddress.Value, deposit.BitcoinAddress.Value); Assert.AreEqual(TransactionStatus.Confirmed, deposit.Status); // Balance instance now will have been created balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId); Assert.IsNotNull(balance); Assert.AreEqual(depositLimit.DailyLimit - 0.01M, balance.AvailableBalance); Assert.AreEqual(depositLimit.DailyLimit - 0.01M, balance.CurrentBalance); Assert.AreEqual(0, balance.PendingBalance); Assert.IsFalse(balance.IsFrozen); }
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> /// Evaluate the limits for Deposit /// </summary> /// <param name="tierLevel"></param> /// <param name="amount"></param> /// <param name="depositLedgers"></param> /// <param name="baseCurrency"> </param> /// <returns></returns> public bool EvaluateDepositLimits(string baseCurrency, string tierLevel, decimal amount, IList <Ledger> depositLedgers) { // If the admin has specified the limits to be calculated as the currency itself if (_adminDefinedLimitsCurrency == LimitsCurrency.Default.ToString()) { // Get the Current Deposit limits for this user DepositLimit depositLimit = _depositLimitRepository.GetLimitByTierLevelAndCurrency(tierLevel, _adminDefinedLimitsCurrency); if (depositLimit != null) { // Check if the current Deposit transaction is within the Deposit limits return(_depositLimitEvaluationService.EvaluateDepositLimit(amount, depositLedgers, depositLimit)); } throw new InvalidOperationException(string.Format("No Deposit Limit found for Tier: Tier Level = {0} & " + "CurrencyType = {1}", tierLevel, _adminDefinedLimitsCurrency)); } // If the admin has specified that limtis must be calculated in a FIAT currency else { // Get the Current Deposit limits for this user which will be in a FIAT currency DepositLimit depositLimit = _depositLimitRepository.GetLimitByTierLevelAndCurrency(tierLevel, _adminDefinedLimitsCurrency); if (depositLimit != null) { // Convert the currency to FIAT decimal amountInFiat = ConvertCurrencyToFiat(baseCurrency, amount); // Return reponse that if the user is allowed to deposit the specified limit of not // NOTE: If there is no best bid and best ask, then the limits will be speficied in the defauly crypto currency itself return(_depositLimitEvaluationService.EvaluateDepositLimit(amountInFiat, depositLedgers, depositLimit, _bestBidBestAsk.Item1, _bestBidBestAsk.Item2)); } throw new InvalidOperationException(string.Format("No Deposit Limit found for Tier: Tier Level = {0} & " + "CurrencyType = {1}", tierLevel, _adminDefinedLimitsCurrency)); } }
public void AssignDepositLimitsTest_ChecksThatDepositLimitsAreNotAssignedWhenNoDepositLimitIsFound_VerifiesThroughReturnedValues() { IDepositApplicationService depositApplicationService = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"]; IDepositLimitRepository depositLimitRepository = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"]; StubTierLevelRetrievalService tierLevelRetrieval = (StubTierLevelRetrievalService)ContextRegistry.GetContext()["TierLevelRetrievalService"]; // Specify invalid tier level tierLevelRetrieval.SetTierLevel("Tier 6"); DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency("Tier 0", LimitsCurrency.Default.ToString()); Assert.IsNotNull(depositLimit); Assert.AreEqual(0, depositLimit.DailyLimit); Assert.AreEqual(0, depositLimit.MonthlyLimit); AccountId accountId = new AccountId(123); Currency currency = new Currency("BTC", true); // Exception occurs here because there is no such level as Tier 6 depositApplicationService.GetThresholdLimits(accountId.Value, currency.Name); }
public void DepositArrivedFailTest_VerifiesThatNewDepositWithAMountGreaterThanLimitsIsSuspendedAndAccountBalanceFrozen_VerifiesThroughDatabaseQuery() { IDepositApplicationService depositApplicationService = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"]; IFundsPersistenceRepository fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"]; IBalanceRepository balanceRepository = (IBalanceRepository)ContextRegistry.GetContext()["BalanceRepository"]; IDepositRepository depositRepository = (IDepositRepository)ContextRegistry.GetContext()["DepositRepository"]; IDepositLimitRepository depositLimitRepository = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"]; AccountId accountId = new AccountId(123); Currency currency = new Currency("BTC", true); BitcoinAddress bitcoinAddress = new BitcoinAddress("bitcoinaddress1"); TransactionId transactionId = new TransactionId("transactionid1"); string category = BitcoinConstants.ReceiveCategory; DepositAddress depositAddress = new DepositAddress(currency, bitcoinAddress, AddressStatus.New, DateTime.Now, accountId); fundsPersistenceRepository.SaveOrUpdate(depositAddress); DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency("Tier 1", "Default"); Assert.IsNotNull(depositLimit, "DepositLimit used initially to compare later"); List <Tuple <string, string, decimal, string> > transactionsList = new List <Tuple <string, string, decimal, string> >(); // Provide the amount which is greater than the daily limit transactionsList.Add(new Tuple <string, string, decimal, string>(bitcoinAddress.Value, transactionId.Value, depositLimit.DailyLimit + 0.001M, category)); depositApplicationService.OnDepositArrival(currency.Name, transactionsList); Deposit deposit = depositRepository.GetDepositByTransactionId(transactionId); Assert.IsNotNull(deposit); Assert.AreEqual(TransactionStatus.Frozen, deposit.Status); Balance balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId); Assert.IsNotNull(balance); Assert.IsTrue(balance.IsFrozen); }
public void AssignDepositLimits2Test_ChecksThatDepositLimitsAreAssignedProperlyWhenLevel1IsVerifiedAndBalanceIsAlreadyPresent_VerifiesThroughReturnedValues() { IDepositApplicationService depositApplicationService = (IDepositApplicationService)ContextRegistry.GetContext()["DepositApplicationService"]; IDepositLimitRepository depositLimitRepository = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"]; IFundsPersistenceRepository fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"]; DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency("Tier 1", LimitsCurrency.Default.ToString()); AccountId accountId = new AccountId(123); Currency currency = new Currency("BTC", true); decimal balanceAmount = 100; Balance balance = new Balance(currency, accountId, balanceAmount, balanceAmount); fundsPersistenceRepository.SaveOrUpdate(balance); DepositLimitThresholdsRepresentation depositLimitThresholds = depositApplicationService.GetThresholdLimits(accountId.Value, currency.Name); Assert.IsNotNull(depositLimitThresholds); Assert.AreEqual(depositLimit.DailyLimit, depositLimitThresholds.DailyLimit); Assert.AreEqual(depositLimit.MonthlyLimit, depositLimitThresholds.MonthlyLimit); Assert.AreEqual(0, depositLimitThresholds.DailyLimitUsed); Assert.AreEqual(0, depositLimitThresholds.MonthlyLimitUsed); Assert.AreEqual(balanceAmount, depositLimitThresholds.CurrentBalance); Assert.AreEqual(depositLimit.DailyLimit, depositLimitThresholds.MaximumDeposit); }
public void DepositArrivedAndConfirmedEventTest_TestsThatDepositArrivedEventIsHandledAsExpected_VerifiesThroughRaisedEvent() { // Withdraw is submitted and events are actually raised from the CoinClientService // Over the limit deposit so account and deposit are frozen IFundsPersistenceRepository fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"]; IBalanceRepository balanceRepository = (IBalanceRepository)ContextRegistry.GetContext()["BalanceRepository"]; IDepositRepository depositRepository = (IDepositRepository)ContextRegistry.GetContext()["DepositRepository"]; IWithdrawRepository withdrawRepository = (IWithdrawRepository)ContextRegistry.GetContext()["WithdrawRepository"]; IDepositLimitRepository depositLimitRepository = (IDepositLimitRepository)ContextRegistry.GetContext()["DepositLimitRepository"]; IWithdrawApplicationService withdrawApplicationService = (IWithdrawApplicationService)ContextRegistry.GetContext()["WithdrawApplicationService"]; AccountId accountId = new AccountId(123); Currency currency = new Currency("BTC", true); BitcoinAddress bitcoinAddress = new BitcoinAddress("bitcoinaddress1"); TransactionId transactionId = new TransactionId("transactionid1"); DepositAddress depositAddress = new DepositAddress(currency, bitcoinAddress, AddressStatus.New, DateTime.Now, accountId); fundsPersistenceRepository.SaveOrUpdate(depositAddress); DepositLimit depositLimit = depositLimitRepository.GetLimitByTierLevelAndCurrency("Tier 1", "Default"); Assert.IsNotNull(depositLimit, "DepositLimit used initially to compare later"); decimal amount = depositLimit.DailyLimit - 0.001M; ManualResetEvent manualResetEvent = new ManualResetEvent(false); Balance balance = new Balance(currency, accountId, 10, 10); fundsPersistenceRepository.SaveOrUpdate(balance); CommitWithdrawResponse commitWithdrawResponse = withdrawApplicationService.CommitWithdrawal(new CommitWithdrawCommand( accountId.Value, currency.Name, currency.IsCryptoCurrency, bitcoinAddress.Value, amount)); Assert.IsNotNull(commitWithdrawResponse); Assert.IsTrue(commitWithdrawResponse.CommitSuccessful); // Get the interval after which the withdraw will be submitted. Once submitted to the StubClientService, the events // of DepositArrived will be raised and handled in Deposit APplicationService, which will create a new Deposit // instance and save in database int withdrawInterval = Convert.ToInt16(ConfigurationManager.AppSettings.Get("WithdrawSubmitInterval")); manualResetEvent.WaitOne(withdrawInterval + 2000); Withdraw withdraw = withdrawRepository.GetWithdrawByWithdrawId(commitWithdrawResponse.WithdrawId); Assert.IsNotNull(withdraw); Deposit deposit = depositRepository.GetDepositByTransactionId(transactionId); Assert.IsNotNull(deposit); Assert.AreEqual(amount - withdraw.Fee, deposit.Amount); Assert.AreEqual(withdraw.TransactionId.Value, deposit.TransactionId.Value); Assert.AreEqual(accountId.Value, deposit.AccountId.Value); Assert.AreEqual(bitcoinAddress.Value, deposit.BitcoinAddress.Value); Assert.AreEqual(TransactionStatus.Confirmed, deposit.Status); balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId); Assert.IsNotNull(balance); // Balance instance now will have been created balance = balanceRepository.GetBalanceByCurrencyAndAccountId(currency, accountId); Assert.IsNotNull(balance); Assert.AreEqual(0, balance.PendingBalance); Assert.IsFalse(balance.IsFrozen); }
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); }