// Let's buy 100 USD for 5 LGT per USD CancelTradeOrderBlock CreateCancelOrderBlock(TransactionBlock previousBlock, TradeOrderBlock order, TransactionBlock previous_to_order_block) { var cancelBlock = new CancelTradeOrderBlock { AccountID = order.AccountID, ServiceHash = string.Empty, Balances = new Dictionary <string, decimal>(), TradeOrderId = order.Hash }; var order_transaction = order.GetTransaction(previous_to_order_block); cancelBlock.Balances.Add(order.SellTokenCode, previousBlock.Balances[order.SellTokenCode] + order_transaction.TotalBalanceChange); // transfer unchanged token balances from the previous block foreach (var balance in previousBlock.Balances) { if (!(cancelBlock.Balances.ContainsKey(balance.Key))) { cancelBlock.Balances.Add(balance.Key, balance.Value); } } if (order.AccountID == AccountId1) { cancelBlock.InitializeBlock(previousBlock, PrivateKey1, AccountId1); } else { cancelBlock.InitializeBlock(previousBlock, PrivateKey2, AccountId2); } return(cancelBlock); }
// The cancellation should restore the balance that was locked by the trade order. // Thus, it should take the balance from the latest block and add the balamce (transactin amount) locked by the order block. APIResultCodes ValidateCancellationBalance(CancelTradeOrderBlock block, TransactionBlock lastBlock, TradeOrderBlock original_order) { var order_previous_block = _accountCollection.FindBlockByHash(original_order.PreviousHash); var order_transaction = original_order.GetTransaction(order_previous_block); var cancel_transaction = block.GetTransaction(lastBlock); if (order_transaction.TotalBalanceChange != cancel_transaction.TotalBalanceChange || order_transaction.TokenCode != cancel_transaction.TokenCode) { return(APIResultCodes.CancelTradeOrderValidationFailed); } return(APIResultCodes.Success); }