Skip to content

Instantly share code, notes, and snippets.

@mwrites
Last active December 18, 2021 10:52
Show Gist options
  • Save mwrites/5c759ca9e148576199491e9ea135acca to your computer and use it in GitHub Desktop.
Save mwrites/5c759ca9e148576199491e9ea135acca to your computer and use it in GitHub Desktop.
Buildspace - Ethereum NFT - Section 2 - Create Our SVG
const main = async () => {
const nftContractFactory = await hre.ethers.getContractFactory('MyEpicNFT');
const nftContract = await nftContractFactory.deploy({
value: hre.ethers.utils.parseEther('0.001'),
});
await nftContract.deployed();
console.log("Contract deployed to:", nftContract.address);
// modify at will at: https://www.svgviewer.dev/ and replace this
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350">
<style>.base { fill: cyan; font-family: serif; font-size: 14px; }</style>
<rect width="100%" height="100%" fill="yellow" />
<text x="50%" y="50%" class="base" dominant-baseline="middle" text-anchor="middle">Zombie Coconut</text>
</svg>
`
// modify at will
var metadata = {
'name': 'Zombie Coconut',
'description': 'Rotten Coconut',
};
const tokenURI = encodeTokenURI(svg, metadata);
// Call the function.
let txn = await nftContract.makeAnEpicNFT(tokenURI);
// Wait for it to be mined.
await txn.wait()
console.log("Minted NFT #1")
// txn = await nftContract.makeAnEpicNFT()
// // Wait for it to be mined.
// await txn.wait()
// console.log("Minted NFT #2")
};
const encodeTokenURI = (svg, metadata) => {
const base64svg = Buffer.from(svg).toString('base64');
metadata['image'] = 'data:image/svg+xml;base64,' + base64svg;
const base64json = Buffer.from(JSON.stringify(metadata)).toString('base64');
return tokenURI = 'data:application/json;base64,' + base64json;
}
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
};
runMain();
pragma solidity ^0.8.0;
// We first import some OpenZeppelin Contracts.
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";
// We inherit the contract we imported. This means we'll have access
// to the inherited contract's methods.
contract MyEpicNFT is ERC721URIStorage {
// Magic given to us by OpenZeppelin to help us keep track of tokenIds.
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
// We need to pass the name of our NFTs token and it's symbol.
constructor() payable ERC721 ("SquareNFT", "SQUARE") {
console.log("This is my NFT contract. Woah!");
}
// A function our user will hit to get their NFT.
function makeAnEpicNFT(string memory tokenURI) public {
// Get the current tokenId, this starts at 0.
uint256 newItemId = _tokenIds.current();
// Actually mint the NFT to the sender using msg.sender.
_safeMint(msg.sender, newItemId);
// Set the NFTs data.
_setTokenURI(newItemId, tokenURI);
// Increment the counter for when the next NFT is minted.
_tokenIds.increment();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment