public async Task <SimpleTradeInfo> InitiateTrade(string inputCoinType, decimal?amount)
        {
            _logger.LogInformation($"[InitiateTrade] Starting InitiateTrade with inputCoinType: {inputCoinType} and amount: {amount}");

            var now      = DateTime.UtcNow;
            var memoGuid = Guid.NewGuid().ToString();

            var r1 = await _blockTradeService.InitiateTrade(inputCoinType, _mappingTradingExchange[inputCoinType], "tst-ll-reception", memoGuid);

            _logger.LogInformation($"[InitiateTrade] Blocktrade reply: {JsonConvert.SerializeObject(r1, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");

            var objTrans = new Transactions();

            //if (amount.HasValue)
            //{
            //    objTrans.Amount = amount.Value;
            //}

            objTrans.Cryptoconfirmed = false;

            objTrans.Cryptoaddress  = r1.InputAddress;
            objTrans.Cryptocurrency = r1.InputCoinType;
            objTrans.Memobc         = memoGuid;

            objTrans.Modifiedby = "System";
            objTrans.Createdby  = "System";
            objTrans.Createdon  = now;
            objTrans.Modifiedon = now;

            await _transRepository.AddAsync(objTrans);

            var error1 = _unitOfWork.CommitHandled();

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

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

            var guidString = Guid.NewGuid().ToString();
            await _commonMessageService.SendMessage <ActionEventDefinition>("llc-event-broadcast", guidString,
                                                                            new ActionEventDefinition()
            {
                ActionName = "InitiateTradeTriggered",
                Message    = string.Format($"New participation initiated => {objTrans.Transactionid} => {r1.InputAddress}"),
                Timestamp  = DateTime.UtcNow,
                Success    = true,
                Reason     = "Subscription"
            });

            return(r1);
        }
Example #2
0
        async Task IJob.Execute(IJobExecutionContext context)
        {
            _logger.LogInformation($"Starting AccountListenerJob ...");

            IList <Task <WebSocketSession> > tasks = new List <Task <WebSocketSession> >();

            tasks.Add(_accountService.SubscribeToAccountBalance("1.2.800691", new string[] { "2.5.2077230", "2.5.2077225" }, async(a1) =>
            {
                var jObj  = JObject.Parse(a1);
                var jArr1 = (JArray)jObj["params"];
                var t1    = jArr1[1][0][0]["asset_type"];

                var guidString = Guid.NewGuid().ToString();
                await _commonMessageService.SendMessage <ActionEventDefinition>("llc-event-broadcast", guidString,
                                                                                new ActionEventDefinition()
                {
                    ActionName = "BalanceChanged",
                    Message    = a1,
                    Timestamp  = DateTime.UtcNow,
                    Success    = true,
                    Reason     = "Subscription"
                });

                await _participateBusinessService.FinalizeTrades(t1.ToString());
            }));

            await Task.WhenAll(tasks);

            var ws1 = tasks.First().Result;

            while (ws1.IsBusy)
            {
                await Task.Delay(1000);
            }

            _logger.LogInformation($"Stopping AccountListenerJob ...");
        }