Exemplo n.º 1
0
        public virtual async Task <IActionResult> GetLimitOrder([FromQuery] string baseSymbol, [FromQuery] string quoteSymbol, [FromQuery] int limit)
        {
            var go = await _limitOrderService.GetLimitOrderHistory(baseSymbol, quoteSymbol, limit);

            return(new ObjectResult(go));
        }
        public async Task FinalizeTrades(string currencyId)
        {
            try
            {
                _logger.LogInformation("[FinalizeTrades] starting...");

                var now = DateTime.UtcNow;

                var obj22 = (IAccountService)ServiceLocatorSingleton.Instance.ServiceProvider.GetService(typeof(IAccountService));

                var lstTrades = await obj22.ListHistory("tst-ll-reception", 4);

                var lstTransNotFilled = _transRepository.DbSet.Where(x1 => !x1.Cryptoconfirmed).ToList();
                var lstTradesOrdered  = lstTrades.OrderByDescending(x1 => x1.Op.BlockNum);
                var itemToProcess     = lstTransNotFilled.Where(x1 => lstTradesOrdered.Select(x2 => x2.Memo).Contains(x1.Memobc));

                _logger.LogInformation($"[FinalizeTrades] Transactions to be processed: {JsonConvert.SerializeObject(itemToProcess, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");

                foreach (var a1 in itemToProcess)
                {
                    var t1 = lstTrades.First(xx1 => xx1.Memo == a1.Memobc);

                    if (currencyId == "1.3.0")
                    {
                        a1.Amount        = Convert.ToInt64(t1.Op.Op.First().Value.Amount.Amount / 100000);
                        a1.AmountDecimal = t1.Op.Op.First().Value.Amount.Amount / 100000;
                        var tradeExchange1 = (await _limitOrderService.GetLimitOrderHistory("1.3.0", "1.3.121", 1)).First();
                        a1.Amountusd = a1.AmountDecimal * tradeExchange1.Price;
                    }
                    else
                    {
                        a1.Amount        = Convert.ToInt64(t1.Op.Op.First().Value.Amount.Amount / 10000);
                        a1.AmountDecimal = t1.Op.Op.First().Value.Amount.Amount / 10000;
                        a1.Amountusd     = a1.AmountDecimal;
                    }

                    a1.Paidonbc        = false;
                    a1.Cryptoconfirmed = true;
                    a1.Modifiedon      = now;

                    await _transRepository.UpdateAsync(a1);

                    var error0 = _unitOfWork.CommitHandled();
                    if (!error0)
                    {
                        _logger.LogError($"Can't Add Transaction ! {JsonConvert.SerializeObject(error0, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                    }

                    _logger.LogInformation($"[FinalizeTrades] Transaction Updated: {JsonConvert.SerializeObject(a1, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");

                    var cal1 = CalculateTokenAmountAndPrice(a1.Amountusd);

                    _logger.LogInformation($"[FinalizeTrades] CalculateTokenAmountAndPrice result: {JsonConvert.SerializeObject(cal1, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");

                    if (cal1.Count > 1)
                    {
                        foreach (var i1 in cal1.Skip(1))
                        {
                            var newTrans = new Transactions();

                            newTrans.Memobc          = a1.Memobc;
                            newTrans.Paidonbc        = false;
                            newTrans.Godfathercode   = a1.Godfathercode;
                            newTrans.Cryptocurrency  = a1.Cryptocurrency;
                            newTrans.Cryptoconfirmed = a1.Cryptoconfirmed;

                            newTrans.Amounttoken   = i1.Item1;
                            newTrans.Purchaseprice = i1.Item2;
                            newTrans.Amount        = a1.Amount;
                            newTrans.Amountusd     = a1.Amountusd;
                            newTrans.Createdon     = now;
                            newTrans.Modifiedon    = now;
                            newTrans.Createdby     = "System";
                            newTrans.Modifiedby    = "System";

                            await _transRepository.AddAsync(newTrans);

                            var errorCurs = _unitOfWork.CommitHandled();
                            if (!errorCurs)
                            {
                                _logger.LogError($"Can't Add Transaction ! {JsonConvert.SerializeObject(errorCurs, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                            }

                            i1.Item3.Remainingtokens = i1.Item3.Remainingtokens - i1.Item1;
                            await _tokenpriceRepository.UpdateAsync(i1.Item3);

                            var errorCursTokenPriceSub = _unitOfWork.CommitHandled();
                            if (!errorCursTokenPriceSub)
                            {
                                _logger.LogError($"Can't Update TokenPrice ! {JsonConvert.SerializeObject(errorCursTokenPriceSub, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                            }

                            _logger.LogInformation($"[FinalizeTrades] Transaction created: {JsonConvert.SerializeObject(newTrans, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");
                        }
                    }

                    var a22 = _transRepository.DbSet.First(dd => dd.Transactionid == a1.Transactionid);

                    a22.Modifiedon = now;

                    a22.Purchaseprice = cal1.First().Item2;
                    a22.Amounttoken   = cal1.First().Item1;

                    await _transRepository.UpdateAsync(a22);

                    var error1 = _unitOfWork.CommitHandled();
                    if (!error1)
                    {
                        _logger.LogError($"Can't Add Transaction ! {JsonConvert.SerializeObject(error1, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                    }

                    _logger.LogInformation($"[FinalizeTrades] TokenPrice updated: {JsonConvert.SerializeObject(a22, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");

                    var tprice1 = cal1.First().Item3;

                    tprice1.Remainingtokens = tprice1.Remainingtokens.Value - cal1.First().Item1;
                    await _tokenpriceRepository.UpdateAsync(tprice1);

                    var errorCursTokenPrice = _unitOfWork.CommitHandled();
                    if (!errorCursTokenPrice)
                    {
                        _logger.LogError($"Can't Update TokenPrice ! {JsonConvert.SerializeObject(errorCursTokenPrice, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                    }

                    _logger.LogInformation($"[FinalizeTrades] TokenPrice updated: {JsonConvert.SerializeObject(tprice1, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");

                    var guidString = Guid.NewGuid().ToString();
                    await _commonMessageService.SendMessage <ActionEventDefinition>("llc-event-broadcast", guidString,
                                                                                    new ActionEventDefinition()
                    {
                        ActionName = "FinalizeTradeTriggered",
                        Message    = string.Format($"[LedgerLocal][Participation][Success] New participation of { a1.Amountusd } USD => {a22.Amounttoken} Token"),
                        Timestamp  = DateTime.UtcNow,
                        Success    = true,
                        Reason     = "Subscription"
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error Finalizing");
            }
        }