Exemple #1
0
        public void NotEnoughtBtcExceptionAreSwallowedCorrectly()
        {
            ArbitrationRun         testRun      = null;
            ArbitrationOpportunity opportunity1 = null;
            ArbitrationOpportunity opportunity2 = null;
            ArbitrationOpportunity opportunity3 = null;

            try
            {
                //Set up a test run
                testRun = TestsHelper.CreateArbitrationRun();

                //Set up the buy and sell exchanges.
                Bitstamp            bitstamp     = new Bitstamp();
                Kraken              kraken       = new Kraken();
                List <BaseExchange> exchangeList = new List <BaseExchange>();
                exchangeList.Add(bitstamp);
                exchangeList.Add(kraken);

                bitstamp.AvailableBtc  = 0.2m;
                bitstamp.AvailableFiat = 100m;
                kraken.AvailableBtc    = 0.2m;
                kraken.AvailableFiat   = 30m;


                //Initial trad; bitstamp will need to transfer 0.1 btc to kraken for this. It will be found in the rollup later.
                //Bitstamp now has 0.4 btc.
                opportunity1 = TestsHelper.CreateArbitrationOpportunity(bitstamp, kraken, testRun.Id.Value, 0.1m);

                //Made up numbers; they don't really matter for this test
                opportunity1.TotalBuyCost  = 10m;
                opportunity1.TotalSellCost = 10m;

                bitstamp.SimulatedBuy(opportunity1.BuyAmount, opportunity1.TotalBuyCost);
                kraken.SimulatedSell(opportunity1.BuyAmount, opportunity1.TotalSellCost);
                Assert.IsTrue(bitstamp.AvailableBtc == 0.3m);

                //Now a couple a trade where btc is sold off at Bitstamp; enough to make the rollup transfer fail
                //Bitstamp now has 0.1
                opportunity2 = TestsHelper.CreateArbitrationOpportunity(kraken, bitstamp, testRun.Id.Value, 0.3m);

                //Made up numbers; they don't really matter for this test
                opportunity2.TotalBuyCost  = 30m;
                opportunity2.TotalSellCost = 30m;

                kraken.SimulatedBuy(opportunity2.BuyAmount, opportunity2.TotalBuyCost);
                bitstamp.SimulatedSell(opportunity2.BuyAmount, opportunity2.TotalSellCost);
                Assert.IsTrue(bitstamp.AvailableBtc == 0.0m);

                //Now assume this trigger a transfer, so lets say 0.5btc is in transfer to bitstamp from kraken
                bitstamp.BTCInTransfer = 0.5m;

                //Now another buy at bitstamp; this will trigger a transfer rollup from bitstamp to kraken
                opportunity3 = TestsHelper.CreateArbitrationOpportunity(bitstamp, kraken, testRun.Id.Value, 0.15m);

                //Made up numbers; they don't really matter for this test
                opportunity3.TotalBuyCost  = 15m;
                opportunity3.TotalSellCost = 15m;

                bitstamp.SimulatedBuy(opportunity3.BuyAmount, opportunity3.TotalBuyCost);
                kraken.SimulatedSell(opportunity3.BuyAmount, opportunity3.TotalSellCost);
                Assert.IsTrue(bitstamp.AvailableBtc == 0.15m);

                //This should detect a transfer, but not actually create it
                List <Transfer> transferFound = TransferManager.DetectAndExecuteRollupTransfers_Simulate(3, testRun.Id.Value, exchangeList);
                Assert.IsTrue(transferFound == null);

                //Erase btc in transfer and look for transfers again, this time the TransferManager should throw an exception because
                //a bad transfer has been found (it's amount is > bitstamp total btc)
                bool errorFound = false;
                bitstamp.BTCInTransfer = 0m;

                try
                {
                    TransferManager.DetectAndExecuteRollupTransfers_Simulate(2, testRun.Id.Value, exchangeList);
                }

                catch (NotEnoughBtcException e)
                {
                    errorFound = true;
                }

                Assert.IsTrue(errorFound, "TransferManager did not throw NotEnoughBtcException.");
            }

            //Clean up test data
            finally
            {
                if (opportunity1 != null)
                {
                    TestsHelper.DeleteArbitrationTrade(opportunity1.Id.Value);
                }

                if (opportunity2 != null)
                {
                    TestsHelper.DeleteArbitrationTrade(opportunity2.Id.Value);
                }

                if (opportunity3 != null)
                {
                    TestsHelper.DeleteArbitrationTrade(opportunity3.Id.Value);
                }

                if (testRun != null)
                {
                    TestsHelper.DeleteArbitrationTrade(testRun.Id.Value);
                }
            }
        }