2.0.0 • Published 9 months ago

evm-nft-utils v2.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

evm-nft-utils

A utility library designed for minting NFTs on the Ethereum and Polygon blockchains with ease. Developed with the Recunited me.NFT smart contract in mind, this library provides straightforward integration for developers aiming to interact with NFT contracts.

Prerequisites

Before you begin, ensure you have the following setup:

  1. Infura/Alchemy API Key: This utility uses service providers like Infura or Alchemy to interact with the Ethereum blockchain. You'll need to register on both Infura and Alchemy to obtain an API key. Ensure you set up the key on the right chain (e.g., Ethereum Mainnet, Polygon, or Mumbai).

  2. Environment Variables: Make sure you've set up the necessary environment variables (SIGNER_PRIVATE_KEY, CONTRACT_ADDRESS, and ALCHEMY_API_KEY).

  3. Private Key: This is used to sign the transaction for minting the NFT. Never expose your private key. Store it securely.

Installation

To install the evm-nft-utils package, use npm:

npm install evm-nft-utils

Usage

Uploading Metadata to IPFS

import { IPFSClient } from 'path-to-your-library';

// Initialize the IPFS client with your credentials
const ipfs = new IPFSClient(process.env.IPFS_PROJECT_ID, process.env.IPFS_PROJECT_SECRET);

const metaData = {
  name: "MyNFT",
  description: "A description for my NFT.",
  image: "url_to_image"
  //... other metadata fields
};

ipfs.uploadMetaData(metaData).then(result => {
  console.log("IPFS result:", result);
}).catch(error => {
  console.error("Failed to upload metadata:", error);
});

Metadata for NFTs

Metadata for NFTs usually contain essential details about the asset, such as its name, description, image, and possibly many other attributes. This metadata is typically a JSON file. Here's a basic example of what an NFT metadata might look like:

{
  "name": "Unique NFT Name",
  "description": "A detailed description of the NFT's unique attributes and characteristics.",
  "image": "ipfs://path_to_the_image_file",
  "attributes": [
    {
      "trait_type": "Size",
      "value": "Large"
    },
    {
      "trait_type": "Color",
      "value": "Blue"
    }
    // ... other attributes
  ]
}

This is a simplified example, and metadata can contain many other details depending on the requirements of the NFT project. For a comprehensive guide on metadata standards, especially if you want to ensure compatibility with platforms like OpenSea, please refer to the OpenSea metadata standards documentation.

Minting an NFT

After you've uploaded your metadata to IPFS, you can then proceed to mint the NFT using the returned IPFS URI. To mint a me.NFT using TypeScript:

import { mintNFT } from 'path-to-your-library';

// Replace with wallet address you want to send the NFT to, metadata link from IPFS, and your contract's ABI, or use the default contract ABI that is connected to the me.NFT contract.
const walletAddress = '0xYourWalletAddress';
const metaDataLink = 'ipfs://your-uploaded-metadata-uri';
const contractABI = [...]; // Your contract ABI, which should include the 'safeMint' function

try {
  const receipt = await mintNFT(walletAddress, metaDataLink, contractABI, 'ethereum' | 'polygon' | 'mumbai');
  console.log(receipt);
} catch (error) {
  console.error("Minting failed:", error);
}

Environment Setup

Before using the library, ensure your environment is properly set up with the necessary variables:

  • SIGNER_PRIVATE_KEY: The private key for the Ethereum address you'll use to sign transactions. Keep this private!

  • CONTRACT_ADDRESS: The contract address for your NFT on the desired chain. By default, it's set to 0xf88db04928aa00ccf610c5d494d42d51ff48def9, which is the address for the me.NFT smart contract for Recunited.

  • ALCHEMY_API_KEY: Your API key for Alchemy. Alchemy provides reliable data access to the Ethereum and Polygon blockchains. Make sure the key corresponds to the right blockchain (Ethereum, Polygon's mainnet or Mumbai testnet).

  • IPFS_PROJECT_ID: Your project ID from Infura, required for interacting with the IPFS network.

  • IPFS_PROJECT_SECRET: The secret corresponding to your IPFS project on Infura. Ensure this is kept confidential.

Ensure that the API keys for both Alchemy and Infura are set for the correct chain you intend to work on. A mismatch can lead to errors or unintended results.

Testing Your NFT

Once you've minted your NFT, you may want to see it in action and ensure everything looks right. There are several platforms where you can test and verify your NFT's details:

  • OpenSea: A popular NFT marketplace where you can view your NFT, its metadata, and even put it up for sale or auction. Simply search for your NFT using the contract address and the token ID. Visit OpenSea.

  • PolygonScan: If you minted your NFT on the Polygon chain, you can verify the minting transaction, view contract details, and more on PolygonScan. Just paste in your transaction ID or contract address into the search. Go to PolygonScan.

  • Etherscan: Similar to PolygonScan, but for the Ethereum chain. Use Etherscan to verify transactions, check contract details, and more for NFTs minted on Ethereum. Check it out at Etherscan.

Remember, correct metadata and adherence to standards are crucial for your NFT to display correctly on these platforms. Make sure everything is in order before showcasing or selling your NFT.

Detailed API Documentation

  • IPFSClient(projectId: string, projectSecret: string)
    • Initializes a new IPFS client to interact with the IPFS network.
    • Parameters:
      • projectId: Your IPFS project ID.
      • projectSecret: Your IPFS project secret.
  • uploadMetaData(metadata: object): Promise<object>

    • Uploads the provided metadata to IPFS.
    • Returns a promise that resolves with the IPFS result or rejects with an error.
  • mintNFT(walletAddress: string, metaDataLink: string, abi: any[], chain: string): Promise<object>

    • Mints an NFT on the specified chain.
    • Returns a promise that resolves with the transaction receipt or rejects with an error.

Note: The default ABI is pre-configured for the me.NFT smart contract for Recunited with the contract address 0xf88db04928aa00ccf610c5d494d42d51ff48def9.

mintNFT

Mints an NFT to a specified wallet on either the Polygon or Ethereum blockchain.

Parameters:

  • walletAddress (string): Ethereum/Polygon address of the recipient.
  • metaDataLink (string): URI or link pointing to the NFT's metadata, coming from the IPFSClient
  • abi (any[]) optional: ABI of the NFT contract. If not provided, the default ABI is used.

    • Default ABI: Designed for the me.NFT smart contract by Recunited at 0xf88db04928aa00ccf610c5d494d42d51ff48def9.
  • chain (string): Supported chains - "polygon", "ethereum", and "mumbai".

Returns:

  • Promise: A promise resolving to the transaction receipt.

Errors:

  • Missing required parameters or environment variables.
  • Unsupported blockchain network.
  • Transaction failure.

Contribute

Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.

License

This project is licensed under the ISC License.

2.0.0

9 months ago

1.0.0

9 months ago