This is the companion sample code of the Mint with NFT.storage and Polygon tutorial at NFTschool.dev.
Follow the detailed step by step guide here.
If you would like to try this out quickly, follow the steps below:
Install npm dependencies. Make sure you your npm version is above 8 or executing scripts of hardhat will fail.
$ npm install
Update your .env
environmnet variables. Replace your_wallet_private_key
with your test account's private key (here is how). Replace your_nftstroage_api_key
with your nft.storage account api key you created (here is a link to it).
PRIVATE_KEY=your_wallet_private_key
NFT_STORAGE_API_KEY=your_nftstroage_api_key
Note: you can get some test MATICs from this faucet.
Upload MyExampleNFT.png to NFT.stroage (to IPFS + Filecoin network) and save the ipfs://xxx/metadata.json link.
$ node scripts/store-asset.mjs
Metadata stored on Filecoin and IPFS with URL: ipfs://xxx/metadata.json
Deploy the contract to Polygon network by executing hardhat script. Save the deployed address for later.
$ npx hardhat run scripts/deploy-contract.mjs --network PolygonMumbai
Contract deployed to address: 0x00xxx
Update mint-nft.mjs
with correct variables. Replace your_deployed_contract_address
with the contract address you saved in Step 4. Replace your_ipfs_nft_metadata_json_link
with the asset metadata json link you saved in Step 3.
const CONTRACT_ADDRESS = "your_deployed_contract_address"
const META_DATA_URL = "your_ipfs_nft_metadata_json_link"
Mint the NFT and send NFT to your wallet by execuiting following script.
$ npx hardhat run scripts/mint-nft.mjs --network PolygonMumbai
NFT minted to: 0x00xxxx
If successful 0x00xxxx
should be your test wallet account address.
To verify, go to your metamask test account. You should see your MATIC amount reduced. If you connect your wallet to Opensea Testnet and authorise, you should see the NFT artwork displayed under your account.
Happy minting!