Create and deploy an Ethereum Blockchain Explorer to GitHub Pages
Tags: Blockchain, Dotnet, Cryptocurrency, Crypto, Ethereum

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:

EthereumService.cs

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:

Program.cs

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.

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.

Index.razor

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() :

Index.razor

When we finish to get the latest block number, we can now display it in the page:

Index.razor

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:

Index.razor

This created LatestBlocksAndTransactions component will now get the data from the last 5 blocks:

LatestBlocksAndTransactions.razor

and then render it on the page:

LatestBlocksAndTransactions.razor

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 :

Block.razor

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:

Block.razor

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:

GetTransactionsByAccount

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:

main.yml

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