Polygon: Building an App NFT With Polygon
Learn how to build an app NFT with Polygon and IPFS.
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 build an app NFT with Polygon and IPFS.
1. Start by creating a new directory for your NFT app, then navigating into that directory with the commands:
mkdir nft-app-contract && cd nft-app-contract
2. Next, initialize NPM and install HardHat with the commands:
npm init
npm install --save-dev hardhat
npx hardhat
3. Install the OpenZeppelin smart contracts package:
npm install @openzeppelin/contracts
4. In the contracts folder, there will be a default contract that was created. Rename this file dAppNFT.sol
, then open the file. Replace the existing content with the following:
dAppNFT.sol
, then open the file. Replace the existing content with the following:5. Save this file.
Then in the root directory there will be another folder called test. Rename the existing file in the test folder as dAppNFT.js
. Replace everything in that file with the following:
6. Then, compile the project with the command:
npx hardhat test
7. Next, we’ll need to create the front end of our application. To do this, run the following command:
npx create-react-app app-nft-frontend
8. To configure IPFS, first we need to open the package.json
file. In this file, add the following key/value pair:
package.json
file. In this file, add the following key/value pair:"homepage": "./"
9. Next, install the AWS CLI tool.
10. Then, configure AWS CLI to use your Filebase account. To do this, open a new terminal window. From there, run the command:
aws configure
This command will generate a series of prompts, which should be filled out as such:
Access Key ID: Filebase Access Key
Secret Access Key: Filebase Secret Key
Region: us-east-1
11. Open the package.json
file again. In the scripts section, add a new script called deploy:
package.json
file again. In the scripts section, add a new script called deploy:"deploy": "npm run build && sh ./upload.sh"
This line includes a script called upload.sh
that we haven’t created yet. We’ll create that next.
12. In the root of the project create the file upload.sh
. Open this new file in a text editor, then add the following line:
upload.sh
. Open this new file in a text editor, then add the following line:aws --endpoint https://s3.filebase.com s3 sync ./build s3://bucket-name
Replace bucket-name
with your Filebase IPFS bucket name.
13. Next, run the following command:
npm run deploy
After running this command, the build script will be initiated, followed by the build folder being uploaded to your Filebase bucket.
14. Navigate to the Filebase dashboard and view the bucket that your build folder was uploaded to.
Take note of this folder’s IPFS CID value:
15. Next, navigate to the smart contract directory in your project and find the hardhat.config.js
file.
hardhat.config.js
file.Replace the existing content with the following:
In this file, you will need to replace two values:
ALCHEMY_KEY: Your Alchemy API key. For instructions on how to get this value, see this guide.
POLYGON MUMBAI WALLET PRIVATE KEY: Polygon wallet private key. For instructions on how to get this value, see this guide.
16. Next, create a file in the root of your project called metadata.json
. In this file, insert the following content:
metadata.json
. In this file, insert the following content:Replace the following values:
BUILD_CID: This value is from the build folder that we uploaded to Filebase in step 14.
CID: This CID represents an image file that will be used for a visual representation of your app. To upload a file to Filebase to be used in this place, follow these steps:
Start by clicking on the ‘Buckets’ option from the menu to open the Buckets dashboard.
Select your IPFS Bucket.
After clicking on the bucket name, you will see any previously uploaded files. To upload another file, select 'Upload', then select 'File' from the options.
Select the file you want to upload to the IPFS.
Once uploaded, you will be able to view and copy the IPFS CID from the 'CID' category, as seen below.
17. Next, upload the metadata file to IPFS:
aws --endpoint https://s3.filebase.com s3 cp metadata.json s3://bucket-name
Replace bucket-name
with your Filebase IPFS bucket name.
18. Finally, open the scripts folder, and then open the deploy.js
file. Replace the existing content with the following:
deploy.js
file. Replace the existing content with the following:Replace the METADATA_CID value with the IPFS CID for the metadata.json file you uploaded to Filebase in the previous step. This value can be found in the Filebase web console dashboard.
19. Run the following command to deploy this script on the Polygon network:
npx hardhat run scripts/deploy.js --network mumbai
From here, you can view this deployed NFT from a public marketplace like OpenSea by connecting your cryptowallet to the platform, or you can configure your app to be customized, since we used the default React app in this example.
Last updated