This is a repo that shows 2 things:
- How to create NFTs with metadata that is 100% on-chain
- How we can generate random art on-chain using the SVG method
Inspired by the NFT Brownie Mix
SVG means "Scalable Vector Graphics", and is an XML language and can be used to create an image either by specifying all the lines and shapes necessary, by modifying already existing raster images, or by a combination of both.
We can then base64 encode this SVG string into a URL that we can set as the imageURI
in our NFT tokenURI
. Like this triangle
You can learn everything about SVG parameters and try some examples here.
We are going to use the following path data commands because they each only take 2 parameters
M = moveto
L = lineto
However, there are a lot more. You can see sample SVGs in the img
folder. These get base64 encoded to URLs. You can see an example of a randomly generated base64 URL here.
Set your RINKEBY_RPC_URL
environment variable.. You can get one for free at Infura's site. You'll also need to set the variable PRIVATE_KEY
which is your private key from you wallet, ie MetaMask. This is needed for deploying contracts to public networks.
You can set these in your .env
file if you're unfamiliar with how setting environment variables work. Check out our .env example. If you wish to use this method to set these variables, update the values in the .env.example file, and then rename it to '.env'
Don't commit and push any changes to .env files that may contain sensitive information, such as a private key! If this information reaches a public GitHub repository, someone can use it to check if you have any Mainnet funds in that wallet address, and steal them!
.env
example:
RINKEBY_RPC_URL='www.infura.io/asdfadsfafdadf'
PRIVATE_KEY='abcdef'
MAINNET_RPC_URL="https://eth-mainnet.alchemyapi.io/v2/your-api-key"
bash
example
export RINKEBY_RPC_URL='www.infura.io/asdfadsfafdadf'
export PRIVATE_KEY='cat dog frog...'
export MAINNET_RPC_URL="https://eth-mainnet.alchemyapi.io/v2/your-api-key"
If you plan on deploying to a local Hardhat network that's a fork of the Ethereum mainnet instead of a public test network like Kovan, you'll also need to set your MAINNET_RPC_URL
environment variable. and uncomment the forking
section in hardhat.config.js
. You can get one for free at Alchemy's site..
You can also use a MNEMONIC
instead of a PRIVATE_KEY
environment variable by uncommenting the section in the hardhat.config.js
, and commenting out the PRIVATE_KEY
line.
Then you can install all the dependencies
git clone https://github.com/patrickalphac/all-on-chain-generated-nft
cd all-on-chain-generated-nft
then
npm install
Or
yarn
Deployment scripts are in the deploy directory. If required, edit the desired environment specific variables or constructor parameters in each script, then run the hardhat deployment plugin as follows. If no network is specified, it will default to the Kovan network.
This will deploy to a local hardhat network.
npx hardhat deploy --tags svg
To deploy to testnet:
You'll need testnet ETH in your wallet
npx hardhat deploy --network rinkeby --tags svg
This will deploy to a local hardhat network.
npx hardhat deploy --tags rsvg
To deploy to testnet:
You'll need testnet LINK and ETH in your wallet
npx hardhat deploy --network rinkeby --tags rsvg
This will also call the create
function after deploying and request a random number from a Chainlink node. And it will call finishMint
to finalize the minting.
Once deployed, each will look something like this:
SVGNFT Opensea Random SVG Opensea Another Random SVG Opensea
- Make sure all the double quotes are single quotes
Using SVGs will allow you to make all the drawings directly on-chain, and store them on chain too! For example, you could store an SVG as a string, and then edit it to change your drawings.
- I wasn't able to make
data:image/png
types work as an imageURI, but hypothetically it should.
Tests are located in the test directory, and are split between unit tests and integration tests. Unit tests should only be run on local environments, and integration tests should only run on live environments.
To run unit tests:
npx harhat test
You'll need an ETHERSCAN_API_KEY
environment variable. You can get one from the Etherscan API site.
npx hardhat verify --network <NETWORK> <CONTRACT_ADDRESS> <CONSTRUCTOR_PARAMETERS>
example:
npx hardhat verify --network kovan 0x9279791897f112a41FfDa267ff7DbBC46b96c296 "0x9326BFA02ADD2366b30bacB125260Af641031331"
yarn lint:fix