Skip to content

Build Nft Marketplace on Ethereum With Polygon From Scratch

Notifications You must be signed in to change notification settings

TravelXML/POLYGON-NFT-MARKETPLACE

Repository files navigation

NFT Marketplace Built With Polygon, Solidity, IPFS & Next.js

Header

NFT Marketplace Built With Polygon, Solidity, IPFS & Next.js

Objective

Create NFT Marketplace With Below Options.

  • Home
  • Sell Digital Asset
  • My Digital Assets
  • Creator Dashboard

Run this project

How to setup locally?

To run this project locally, follow these steps.

1. Clone the project locally, change into the directory, and install the dependencies:

git clone https://github.com/TravelXML/POLYGON-NFT-MARKETPLACE.git

cd POLYGON-NFT-MARKETPLACE

# install using NPM or Yarn
npm install

# or

yarn

2. Start the local Hardhat node

npx hardhat node

3. With the network running, deploy the contracts to the local network in a separate terminal window

npx hardhat run scripts/deploy.js --network localhost

4. Start the app

npm run dev

image

Navigate http://localhost:3000 on the browser you can see your marketplace is up.

Click on Sell Digital Asset

image

Click on Home

image

Configuration

To deploy to Polygon test or main networks, update the configurations located in hardhat.config.js to use a private key and, optionally, deploy to a private RPC like Infura.

require("@nomiclabs/hardhat-waffle");
const fs = require('fs');
const privateKey = fs.readFileSync(".secret").toString().trim() || "01234567890123456789";
const infuraId = fs.readFileSync(".infuraid").toString().trim() || "";

module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    hardhat: {
      chainId: 1337
    },
    /*
    mumbai: {
      // Infura
      url: `https://polygon-mumbai.infura.io/v3/${infuraId}`
      //url: "https://rpc-mumbai.matic.today",
      accounts: [privateKey]
    },
    matic: {
      // Infura
      url: `https://polygon-mainnet.infura.io/v3/${infuraId}`,
      //url: "https://rpc-mainnet.maticvigil.com",
      accounts: [privateKey]
    }
    */
  },
  solidity: {
    version: "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  }
};

If using Infura, update .infuraid with your Infura project ID.

Deploy into Mumbai test network

Here, we will add the following configurations for the Mumbai test network as listed here:

image

Save this, then you should be able to switch to and use the new network!

Finally, you will need some testnet Matic tokens in order to interact with the applications[.secret->privatekey].

To get these, you can visit the Matic Faucet, inputting the address of the wallets that you would like to request the tokens for. Deploying to the Matic / Polygon network. image

Now that you have some Matic tokens, you can deploy to the Polygon network!

image

To do so, be sure that the address associated with the private key you are deploying your contract with has received some Matic tokens in order to pay the gas fees for the transaction.

To deploy to Matic, run the following command:

npx hardhat run scripts/deploy.js --network mumbai

Once the contracts have been deployed, you should be able to update the contract addresses in your project and test on the new network 🎉!

File Name: config.js

image

change to smart contract deployed address:

image

Change the JSON Provider in index.js file loadNFTs()

const provider = new ethers.providers.JsonRpcProvider()

Change to

image

Then good to up the server, Run below command

npm run dev

After Deploying to Mumbai Test Network, you scan your address and see what all transactions occurred. image

Finally Here is the NFT Marketplace!

image

Deploying to Mainnet

To deploy to the main Matic / Polygon network, you can use the same steps we set up for the Mumbai test network.

The main difference is that you'll need to use an endpoint for Matic as well as import the network into your MetaMask wallet as listed here.

An example update in your project to make this happen might look like this:

/* hardhat.config.js */

/* adding Matic main network config to existing config */
...
matic: {
  url: "https://rpc-mainnet.maticvigil.com",
  accounts: [privateKey]
}

Public RPCs like the one listed above may have traffic or rate-limits depending on usage. You can sign up for a dedicated free RPC URL using services like Infura, MaticVigil, QuickNode, Chainstack, or Ankr.

For example, using something like Infura:

url: https://polygon-mainnet.infura.io/v3/${infuraId}

Congratulations! You've deployed a NFT Marketplace to Polygon.

Happy Coding! 💓