private async Task SeedBlocksByChain(ExplorerDbContext context, Chain chain) { try { var height = await _phantasmaRpcService.GetBlockHeight.SendRequestAsync(chain.Address); using (var progress = new ProgressBar()) { for (int i = 1; i <= height; i++) { progress.Report((double)i / height); var blockDto = await _phantasmaRpcService.GetBlockByHeight.SendRequestAsync(chain.Address, i); var block = new Block { Chain = chain, ChainName = chain.Name, Hash = blockDto.Hash, PreviousHash = blockDto.PreviousHash, Timestamp = blockDto.Timestamp, Height = blockDto.Height, Payload = blockDto.Payload, Reward = blockDto.Reward, ValidatorAddress = blockDto.ValidatorAddress }; //Transactions foreach (var transactionDto in blockDto.Txs) { var transaction = new Transaction { Block = block, Hash = transactionDto.Txid, Timestamp = transactionDto.Timestamp, Script = transactionDto.Script, Result = transactionDto.Result }; bool addedToTokenList = false; //Events foreach (var eventDto in transactionDto.Events) { var domainEvent = new Domain.ValueObjects.Event { Data = eventDto.Data, EventAddress = eventDto.EventAddress, EventKind = eventDto.EventKind, Contract = eventDto.Contract }; transaction.Events.Add(domainEvent); if (!addedToTokenList) { if (domainEvent.EventKind == EventKind.TokenBurn || domainEvent.EventKind == EventKind.TokenSend || domainEvent.EventKind == EventKind.TokenStake || domainEvent.EventKind == EventKind.TokenUnstake || domainEvent.EventKind == EventKind.TokenReceive || domainEvent.EventKind == EventKind.TokenClaim || domainEvent.EventKind == EventKind.TokenMint ) { var data = Serialization.Unserialize <TokenEventData>(eventDto.Data.Decode()); var token = context.Tokens.SingleOrDefault(p => p.Symbol == data.Symbol); if (token != null) { token.Transactions.Add(transaction); addedToTokenList = true; await context.SaveChangesAsync(); } } } await SyncUtils.UpdateAccount(context, transaction, eventDto.EventAddress); } block.Transactions.Add(transaction); } chain.Height = block.Height; chain.Blocks.Add(block); } } await context.SaveChangesAsync(); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
private async Task SyncBlock(ExplorerDbContext context, Chain chain, BlockDto blockDto) { if (context.Blocks.FirstOrDefault(p => p.Hash.Equals(blockDto.Hash)) != null) { return; } Console.WriteLine($"Seeding block {blockDto.Height}"); var block = new Block { Chain = chain, ChainName = chain.Name, Hash = blockDto.Hash, PreviousHash = blockDto.PreviousHash, Timestamp = blockDto.Timestamp, Height = blockDto.Height, Payload = blockDto.Payload, Reward = blockDto.Reward, ValidatorAddress = blockDto.ValidatorAddress }; //Transactions foreach (var transactionDto in blockDto.Txs) { var transaction = new Transaction { Block = block, Hash = transactionDto.Txid, Timestamp = transactionDto.Timestamp, Script = transactionDto.Script, Result = transactionDto.Result }; //Events foreach (var eventDto in transactionDto.Events) { var domainEvent = new Event { Data = eventDto.Data, EventAddress = eventDto.EventAddress, EventKind = eventDto.EventKind, Contract = eventDto.Contract }; transaction.Events.Add(domainEvent); AddToUpdateList(eventDto.EventAddress); await SyncUtils.UpdateAccount(context, transaction, eventDto.EventAddress); } } chain.Height = block.Height; context.Update(chain); await context.SaveChangesAsync(); Console.WriteLine($"Finished syncing block {blockDto.Height}"); Console.WriteLine("****************************************"); Console.WriteLine(); }
private async Task SeedBlocksByChain(ExplorerDbContext context, Chain chain) { try { var height = await _phantasmaRpcService.GetBlockHeight.SendRequestAsync(chain.Address); using (var progress = new ProgressBar()) { for (int i = 1; i <= height; i++) { progress.Report((double)i / height); var blockDto = await _phantasmaRpcService.GetBlockByHeight.SendRequestAsync(chain.Address, i); var block = new Block { Chain = chain, ChainName = chain.Name, Hash = blockDto.Hash, PreviousHash = blockDto.PreviousHash, Timestamp = blockDto.Timestamp, Height = blockDto.Height, Payload = blockDto.Payload, Reward = blockDto.Reward, ValidatorAddress = blockDto.ValidatorAddress }; //Transactions foreach (var transactionDto in blockDto.Txs) { var transaction = new Transaction { Block = block, Hash = transactionDto.Txid, Timestamp = transactionDto.Timestamp, Script = transactionDto.Script, Result = transactionDto.Result }; bool counterIncremented = false; //Events foreach (var eventDto in transactionDto.Events) { var domainEvent = new Event { Data = eventDto.Data, EventAddress = eventDto.EventAddress, EventKind = eventDto.EventKind, }; transaction.Events.Add(domainEvent); await SyncUtils.UpdateAccount(context, transaction, eventDto.EventAddress); if (!counterIncremented) { if (TransactionUtils.IsTransferEvent(domainEvent)) { var tokenSymbol = TransactionUtils.GetTokenSymbolFromTokenEventData(domainEvent); SyncUtils.AddToTokenTxCounter(context, tokenSymbol); counterIncremented = true; } } } block.Transactions.Add(transaction); } chain.Height = block.Height; chain.Blocks.Add(block); } } await context.SaveChangesAsync(); } catch (Exception ex) { } }