bulkupload

0.2.0 • Public • Published

contract-interface 🦍

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.

trackgit-views

Installation

npm i @niftymints/contract-interface --save

Import class

import NFTManager from "@niftymints/contract-interface"

Supported functions

NFTManager

constructor (contractName: string = "NFT", rpcURL?: string, privateKey?: string)

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);

connect (signer: Signer): NFTManager

Set the signer. Example:

nft.connect(signer);

deployContract (tokenName: string, tokenSymbol: string): Promise<string>

Deploy NFT.sol with the given constructor args. Returns the contract address. Example:

const contractAddress = await nft.deployContract("name", "symbol");

mintNFT (contractAddress: string, tokenURI: string, artist: string): Promise<string>

Mint a new NFT belonging to the given artist. Returns the transaction hash. Example:

const txHash = await nft.mintNFT(contractAddress, tokenURI, artist);

getTokenID (txHash: string, contractAddress?: string): Promise<string>

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);

txWait (txHash: string, confirmations: number): Promise<void>

Wait for block confirmations of the transaction. Example:

await nft.txWait(txHash, 5); // wait for 5 block confirmations

getContract (contractAddress: string): Promise<Contract>

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 (contractAddress: string, ...args: any[]): Promise<void>

Verify the smart contract on the blockchain. Requires POLYGONSCAN_KEY environment variable to be set.

More examples

Deploy a contract

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");
}

Mint NFT

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)
}

Local development

npm i                   # install
npx hardhat compile     # compile
npx hardhat test        # unit test

Deploy on-chain

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)

Contributing 👋

Contributions are always welcome! Feel free to open any issue or send a pull request. For questions, please contact shanzid.com.

Package Sidebar

Install

npm i bulkupload

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

8.74 MB

Total Files

52

Last publish

Collaborators

  • kartik_rajendran