How To Build A User Interface For DAO Smart Contract pt.2
This guide will focus exclusively on implementing Read Operations, which fetches data from your deployed DAO Smart Contract. By the end of this article, you’ll be able to:
Understand how to read data from your smart contract using viem.
Implement functions to fetch the total number of proposals and specific proposal details.
Integrate these READ operations into your Next.js pages to display dynamic data.
Prerequisite:
Ensure you’ve completed Part 1 of this series, where you initialized a Next.js project, set up a WalletContext
for global state management, and added a global NavBar
.
Understand READ Operations
In decentralized applications (dApps), Read Operations involve fetching data from the blockchain without altering its state. In the example DAO Smart Contract, this is crucial for displaying dynamic information such as:
Total Number of Proposals: How many proposals have been created.
Proposal Details: Information about a specific proposal, including its description, votes, and execution status.
These operations are read-only and do not require the user to sign any transactions, making them free of gas costs.
We’ll use the viem library to interact with our smart contract and perform these READ operations.
Expand walletcontext.js for Read Operations
walletcontext.js
is the central hub for managing wallet connections and interacting with your Smart Contract. We’ll add two primary READ functions:
fetchTotalProposals()
: Retrieves the total number of proposals created.fetchProposal(proposalId)
: Fetches details of a specific proposal by its ID.
Fetch Total Proposals
Functionality: This function calls the totalProposals
method in your smart contract to determine how many proposals have been created so far.
fetchTotalProposals()
uses publicClient.readContract
to call the totalProposals
function in the Smart Contract. This function returns a BigInt
representing the total number of proposals.
fetchProposal(proposalId)
calls the proposals mapping in your contract to retrieve details of a specific proposal by its ID
. It returns a struct containing the proposal's description, deadline, votes, execution status, and proposer.
Integrate Read Operations into Pages
With the read functions in place, let’s integrate them into Next.js pages to display dynamic data.
Home Page
Update the index.js
page to show the total number of proposals created in your DAO on the home page.
Here we set the totalProposals
state variable to store the fetched total number of proposals.
The ConnectButton
implements the MetaMask authentication. The useWallet hook parse the function from WalletContext.
The useEffect
Hook is applied on component mount, fetchTotalProposals()
is then called to retrieve the total number of proposals from the Smart Contract.
The page displays a loading message until totalProposals is fetched. Once fetched, it displays the total number of proposals. Users will have to click the ConnectButton to connect their wallets for WRITE operations. See part 3.
Fetch-Proposal Page
This page allow users to input a proposal ID, fetch its details, and display them. Additionally, on the page users are provided options for voting on or executing the proposal.
Implementation:
The following React states are implemented
proposalId
: Stores the user-inputted proposal ID.proposalData
: Stores the fetched proposal details.error
: Captures any errors during fetch, vote, or execute operations.
The handleSubmit
function is used to validate the input and connection status. It then calls the fetchProposal(proposalId)
to retrieve proposal details.
We use a Form element for users to input a proposal ID and fetch its details. The Error Display is implemented to show any errors that occur during operations. The Proposal Details displays the fetched proposal information in a styled card.
The card contains Vote and Execute buttons for users to vote YES/NO or execute the proposal if eligible.
Edge Cases and Errors
For better UX, consider adding loading indicators while fetching data or awaiting transaction confirmations.
Example:
Test Read Operations
Populate Some Data
Before testing read operations, make sure there are some proposals created:
Load your Smart Contract on the Remix IDE.
Deposit 0.001 ETH to gain voting power.
Create one or more proposals via the Create Proposal page.
Verify Read Operations
Run your application using the command:
Your application will be running on localhost:3000
in your web browser. Check for the following in the User Interface:
Total Proposals: On the Home page, verify that the total number of proposals matches the number you’ve created via Remix IDE.
Fetch Proposal Details: - Navigate to the Fetch-Proposal page. - Input a valid proposalId (e.g., 0 for the first proposal). - Verify that all proposal details are accurately displayed.
Monitor the browser console for any errors or logs that can help in debugging.
Conclusion and Next Steps
In Part 2, you successfully implemented Read Operations in your DAO front end:
fetchTotalProposals()
: Displayed the total number of proposals on the Home page.fetchProposal(proposalId): Retrieved and displayed specific proposal details on the Fetch-Proposal page.
What's Next?
Stay tuned for Part 3 of this series, where we’ll dive into building UI Components—crafting forms, buttons, and enhancing event handling to create a more polished and user-friendly interface for your DAO dApp.
Congratulations! You’ve now built a robust foundation for reading data from your DAO smart contract within your Next.js front end. Keep experimenting and enhancing your dApp’s capabilities in the upcoming sections!
Last updated