public async Task <bool> Transfer() { var autoPush = await _ctx.Transactions.Where(x => x.Status == EnumType.Pending && x.StatusType == EnumType.Transfer).OrderBy(x => x.CreationDate).Take(150).ToListAsync(); if (autoPush.Count <= 0) { return(true); } PushResp result = new PushResp(); var objTransfer = new { signatures = autoPush.Select(x => x.Signature).ToArray(), toes = autoPush.Select(x => x.ToKey).ToArray(), values = autoPush.Select(x => ((BigInteger)(x.CoinAmount * p)).ToString()).ToArray(), //fees= Enumerable.Repeat("0", autoPush.Count).ToArray() fees = autoPush.Select(x => ((BigInteger)(x.FeeInCoinAmount * p)).ToString()).ToArray() }; var json_obj = Newtonsoft.Json.JsonConvert.SerializeObject(objTransfer); try { result = await nodeServices.InvokeExportAsync <PushResp>("wwwroot/Scripts/AutoPush.js", "transferArray", json_obj, Admin.PrivateKey); AddMineTransaction(result, json_obj, autoPush.Count()); // foreach (var trn in autoPush) { trn.Status = EnumType.Success; trn.HashHex = result.Hash; } } catch (Exception ex) { autoPush.ForEach(x => x.Status = EnumType.Failed); await SendEmail.SendEmailAsync("*****@*****.**", "Node(Transfer) Error:exception: " + json_obj + " resp " + Newtonsoft.Json.JsonConvert.SerializeObject(result) + " EXCEPTN::" + ex.Message); } await _ctx.SaveChangesAsync(); return(true); }
private void AddMineTransaction(PushResp result, string json, int trnxCount, decimal totalCoinFee = 0) { var mineTrnx = new MineTransaction(); mineTrnx.Id = new Guid(); mineTrnx.CreationDate = DateTime.UtcNow; mineTrnx.GasFee = Convert.ToUInt64(result.GasPrice.Hex, 16); mineTrnx.LastTransactinTime = DateTime.Now; //trnxs.Last().CreationDate; mineTrnx.totalCoinFee = totalCoinFee; //trnxs.Sum(x => x.FeeInCoinAmount); mineTrnx.TotalFee = Convert.ToUInt64(result.GasLimit.Hex, 16); mineTrnx.txHash = result.Hash; mineTrnx.StatusType = totalCoinFee == 0 ? EnumType.Buy : EnumType.Transfer; mineTrnx.TransactionCount = trnxCount; mineTrnx.JsonTransactions = json; mineTrnx.Nonce = result.Nonce; _ctx.MineTransactions.Add(mineTrnx); }
public async Task <bool> AirDrop() { var airdrop = await _ctx.Transactions.Where(x => x.Status == EnumType.Pending && x.StatusType == EnumType.Buy).OrderBy(x => x.CreationDate).Take(500).ToListAsync(); if (airdrop.Count <= 0) { return(true); } PushResp result = new PushResp(); var objAirdrop = new { toes = airdrop.Select(x => x.ToKey).ToArray(), values = airdrop.Select(x => ((BigInteger)(x.CoinAmount * p)).ToString()).ToArray(), }; var json_obj = Newtonsoft.Json.JsonConvert.SerializeObject(objAirdrop); try { result = await nodeServices.InvokeExportAsync <PushResp>("wwwroot/Scripts/AutoPush.js", "sendBatchCS", json_obj, Admin.PrivateKey); AddMineTransaction(result, json_obj, airdrop.Count()); // foreach (var trn in airdrop) { trn.Status = EnumType.Success; trn.HashHex = result.Hash; } } catch (Exception ex) { airdrop.ForEach(x => x.Status = EnumType.Failed); await SendEmail.SendEmailAsync("*****@*****.**", "Node(Buy) Error:exception: " + json_obj + " resp " + Newtonsoft.Json.JsonConvert.SerializeObject(result) + " EXCEPTN::" + ex.Message); } await _ctx.SaveChangesAsync(); return(true); }
public async Task <IActionResult> AutoPush([FromServices] INodeServices nodeServices) { var trnxs = await _ctx.Transactions.Where(x => x.Status == EnumType.Pending).OrderBy(x => x.CreationDate).ToListAsync(); if (trnxs.Count <= 0) { return(null); } var p = System.Convert.ToDecimal(Math.Pow(10, Admin.decimalNumber)); var autoPush = new List <Transaction>(); var airdrop = new List <Transaction>(); trnxs.ForEach (i => { if (i.StatusType == EnumType.Buy) { airdrop.Add(i); } else { autoPush.Add(i); } }); string json_obj = ""; PushResp result = new PushResp(); bool isAirdropped = false;; try { if (airdrop.Count > 0) { var objAirdrop = new { toes = airdrop.Select(x => x.ToKey).ToArray(), values = airdrop.Select(x => ((BigInteger)(x.CoinAmount * p)).ToString()).ToArray(), }; json_obj = Newtonsoft.Json.JsonConvert.SerializeObject(objAirdrop); result = await nodeServices.InvokeExportAsync <PushResp>("wwwroot/Scripts/AutoPush.js", "sendBatchCS", json_obj); //AddMineTransaction(result, json_obj, airdrop.Count()); // isAirdropped = true; } else if (autoPush.Count > 0) { var objTransfer = new { signatures = autoPush.Select(x => x.Signature).ToArray(), toes = autoPush.Select(x => x.ToKey).ToArray(), values = autoPush.Select(x => ((BigInteger)(x.CoinAmount * p)).ToString()).ToArray(), //fees= Enumerable.Repeat("0", autoPush.Count).ToArray() fees = autoPush.Select(x => ((BigInteger)(x.FeeInCoinAmount * p)).ToString()).ToArray() }; json_obj = Newtonsoft.Json.JsonConvert.SerializeObject(objTransfer); result = await nodeServices.InvokeExportAsync <PushResp>("wwwroot/Scripts/AutoPush.js", "transferArray", json_obj); //AddMineTransaction(result, json_obj, autoPush.Count()); // } //foreach (var trn in trnxs) //{ // trn.Status = EnumType.Success; //} trnxs.ForEach(x => x.Status = EnumType.Success); } catch (Exception ex) { //email if (isAirdropped) { trnxs.Where(x => x.StatusType != EnumType.Buy).ToList().ForEach(x => x.Status = EnumType.Failed); } else { trnxs.ForEach(x => x.Status = EnumType.Failed); } await SendEmail.SendEmailAsync("*****@*****.**", "Node Error:exception: " + json_obj + " resp " + Newtonsoft.Json.JsonConvert.SerializeObject(result) + Newtonsoft.Json.JsonConvert.SerializeObject(ex)); //await _ctx.SaveChangesAsync(); } // await _ctx.SaveChangesAsync(); //var trnxs = _ctx.Transactions.Where(x => x.Status == EnumType.Pending).OrderBy(x => x.CreationDate).ToList(); //if (trnxs.Count > 0) //{ // var Signatures = trnxs.Select(x => x.Signature).ToArray(); // //string signs = string.Join(".", Signature); // string[] _toes = trnxs.Select(x => x.ToKey).ToArray(); // var p = System.Convert.ToDecimal(Math.Pow(10, 18)); // string[] _values = trnxs.Select(x => (x.CoinAmount * p).ToString().Split('.')[0]).ToArray(); // //decimal[] _fees = trnxs.Select(x => x.FeeInCoinAmount * p).ToArray(); // string[] _nonces = Enumerable.Repeat("0", trnxs.Count).ToArray(); // var obj = new { signatures = Signatures, _toes, _values, _fees = _nonces }; // string json_obj = Newtonsoft.Json.JsonConvert.SerializeObject(obj); // var result = await nodeServices.InvokeAsync<PushResp>("wwwroot/Scripts/AutoPush.js", json_obj); // foreach (var trn in trnxs) // { // trn.Status = EnumType.Success; // } // var mineTrnx = new MineTransaction(); // mineTrnx.Id = new Guid(); // mineTrnx.CreationDate = DateTime.UtcNow; // mineTrnx.GasFee = Convert.ToInt32(result.GasPrice.Hex, 16); // mineTrnx.LastTransactinTime = trnxs.Last().CreationDate; // mineTrnx.totalCoinFee = trnxs.Sum(x => x.FeeInCoinAmount); // mineTrnx.TotalFee = Convert.ToInt32(result.GasLimit.Hex, 16); // mineTrnx.txHash = result.Hash; // _ctx.MineTransactions.Add(mineTrnx); // await _ctx.SaveChangesAsync(); // return Content(Newtonsoft.Json.JsonConvert.SerializeObject(result)); //} return(null); }