Welcome to this comprehensive guide where we'll build a Web3 Decentralized Charity Platform using Next.js, Solidity, and TypeScript. By the end of this tutorial, you'll have a clear understanding of:
- Building dynamic interfaces with Next.js
- Crafting Ethereum smart contracts with Solidity
- Incorporating static type checking using TypeScript
- Deploying and interacting with your smart contracts
- Understanding the fundamentals of blockchain-based charity platforms
As a bonus for partaking in this tutorial, you can easily win a copy of our prestigious book on becoming an in-demand solidity developer. This offer is free for the first 300 people, for instructions on how to win watch the short video below.
You will need the following tools installed to build along with me:
- Git Bash
- Redux Toolkit
- Tailwind CSS
To set up MetaMask for this tutorial, please watch the instructional video below:
Once you have completed the setup, you are eligible to receive a free copy of our book. To claim your book, please fill out the form to submit your proof-of-work.
Watch the following instructional videos to receive up to 3-months of free premium courses on
Dapp Mentors Academy, including:
Start your journey with Bitfinity today. Experience a fast, simple, and hassle-free development process as you build your first Charity Donation Tracking dApp. Deploy your smart contracts to the Bitfinity network and make a positive impact.
With that said, let’s jump into the tutorial and set up our project.
We'll start by cloning a prepared frontend repository and setting up the environment variables. Run the following commands:
git clone https://github.com/Daltonic/dappFundsX
git checkout no_redux_no_blockchain
Next, create a
.env file at the root of the project and include the following keys:
<WALLET_CONNECT_PROJECT_ID> with your respective project IDs.
yarn dev to start the project.
Our frontend for this project is solid and ready for some smart contract integration, but we will need Reduxify our application to enable a shared data space.
Building the Redux Store
The above image represents the structure of our Redux store, it will be simple since we are not creating some overly complex project.
We'll set up Redux to manage our application's global state. Follow these steps:
- Create a
storefolder at the project root.
store, create two folders:
states, create a
actions, create a
- Create a
globalSlices.tsfile inside the
- Create an
index.tsfile inside the
- Update the
pages/_app.tsfile with the Redux provider.
Our application has been wrapped with Redux toolkit, and we will revisit Redux when integrating the backend with the frontend.
Smart Contract Development
Next, we'll develop the smart contract for our platform:
- Create a
contractsfolder at the project root.
contracts, create a
DappFund.solfile and add the contract code below.
DappFund contract will facilitate the creation, update, and donation to charities, as well as administrative functions like changing the charity tax and banning a charity.
Here is a function-by-function breakdown:
constructor(): This function sets the initial charity tax when the contract is deployed. It's executed only once during the contract's deployment.
createCharity(): This function allows users to create a new charity. It requires several parameters such as the name, description, image, profile, and amount of the charity. It checks that these parameters are valid and then creates a new
CharityStructand adds it to the
updateCharity(): This function allows the owner of a charity to update its details. It checks that the charity exists and that the sender is the owner of the charity before updating the charity's details.
deleteCharity(): This function allows the owner of a charity to mark it as deleted. It checks that the charity exists and that the sender is the owner of the charity before marking it as deleted.
toggleBan(): This function allows the contract owner to ban or unban a charity. It checks that the charity exists before toggling its banned status.
donate(): This function allows users to donate to a charity. It checks that the charity exists, is not banned, and has not yet reached its fundraising goal. It then increments the total donation count, creates a new
SupportStruct, and adds it to the
supportersOfmapping. It also updates the raised amount and donation count of the charity.
changeTax(): This function allows the contract owner to change the charity tax. It checks that the new tax percentage is valid before updating the tax.
getCharity(): This function allows anyone to get the details of a charity. It returns the
CharityStructassociated with the given ID.
getCharities(): This function allows anyone to get the details of all active charities. It returns an array of
getMyCharities(): This function allows a user to get the details of all their active charities. It returns an array of
getSupports(): This function allows anyone to get the details of all supporters of a specific charity. It returns an array of
payTo(): This internal function is used to transfer funds. It sends the specified amount of Ether to the given address.
currentTime(): This internal function returns the current time in seconds since the Unix Epoch. It's used to timestamp donations and charity creations.
Contract Deployment and Seeding
Now, let's deploy our smart contract and populate it with some dummy data:
- Create a
scriptsfolder at the project root.
scripts, create a
seed.jsfile and add the following codes.
Run the following commands to deploy the contract and seed it with data:
yarn hardhat node # Run in terminal 1 yarn hardhat run scripts/deploy.js # Run in terminal 2 yarn hardhat run scripts/seed.js # Run in terminal 2
If you did that correctly, you should see a similar output like the one below:
At this point we can start the integration of our smart contract to our frontend.
First, create a
services folder at the project root, and inside it, create a
blockchain.tsx file. This file will contain functions to interact with our smart contract.
The provided code is a TypeScript implementation of our blockchain service for interacting with our smart contract. This service enables users to perform actions on our charity platform, including creating and updating charities, making donations, and more.
Here is a function-by-function breakdown:
getEthereumContracts(): This function gets the Ethereum contract instances that the service will interact with. It checks if the user has a connected Ethereum account and uses that to interact with the contract, otherwise, it uses a randomly created wallet.
getAdmin(): This function returns the owner of the contract.
getCharity(): These functions fetch all charities, the charities created by the current user, and a specific charity by its ID, respectively.
getSupporters(): This function fetches the supporters of a specific charity by its ID.
updateCharity(): These functions allow a user to create a new charity or update an existing charity, respectively.
makeDonation(): This function allows a user to make a donation to a specific charity.
deleteCharity(): This function allows the owner of a charity to delete it.
banCharity(): This function allows the contract owner to ban a charity.
structuredSupporters(): These are helper functions that structure the returned data from the contract into a more usable format.
provider.tsx file inside
services to include the
bitfinity network using the following codes.
Page Interacting with Smart Contract
Next, we'll link the functions in the blockchain service to their respective interfaces in the frontend:
No 1: Displaying all Charities
pages/index.tsx to get data from the
Notice how Redux is used to store blockchain data before it is displayed on the screen.
No 2: Displaying User’s Charities
pages/projects.tsx to use the
useEffect() hook to get the current user’s charities.
Please note the use of the
useEffect() hook to retrieve the current user's charities and how Redux is used to handle and display data from the blockchain across multiple components. This will be a repeated pattern across the pages and components.
No 3: Creating New Charities
pages/donations/create.tsx to use the
createCharity() function for form submission.
No 4: Displaying Single Charity
pages/donations/[id].tsx to use the
getSupporters() functions to retrieve charity and supporters by Id.
No 5: Editing Existing Charities
pages/donations/edit/[id].tsx to use the
getCharity() function to retrieve charity by Id and populate the form fields.
Did you see how the
getCharity() function was used to retrieve charity by Id and also how it was used to populate the form fields?
Components with Smart Contract
Like we did with the pages above, let’s Update the following components to interact with the smart contract:
No 1: Handling Charity Banning
components/Ban.tsx to use the
handleBanning() function to call the
No 2: Handling Charity Deletion
components/Delete.tsx file to use the
deleteCharity() functions to execute a charity unlisting procedure.
No 3: Making Donations to Charities
components/Donor.tsx file to use the
handleSubmit() function to send the payment to the smart contract using the
Here are the rest of the components you should also update because the integration of Redux in them.
The NavBtn Component Review and update the component that enables users to navigate, delete, and edit charity information. Utilize Redux to trigger the delete modal upon clicking.
The Payment Component Review and update the implementation of Redux to trigger the donation, supporters, and ban modals upon clicking their respective buttons.
The Supports Component Review and update the implementation of Redux to close the supporters modal when the close button is clicked.
By implementing these updates, all components and pages are now connected to the smart contract, marking the completion of the project.
If your Nextjs server was down all this while, you can spin it up again by running
For further learning, we recommend watching the full video of this build on our YouTube channel.
In this tutorial, we've built a Decentralized Charity Platform using Next.js, Solidity, and TypeScript. We set up the development environment, built the Redux store, and deployed the smart contract to the blockchain. By integrating the smart contract with the frontend, we created a seamless user experience.
Throughout the tutorial, you gained valuable skills in building Web3 applications, crafting smart contracts, and incorporating static type checking.
Now, you're ready to create your own Decentralized Charity Platform. Happy coding and unleash your innovation in the world of Web3!
The Opt Out Writing Contest is sponsored by Āut - a collective building decentralized standards for self-sovereign Identity (SSID, IAM), Reputation and Communities (DAOs, Hacktivists, Network States, etc.). A laboratory of experimentation, pushing the boundaries imposed by the status quo. Our protocols are designed to implement autonomy and personal freedom at a systemic level - in an effort to accelerate the transition from hierarchical power structures, to self-organizing forms of coordination. Opt out of the system that doesn’t represent you!