public async Task CheckIncomingTransactions() { var lastBlockId = await wallet.GetLastBlockId(); int lastBlockHeight = 0; try { var block = await blockService.GetBlock(BlockLocator.ByBlockId(lastBlockId)); lastBlockHeight = block.Height; Console.WriteLine($"Last known block id {lastBlockId} is on height {lastBlockHeight}"); } catch (NxtException e) { if (e.Message == "Unknown block") { Console.WriteLine($"Fork detected, unable to find block with id: {lastBlockId}. Manual rollback is needed!"); Environment.Exit(-1); } else { throw; } } var blockchainStatus = await serverInfoService.GetBlockchainStatus(); var currentBlockHeight = blockchainStatus.NumberOfBlocks - 1; var blocksToProcess = Math.Max(0, currentBlockHeight - lastBlockHeight - confirmations); Console.WriteLine($"Current block height is: {currentBlockHeight} ({blocksToProcess} block(s) to process)"); var depositAccounts = await wallet.GetAllDepositAccounts(); var depositAddresses = new HashSet <string>(depositAccounts.Select(a => a.Address)); while (currentBlockHeight > lastBlockHeight + confirmations) { lastBlockHeight++; Console.WriteLine($"Processing block @ height {lastBlockHeight}"); var block = await blockService.GetBlockIncludeTransactions(BlockLocator.ByHeight(lastBlockHeight), true); var nxtTransactions = block.Transactions.Where(t => depositAddresses.Contains(t.RecipientRs) && !t.Phased) .Union(block.ExecutedPhasedTransactions.Where(t => depositAddresses.Contains(t.RecipientRs))) .Where(t => t.Amount.Nqt > 0); foreach (var transaction in nxtTransactions) { Console.WriteLine($"Incoming {transaction.Amount.Nxt} NXT to {transaction.RecipientRs}"); var account = depositAccounts.Single(d => d.Address == transaction.RecipientRs); account.BalanceNqt += transaction.Amount.Nqt; await wallet.UpdateAccountBalance(account.Id, account.BalanceNqt); } await wallet.UpdateLastBlockId(block.BlockId); } }
public async Task <Block <ulong> > GetBlockAsync(ulong blockId) { try { var blockService = _serviceFactory.CreateBlockService(); var blockReply = await blockService.GetBlock(BlockLocator.ByBlockId(blockId)); IsOnline = true; return(blockReply); } catch (HttpRequestException) { IsOnline = false; throw; } catch (JsonReaderException e) { IsOnline = false; throw new Exception("Error when parsing response", e); } }
public async Task <BlockchainStatus> GetBlockchainStatus() { var status = new BlockchainStatus(); var blockchainStatusReply = await _serverInfoService.GetBlockchainStatus(); var lastBlock = await _blockService.GetBlock(BlockLocator.ByBlockId(blockchainStatusReply.LastBlockId)); var confirmedBlock = await _blockService.GetBlock(BlockLocator.ByHeight(blockchainStatusReply.NumberOfBlocks - 11)); var secureBlock = await _blockService.GetBlock(BlockLocator.ByHeight(blockchainStatusReply.NumberOfBlocks - 721)); status.LastKnownBlockId = blockchainStatusReply.LastBlockId.ToSigned(); status.LastKnownBlockTimestamp = lastBlock.Timestamp; status.LastConfirmedBlockId = confirmedBlock.BlockId.ToSigned(); status.LastConfirmedBlockTimestamp = confirmedBlock.Timestamp; status.LastSecureBlockId = secureBlock.BlockId.ToSigned(); status.LastSecureBlockTimestamp = secureBlock.Timestamp; return(status); }