Blockchain Explorers are like search engines for Cryptocurrencies and the Blockchain. It allows users to access information about transactions, blocks and addresses. They are very powerful and are used daily by users.
The most popular Blockchain Explorer for Ethereum is Etherscan where we get real time analytics on Ethereum.
Ethereum (ETH) Blockchain Explorer (etherscan.io)
In this article we will build and deploy a Blockchain Explorer in order to get information about the Ethereum Blockchain. We will build a client-side application that runs directly in the browser via WebAssembly. In order to do that, we are going to use Blazor WebAssembly. We will then automate the deployment of our application to GitHub Pages with GitHub Actions.
Building the Blockchain Explorer
First, we need to create our Blazor WebAssembly application. In order to do that, run the following cli command:
dotnet new blazorwasm -o Your-Project-Name
Now, we add the library that will allow us to get data from Ethereum, the Nethereum. It is an open source .NET integration library for blockchain. To add the library to your project:
dotnet add package Nethereum.Web3 --version 3.8.0
We will create a service class to encapsulate all the logic of interacting with Ethereum. This class will be called EthereumService
and will receive as construction injection the IWeb3
interface:
In order to obtain the implementation of the IWeb3 interface in runtime, we will need to add it to the Dependency Injection container. We will also add the EthereumService
class to the DI container so the pages we will create can get data from the Blockchain using this class.
In the Program.cs
class:
Note that when we instantiate the Web3 class, we need the URL from the Infura mainnet with your API Key. If you don’t have one, you can go to Infura website and create an account.
Infura interacts with the Ethereum blockchain and runs nodes on behalf of its users.
Creating our First page
Now we will create our first page, the index.razor
. We will inject two services to this page, the IEthereumService
in order to get data from Ethereum and NavigationManager
in order to navigate to others pages.
On this page we will want to display the following information:
- The latest block number
- The latest blocks
- The latest transactions
To get the latest block number when we the page renders, we will override the method OnInitializedAsync()
:
When we finish to get the latest block number, we can now display it in the page:
The method in EthereumService
class that gets the latest block number:
Get Latest Blocks and Transactions
Blazor applications are built using components. Components include a self-contained piece of the user interface and the logic necessary to get the data or respond to UI events.
We will create a razor component (LatestBlocksAndTransactions.razor
) to encapsulate the User Interface that is responsible of getting the latest 5 Ethereum blocks and the 5 latest approved transactions.
This component will receive as parameter the latest block number. The Index.razor
will render this component and pass this parameter by declaring them using the HTML element syntax:
This created LatestBlocksAndTransactions
component will now get the data from the last 5 blocks:
and then render it on the page:
Block Page
This page will contain information about a block. We will receive as a parameter for this page the BlockNumber
. With this information, we can search for the desired block and its transactions by overriding the OnInitializedAsync()
method and calling the GetBlockInfo
method from IEthereumService
:
The field _hasLoaded
was created because when the page renders there is still no information about the block and the transactions that it contains. We will only display the block’s information when we get the data from Ethereum. So after we successfully get the data, we set the _hasLoaded
to true and we can display this information as seen below:
Address Page
On this page we will display two information for the Address:
- The Balance in Ether
- The transactions From/To this Address on the last 10 blocks
In order to get the balance for an address, we call the GetBalance
method from the Web3
class:
To get the transactions From/To the Address, we will iterate over each block and verify the transactions. To keep things simple, we will iterate over the last 10 blocks and verify among the transactions if the Address was the source or the destination:
Deploy to GitHub Pages with GitHub Actions
Since the publish output of a Blazor WebAssembly project are static files, we can deploy the application to static site hosts such as Azure Static Web Apps and GitHub Pages. We will automate the deployment of our Blockchain Explorer to GitHub Pages with GitHub Actions.
In order to create a GitHub Actions, go to your repository, navigate to your Actions tab then click on the link “set up a workflow yourself”. GitHub will display a template of a YAML file with instructions on how to build and deploy the application.
For our Blazor WebAssembly application, the created workflow YAML file to deploy our Blockchain Explorer can be found below:
There is a great article by Niels Swimberghe about how to deploy ASP.NET Blazor WebAssembly to GitHub Pages. It has a video and a step by step guide on how to deploy a Blazor WebAssembly application to GitHub Pages and how to solve the problems when doing so. The link can be found on the references section below.
Conclusion
In this article we have created an Ethereum Blockchain Explorer using the new Blazor WebAssembly framework. We also have published this application to GitHub Pages using GitHub Actions.
The source code for this application and the URL for the deployed app can be found below:
strykerin/Ethereum-Blockchain-Explorer (github.com)
EthereumBlockchainExplorer (strykerin.github.io)
References
strykerin/Ethereum-Blockchain-Explorer (github.com)
EthereumBlockchainExplorer (strykerin.github.io)
Blockchain Explorer Tutorial — What Is A Blockchain Explorer? (softwaretestinghelp.com)
What is Blazor WebAssembly? — Learn | Microsoft Docs
What Is Infura? | The Beginner’s Guide — Decrypt
Ethereum API | IPFS API Gateway | ETH Nodes as a Service | Infura
Create and use ASP.NET Core Razor components | Microsoft Docs
How to deploy ASP.NET Blazor WebAssembly to GitHub Pages (swimburger.net)
Also, Read
- Learn about Ethereum and Web3
- Crypto Trading Bots
- 3Commas Review
- AAX Exchange Review | Referral Code, Trading Fee, Pros and Cons
- Deribit Review | Options, Fees, APIs and Testnet
- FTX Crypto Exchange Review
- NGRAVE ZERO review
- Bybit Exchange Review
- 3Commas vs Cryptohopper
- The Best Bitcoin Hardware wallet
- Crypto Copy Trading Platforms
- Best monero wallet
- ledger nano s vs x
- Bitsgap vs 3Commas vs Quadency
- The Best Crypto Tax Software
- Best Crypto Trading Platforms
- Best Crypto Lending Platforms
- Ledger Nano S vs Trezor one vs Trezor T vs Ledger Nano X
- BlockFi vs Celsius vs Hodlnaut
- Bitsgap review — A Crypto Trading Bot That Makes Easy Money
- Quadency Review- A Crypto Trading Bot Made For Professionals
- PrimeXBT Review | Leverage Trading, Fee and Covesting
- Altrady review
- Ellipal Titan Review
- SecuX Stone Review
- BlockFi Review | Earn up to 8.6% interests on your Crypto
- Coinrule review
- Best Blockchain Analysis Tools
- Crypto arbitrage guide: How to make money as a beginner
- Best Crypto Charting Tool
- What are the best books to learn about Bitcoin?