Skip to content

IggyPostNft1155.sol

Tempe Techie edited this page Jun 17, 2023 · 1 revision

Iggy Social Post NFT Contract Documentation

The Iggy Social Post NFT contract is a smart contract written in Solidity that implements the ERC-1155 token standard. It allows Opti Social posts be transformed into NFTs. The contract supports features such as setting default prices, changing text previews, and setting minting deadlines for posts.

Contract Details

  • Contract Name: IggyPostNft1155
  • Solidity Version: ^0.8.17
  • License: GPL-3.0-or-later
  • Inherits From: ERC1155, Ownable, ReentrancyGuard

Contract Structure

The contract consists of the following main components:

  1. State Variables: Storage for contract data and configuration.
  2. Structs: Custom struct to represent a social media post.
  3. Mappings: Storage mappings to access post data, prices, and minting deadlines.
  4. Events: Event emitted when a post NFT is minted.
  5. Constructor: Initializes the contract with default values.
  6. Read Functions: Functions to read contract state and post information.
  7. Write Functions: Functions to modify contract state and mint NFTs.
  8. Owner Functions: Functions accessible only by the contract owner.

State Variables

  • metadataAddress: Address of the metadata contract.
  • minterAddress: Address of the minter contract.
  • textPreviewChangingDisabledForever: Boolean flag indicating if the owner can change the text preview of a post.
  • counter: ID counter for token IDs.
  • defaultPrice: Default price for minting a post.
  • maxTextPreviewLength: Maximum length of the text preview.
  • name: Name of the NFT token.
  • symbol: Symbol of the NFT token.

Structs

Post

  • tokenId: Unique identifier for the post NFT.
  • postId: Post ID on the Ceramic network.
  • author: Address of the author of the post.
  • textPreview: Preview of the post's text content.
  • timestamp: Timestamp indicating when the post was created.

Mappings

  • getPost: Mapping from token ID to Post struct.
  • getPostTokenId: Mapping from post ID to mapping from author address to token ID.
  • getAuthorsDefaultPrice: Mapping from author address to their default price.
  • getPriceForPost: Mapping from post ID to mapping from author address to custom price.
  • getPostMintingTime: Mapping from post ID to mapping from author address to minting deadline.

Events

  • MintPost: Event emitted when a post NFT is minted. It includes the NFT receiver, post ID, author address, and quantity minted.

Constructor

The constructor function initializes the contract with the following parameters:

  • _defaultPrice: Default price for minting a post.
  • _metadataAddress: Address of the metadata contract.
  • _name: Name of the NFT token.
  • _symbol: Symbol of the NFT token.

Read Functions

The contract provides the following read functions:

  • doesPostExist: Checks if a post exists based on the post ID and author address.
  • getPostPrice: Retrieves the price for minting a post based on the post ID and author address.
  • uri: Overrides the ERC-1155 function to retrieve the metadata URI for a given token ID.

Write Functions

The contract provides the following write functions:

  • authorSetDefaultPrice: Sets the default price for an author's posts.
  • authorSetMintTime: Sets the minting time (deadline) for a specific post authored by the caller.
  • `authorSet

PostPrice`: Sets a custom price for minting a specific post authored by the caller.

  • mint: Mints a post NFT and assigns it to the specified receiver. It includes the post ID, author address, NFT receiver address, text preview, and quantity to mint.

Owner Functions

The contract provides the following functions accessible only by the contract owner:

  • ownerChangeDefaultPrice: Changes the default price for minting a post.
  • ownerChangeMetadataAddress: Changes the address of the metadata contract.
  • ownerChangeMinterAddress: Changes the address of the minter contract.
  • ownerChangeTextPreview: Changes the text preview for a post NFT.
  • ownerChangeMaxTextPreviewLength: Changes the maximum length of the text preview.
  • ownerDisableTextPreviewChangingForever: Disables text preview changing forever (irreversible action).

Please note that the documentation provided here explains the purpose and usage of the smart contract. It is important to review and test the contract thoroughly before deploying it to a production environment.

Code examples

Read Functions

  1. doesPostExist
function doesPostExist(string memory _postId, address _authorId) external view returns (bool) {
  return getPostTokenId[_postId][_authorId] != 0;
}

This function checks if a post exists based on the provided post ID and author address. It returns a boolean value indicating the existence of the post. Here's an example usage:

bool postExists = contractInstance.doesPostExist("post123", 0x1234567890abcdef);
  1. getPostPrice
function getPostPrice(string memory _postId, address _author) external view returns (uint256) {
  uint256 price = getPriceForPost[_postId][_author];

  if (price == 0) {
    price = getAuthorsDefaultPrice[_author];
    
    if (price == 0) {
      price = defaultPrice;
    }
  }

  return price;
}

This function retrieves the price for minting a post based on the provided post ID and author address. If a custom price is set for the specific post and author, that price is returned. Otherwise, if the author has a default price set, that price is returned. If neither a custom price nor a default price is set, the defaultPrice value is returned. Here's an example usage:

uint256 postPrice = contractInstance.getPostPrice("post123", 0x1234567890abcdef);
  1. uri
function uri(uint256 _tokenId) public view override returns (string memory) {
  require(_tokenId < counter, "IggyPost: Token id does not exist");

  Post memory post = getPost[_tokenId];
  return IIggyPostNftMetadata(metadataAddress).getMetadata(_tokenId, post.postId, post.author, post.textPreview, post.timestamp);
}

This function overrides the uri function from the ERC-1155 standard. It returns the metadata URI for a given token ID. The metadata URI is obtained from the metadata contract's getMetadata function, which requires the token ID, post ID, author address, text preview, and timestamp. Here's an example usage:

string memory metadataURI = contractInstance.uri(123);

Write Functions

  1. authorSetDefaultPrice
function authorSetDefaultPrice(uint256 _price) external {
  getAuthorsDefaultPrice[_msgSender()] = _price;
}

This function allows an author to set their default price for minting posts. The _msgSender() function is used to identify the caller (author) and sets their default price in the getAuthorsDefaultPrice mapping. Here's an example usage:

contractInstance.authorSetDefaultPrice(100);
  1. authorSetMintTime
function authorSetMintTime(string memory _postId, uint256 _secondsToMint) external {
  getPostMintingTime[_postId][_msgSender()] = _secondsToMint;
}

This function allows an author to set the minting time (deadline) for a specific post they authored. The _msgSender() function is used to identify the caller (author), and the minting time is set in the getPostMintingTime mapping using the post ID as the key. Here's an example usage:

contractInstance.authorSetMintTime("post123

", 3600); // Set minting time to 1 hour (3600 seconds) for post123
  1. authorSetPostPrice
function authorSetPostPrice(string memory _postId, uint256 _price) external {
  getPriceForPost[_postId][_msgSender()] = _price;
}

This function allows an author to set a custom price for minting a specific post they authored. The _msgSender() function is used to identify the caller (author), and the custom price is set in the getPriceForPost mapping using the post ID as the key. Here's an example usage:

contractInstance.authorSetPostPrice("post123", 200); // Set custom price of 200 for post123
  1. mint
function mint(
  string memory _postId, 
  address _author, 
  address _nftReceiver, 
  string memory _textPreview,
  uint256 _quantity
) nonReentrant external returns(uint256 tokenId) {
  require(_msgSender() == minterAddress, "IggyPost: Only minter can mint");
  require(bytes(_textPreview).length <= maxTextPreviewLength, "IggyPost: Text preview is too long");

  tokenId = getPostTokenId[_postId][_author];

  if (tokenId == 0) {
    tokenId = counter;
    counter++;

    getPostTokenId[_postId][_author] = tokenId;
    getPost[tokenId] = Post(tokenId, _postId, _author, _textPreview, block.timestamp);
  }

  // check if author has set up a mint time
  uint256 mintTime = getPostMintingTime[_postId][_author];

  if (mintTime != 0) {
    require(block.timestamp <= (getPost[tokenId].timestamp + mintTime), "IggyPost: Minting deadline has passed");
  }

  _mint(_nftReceiver, tokenId, _quantity, "");

  emit MintPost(_nftReceiver, _postId, _author, _quantity);
}

This function allows the minter (specified during contract deployment) to mint post NFTs. It requires the post ID, author address, NFT receiver address, text preview, and quantity to mint. The function checks if the minter is the caller, verifies the text preview length, and mints the NFTs using the OpenZeppelin _mint function. If the post doesn't exist, it assigns a new token ID and stores the post details. If a minting time is set for the post author, it checks if the minting deadline has passed. Here's an example usage:

contractInstance.mint("post123", 0x1234567890abcdef, 0xabcdef123456, "Hello, world!", 1);