We Are the Product

Dear Platforms of All Stripes, I visit you on your website, your mobile application and wherever else to READ content that has interested me. To Learn something interesting or grow. I get it, Ads pay…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Create and deploy an Ethereum Blockchain Explorer to GitHub Pages

Ethereum Blockchain Explorer

Use WebAssembly and dotnet to create an Ethereum Blockchain Explorer

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.

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.

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

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:

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:

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:

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:

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:

On this page we will display two information for the Address:

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:

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:

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:

Add a comment

Related posts:

The End of an Incredible Summer

On the morning of our flight out to Costa Rica, way back in May, I was a bit of a mess. I had known this day was fast approaching, but I still hadn’t quite wrapped my head around the fact that I…