Пример #1
0
        public void SyncTransaction(int blockCount)
        {
            var transactions       = new List <TransactionPO>();
            var transactionDetails = new List <TransactionDetailsPO>();
            var receiveNotifyLogs  = new List <ReceiveNotifyLogPO>();
            var blocks             = context.Blocks.Where(x => x.State == 0).OrderBy(x => x.Height).Take(blockCount);

            foreach (var block in blocks)
            {
                var blockResp = rpcClient.Call <GetBlockResponse>(JsonRPCMethods.GetBlock, new GetBlockParams()
                {
                    HeaderHash = block.Hash
                });
                foreach (var txid in blockResp.Result.Tx)
                {
                    var transactionResp = rpcClient.Call <GetTransactionResponse>(JsonRPCMethods.GetTransaction, new GetTransactionParams()
                    {
                        TxId = txid
                    });
                    if (transactionResp.Error == null)
                    {
                        var tranPO = new TransactionPO()
                        {
                            BlockHash     = block.Hash,
                            TxId          = transactionResp.Result.TxId,
                            Confirmations = transactionResp.Result.Confirmations,
                            State         = transactionResp.Result.Confirmations >= 2 ? 1 : 0
                        };
                        foreach (var item in transactionResp.Result.Details)
                        {
                            var td = new TransactionDetailsPO()
                            {
                                Address  = item.Address,
                                Amount   = item.Amount,
                                Category = item.Category,
                                TxId     = txid
                            };
                            transactionDetails.Add(td);

                            if (transactionResp.Result.Confirmations >= 2 && item.Category == "receive")
                            {
                                receiveNotifyLogs.Add(new ReceiveNotifyLogPO()
                                {
                                    Address            = item.Address,
                                    Amount             = item.Amount,
                                    NextNotifyTime     = DateTime.Now,
                                    NotifiedCount      = 0,
                                    NotifyResponseText = string.Empty,
                                    TxId = txid
                                });
                            }
                        }
                        transactions.Add(tranPO);
                    }
                }
            }

            using (var tran = context.Database.BeginTransaction())
            {
                try
                {
                    foreach (var block in blocks)
                    {
                        block.State = 1;
                    }
                    foreach (var item in transactions)
                    {
                        context.Transactions.Add(item);
                    }

                    foreach (var item in transactionDetails)
                    {
                        context.TransactionDetails.Add(item);
                    }

                    foreach (var item in receiveNotifyLogs)
                    {
                        context.ReceiveNotifyLogs.Add(item);
                    }

                    context.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    logger.Error(ex);
                }
            }
        }
Пример #2
0
        public void SyncTransaction(int blockCount)
        {
            var transactions       = new List <TransactionPO>();
            var transactionDetails = new List <TransactionDetailsPO>();
            var receiveNotifyLogs  = new List <ReceiveNotifyLogPO>();
            var blocks             = context.Blocks.Where(x => x.State == 0).OrderBy(x => x.Height).Take(blockCount);

            foreach (var block in blocks)
            {
                var blockdto = rpcClient.GetBlockByHash(block.Hash);

                if (blockdto != null &&
                    blockdto.Transactions != null &&
                    blockdto.Transactions.Count > 0)
                {
                    foreach (var hash in blockdto.Transactions)
                    {
                        var transactionDto = rpcClient.GetTransactionByHash(hash);

                        if (transactionDto != null)
                        {
                            var     transactionReceipt = rpcClient.GetTransactionReceipt(hash);
                            decimal transactionAmount  = transactionDto.Value.HexToEther();

                            var tranPO = new TransactionPO()
                            {
                                BlockHash     = block.Hash,
                                TxId          = transactionDto.TransactionHash,
                                Confirmations = transactionReceipt != null ? 1 : 0,
                                State         = transactionReceipt != null ? 1 : 0
                            };

                            var td = new TransactionDetailsPO()
                            {
                                Address  = transactionDto.From,
                                Amount   = transactionAmount,
                                Category = "receive",
                                TxId     = transactionDto.TransactionHash
                            };
                            transactionDetails.Add(td);

                            if (transactionReceipt != null)
                            {
                                receiveNotifyLogs.Add(new ReceiveNotifyLogPO()
                                {
                                    Address            = transactionDto.From,
                                    Amount             = transactionAmount,
                                    NextNotifyTime     = DateTime.Now,
                                    NotifiedCount      = 0,
                                    NotifyResponseText = string.Empty,
                                    TxId = transactionDto.TransactionHash
                                });
                            }

                            transactions.Add(tranPO);
                        }
                    }
                }
            }

            using (var tran = context.Database.BeginTransaction())
            {
                try
                {
                    foreach (var block in blocks)
                    {
                        block.State = 1;
                    }
                    foreach (var item in transactions)
                    {
                        context.Transactions.Add(item);
                    }

                    foreach (var item in transactionDetails)
                    {
                        context.TransactionDetails.Add(item);
                    }

                    foreach (var item in receiveNotifyLogs)
                    {
                        context.ReceiveNotifyLogs.Add(item);
                    }

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