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.
MetaMask is the most commonly used browser wallet for Ethereum like blockchain networks. We can use MetaMask to connect to CLV network since CLV is fully compatible with Ethereum.
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
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!
The @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 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.
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.
<ConnectChaintriedEager={triedEager} />
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
$yarnadd@ethersproject/contracts
@ethersproject/contracts is a sub module of the ethers project.It is creating (at run-time) an object which interacts with an on-chain contract as a native JavaScript object.
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 useContract hook 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 useContractCallData hook calls the contract method and will keep up to date with the latest block.
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.
The source code of this chapter could be found at the revision fc93d686aabin the counter-dapp source repo.
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 useBlockNumber hook
The current value was not updated in real time, improve it
The loading spinner shows when the confirm transaction dialog 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.