January 26, 2026

Capitalizations Index – B ∞/21M

Ethereum DApp with Ethers.js and IPFS using Angular, Angular Material and NgRx. Part I

Ethereum DApp with Ethers.js and IPFS using Angular, Angular Material and NgRx. Part I

We can also verify that the smart contract has been deployed successfully by proceeding to the Etherscan to examine our Ethereum Wallet account which we specified in the the HDWalletProvider constructor.

A quick way to test functionality of the smart contract is by using the Truffle console.

truffle console --network ropsten

To get access to the deployed contract instance run the following commands:

> let contr = await FleaMarket.deployed()
 > contr.address //‘0x4d0b57A8226a9A1512C22DCe6D643B881853aA43’

Next, let’s create a few instances of the child asset contract:

> let itemOne = await contr.createPurchaseContract(‘ITEM00001’, ‘Sport Bike’, ‘0xfile887’)
 > let itemTwo = await contr.createPurchaseContract(‘ITEM00002’, ‘Old Hummer’, ‘0xfile556’)
 > contr.getKeyCount() //2

Finally, invoke the contract function to retrieve a corresponding asset contract address:

> contr.getElementByKey(‘ITEM00001') //‘0xc671DF84F230aa51B076A8cdAa3619Dd3c022558’

Setting up the Angular Project

Let’s get started by installing the latest major versions of the Angular CLI.

npm install -g @angular/cli

To create a new Angular project, open a new terminal in the root folder of our Truffle project and run the following CLI command:

ng new ClientApp --skip-install=true --minimal=true --style=css --routing=true --skipGit=true

Next we install the Angular Material by running the following schematic

ng add @angular/material

Let’s add a navigation component to the application by running the navigation schematic

ng generate @angular/material:nav nav

We can also add a dashboard component using the material schematic

ng generate @angular/material:dashboard dashboard

Next we install the Flex-layout module which is a superior layout engine to assist with the CSS flex-box features:

npm install @angular/flex-layout --save

The next thing is to wire up the @NgRx state management library that lays in the core of our application design.

npm install @ngrx/store, @ngrx/effects, @ngrx/router-store, @ngrx/entity, @ngrx/store-devtools, ngrx-store-freeze --save

To interact with Ethereum blockchain we will use the Ethers.js library developed my RicMoo (Richard Moore)

npm install ethers --save

The Root State and the Guard

The application state in NgRx is a single immutable data tree structure. The state tree will start from the root state once the AppModule is loaded. The root state is shared globally among all components. It will then continue to grow in size as more lazily-loaded Angular modules are loaded into the application. We define our root state interface as follows:

It is composed from the router state, the spinner state, the error state and the web3Provider state.

The spinner state is responsible for toggling the display of a loading indicator defined in the loader component. We manage the spinner state by dispatching the following actions to the store:

Notice that we’re using the Action Creator that was recently introduced by Alex Okrushko in his super blog post.

The error state is listening for the action:

and it is handled by the side effect:

On the side note, there was some very interesting discussion recently about how to properly manage loading or error state in NgRx. Regarding this topic please check this and this.

The web3Provider root state is where all the fun begins! It has the following properties

and is responsible for monitoring the gateway between our DApp and the Ethereum blockchain.

The approach we use closely resembles the idea discussed in the blog written by GrandSchtroumpf. To be able to communicate with the Ethereum network using MetaMask we need to setup the corresponding ethers.js Web3 provider.

First we define the tree-shakable InjectionToken MetamaskWeb3Provider and instruct it to inject the Ethereum web3 provider injected by MetaMask windows.ethereum. Then, we extend the ethers.js Web3Provider provider and inject the MetamaskWeb3Provider token into its constructor. To get access to the user account on blockchain, MetaMask requires that we call enable() method on its native web3 provider. We manage it in the metaMaskEnable$ effect by listening for the '[Web3/Provider] Init]action.

Here is what is happening behind the scenes. We introduced the EthInitGuard guard which we built to control the route resolution process.

The guard is observing the metamaskEnable property of the state. If this value is false, the guard will dispatch the '[Web3/Provider] Init]action to broadcast the request to connect to the Ethereum blockchain. The biggest benefit of this approach is that we are now in control of the MetaMask’s popup window for user-approved account access.

Once we compile and run the application, it will load our root state and result in the following home page:

If we try to navigate to any other route guarded by the EthInitGuard, it will trigger an approval popup from MetaMask asking permission to access the user account.

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
Icons made by
photo3idea_studio from www.flaticon.com is licensed by CC 3.0

🐯 Thank you! Please stay tuned for another articles for this blog series.

Reference:

Please Follow me on Medium , Twitter or LinkedIn.

👍 Special thanks to Mike Gibbons for reviewing this article.

Published at Sun, 21 Apr 2019 23:00:12 +0000

Previous Article

Bitcoin (BTC) May Have Bottomed, But Crypto Could Still See A “Black Swan” Event

Next Article

Blockchain Research Institute Chairman Says Chinese Renminbi Will Become Crypto in 20 Years

You might be interested in …

Webcoin – Oferuj swoje usługi z użyciem systemu blockchain!

YouTube: blockchain Webcoin – Oferuj swoje usługi z użyciem systemu blockchain! https://webcoin.today – Webcoin pioneers the technological dawn of global marketing digitalization. ✅Whitepaper: https://webcoin.today/docs/Whitepaper.pdf ✅Github: https://github.com/webcoi… more info… YouTube: ethereum litecoin increasing in value – […]

Judging at the in crypto we trust hackathon at decentral toronto

Judging at the IN CRYPTO WE TRUST HACKATHON at DECENTRAL Toronto

Judging at the IN CRYPTO WE TRUST HACKATHON at DECENTRAL Toronto— Commercial Photo Rights held exclusively by Jan Miranda Photography. Also licensed for sharing under Creative Commons Attribution-NonCommercial 4.0 International License. For commercial use inquiries […]