Esempio n. 1
0
        public void ConfirmSend()
        {
            var sendRequests = context.SendRequests.Where(x => x.State == 0).OrderBy(x => x.CreateTime).Take(50);

            if (sendRequests != null && sendRequests.Count() > 0)
            {
                if (!string.IsNullOrEmpty(rpcClient.WalletPassphrase))
                {
                    var walletPassphraseResp = rpcClient.Call <WalletPassphraseResponse>(JsonRPCMethods.WalletPassphrase, new WalletPassphraseParams()
                    {
                        Passphrase = rpcClient.WalletPassphrase,
                        Seconds    = 60
                    });
                    if (walletPassphraseResp.Error != null || walletPassphraseResp.Result != null)
                    {
                        return;
                    }
                }

                using (var tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        var outputs = new Dictionary <string, decimal>();
                        foreach (var item in sendRequests)
                        {
                            if (outputs.ContainsKey(item.Address))
                            {
                                outputs[item.Address] += item.Amount;
                            }
                            else
                            {
                                outputs[item.Address] = item.Amount;
                            }
                        }

                        var sendManyResp = rpcClient.Call <SendManyResponse>(JsonRPCMethods.SendMany, new SendManyParams()
                        {
                            FromAccount   = "",
                            Outputs       = outputs,
                            Confirmations = 1,
                            Comment       = string.Empty
                        });

                        if (sendManyResp.Error == null)
                        {
                            foreach (var item in sendRequests)
                            {
                                item.State = 1;
                            }

                            foreach (var item in outputs)
                            {
                                var sendTransactionDetailsPO = new SendTransactionDetailsPO()
                                {
                                    TxId    = sendManyResp.Result,
                                    Address = item.Key,
                                    Amount  = item.Value
                                };
                                context.SendTransactionDetails.Add(sendTransactionDetailsPO);
                            }

                            var transactionResp = rpcClient.Call <GetTransactionResponse>(JsonRPCMethods.GetTransaction, new GetTransactionParams()
                            {
                                TxId = sendManyResp.Result
                            });

                            var sendTransactionPO = new SendTransactionPO()
                            {
                                Amount = sendRequests.Sum(x => x.Amount),
                                TxId   = sendManyResp.Result,
                                Fee    = transactionResp.Result.Fee
                            };

                            context.SendTransactions.Add(sendTransactionPO);

                            foreach (var item in sendRequests)
                            {
                                var sendNotifyLogPO = new SendNotifyLogPO()
                                {
                                    OutRequestNo       = item.OutRequestNo,
                                    Address            = item.Address,
                                    NextNotifyTime     = DateTime.Now,
                                    NotifiedCount      = 0,
                                    NotifyResponseText = string.Empty,
                                    TxId = sendManyResp.Result
                                };
                                context.SendNotifyLogs.Add(sendNotifyLogPO);
                            }

                            context.SaveChanges();
                            tran.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        logger.Error(ex);
                    }
                }
            }
        }
Esempio n. 2
0
        public void ConfirmSend()
        {
            var sendRequests = context.SendRequests.Where(x => x.State == 0).OrderBy(x => x.CreateTime).Take(50);

            if (sendRequests != null && sendRequests.Count() > 0)
            {
                string fromAccount = string.Empty;

                if (!string.IsNullOrEmpty(rpcClient.WalletPassphrase))
                {
                    var listAccount = rpcClient.ListAccount();
                    if (listAccount == null || listAccount.Count == 0)
                    {
                        return;
                    }
                    fromAccount = listAccount[0];
                    if (!rpcClient.UnlockAccount(fromAccount, rpcClient.WalletPassphrase, duration: 60))
                    {
                        return;
                    }
                }

                using (var tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        foreach (var item in sendRequests)
                        {
                            string transactionHash = rpcClient.SendTransaction(fromAccount, item.Address, item.Amount);

                            if (!string.IsNullOrWhiteSpace(transactionHash))
                            {
                                item.State = 1;
                            }

                            var sendTransactionDetailsPO = new SendTransactionDetailsPO()
                            {
                                TxId    = transactionHash,
                                Address = item.Address,
                                Amount  = item.Amount
                            };
                            context.SendTransactionDetails.Add(sendTransactionDetailsPO);

                            var transactiondto = rpcClient.GetTransactionByHash(transactionHash);
                            if (transactiondto != null)
                            {
                                var sendTransactionPO = new SendTransactionPO()
                                {
                                    Amount = sendRequests.Sum(x => x.Amount),
                                    TxId   = transactiondto.TransactionHash,
                                    //Fee = transactiondto.GasPrice:.Fee
                                };

                                context.SendTransactions.Add(sendTransactionPO);

                                var sendNotifyLogPO = new SendNotifyLogPO()
                                {
                                    OutRequestNo       = item.OutRequestNo,
                                    Address            = item.Address,
                                    NextNotifyTime     = DateTime.Now,
                                    NotifiedCount      = 0,
                                    NotifyResponseText = string.Empty,
                                    TxId = transactiondto.TransactionHash
                                };
                                context.SendNotifyLogs.Add(sendNotifyLogPO);
                            }

                            context.SaveChanges();
                            tran.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        logger.Error(ex);
                    }
                }
            }
        }