DEV Community

CoinMonks
CoinMonks

Posted on • Originally published at Medium on

Create and deploy an Ethereum Blockchain Explorer to GitHub Pages

Ethereum Blockchain Explorer

Use WebAssembly and dotnet to create an Ethereum Blockchain Explorer

Money Coin Finance — Free photo on Pixabay

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 GetBlockInfomethod 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 GetBalancemethod 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

Get Best Software Deals Directly In Your Inbox


Top comments (0)