Example #1
0
        public void SaveFeeAndRetreiveByCurrencyPairNameTest_SavesAnObjectToDatabaseAndManipulatesIt_ChecksIfItIsUpdatedAsExpected()
        {
            Fee fee = new Fee("LTC/BTC", 4000, 500);

            _persistanceRepository.SaveOrUpdate(fee);

            List <Fee> retrievedFee = _feeRepository.GetFeeByCurrencyPair("LTC/BTC");

            Assert.IsNotNull(retrievedFee);

            Assert.AreEqual(fee.PercentageFee, retrievedFee[0].PercentageFee);
            Assert.AreEqual(fee.Amount, retrievedFee[0].Amount);
        }
Example #2
0
        public void GetFeeTest_TestsIfTheFeeIsasReturnedAsExpectedWhenFinalElementInTheTableIsReached_VerifiesThroughDatabaseQueries()
        {
            IFeeCalculationService      feeCalculationService      = (IFeeCalculationService)ContextRegistry.GetContext()["FeeCalculationService"];
            IFeeRepository              feeRepository              = (IFeeRepository)ContextRegistry.GetContext()["FeeRepository"];
            IFundsPersistenceRepository fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"];


            AccountId  accountId = new AccountId(1);
            List <Fee> allFees   = feeRepository.GetAllFees();

            Assert.IsNotNull(allFees);
            string currencyPair = allFees[0].CurrencyPair;
            Tuple <string, string> splittedCurrencyPair = CurrencySplitterService.SplitCurrencyPair(currencyPair);

            // As amount, the value is so large that it is always greater than any fee in the table
            Ledger ledger = new Ledger("1", DateTime.Now, LedgerType.Trade, new Currency(splittedCurrencyPair.Item2),
                                       2000000, 0, 0, 0, "1", "1", false, accountId);

            fundsPersistenceRepository.SaveOrUpdate(ledger);
            List <Fee> feeByCurrencyPair = feeRepository.GetFeeByCurrencyPair(currencyPair);

            Assert.IsNotNull(feeByCurrencyPair);
            decimal lastElementFee = (feeByCurrencyPair.Last().PercentageFee / 100) * (1000 * 2000);

            decimal feeFromService = feeCalculationService.GetFee(new Currency(splittedCurrencyPair.Item1),
                                                                  new Currency(splittedCurrencyPair.Item2),
                                                                  accountId, 1000, 2000);

            Assert.AreEqual(lastElementFee, feeFromService);
        }
Example #3
0
        public void GetFeeTest_TestsIfTheFeeIsasReturnedAsExpectedWhenMiddleElementsAreReached_VerifiesThroughDatabaseQueries()
        {
            IFeeCalculationService      feeCalculationService      = (IFeeCalculationService)ContextRegistry.GetContext()["FeeCalculationService"];
            IFeeRepository              feeRepository              = (IFeeRepository)ContextRegistry.GetContext()["FeeRepository"];
            IFundsPersistenceRepository fundsPersistenceRepository = (IFundsPersistenceRepository)ContextRegistry.GetContext()["FundsPersistenceRepository"];


            AccountId  accountId = new AccountId(1);
            List <Fee> allFees   = feeRepository.GetAllFees();

            Assert.IsNotNull(allFees);
            string currencyPair = allFees[0].CurrencyPair;
            Tuple <string, string> splittedCurrencyPair = CurrencySplitterService.SplitCurrencyPair(currencyPair);

            List <Fee> feeByCurrencyPair = feeRepository.GetFeeByCurrencyPair(currencyPair);

            Assert.IsNotNull(feeByCurrencyPair);

            if (feeByCurrencyPair.Count > 4)
            {
                decimal amount = feeByCurrencyPair[feeByCurrencyPair.Count - 2].Amount;
                Ledger  ledger = new Ledger("1", DateTime.Now, LedgerType.Trade, new Currency(splittedCurrencyPair.Item2),
                                            amount, 0, 0, 0, "1", "1", false, accountId);
                fundsPersistenceRepository.SaveOrUpdate(ledger);

                decimal lastElementFee = (feeByCurrencyPair[feeByCurrencyPair.Count - 2].PercentageFee / 100) * (10 * 20);

                decimal feeFromService = feeCalculationService.GetFee(new Currency(splittedCurrencyPair.Item1),
                                                                      new Currency(splittedCurrencyPair.Item2),
                                                                      accountId, 10, 20);

                Assert.AreEqual(lastElementFee, feeFromService);
            }
        }
        /// <summary>
        /// Get the Fee Instance from the database
        /// </summary>
        /// <param name="baseCurrency"></param>
        /// <param name="quoteCurrency"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        private decimal GetFeeInstance(Currency baseCurrency, Currency quoteCurrency, decimal amount)
        {
            string currencyPair = baseCurrency.Name + quoteCurrency.Name;

            List <Fee> feeList = _feeRepository.GetFeeByCurrencyPair(currencyPair);

            for (int i = 0; i < feeList.Count; i++)
            {
                // E.g., if amount == 500 && currentFee element = 1000
                if (feeList[i].Amount >= amount)
                {
                    Fee currentFee = _feeRepository.GetFeeByCurrencyAndAmount(currencyPair, feeList[i].Amount);
                    return(currentFee.PercentageFee);
                }
                // If this is not the last element in the list
                if ((feeList.Count - i) != 1)
                {
                    // E.g., if amount == 1100 && currentFee = 1000 && currentFee + 1 = 2000
                    if (feeList[i].Amount <amount && feeList[i + 1].Amount> amount)
                    {
                        Fee currentFee = _feeRepository.GetFeeByCurrencyAndAmount(currencyPair, feeList[i].Amount);
                        return(currentFee.PercentageFee);
                    }
                }
                // If this is the last element, that means the amount is greater than or equal to the last percentage element,
                // so we provide the last element
                else
                {
                    if (feeList[i].Amount <= amount)
                    {
                        Fee currentFee = _feeRepository.GetFeeByCurrencyAndAmount(currencyPair, feeList[i].Amount);
                        return(currentFee.PercentageFee);
                    }
                }
            }
            return(0);
        }