Exemplo n.º 1
0
        public void TransferInsertTest()
        {
            BaseExchange sellExchange = new Kraken();
            BaseExchange buyExchange  = new Bitstamp();
            int          MaxId        = 0;

            sellExchange.AvailableBtc = 1.0234234234m;
            buyExchange.AvailableBtc  = 2.4534534m;
            decimal transferAmount = 0.6319841998m;

            //First need to create an arbitration run and opportunity
            ArbitrationRun         testRun         = TestsHelper.CreateArbitrationRun();
            ArbitrationOpportunity testOpportunity = TestsHelper.CreateArbitrationOpportunity(buyExchange, sellExchange, testRun.Id.Value);

            //Before the test can continue, ensure the arbitraiton opportunity was properly inserted
            Assert.IsTrue(testOpportunity.Id != null);

            Transfer testTransfer = new Transfer(buyExchange, sellExchange, transferAmount);

            //Get the max id from TRANSFER table
            Object result = DatabaseManager.ExecuteQuery("select MaxId = max(id) from TRANSFER").Rows[0]["MaxId"];

            if (result != DBNull.Value)
            {
                MaxId = (int)result;
            }

            //Now save the TRANSFER to the db
            testTransfer.PersistToDb();

            //Select the transfer from the db
            result = DatabaseManager.ExecuteQuery(string.Format("select id from TRANSFER where id = {0} AND ORIGIN_EXCHANGE = '{1}' and DESTINATION_EXCHANGE = '{2}' AND AMOUNT = {3}", testTransfer.Id, buyExchange.Name, sellExchange.Name, transferAmount)).Rows[0][0];

            //Clean up before testing result
            if (result != DBNull.Value)
            {
                //Remove the test transfer
                DatabaseManager.ExecuteNonQuery(String.Format("delete from TRANSFER where id = {0}", testTransfer.Id));
            }

            TestsHelper.DeleteArbitrationTrade(testOpportunity.Id.Value);
            TestsHelper.DeleteArbitrationRun(testRun.Id.Value);

            //The above query should return the transfer that was created.
            Assert.IsTrue(result != DBNull.Value);
        }
Exemplo n.º 2
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);
                }
            }
        }