21
loading...
This website collects cookies to deliver better user experience
You can collect this article as an NFT Here
Photo by Atahan Guc on Unsplash
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "hardhat/console.sol";
contract NFTFactory is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
// Note that you don't have to do this, only if you want to pass the name and symbol of the ERC-721 token when you'd like to deploy
constructor(string memory name, string memory symbol)
ERC721(name, symbol)
{}
function createToken(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
setApprovalForAll(contractAddress, true);
return newItemId;
}
}
npx hardhat compile
, you can deploy it using Hardhat or build the frontend and deploy from there like I did on my Minting app:import { ethers } from 'ethers';
// We're importing the contract's artifacts here
import NFTFactory from '../../../artifacts/contracts/NFTFactory.sol/NFTFactory.json';
// Get the user signature
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
// Prepare contract facotry
const factory = new ethers.ContractFactory(NFTFactory.abi, NFTFactory.bytecode, user.data.signature || signer);
// Deploy the smart contract (ERC 721)
const deployedContract = await factory.deploy('Much wow', 'WOW');
await deployedContract.deployed();
import { create as ipfsHttpClient } from 'ipfs-http-client';
// You can grab those keys from Infura.io
const auth = 'Basic ' + Buffer.from(`${INFURA_IPFS_PROJECT_ID}:${INFURA_IPFS_PROJECT_SECRET}`).toString('base64');
const client = ipfsHttpClient({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https',
headers: {
authorization: auth,
},
});
// Upload image to IPFS (getting blob from url, might not needed if you already have the blob)
const blob = await fetch(values.image).then((r) => r.blob());
const image = await client.add(blob);
const data = JSON.stringify({
attributes: [],
description: 'description'
external_url: 'external_url'
name: 'name'
image: `https://ipfs.infura.io/ipfs/${image.path}`,
});
// Upload the rest to IPFS
const added = await client.add(data);
const metadataUrl = `https://ipfs.infura.io/ipfs/${added.path}`;
import { ethers } from 'ethers';
import NFTFactory from '../../../artifacts/contracts/NFTFactory.sol/NFTFactory.json';
const provider = new ethers.providers.Web3Provider(window.ethereum);
// Ask for user signature
const signer = provider.getSigner();
// Mint the NFT using the deployed smart contract
const contract = new ethers.Contract(<YOUR ERC-721 deployed contract address>, NFTFactory.abi, signer);
// Token gets created
// metadataUrl is the url returend by IPFS above
const transaction = await contract.createToken(metadataUrl);
await transaction.wait();