Skip to content

Commit

Permalink
Adding nft example
Browse files Browse the repository at this point in the history
  • Loading branch information
prayagsingh committed Aug 9, 2021
1 parent 8610ac6 commit 1b464bc
Show file tree
Hide file tree
Showing 10 changed files with 14,320 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules

#Hardhat files
cache
artifacts
.env
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# hardhat_polygon_nft
# Hardhat-Polygon-Nft example
An Nft example using hardhat and Polygon blockchain


**Commands to create a new project using Hardhat**

`mkdir hardhat-example3-nfts`
`cd hardhat-example3-nfts`
`npm init --yes`
`npm install --save-dev hardhat`
`npx hardhat`

**NOTE:** create a `.env` file and add the private-key and rpc api-key in this file. Please add `.env` file to `.gitignore` as well.

we will use above keys in hardhat.config.js file.

`npm i dotenv`

If using openzeppelin then `npm install @openzeppelin/contracts` command to add the packages.

**Compile Project**

`npx hardhat compile`

**Test Project**

`npx hardhat test`

**Deploy Project**

`npx hardhat run scripts/deploy.js --network <network_name from hardhat.config.js file>`
for eg: `npx hardhat run scripts/deploy.js --network polygon_test`

**Reference:** Hardhat [docs]("https://hardhat.org/tutorial/setting-up-the-environment.html")
37 changes: 37 additions & 0 deletions contracts/nft.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

//import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/**
@title Creating Nfts
@dev Contract for creating nfts using ERC721. Unlike ERC20, ERC721 lacks a decimals field,
since each token is distinct and cannot be partitioned
*/
contract Nfts is ERC721URIStorage, Ownable {

using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
using Strings for uint256;

mapping (uint256 => string) private _tokenURIs;

constructor() ERC721("SuperNFT", "SNFT") {}

/**
@dev miniting NFT
*/
function mint(address recepient, string memory _tokenURI) public returns (uint) {

_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recepient, newItemId);
_setTokenURI(newItemId, _tokenURI);

return newItemId;
}

}
74 changes: 74 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require("@nomiclabs/hardhat-waffle");
// for using .env file which contains private info.
require('dotenv').config();
const POLYGON_TEST_PRIVATE_KEY = process.env.POLYGON_TEST_PRIVATE_KEY;
const POLYGON_TEST_API_KEY = process.env.POLYGON_TEST_API_KEY;

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();

for (const account of accounts) {
console.log(account.address);
}
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
// You can customize which network is used by default when running Hardhat by setting
// the config's defaultNetwork field. If you omit this config, its default value is "hardhat"
defaultNetwork: "polygon_test",
networks: {
hardhat: {
},
// rinkeby: {
// url: "https://eth-mainnet.alchemyapi.io/v2/123abc123abc123abc123abc123abcde",
// accounts: [privateKey1, privateKey2, ...]
// }
polygon_test: {
url: `https://rpc-mumbai.maticvigil.com/v1/${POLYGON_TEST_API_KEY}`,
chainId: 80001,
// The address to use as default sender. If not present the first account of the node is used
//from: ,
// Its value should be "auto" or a number. If a number is used, it will be the gas limit used by default in every transaction.
// If "auto" is used, the gas limit will be automatically estimated. Default value: "auto"
gas: "auto",
// Its value should be "auto" or a number. This parameter behaves like gas. Default value: "auto"
gasPrice: "auto",
// A number used to multiply the results of gas estimation to give it some slack due to the uncertainty
// of the estimation process. Default value: 1
//gasMultiplier: 1,
// This field controls which accounts Hardhat uses. It can use the node's accounts (by setting it to "remote"), a list
// of local accounts (by setting it to an array of hex-encoded private keys), or use an HD Wallet. Default value: "remote"
accounts: [`0x${POLYGON_TEST_PRIVATE_KEY}`],
// You can use this field to set extra HTTP Headers to be used when making JSON-RPC requests. It accepts a JavaScript
// object which maps header names to their values. Default value: undefined
//httpHeaders: '',
timeout: 20000
}
},
solidity: {
version: "0.8.4",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 20000
}
};
Loading

0 comments on commit 1b464bc

Please sign in to comment.