Part 3 of 4 - Deploying an NFT
First, we’ll need a deployment script, let’s create a deploy-script.js in the scripts directory.
const hre = require("hardhat");
async function main() {
const NFT = await hre.ethers.getContractFactory("AllCodeNFT");
const nft = await NFT.deploy();
await nft.deployed();
console.log("AllCode NFT deployed to:", nft.address);
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});
This script is pretty simple. Using Hardhat, we grab the AllCodeNFT, which we’ve already compiled. We stick in the compiled contract into the NFT object. Next, we deploy the AllCodeNFT to the appropriate network. Last, we wait for the NFT contract to be deployed. Once, it’s deployed we print out the address of the contract.
Next, we’ve got to make changes to get our hardhat.config.js to point back to Mumbai. We have to revert our module.exports to their previous state.
module.exports = {
defaultNetwork: "matic",
networks: {
hardhat: {
},
matic: {
url: "https://rpc-mumbai.maticvigil.com",
accounts: [PRIVATE_KEY]
}
},
Next, we’ll run a command to invoke the deployment script, and specify the Mumbai network
npx hardhat run scripts/deploy-script.js --network matic
The output will be the contract address.
AllCode NFT deployed to: 0xD931d7bAA004A1DA25bc6E877E6f669cB8559219
So the contract is deployed to Mumbai, which is great, but now we need to mint the tokens. We’ll mint the tokens with a different piece of javascript, let’s call it mint-script.js
const hre = require("hardhat");
async function main() {
const NFT = await hre.ethers.getContractFactory("AllCodeNFT");
//The IPFS Address of your image that you uploaded to Pinata
const URI = "ipfs://QmVH5T7MFVU52hTfQdWvu73iFPEF3jizuGfyVLccTmBCX2"
//Your Wallet Address that contains Matic whose private key you put into .gitignore
const WALLET_ADDRESS = "0x44f2b515211953d5f07038be619D58a91accB8E7"
//The AllCodeNFT contract address that you deployed above
const CONTRACT_ADDRESS = "0xD931d7bAA004A1DA25bc6E877E6f669cB8559219"
const contract = NFT.attach(CONTRACT_ADDRESS);
await contract.mint(WALLET_ADDRESS, URI);
console.log("NFT minted:", contract);
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});
Again, we snag the compiled asset from the Contract Factory. Next, we specify some metadata for the ipfs portion of the contract. We also specify the wallet address that we’re using and the contract address output from the previous javascript invocation. We associate the NFT contract with the address, and we call mint. Once we’re done minting, then we dump the contract to the console.log.
Let’s mint a contract by invoking the javascript command from the command line.
npx hardhat run scripts/mint-script.js --network matic
The output will look like definition for the contract because we dumped the contract to console.log().
NFT minted: Contract {
interface: Interface {
fragments: [
[ConstructorFragment], [EventFragment],
[EventFragment], [EventFragment],
[EventFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment],
[FunctionFragment], [FunctionFragment]
],
_abiCoder: AbiCoder { coerceFunc: null },
functions: {
'approve(address,uint256)': [FunctionFragment],
'balanceOf(address)': [FunctionFragment],
'getApproved(uint256)': [FunctionFragment],
'isApprovedForAll(address,address)': [FunctionFragment],
'mint(address,string)': [FunctionFragment],
'name()': [FunctionFragment],
'owner()': [FunctionFragment],
'ownerOf(uint256)': [FunctionFragment],
'renounceOwnership()': [FunctionFragment],
'safeTransferFrom(address,address,uint256)': [FunctionFragment],
'safeTransferFrom(address,address,uint256,bytes)': [FunctionFragment],
'setApprovalForAll(address,bool)': [FunctionFragment],
'supportsInterface(bytes4)': [FunctionFragment],
'symbol()': [FunctionFragment],
'tokenURI(uint256)': [FunctionFragment],
'transferFrom(address,address,uint256)': [FunctionFragment],
'transferOwnership(address)': [FunctionFragment]
},
Next, we’ll ask for the first minted object in contract to demonstrate that the URI is populated correctly. We’ll create a new javascript function entitled get-uri-script.js.
const hre = require("hardhat");
async function main() {
const NFT = await hre.ethers.getContractFactory("AllCodeNFT");
const CONTRACT_ADDRESS = "0xD931d7bAA004A1DA25bc6E877E6f669cB8559219"
const contract = NFT.attach(CONTRACT_ADDRESS);
const owner = await contract.ownerOf(1);
console.log("Owner:", owner);
const uri = await contract.tokenURI(1);
console.log("URI: ", uri);
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});
The output should be
Owner: 0x44f2b515211953d5f07038be619D58a91accB8E7
URI: ipfs://QmVH5T7MFVU52hTfQdWvu73iFPEF3jizuGfyVLccTmBCX2
That’s it. We minted a unique token to the Polygon Mumbai blockchain with the URI to our Pinata asset.
You can go to the blockchain to see it here.
https://mumbai.polygonscan.com/address/0x44f2b515211953d5f07038be619D58a91accB8E7#tokentxnsErc721