Ejemplo n.º 1
0
        public OrderMatcher(OrderRequest orderRequest, EffectProcessorsContainer effectsContainer)
        {
            resultEffects = effectsContainer;

            takerOrder = new Order
            {
                OrderId        = OrderIdConverter.FromRequest(orderRequest, effectsContainer.Apex),
                AccountWrapper = orderRequest.AccountWrapper,
                Amount         = orderRequest.Amount,
                Price          = orderRequest.Price
            };
            timeInForce = orderRequest.TimeInForce;

            //parse data from the ID of the newly arrived order
            var orderData = OrderIdConverter.Decode(takerOrder.OrderId);

            asset = orderData.Asset;
            side  = orderData.Side;
            //get asset orderbook
            market    = effectsContainer.Context.Exchange.GetMarket(asset);
            orderbook = market.GetOrderbook(side.Inverse());
            //fetch balances
            if (!takerOrder.AccountWrapper.Account.HasBalance(asset))
            {
                resultEffects.AddBalanceCreate(orderRequest.AccountWrapper, asset);
            }
        }
Ejemplo n.º 2
0
        byte[] GetEffectsData(EffectProcessorsContainer effectsContainer)
        {
            var resultEffectsContainer = new EffectsContainer {
                Effects = effectsContainer.Effects
            };

            return(resultEffectsContainer.ToByteArray(buffer.Buffer));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Process customer's order request.
        /// </summary>
        /// <param name="orderRequest">Order request quantum</param>
        /// <returns></returns>
        public void ExecuteOrder(EffectProcessorsContainer effectsContainer)
        {
            RequestQuantum orderRequestQuantum = (RequestQuantum)effectsContainer.Envelope.Message;
            var            orderRequest        = (OrderRequest)orderRequestQuantum.RequestEnvelope.Message;
            var            updates             = new OrderMatcher(orderRequest, effectsContainer).Match();

            awaitedUpdates?.Add(updates);
        }
Ejemplo n.º 4
0
 public void RemoveOrder(EffectProcessorsContainer effectsContainer, OrderbookBase orderbook, Order order)
 {
     effectsContainer.AddOrderRemoved(orderbook, order);
     if (awaitedUpdates != null)
     {
         var updateTime   = new DateTime(((Quantum)effectsContainer.Envelope.Message).Timestamp, DateTimeKind.Utc);
         var exchangeItem = new ExchangeUpdate(orderbook.Market, updateTime);
         exchangeItem.OrderUpdates.Add(order.ToOrderInfo(OrderState.Deleted));
         awaitedUpdates.Add(exchangeItem);
     }
 }
 public static void AddBalanceCreate(this EffectProcessorsContainer effectProcessors, AccountWrapper account, int asset)
 {
     effectProcessors.Add(new BalanceCreateEffectProcessor(
                              new BalanceCreateEffect
     {
         AccountWrapper = account,
         Asset          = asset,
         Apex           = effectProcessors.Apex
     }
                              ));
 }
 public static void AddCursorUpdate(this EffectProcessorsContainer effectProcessors, TxCursorManager txManager, long newCursor, long prevCursor)
 {
     effectProcessors.Add(new TxCursorUpdateEffectProcessor(
                              new TxCursorUpdateEffect
     {
         Apex       = effectProcessors.Apex,
         Cursor     = newCursor,
         PrevCursor = prevCursor
     },
                              txManager
                              ));
 }
 public static void AddAccountCreate(this EffectProcessorsContainer effectProcessors, AccountStorage accountStorage, int accountId, RawPubKey publicKey)
 {
     effectProcessors.Add(new AccountCreateEffectProcessor(
                              new AccountCreateEffect
     {
         AccountId = accountId,
         Pubkey    = publicKey,
         Apex      = effectProcessors.Apex
     },
                              accountStorage
                              ));
 }
 public static void AddNonceUpdate(this EffectProcessorsContainer effectProcessors, AccountWrapper account, long newNonce, long currentNonce)
 {
     effectProcessors.Add(new NonceUpdateEffectProcessor(
                              new NonceUpdateEffect
     {
         Nonce          = newNonce,
         PrevNonce      = currentNonce,
         AccountWrapper = account,
         Apex           = effectProcessors.Apex
     }
                              ));
 }
        public static void AddWithdrawalCreate(this EffectProcessorsContainer effectProcessors, WithdrawalWrapper withdrawal, WithdrawalStorage withdrawalStorage)
        {
            var effect = new WithdrawalCreateEffect
            {
                Apex           = effectProcessors.Apex,
                AccountWrapper = withdrawal.Source,
                Items          = withdrawal.Withdrawals.Select(w => new WithdrawalEffectItem {
                    Asset = w.Asset, Amount = w.Amount
                }).OrderBy(a => a.Asset).ToList()
            };

            effectProcessors.Add(new WithdrawalCreateEffectProcessor(effect, withdrawal, withdrawalStorage));
        }
 public static void AddAccountCreate(this EffectProcessorsContainer effectProcessors, AccountStorage accountStorage, int accountId, RawPubKey publicKey)
 {
     effectProcessors.Add(new AccountCreateEffectProcessor(
                              new AccountCreateEffect
     {
         Account = accountId,
         Pubkey  = publicKey,
         Apex    = effectProcessors.Apex
     },
                              accountStorage,
                              effectProcessors.Context.Constellation.RequestRateLimits
                              ));
 }
        public static void AddTrade(this EffectProcessorsContainer effectProcessors, Order order, long assetAmount, long quoteAmount, bool isNewOrder)
        {
            var trade = new TradeEffect
            {
                Apex           = effectProcessors.Apex,
                AccountWrapper = order.AccountWrapper,
                AssetAmount    = assetAmount,
                QuoteAmount    = quoteAmount,
                OrderId        = order.OrderId,
                IsNewOrder     = isNewOrder
            };

            effectProcessors.Add(new TradeEffectProcessor(trade, order));
        }
 public static void AddOrderRemoved(this EffectProcessorsContainer effectProcessors, Orderbook orderbook, Order order)
 {
     effectProcessors.Add(new OrderRemovedEffectProccessor(
                              new OrderRemovedEffect
     {
         Apex           = effectProcessors.Apex,
         OrderId        = order.OrderId,
         AccountWrapper = order.AccountWrapper,
         Amount         = order.Amount,
         QuoteAmount    = order.QuoteAmount,
         Price          = order.Price
     },
                              orderbook
                              ));
 }
 public static void AddConstellationInit(this EffectProcessorsContainer effectProcessors, ConstellationInitQuantum initQuantum)
 {
     effectProcessors.Add(new ConstellationInitEffectProcessor(
                              new ConstellationInitEffect
     {
         Apex              = initQuantum.Apex,
         Assets            = initQuantum.Assets,
         Auditors          = initQuantum.Auditors,
         MinAccountBalance = initQuantum.MinAccountBalance,
         MinAllowedLotSize = initQuantum.MinAllowedLotSize,
         Vault             = initQuantum.Vault,
         RequestRateLimits = initQuantum.RequestRateLimits,
         TxCursor          = initQuantum.TxCursor
     }
                              ));
 }
        public static void AddOrderPlaced(this EffectProcessorsContainer effectProcessors, Orderbook orderBook, Order order)
        {
            var decodedOrderId = OrderIdConverter.Decode(order.OrderId);
            var effect         = new OrderPlacedEffect
            {
                Apex           = effectProcessors.Apex,
                AccountWrapper = order.AccountWrapper,
                Asset          = decodedOrderId.Asset,
                Amount         = order.Amount,
                QuoteAmount    = order.QuoteAmount,
                Price          = order.Price,
                OrderId        = order.OrderId,
                OrderSide      = decodedOrderId.Side
            };

            effectProcessors.Add(new OrderPlacedEffectProcessor(effect, orderBook, order));
        }
 public override WithdrawalCleanupProcessorContext GetContext(EffectProcessorsContainer container)
 {
     return(new WithdrawalCleanupProcessorContext(container));
 }
 public override LedgerCommitProcessorContext GetContext(EffectProcessorsContainer container)
 {
     return(new LedgerCommitProcessorContext(container));
 }
Ejemplo n.º 17
0
 public RequestContext(EffectProcessorsContainer effectProcessors)
     : base(effectProcessors)
 {
     Request = (RequestQuantum)Envelope.Message;
 }
 public WithdrawalCleanupProcessorContext(EffectProcessorsContainer effectProcessorsContainer)
     : base(effectProcessorsContainer)
 {
 }
Ejemplo n.º 19
0
        public static void Aggregate(this EffectProcessorsContainer processorsContainer, MessageEnvelope quantumEnvelope, Effect quatumEffect, int effectIndex)
        {
            if (processorsContainer == null)
            {
                throw new ArgumentNullException(nameof(processorsContainer));
            }

            var pendingDiffObject = processorsContainer.PendingDiffObject;

            if (pendingDiffObject == null)
            {
                throw new ArgumentNullException(nameof(pendingDiffObject));
            }

            if (quantumEnvelope == null)
            {
                throw new ArgumentNullException(nameof(quantumEnvelope));
            }

            if (quatumEffect == null)
            {
                throw new ArgumentNullException(nameof(quatumEffect));
            }

            var accountWrapper = quatumEffect.AccountWrapper;
            var apex           = processorsContainer.Apex;

            processorsContainer.QuantumModel.AddEffect(accountWrapper?.Account.Id ?? 0, quatumEffect.FromEffect(effectIndex));

            switch (quatumEffect)
            {
            case ConstellationInitEffect constellationInit:
                pendingDiffObject.ConstellationSettings = GetConstellationSettings(constellationInit);
                pendingDiffObject.StellarInfoData       = new DiffObject.ConstellationState {
                    TxCursor = constellationInit.TxCursor, IsInserted = true
                };
                pendingDiffObject.Assets = GetAssets(constellationInit, null);
                break;

            case ConstellationUpdateEffect constellationUpdate:
                throw new NotImplementedException();
                pendingDiffObject.ConstellationSettings = GetConstellationSettings(constellationUpdate);
                pendingDiffObject.Assets = GetAssets(constellationUpdate, Global.PermanentStorage.LoadAssets(long.MaxValue).Result);
                break;

            case AccountCreateEffect accountCreateEffect:
            {
                var pubKey = accountCreateEffect.Pubkey;
                var accId  = accountCreateEffect.AccountId;
                pendingDiffObject.Accounts.Add(accId, new DiffObject.Account {
                        PubKey = pubKey, Id = accId, IsInserted = true
                    });
            }
            break;

            case NonceUpdateEffect nonceUpdateEffect:
            {
                var accId = nonceUpdateEffect.AccountWrapper.Account.Id;
                GetAccount(pendingDiffObject.Accounts, accId).Nonce = nonceUpdateEffect.Nonce;
            }
            break;

            case BalanceCreateEffect balanceCreateEffect:
            {
                var accId     = balanceCreateEffect.AccountWrapper.Account.Id;
                var balanceId = BalanceModelIdConverter.EncodeId(accId, balanceCreateEffect.Asset);
                GetBalance(pendingDiffObject.Balances, balanceId).IsInserted = true;
            }
            break;

            case BalanceUpdateEffect balanceUpdateEffect:
            {
                var accId     = balanceUpdateEffect.AccountWrapper.Account.Id;
                var balanceId = BalanceModelIdConverter.EncodeId(accId, balanceUpdateEffect.Asset);
                GetBalance(pendingDiffObject.Balances, balanceId).AmountDiff += balanceUpdateEffect.Amount;
            }
            break;

            case RequestRateLimitUpdateEffect requestRateLimitUpdateEffect:
            {
                var accId = requestRateLimitUpdateEffect.AccountWrapper.Account.Id;
                GetAccount(pendingDiffObject.Accounts, accId).RequestRateLimits = new RequestRateLimitsModel
                {
                    HourLimit   = requestRateLimitUpdateEffect.RequestRateLimits.HourLimit,
                    MinuteLimit = requestRateLimitUpdateEffect.RequestRateLimits.MinuteLimit
                };
            }
            break;

            case OrderPlacedEffect orderPlacedEffect:
            {
                var accId   = orderPlacedEffect.AccountWrapper.Account.Id;
                var orderId = orderPlacedEffect.OrderId;
                pendingDiffObject.Orders[orderId] = new DiffObject.Order
                {
                    AmountDiff      = orderPlacedEffect.Amount,
                    QuoteAmountDiff = orderPlacedEffect.QuoteAmount,
                    IsInserted      = true,
                    OrderId         = orderId,
                    Price           = orderPlacedEffect.Price,
                    Account         = accId
                };
                //update liabilities
                var decodedId = OrderIdConverter.Decode(orderId);
                if (decodedId.Side == OrderSide.Buy)
                {
                    GetBalance(pendingDiffObject.Balances, BalanceModelIdConverter.EncodeId(accId, 0)).LiabilitiesDiff += orderPlacedEffect.QuoteAmount;
                }
                else
                {
                    GetBalance(pendingDiffObject.Balances, BalanceModelIdConverter.EncodeId(accId, decodedId.Asset)).LiabilitiesDiff += orderPlacedEffect.Amount;
                }
            }
            break;

            case OrderRemovedEffect orderRemovedEffect:
            {
                var accId   = orderRemovedEffect.AccountWrapper.Account.Id;
                var orderId = orderRemovedEffect.OrderId;
                GetOrder(pendingDiffObject.Orders, orderId).IsDeleted = true;
                //update liabilities
                var decodedId = OrderIdConverter.Decode(orderId);
                if (decodedId.Side == OrderSide.Buy)
                {
                    GetBalance(pendingDiffObject.Balances, BalanceModelIdConverter.EncodeId(accId, 0)).LiabilitiesDiff -= orderRemovedEffect.QuoteAmount;
                }
                else
                {
                    GetBalance(pendingDiffObject.Balances, BalanceModelIdConverter.EncodeId(accId, decodedId.Asset)).LiabilitiesDiff -= orderRemovedEffect.Amount;
                }
            }
            break;

            case TradeEffect tradeEffect:
            {
                var order = GetOrder(pendingDiffObject.Orders, tradeEffect.OrderId);
                order.AmountDiff      -= tradeEffect.AssetAmount;
                order.QuoteAmountDiff -= tradeEffect.QuoteAmount;
            }
            break;

            case TxCursorUpdateEffect cursorUpdateEffect:
            {
                if (pendingDiffObject.StellarInfoData == null)
                {
                    pendingDiffObject.StellarInfoData = new DiffObject.ConstellationState {
                        TxCursor = cursorUpdateEffect.Cursor
                    }
                }
                ;
                else
                {
                    pendingDiffObject.StellarInfoData.TxCursor = cursorUpdateEffect.Cursor;
                }
            }
            break;

            case WithdrawalCreateEffect withdrawalCreateEffect:
            {
                var accId = withdrawalCreateEffect.AccountWrapper.Account.Id;
                GetAccount(pendingDiffObject.Accounts, accId).Withdrawal = withdrawalCreateEffect.Apex;
                foreach (var withdrawalItem in withdrawalCreateEffect.Items)
                {
                    GetBalance(pendingDiffObject.Balances, BalanceModelIdConverter.EncodeId(accId, withdrawalItem.Asset)).LiabilitiesDiff += withdrawalItem.Amount;
                }
            }
            break;

            case WithdrawalRemoveEffect withdrawalRemoveEffect:
            {
                var accId = withdrawalRemoveEffect.AccountWrapper.Account.Id;
                GetAccount(pendingDiffObject.Accounts, accId).Withdrawal = 0;
                foreach (var withdrawalItem in withdrawalRemoveEffect.Items)
                {
                    var balance = GetBalance(pendingDiffObject.Balances, BalanceModelIdConverter.EncodeId(accId, withdrawalItem.Asset));
                    if (withdrawalRemoveEffect.IsSuccessful)
                    {
                        balance.AmountDiff -= withdrawalItem.Amount;
                    }
                    balance.LiabilitiesDiff -= withdrawalItem.Amount;
                }
            }
            break;

            default:
                break;
            }
        }
Ejemplo n.º 20
0
 public override OrderCancellationProcessorContext GetContext(EffectProcessorsContainer effectProcessors)
 {
     return(new OrderCancellationProcessorContext(effectProcessors));
 }
Ejemplo n.º 21
0
 public PaymentProcessorContext(EffectProcessorsContainer effectProcessors)
     : base(effectProcessors)
 {
     Payment       = (PaymentRequest)Request.RequestEnvelope.Message;
     SourceAccount = Payment.AccountWrapper;
 }
Ejemplo n.º 22
0
 public LedgerCommitProcessorContext(EffectProcessorsContainer effectProcessors)
     : base(effectProcessors)
 {
 }
Ejemplo n.º 23
0
 ProcessorContext IQuantumRequestProcessor.GetContext(EffectProcessorsContainer container) => GetContext(container);
 /// <summary>
 ///
 /// </summary>
 /// <param name="effectProcessors">Current context effects processor container</param>
 public ProcessorContext(EffectProcessorsContainer effectProcessors)
 {
     EffectProcessors = effectProcessors ?? throw new ArgumentNullException(nameof(effectProcessors));
 }
Ejemplo n.º 25
0
 public WithdrawalProcessorContext(EffectProcessorsContainer effectProcessorsContainer)
     : base(effectProcessorsContainer)
 {
     WithdrawalRequest = (WithdrawalRequest)Request.RequestEnvelope.Message;
 }
Ejemplo n.º 26
0
 public override PaymentProcessorContext GetContext(EffectProcessorsContainer container)
 {
     return(new PaymentProcessorContext(container));
 }
Ejemplo n.º 27
0
 public abstract T GetContext(EffectProcessorsContainer container);