Polygon: Make a Donation App with IPFS and Polygon
Learn how to create a Web3 donation app using IPFS and Polygon.
What is Polygon?
Polygon is a layer two blockchain network that is designed as a sidechain solution for scaling alongside the Ethereum blockchain network.
Read below to learn how to create a Web3 donation app using IPFS and Polygon.
1. Begin by installing the Truffle NPM package with the following command:
npm install -g truffle
2. Then, create a NextJS app with the commands:
npm install next
npx create-next-app --typescript
When prompted, provide a name for your app. This tutorial uses the name ‘polygon-dapp’.
3. Navigate into the newly created application directory and initialize the project:
cd polygon-dapp
truffle init
The ‘init’ command will create the following files and directories:
contracts/:
Used to store the project’s smart contracts.migrations/:
Used to store the Truffle scripts for deploying the smart contracts.test/:
Used to store the project’s test scripts for validating the smart contracts.truffle-config.js:
The Truffle configuration setting file.
4. Navigate into the contracts
directory and create a new file called DonationContract.sol
.
contracts
directory and create a new file called DonationContract.sol
.In the file, insert the following code:
This smart contract handles the donation process when users interact with our application. Save this file, then compile it with the command:
truffle compile
You should receive output that resembles the following:
5. Navigate to the migrations
directory, then create a new file called 1_initial_migration.js
.
migrations
directory, then create a new file called 1_initial_migration.js
.In this file, insert the following code:
6. Save this file. Then, navigate back to the contracts
directory and create a new file called Migrations.sol
.
contracts
directory and create a new file called Migrations.sol
.Insert the following code into this file:
7. In the migrations
directory, create another new file called 2_donation_contract_migration.js
.
migrations
directory, create another new file called 2_donation_contract_migration.js
.In this file, insert the following code:
8. Next, install Ganache.
You can install either the GUI application or the CLI tool. We’ll be using the GUI application. Once downloaded, launch the GUI app, then select ‘Quickstart Ethereum’ when prompted to create a workspace.
9. Then select ‘Save’ to start your Ganache development blockchain environment.
10. Next, open the truffle-config.js
file and replace its existing contents with the following:
truffle-config.js
file and replace its existing contents with the following:11. Now it’s time to deploy our Truffle configuration to the Ganache development network with the command:
truffle migrate
The output should resemble the following:
12. Before moving on with our app, we’ll run a few test scripts. In the tests
directory, create a new file called donation-contract-tests.js
that contains the following code:
tests
directory, create a new file called donation-contract-tests.js
that contains the following code:13. Next, install the chai
package, which is Truffle’s test script package used in the script above:
chai
package, which is Truffle’s test script package used in the script above:npm install chai chai-as-promised
14. Then, run the test script with the command:
truffle test
Your output should look like this:
15. In the root directory of your project, create a new directory called contexts
, then create a new file called DataContext.tsx
.
contexts
, then create a new file called DataContext.tsx
.In this file, insert the following content:
This script uses the package web3, so let’s install it with the command:
npm install web3
16. Next, open the pages/_app.tsx
file and insert the following code:
pages/_app.tsx
file and insert the following code:17. For our app’s user interface, we’ll be using the TailwindCSS package. Install it, along with HeadlessUI, with the command:
npm install tailwindcss @headlessui/react
18. Open the pages/index.tsx
file and replace the existing code with the following content:
pages/index.tsx
file and replace the existing code with the following content:19. There are three components referenced in this code that don’t exist yet - Body.tsx
, UploadImage.tsx
, and Header.tsx
.
Body.tsx
, UploadImage.tsx
, and Header.tsx
.Create a new directory called components
, then create a new file for each of these in the components directory. Then, insert the following code into each:
Body.tsx:
Header.tsx:
UploadImage.tsx:
20. Now, to use the IPFS client, let’s install the IPFS HTTP client package:
npm install ipfs-http-client
21. Lastly, before we can publish this contract, we need to set up authentication through our cryptowallet.
First, make sure that your wallet is connected to the Polygon Mumtai testnet. For instructions on how to do this, check out this guide here.
22. Next, create a new file called .secret
in the root of your project.
.secret
in the root of your project.In this file, insert your cryptowallet’s Mnemonic phrase or Secret Recovery Phrase. Please note that this phrase should NEVER be shared with anyone, and should be placed in your .gitignore
file if you plan to publish this project to GitHub.
23. For submitted transactions, you will need some testnet MATIC, which you can get from the Polygon Matic faucet.
24. To cover gas fees, we’ll use the hdwallet-provider
package, which can be installed with the command:
hdwallet-provider
package, which can be installed with the command:npm install @truffle/hdwallet-provider
25. Now that we have these configuration details, update your truffle-config.js
file with the following:
truffle-config.js
file with the following:26. Now it’s time to deploy your donation app’s smart contract to the Polygon Mumbai network with the command:
truffle migrate --network matic
A successful deployment will produce the following output:
27. Then, start the web app with the command:
npm run build
npm start
28. The app will be running at localhost:3000
. It will look something like this:
localhost:3000
. It will look something like this:29. Select ‘Upload Image’ to upload an image to the application.
30. Select a file from your local computer, provide an optional description, then select ‘Upload’.
Last updated