Ejemplo n.º 1
0
        public async Task <MessageEnvelope> CreateOrder(long amount, double price, OrderSide side, ConstellationInfo.Asset asset, bool waitForFinalize = true)
        {
            var order = new OrderRequest {
                Amount = amount, Price = price, Side = side, Asset = asset.Id
            };

            var response = (CentaurusResponse)await connection.SendMessage(order.CreateEnvelope());

            var result = await(waitForFinalize ? response.ResponseTask : response.AcknowledgmentTask);

            return(result);
        }
        public async Task OrderQuantumTest(int nonce, int asset, int amount, OrderSide side, Type excpectedException)
        {
            var accountWrapper = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair);
            var order          = new OrderRequest
            {
                Account        = accountWrapper.Account.Id,
                RequestId      = nonce,
                Amount         = amount,
                Asset          = asset,
                Price          = 100,
                Side           = side,
                AccountWrapper = accountWrapper
            };

            var envelope = order.CreateEnvelope();

            envelope.Sign(TestEnvironment.Client1KeyPair);

            if (!context.IsAlpha)
            {
                var quantum = new RequestQuantum {
                    Apex = context.QuantumStorage.CurrentApex + 1, RequestEnvelope = envelope, Timestamp = DateTime.UtcNow.Ticks
                };
                envelope = quantum.CreateEnvelope();
                envelope.Sign(TestEnvironment.AlphaKeyPair);
            }

            var res = await AssertQuantumHandling(envelope, excpectedException);

            if (excpectedException == null)
            {
                var currentMarket = context.Exchange.GetMarket(asset);
                Assert.IsTrue(currentMarket != null);

                var requests = side == OrderSide.Buy ? currentMarket.Bids : currentMarket.Asks;
                Assert.AreEqual(1, requests.Count);
            }
        }
        public async Task OrderCancellationQuantumTest(int asset, int amount, OrderSide side, int apexMod, bool useFakeSigner, Type excpectedException)
        {
            var acc = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair);

            var order = new OrderRequest
            {
                Account        = acc.Account.Id,
                RequestId      = 1,
                Amount         = amount,
                Asset          = asset,
                Price          = 100,
                Side           = side,
                AccountWrapper = acc
            };

            var envelope = order.CreateEnvelope().Sign(useFakeSigner ? TestEnvironment.Client2KeyPair : TestEnvironment.Client1KeyPair);

            if (!context.IsAlpha)
            {
                var quantum = new RequestQuantum {
                    Apex = context.QuantumStorage.CurrentApex + 1, RequestEnvelope = envelope, Timestamp = DateTime.UtcNow.Ticks
                };
                envelope = quantum.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);
            }

            var submitResult = await AssertQuantumHandling(envelope, excpectedException);

            if (excpectedException != null)
            {
                return;
            }

            var apex = ((Quantum)submitResult.OriginalMessage.Message).Apex + apexMod;

            var orderCancellation = new OrderCancellationRequest
            {
                Account        = acc.Account.Id,
                RequestId      = 2,
                OrderId        = OrderIdConverter.Encode((ulong)apex, asset, side),
                AccountWrapper = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair)
            };

            envelope = orderCancellation.CreateEnvelope().Sign(TestEnvironment.Client1KeyPair);

            if (!context.IsAlpha)
            {
                var quantum = new RequestQuantum {
                    Apex = context.QuantumStorage.CurrentApex + 1, RequestEnvelope = envelope, Timestamp = DateTime.UtcNow.Ticks
                };
                envelope = quantum.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);
            }

            var cancelResult = await AssertQuantumHandling(envelope, excpectedException);

            if (excpectedException != null)
            {
                return;
            }
            var currentMarket = context.Exchange.GetMarket(asset);

            Assert.IsTrue(currentMarket != null);

            var requests = side == OrderSide.Buy ? currentMarket.Bids : currentMarket.Asks;

            Assert.AreEqual(requests.Count, 0);
        }