Programmatically deploy and interact with NFT smart contracts. Deployed contracts and their NFTs are compatible with OpenSea, Rarible, and other markeplaces which support ERC-721 NFTs.
Bootstrapped with Hardhat and Solidity 0.8.9.
npm i @niftymints/contract-interface --save
Import class
import NFTManager from "@niftymints/contract-interface"
Initialize the NFTManager using the name of the smart contract (default is "NFT"). Optionally also accepts rpc URL (Alchemy, Infura, etc.) and private key of wallet. If rpcURL and privateKey are left blank, use connect
function to set a signer. Example:
const nft = new NFTManager("NFT", rpcUrl, privateKey);
// or without any arguments:
// const nft2 = new NFTManager();
// nft2.connect(signer);
Set the signer. Example:
nft.connect(signer);
Deploy NFT.sol
with the given constructor args. Returns the contract address. Example:
const contractAddress = await nft.deployContract("name", "symbol");
Mint a new NFT belonging to the given artist. Returns the transaction hash. Example:
const txHash = await nft.mintNFT(contractAddress, tokenURI, artist);
Get the ID of a minted NFT using the transaction hash. Optionally also accepts the contract address to make computation faster. Example:
const nftID = await nft.getTokenID(txHash, contractAddress);
Wait for block confirmations of the transaction. Example:
await nft.txWait(txHash, 5); // wait for 5 block confirmations
Get the deployed instanced of a smart contract. Can be used to execute functions in the smart contract (ERC721URIStorage
). Example:
const myContract = await nft.getContract(contractAddress);
await myContract.transfer(from, to, tokenID); // "transfer" function from smart contract
Verify the smart contract on the blockchain. Requires POLYGONSCAN_KEY
environment variable to be set.
import NFTManager from "@niftymints/contract-interface";
const rpcURL = "<rpc-url>";
const privateKey = "<wallet-key>";
async function deploy() {
const nft = new NFTManager("NFT", rpcURL, privateKey);
const address = await nft.deployContract("Test NFT", "TNFT");
}
async function depoyWithSigner(signer) {
const nft = (new NFTManager()).connect(signer);
const address = await nft.deployContract("Test NFT", "TNFT");
}
import NFTManager from "@niftymints/contract-interface";
const rpcURL = "<rpc-url>";
const privateKey = "<wallet-key>";
async function mintNFT(tokenURI) {
const nft = new NFTManager("NFT", rpcURL, privateKey);
const address = await nft.deployContract("Test NFT", "TNFT");
const txHash = await nft.mintNFT(address, tokenURI);
console.log(txHash)
}
npm i # install
npx hardhat compile # compile
npx hardhat test # unit test
Create a .env
file at the root directory with the following variables:
- POLYGONSCAN_KEY: Polygonscan API key [video tutorial]
- ALCHEMY_PROD_URL: Alchemy project key (prod env) [video tutorial]
- ALCHEMY_DEV_URL: Alchemy project key (dev env) [video tutorial]
- PRIVATE_KEY_DEV: Crypto wallet account private key. You can request some
MATIC
tokens from the polygon faucet. - PRIVATE_KEY_PROD: Crypto wallet account private key. Can be the same as PRIVATE_KEY_DEV but I like to use two separate accounts for dev and prod.
Deploy to Polygon mumbai testnet
npx hardhat deploy --network mumbai_dev
Deploy to Polygon mainnet
npx hardhat deploy --network matic_prod
Note: Please ensure you have the minimum number of MATIC
tokens in your wallet (roughly 0.008 MATIC
, or $0.20 USD - see latest MATIC-USD rate)
Contributions are always welcome! Feel free to open any issue or send a pull request. For questions, please contact shanzid.com.