Counter Webapp
Now we have the Counter smart contract and deployed it to the CLV local node. It's time to make changes to the web app so that it can interact with the Counter smart contract instance.
Preparation
Setup the browser wallet
As it's a web application, make sure you've setup a browser wallet that connects to the local node. Follow the tutorials in the Quick Start section if you have set it up yet. In this guide, we assume you connect to the local node using the MetaMask wallet.
After you have the wallet installed, import the dev account , you can import the dev account using the seed phase:
bottom drive obey lake curtain smoke basket hold race lonely fit walk
Or using just the private key:
0x03183f27e9d78698a05c24eb6732630eb17725fcf2b53ee3a6a635d6ff139680
Do not use this key or seed phase in any real chain to store your assets! It's for test only, you will lose your money if you used them in any real chain!
Install Packges
Install necessary packages before we start:
$ yarn add @web3-react/core @web3-react/injected-connector @ethersproject/providersThe @web3-react packages provide pretty good web3 utilities for react application to talk with web3 compatible blockchain. @ethersproject/providers give use the web3 providers.
Connecting to the wallet
The first step is connect our Counter DApp to the browser wallet. Firstly we need to create several files.
We make a InjectedConnector and specified the supported chain id to include the CLV chain id 1337.
hooks.js provides several hooks to help connect with the wallet.
Spinner.js implements a simple spinner component which could be used as the loading status.
Update the App.js to set its content to:
We added the Web3ReactProvider to the root of the application and include the useEagerConnect hooks in the App component. We also includes the ChainId component which will show the connected chain id and show not connected if no connection detected.
Start the application you will see the not connected in the ChainId component. It's find since we haven't implement the connection logic. But you can test it by manually connect to the web app from MetaMask, try to figure out how to do it by yourself.
Add the Connect Button
Now let's add a button to trigger the wallet connect dialog.
Edit App.js to add some imports:
And create the ConnectChain component:
and the css class for the button
The ConnectChain component simple renders a button if it's not connected, click it will trigger the web3 connection dialog.
Let's add the ConnectChain component to the App component, you could place it under the <h1> title or somewhere else as you like.
Reload the page, the Connect button will show up and you can click it to open the connect dialog. After connected to the wallet, click the button will disconnect it.
Read contract state
First install the @ethersproject/contracts package, i
We need add extra hooks to interact with the contract:
We add 3 hooks, the
useBlockNumberhook returns the latest block number from the chain (we emulate the data with 1 second refresh interval).The
useContracthook creates a smart contract instance from the json definition which was created by truffle. It will automatically detect the smart contract address on the chain.The
useContractCallDatahook calls the contract method and will keep up to date with the latest block.
Now edit App.js and update it's content:
Here we create the counter contract using the useContract hook. And then we use the useContractCallData hook to fetch the current_value state from the smart contract.
In the renderer function, we set the current value text to the value on the chain.
Save App.js and reload the webpage, you should see the current counter value on the page.
Write contract state
Now we can work on the inc/dec buttons, we'll add the onClick handler to them and call the inc/dec method correspondingly.
Besides calling the inc/decmethod, we also added a loading state to indicate we're waiting for some operation.
Save App.js and reload the webpage.
Click the Inc or Dec button, the sign transaction dialog will show up, click confirm to sign and send the transaction. The current value will be updated after a short while (~around 10 seconds).
That's it! We've implemented read/write the smart contracts in the DApp.
Conclusion
In this tutorial we completed an e2e DApp development process which includes smart contract development, deployment and setup a frontend application to interact with the smart contract.
There're several things to improve which you can do:
The smart contract should send the update when the current value changes.
Implement a real time
useBlockNumberhookThe current value was not updated in real time, improve it
The loading spinner shows when the
confirm transactiondialog open, improve it so that it will wait for the transaction completes.
As we only demo a smallest DApp development, some code/functions are not written in a performant style, you should adjust them if you want to use them in a real project.
Last updated