Skip to content

Instantly share code, notes, and snippets.

@ArpitxGit
Created February 22, 2023 06:34
Show Gist options
  • Save ArpitxGit/ecda7388087ace112c126c7f57eeb291 to your computer and use it in GitHub Desktop.
Save ArpitxGit/ecda7388087ace112c126c7f57eeb291 to your computer and use it in GitHub Desktop.
Dynamic NFT Collection

Building dynamic NFT collections:

Smart contract:

Use a smart contract to manage the NFT collection.
This contract should define the NFT's metadata (such as name, symbol, and image) and include functions for creating, transferring, and managing ownership of the NFTs.

Token standard:

Use a widely adopted token standard, such as ERC-721 or ERC-1155, for the NFTs in the collection. This will ensure that the NFTs are compatible with existing marketplaces and wallets.

Metadata standard:

Use a metadata standard, such as OpenSea's Metadata Standard or the ERC-721 Metadata Standard, to define the metadata for each NFT in the collection.
This metadata should include the name, description, and image of the NFT.

Royalties:

Consider including a royalty fee for each transfer of the NFT. This will ensure that the creator of the NFT receives a portion of the sale price each time the NFT is resold.

Rarity:

Use a rarity system to make some NFTs in the collection more valuable and desirable than others.
This can be based on factors such as the NFT's design, scarcity, or utility.

Dynamic properties:

Consider making the NFTs in the collection dynamic, with properties that can change over time.
This can increase the value and desirability of the NFTs and make them more engaging for collectors.

Utility:

Consider giving the NFTs in the collection utility beyond just being collectibles.
This can include access to exclusive content, membership in a community, or special perks and benefits.

Interoperability:

Consider making the NFTs in the collection interoperable with other NFTs or tokens on other platforms.
This can increase the value and utility of the NFTs and make them more versatile for collectors.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract AvatarCollection {
struct Avatar {
uint id;
string name;
string imageURI;
address owner;
}
Avatar[] public avatars;
mapping (uint => address) public avatarToOwner;
mapping (address => uint) public ownedAvatars;
uint public totalAvatars;
function createAvatar(string memory _name, string memory _imageURI) public {
Avatar memory newAvatar = Avatar(totalAvatars, _name, _imageURI, msg.sender);
avatars.push(newAvatar);
avatarToOwner[totalAvatars] = msg.sender;
ownedAvatars[msg.sender]++;
totalAvatars++;
}
function getOwnedAvatars(address _owner) public view returns (uint[] memory) {
uint[] memory ownedAvatarIds = new uint[](ownedAvatars[_owner]);
uint currentIndex = 0;
for (uint i = 0; i < totalAvatars; i++) {
if (avatarToOwner[i] == _owner) {
ownedAvatarIds[currentIndex] = i;
currentIndex++;
}
}
return ownedAvatarIds;
}
function getTotalAvatars() public view returns (uint) {
return totalAvatars;
}
}
// SPDX-License-Identifier: MIT
/*
This smart contract uses the widely adopted ERC-721 token standard and OpenSea's Metadata Standard to define the NFT's metadata, and includes functions for creating, transferring, and managing ownership of the NFTs.
It also includes a royalty fee for each transfer of the NFT, and uses a rarity system to make some NFTs in the collection more valuable and desirable than others.
The NFTs in the collection are also dynamic, with properties that can change over time, and can be made interoperable with other NFTs or tokens on other platforms.
*/
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFTCollection is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
string private _baseUri;
uint256 private _royaltyFee;
struct NFT {
uint256 id;
string name;
string description;
string imageURI;
uint256 rarity;
uint256 dynamicProperty;
}
mapping(uint256 => NFT) private _nfts;
constructor(string memory name_, string memory symbol_, string memory baseURI_, uint256 royaltyFee_) ERC721(name_, symbol_) {
_baseUri = baseURI_;
_royaltyFee = royaltyFee_;
}
function createNFT(string memory name, string memory description, string memory imageURI, uint256 rarity, uint256 dynamicProperty) public returns (uint256) {
_tokenIds.increment();
uint256 newNFTId = _tokenIds.current();
_mint(msg.sender, newNFTId);
_nfts[newNFTId] = NFT(newNFTId, name, description, imageURI, rarity, dynamicProperty);
return newNFTId;
}
function getNFTMetadata(uint256 tokenId) public view returns (string memory, string memory, string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
return (_nfts[tokenId].name, _nfts[tokenId].description, _nfts[tokenId].imageURI);
}
function royaltyInfo(uint256 tokenId, uint256 salePrice) public view returns (address receiver, uint256 royaltyAmount) {
require(_exists(tokenId), "ERC721: royalty query for nonexistent token");
return (ownerOf(tokenId), (salePrice * _royaltyFee) / 10000);
}
function transferFROM(address from, address to, uint256 tokenId) public payable {
require(_isApprovedOrOwner(msg.sender, tokenId), "ERC721: transfer caller is not owner nor approved");
require(to != address(0), "ERC721: transfer to the zero address");
(address receiver, uint256 royaltyAmount) = royaltyInfo(tokenId, msg.value);
if (royaltyAmount > 0) {
payable(receiver).transfer(royaltyAmount);
}
_transfer(from, to, tokenId);
}
function setBaseURI(string memory baseURI) public {
_baseUri = baseURI;
}
function setRoyaltyFee(uint256 royaltyFee) public {
_royaltyFee = royaltyFee;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment