public double GetPrize(NodeIndex nodeIndex, OrdersAssigned message) { if (nodeIndex.IsEmpty) { throw new TransactionDeclinedException("index is empty"); } var prize = nodeIndex.Orders.SelectUnassignedOrders(message.Orders).Sum(o => o.Order.Payment * message.Fee); return(prize); }
public void AssignOrdersToMe(double fee, params IUserOrder[] orders) { if (fee < 0) { throw new ArgumentException("fee"); } if (orders == null || orders.Length == 0) { throw new ArgumentException("orders"); } if (orders.Any(o => o.IsAssigned)) { throw new ArgumentException("orders contain already assigned order."); } var assigedOrder = new OrdersAssigned(fee, orders.Select(o => o.Signature).ToImmutableList()); SendMessage(assigedOrder); }
public NewTransactionIncludeResult Calculate(NodeIndex nodeIndex, OngoingBlock ongoingBlock, OrdersAssigned newOrder) { var newPrize = _transactionPrizeCalculator.GetPrize(nodeIndex, newOrder); var usedOrders = new HashSet <HashValue>(newOrder.Orders); var toRemoveTransactionsDic = new Dictionary <HashValue, Transaction>(); foreach (var ongoingTransaction in ongoingBlock.Transactions) { if (!(ongoingTransaction.Message is OrdersAssigned)) { continue; } var ongoingOrders = ((OrdersAssigned)ongoingTransaction.Message).Orders; foreach (var ongoingOrder in ongoingOrders) { if (usedOrders.Contains(ongoingOrder)) { toRemoveTransactionsDic[ongoingTransaction.Signature] = ongoingTransaction; break; } } } double lostPrize = 0; foreach (var t in toRemoveTransactionsDic.Values) { lostPrize += _transactionPrizeCalculator.GetPrize(nodeIndex, (OrdersAssigned)t.Message); } if (newPrize <= lostPrize) { return(NewTransactionIncludeResult.None); } return(new NewTransactionIncludeResult(toRemoveTransactionsDic.Values.ToArray())); }